Example usage for org.eclipse.jdt.core ToolFactory createCodeFormatter

List of usage examples for org.eclipse.jdt.core ToolFactory createCodeFormatter

Introduction

In this page you can find the example usage for org.eclipse.jdt.core ToolFactory createCodeFormatter.

Prototype

public static CodeFormatter createCodeFormatter(Map options) 

Source Link

Document

Create an instance of the built-in code formatter.

Usage

From source file:ac.at.tuwien.dsg.uml.statemachine.export.transformation.util.JavaClassOutputter.java

License:Open Source License

public static void outputFile(ITransformContext context, Document doc, String filename,
        String generationStrategyName, String stateMachineName) {
    //generate CLass output file
    IResource res = (IResource) context.getTargetContainer();
    IPath targetPath = res.getLocation();

    CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(null);

    String code = doc.get();// w  ww .ja va  2  s  .c om

    TextEdit textEdit = codeFormatter.format(CodeFormatter.K_UNKNOWN, code, 0, code.length(), 0, null);
    try {
        //unsure why but sometimes formatted is null
        if (textEdit != null) {
            textEdit.apply(doc);
        } else {
            //usually errors appear due to spaces or illegal characters in property names
            IOException exception = new IOException("Generated document has formatting errors: \n" + code);
            throw new UncheckedIOException("Generated document has formatting errors: \n" + code, exception);
        }

        File myFile = new File(filename);
        PrintWriter fw;
        try {
            fw = new PrintWriter(myFile);

            fw.write("/* Generated by " + generationStrategyName + " from state diagram  " + stateMachineName
                    + " */ \n\n");

            for (String importString : defaultImports) {
                fw.write("import " + importString + ";");
                fw.write("\n");
            }
            fw.write("\n");
            fw.write(doc.get());
            fw.flush();
            fw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    } catch (MalformedTreeException e) {
        e.printStackTrace();
    } catch (BadLocationException e) {
        e.printStackTrace();
    }

}

From source file:ac.at.tuwien.dsg.uml.stereotype.export.transformation.util.JavaClassOutputter.java

License:Open Source License

public static void outputFile(ITransformContext context, IDOMNode content) {
    //generate CLass output file
    IResource res = (IResource) context.getTargetContainer();
    IPath targetPath = res.getLocation();
    String filename = targetPath.toOSString() + File.separatorChar + content.getName() + ".java";

    //format Code

    String code = content.getContents();
    CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(null);

    TextEdit textEdit = codeFormatter.format(CodeFormatter.K_UNKNOWN, code, 0, code.length(), 0, null);
    IDocument doc = new Document(code);
    try {//from w  w w.ja  v a  2  s .c  o  m
        //unsure why but sometimes formatted is nu
        if (textEdit != null) {
            textEdit.apply(doc);
        } else {
            //usually errors appear due to spaces or illegal characters in property names
            IOException exception = new IOException("Generated document has formatting errors: \n" + code);
            throw new UncheckedIOException("Generated document has formatting errors: \n" + code, exception);
        }

        File myFile = new File(filename);
        PrintWriter fw;
        try {
            fw = new PrintWriter(myFile);

            for (String importString : defaultImports) {
                fw.write("import " + importString + ";");
                fw.write("\n");
            }

            fw.write(doc.get());
            fw.flush();
            fw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    } catch (MalformedTreeException e) {
        e.printStackTrace();
    } catch (BadLocationException e) {
        e.printStackTrace();
    }

}

From source file:at.bestsolution.efxclipse.tooling.converter.AbstractConverterHandler.java

License:Open Source License

public static Object createCodeFormatter(IProject project) {
    IJavaProject javaProject = JavaCore.create(project);
    Map options = javaProject.getOptions(true);
    return ToolFactory.createCodeFormatter(options);
}

From source file:com.android.ide.eclipse.adt.internal.wizards.templates.TemplateHandler.java

License:Open Source License

private static String format(IProject project, String contents, IPath to) {
    String name = to.lastSegment();
    if (name.endsWith(DOT_XML)) {
        XmlFormatStyle formatStyle = EclipseXmlPrettyPrinter.getForFile(to);
        EclipseXmlFormatPreferences prefs = EclipseXmlFormatPreferences.create();
        return EclipseXmlPrettyPrinter.prettyPrint(contents, prefs, formatStyle, null);
    } else if (name.endsWith(DOT_JAVA)) {
        Map<?, ?> options = null;
        if (project != null && project.isAccessible()) {
            try {
                IJavaProject javaProject = BaseProjectHelper.getJavaProject(project);
                if (javaProject != null) {
                    options = javaProject.getOptions(true);
                }//ww  w. j a  va  2 s  .  co  m
            } catch (CoreException e) {
                AdtPlugin.log(e, null);
            }
        }
        if (options == null) {
            options = JavaCore.getOptions();
        }

        CodeFormatter formatter = ToolFactory.createCodeFormatter(options);

        try {
            IDocument doc = new org.eclipse.jface.text.Document();
            // format the file (the meat and potatoes)
            doc.set(contents);
            TextEdit edit = formatter.format(
                    CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, contents, 0,
                    contents.length(), 0, null);
            if (edit != null) {
                edit.apply(doc);
            }

            return doc.get();
        } catch (Exception e) {
            AdtPlugin.log(e, null);
        }
    }

    return contents;
}

From source file:com.arcbees.gwtp.plugin.core.util.CodeFormattingUtil.java

License:Apache License

private CodeFormatter createCodeFormatter() {
    Map options = javaProject.getOptions(true);
    return ToolFactory.createCodeFormatter(options);
}

From source file:com.diffplug.gradle.spotless.java.EclipseFormatterStep.java

License:Apache License

private EclipseFormatterStep(Properties settings) {
    this.codeFormatter = ToolFactory.createCodeFormatter(settings);
}

From source file:com.google.gdt.eclipse.appengine.rpc.util.CodegenUtils.java

License:Open Source License

/**
 * Format the source//from w  w  w .j  a  va 2s  .co  m
 */
public static String format(String source, int formatType) {

    TextEdit textEdit = null;
    textEdit = ToolFactory.createCodeFormatter(null).format(formatType, source, 0, source.length(), 0, null);

    String formattedContent;
    if (textEdit != null) {
        Document document = new Document(source);
        try {
            textEdit.apply(document);
        } catch (MalformedTreeException e) {
            AppEngineRPCPlugin.log(e);
        } catch (BadLocationException e) {
            AppEngineRPCPlugin.log(e);
        }
        formattedContent = document.get();
    } else {
        formattedContent = source;
    }

    return formattedContent;
}

From source file:com.google.gdt.eclipse.appengine.rpc.wizards.helpers.RpcServiceLayerCreator.java

License:Open Source License

/**
 * Format the source//from   ww w  .j a  va  2s. com
 */
private String format(String source, int formatType) {

    TextEdit textEdit = null;
    textEdit = ToolFactory.createCodeFormatter(null).format(formatType, source, 0, source.length(), 0, null);

    String formattedContent;
    if (textEdit != null) {
        Document document = new Document(source);
        try {
            textEdit.apply(document);
        } catch (MalformedTreeException e) {
            AppEngineRPCPlugin.log(e);
        } catch (BadLocationException e) {
            AppEngineRPCPlugin.log(e);
        }
        formattedContent = document.get();
    } else {
        formattedContent = source;
    }

    return formattedContent;
}

From source file:com.google.gdt.eclipse.core.TypeCreator.java

License:Open Source License

private String formatJava(IType type) throws JavaModelException {
    String source = type.getCompilationUnit().getSource();
    CodeFormatter formatter = ToolFactory.createCodeFormatter(type.getJavaProject().getOptions(true));
    TextEdit formatEdit = formatter.format(CodeFormatterFlags.getFlagsForCompilationUnitFormat(), source, 0,
            source.length(), 0, lineDelimiter);
    if (formatEdit == null) {
        CorePluginLog.logError("Could not format source for " + type.getCompilationUnit().getElementName());
        return source;
    }// w  ww .  j  av a2  s  . co  m

    Document document = new Document(source);
    try {
        formatEdit.apply(document);
        source = document.get();
    } catch (BadLocationException e) {
        CorePluginLog.logError(e);
    }

    source = Strings.trimLeadingTabsAndSpaces(source);
    return source;
}

From source file:com.gwtplatform.plugin.SourceWriter.java

License:Apache License

SourceWriter(int formatKind) {
    this.formatKind = formatKind;
    this.indentationLevel = 0;
    codeFormatter = ToolFactory.createCodeFormatter(null);
    methodRange = null;/* w w  w  . jav a 2s .c o m*/
    prefix = suffix = "";
}