OSDN Git Service

adding format util.
authoriga <tosiki.iga@nifty.ne.jp>
Mon, 31 Mar 2014 12:03:53 +0000 (21:03 +0900)
committeriga <tosiki.iga@nifty.ne.jp>
Mon, 31 Mar 2014 12:23:01 +0000 (21:23 +0900)
jcfa/src/jp/igapyon/jcfa/util/JcfaEclipseUtil.java [new file with mode: 0644]

diff --git a/jcfa/src/jp/igapyon/jcfa/util/JcfaEclipseUtil.java b/jcfa/src/jp/igapyon/jcfa/util/JcfaEclipseUtil.java
new file mode 100644 (file)
index 0000000..ec8e6a7
--- /dev/null
@@ -0,0 +1,35 @@
+package jp.igapyon.jcfa.util;
+
+import org.eclipse.jdt.core.ToolFactory;
+import org.eclipse.jdt.core.formatter.CodeFormatter;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.Document;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.text.edits.MalformedTreeException;
+import org.eclipse.text.edits.TextEdit;
+
+public class JcfaEclipseUtil {
+       /**
+        * Format source using eclipse.
+        * 
+        * @param source
+        *            Input source.
+        * @return Formatted source.
+        */
+       public static String formatSource(final String source) {
+               final CodeFormatter cf = ToolFactory.createCodeFormatter(null);
+               final TextEdit te = cf.format(CodeFormatter.K_UNKNOWN, source, 0,
+                               source.length(), 0, null);
+               IDocument doc = new Document(source);
+               try {
+                       te.apply(doc);
+                       return doc.get();
+               } catch (MalformedTreeException e) {
+                       e.printStackTrace();
+                       throw new IllegalArgumentException(e);
+               } catch (BadLocationException e) {
+                       e.printStackTrace();
+                       throw new IllegalArgumentException(e);
+               }
+       }
+}