List of usage examples for org.eclipse.jdt.internal.core.util SimpleDocument SimpleDocument
public SimpleDocument(String source)
From source file:net.sf.fast.ibatis.build.AbstractCodeBuilder.java
License:Apache License
@SuppressWarnings("unchecked") private String generateCode(ICompilationUnit cu, FastIbatisConfig fc) { if (cu == null || Utils.isMethodExist(cu, fc.getMethodName())) return null; ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(cu);// www .ja va2s . c o m // parser.setResolveBindings(false); CompilationUnit astRoot = (CompilationUnit) parser.createAST(null); AST ast = astRoot.getAST();// AST.newAST(AST.JLS3); TypeDeclaration type = ((TypeDeclaration) astRoot.types().get(0)); MethodDeclaration methodDeclaration = createMethodDeclaration(ast, type, fc); if (type.isInterface()) { } else { Block block = createBlock(ast, fc); methodDeclaration.setBody(block); } Javadoc jc = Utils.getJavadoc(ast, fc.getMethodComment()); if (jc != null) methodDeclaration.setJavadoc(jc); type.bodyDeclarations().add(methodDeclaration); String str = astRoot.toString(); IDocument document = new SimpleDocument(str); //writeContent(fileName,newSource); Map options = cu.getJavaProject().getOptions(true); CodeFormatter formatter = new DefaultCodeFormatter(options); int indentationLevel = 0;//StringUtils.inferIndentationLevel(" ", getTabSize()); TextEdit textEdit = formatter.format(CodeFormatter.K_COMPILATION_UNIT, str, 0, str.length(), indentationLevel, "\n"); try { textEdit.apply(document, TextEdit.NONE); String result = document.get(); return result; } catch (Exception e) { } return null; }
From source file:org.nabucco.framework.mda.model.java.file.JavaCodeFormatter.java
License:Open Source License
/** * Formats code of a {@link CompilationUnitDeclaration} appropriate to the default eclipse code * formatter options.//from w ww. j ava 2 s. co m * * @see DefaultCodeFormatterOptions * * @param unit * the compilation unit to format. * @param configFile * the formatter configuration file. * * @return the formatter code string * * @throws JavaModelException * if the code formatting was not successful */ public String formatCode(CompilationUnitDeclaration unit, String configFile) throws JavaModelException { String sourceCode = JavaAstElementFactory.getInstance().getJavaAstCommon().getSourceCode(unit).toString(); String typeName = new String( JavaAstElementFactory.getInstance().getJavaAstUnit().getPublicJavaClass(unit).name); CodeFormatter formatter; if (configFile != null) { formatter = ToolFactory.createCodeFormatter(this.readConfig(configFile), ToolFactory.M_FORMAT_EXISTING); } else { formatter = this.createDefaultCodeFormatter(); } TextEdit edit = formatter.format(CodeFormatter.K_COMPILATION_UNIT, sourceCode, OFFSET, sourceCode.length(), INDENTATION, LINE_SEPARATOR); if (edit == null) { logger.warning("Cannot format Java File '", typeName, ".java'."); return sourceCode; } try { IDocument document = new SimpleDocument(sourceCode); edit.apply(document); return document.get(); } catch (MalformedTreeException e) { throw new JavaModelException("Error formatting [" + typeName + "].", e); } catch (BadLocationException e) { throw new JavaModelException("Error formatting [" + typeName + "].", e); } }