Example usage for org.eclipse.jdt.core.jdom IDOMNode getContents

List of usage examples for org.eclipse.jdt.core.jdom IDOMNode getContents

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.jdom IDOMNode getContents.

Prototype

public String getContents();

Source Link

Document

Returns the current contents of this document fragment.

Usage

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 {// w w  w .  ja  va  2s.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:org.eclipse.emf.codegen.jmerge.JMerger.java

License:Open Source License

protected IDOMNode insertClone(IDOMNode sourceNode) {
    IDOMNode targetNode = null;/*from  ww w  .j  a v  a 2 s .  c  o m*/
    switch (sourceNode.getNodeType()) {
    case IDOMNode.COMPILATION_UNIT: {
        targetNode = jdomFactory.createCompilationUnit(applyFormatRules(sourceNode.getContents()),
                ((IDOMCompilationUnit) sourceNode).getName());
        break;
    }
    case IDOMNode.PACKAGE: {
        targetNode = jdomFactory.createPackage(applyFormatRules(sourceNode.getContents()));
        break;
    }
    case IDOMNode.IMPORT: {
        targetNode = jdomFactory.createImport(applyFormatRules(sourceNode.getContents()));
        break;
    }
    case IDOMNode.TYPE: {
        targetNode = jdomFactory.createType(applyFormatRules(sourceNode.getContents()));
        break;
    }
    case IDOMNode.INITIALIZER: {
        targetNode = jdomFactory.createInitializer(applyFormatRules(sourceNode.getContents()));
        break;
    }
    case IDOMNode.FIELD: {
        targetNode = jdomFactory.createField(applyFormatRules(sourceNode.getContents()));
        break;
    }
    case IDOMNode.METHOD: {
        targetNode = jdomFactory.createMethod(applyFormatRules(sourceNode.getContents()));
        break;
    }
    default: {
        targetNode = (IDOMNode) sourceNode.clone();
        break;
    }
    }

    if (targetNode != null) {
        map(sourceNode, targetNode);
        mapChildren(sourceNode, targetNode);
    } else {
        // System.err.println("Warning: Cannot clone '" + sourceNode.getContents() + "'");
    }
    for (IDOMNode previousNode = sourceNode.getPreviousNode(); previousNode != null; previousNode = previousNode
            .getPreviousNode()) {
        IDOMNode targetSibling = (IDOMNode) sourceToTargetMap.get(previousNode);
        if (targetSibling != null) {
            IDOMNode targetNextSibling = targetSibling.getNextNode();
            if (targetNextSibling == null) {
                targetSibling.getParent().addChild(targetNode);
            } else {
                targetNextSibling.insertSibling(targetNode);
            }

            return targetNode;
        }
    }
    if (sourceNode.getParent() != null) {
        IDOMNode targetParent = (IDOMNode) sourceToTargetMap.get(sourceNode.getParent());
        IDOMNode targetSibling = targetParent.getFirstChild();
        if (targetSibling == null) {
            targetParent.addChild(targetNode);
        } else {
            targetSibling.insertSibling(targetNode);
        }
    }
    return targetNode;
}

From source file:org.eclipse.emf.codegen.jmerge.JPatternDictionary.java

License:Open Source License

public void dumpNodeContents(IDOMNode node) {
    System.out.println("____ " + getQualifiedName(node) + " ____________________________________________");
    System.out.print(node.getContents());
    System.out.println("_____________________________________________________________________");
}

From source file:org.eclipse.emf.codegen.jmerge.JPatternDictionary.java

License:Open Source License

protected void match(IDOMNode node) {
    for (Iterator dictionaryPatterns = options.getDictionaryPatterns().iterator(); dictionaryPatterns
            .hasNext();) {//from  w ww.  j  a v  a2  s  . co m
        JControlModel.DictionaryPattern dictionaryPattern = (JControlModel.DictionaryPattern) dictionaryPatterns
                .next();
        if (dictionaryPattern.getSelectorFeature().getFeatureClass() != null
                && dictionaryPattern.getSelectorFeature().getFeatureClass().isInstance(node)) {
            try {
                String selection = (String) dictionaryPattern.getSelectorFeature().getFeatureMethod()
                        .invoke(node, noArguments);
                if (dictionaryPattern.getSelectorFeature().getFeatureMethod().getName().equals("getComment")) {
                    String contents = node.getContents();
                    for (int start = 0, end = contents.length(), count = 0; start < end;) {
                        Matcher matcher = comment.matcher(contents.subSequence(start, end));
                        if (matcher.find()) {
                            // Ignore it if there are multiple comments.
                            //
                            if (++count > 1) {
                                int braceIndex = contents.indexOf("{", start); // }
                                if (braceIndex > start + matcher.start(0)) {
                                    selection = null;
                                }

                                break;
                            }
                            start += matcher.end(0) + 1;
                        } else {
                            break;
                        }
                    }
                }

                if (selection != null) {
                    Matcher matcher = dictionaryPattern.getPattern().matcher(selection);
                    if (matcher.find()) {
                        for (int i = 1; i <= matcher.groupCount(); ++i) {
                            String markup = matcher.group(i);
                            Collection collection = (Collection) markupMap.get(markup);
                            if (collection == null) {
                                collection = new HashSet();
                                markupMap.put(markup, collection);
                            }
                            collection.add(node);
                        }
                    }
                }
            } catch (IllegalAccessException exception) {
                // exception.printStackTrace();
            } catch (InvocationTargetException exception) {
                // exception.printStackTrace();
            }
        }
    }
}

From source file:org.eclipse.emf.codegen.merge.java.facade.jdom.JDOMJInitializer.java

License:Open Source License

@Override
public String getComment() {
    String comment = super.getComment();
    IDOMNode previousNode = getWrappedObject().getPreviousNode();
    if (comment == null && previousNode instanceof IDOMMember) {
        String text = splitLastComment(previousNode.getContents())[1];
        if (text.indexOf("/**") >= 0) {
            int index = text.lastIndexOf("*/");
            if (index > 0) {
                comment = text.substring(0, index + "*/".length());
            }//from  w ww . ja  v  a2s.  c o m
        }
    }
    return comment;
}

From source file:org.eclipse.emf.codegen.merge.java.facade.jdom.JDOMJInitializer.java

License:Open Source License

@Override
public String getContents() {
    String contents = super.getContents();
    IDOMNode previousNode = getWrappedObject().getPreviousNode();
    if (previousNode instanceof IDOMMember) {
        contents = splitLastComment(previousNode.getContents())[1] + contents;
    }/*from www. j  a  v  a  2s . co  m*/
    return contents;
}