OSDN Git Service

fe3510ab1624a2da9113a2fb8fa29c8e91c19928
[jcfa/jcfa.git] / jcfa / src / jp / igapyon / jcfa / util / JcfaEclipseUtil.java
1 package jp.igapyon.jcfa.util;
2
3 import org.eclipse.jdt.core.ToolFactory;
4 import org.eclipse.jdt.core.formatter.CodeFormatter;
5 import org.eclipse.jface.text.BadLocationException;
6 import org.eclipse.jface.text.Document;
7 import org.eclipse.jface.text.IDocument;
8 import org.eclipse.text.edits.MalformedTreeException;
9 import org.eclipse.text.edits.TextEdit;
10
11 public class JcfaEclipseUtil {
12         /**
13          * Format source using eclipse.
14          * 
15          * @param source
16          *            Input source.
17          * @return Formatted source.
18          */
19         public static String formatSource(final String source) {
20                 if (false)
21                         System.out.println("TRACE: " + source);
22
23                 final CodeFormatter cf = ToolFactory.createCodeFormatter(null);
24                 final TextEdit te = cf.format(CodeFormatter.K_UNKNOWN, source, 0,
25                                 source.length(), 0, null);
26                 IDocument doc = new Document(source);
27                 try {
28                         te.apply(doc);
29                         return doc.get();
30                 } catch (MalformedTreeException e) {
31                         e.printStackTrace();
32                         throw new IllegalArgumentException(e);
33                 } catch (BadLocationException e) {
34                         e.printStackTrace();
35                         throw new IllegalArgumentException(e);
36                 }
37         }
38 }