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

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

Introduction

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

Prototype

public void addChild(IDOMNode child) throws DOMException, IllegalArgumentException;

Source Link

Document

Adds the given un-parented node (document fragment) as the last child of this node.

Usage

From source file:ac.at.tuwien.dsg.uml2java.stereotype.export.ClassWithStereotypeExportRule.java

License:Open Source License

protected Object createTarget(ITransformContext context) {

    /**/*from w  w  w  .j a  v  a  2 s  .  c  o m*/
     * Retrieve the target DOM generated by the UML2Java standard transformation
     */
    DOMNode target = (DOMNode) context.getTarget();

    /**
     * Create a DOMFactory to be used in instantiating new DOM elements
     */
    DOMFactory domFactory = new DOMFactory();

    /**
     * Get the transformation UML Class
     */
    Class umlCls = (Class) context.getSource();

    /**
     * We want to add something as default imports. Mainly org.eclipse.uml2.uml.* but maybe something more in the future.
     * To do that, as in this rule the Class java file is already partially generated by super ClassRule, we get its parent (i.e., the CompilationUnit), 
     * remove all parent children (actually just one, the generated class), add all import statements we need, and add the removed children back.
     * 
     * Otherwise the imports are added after the end of the class, which triggers compilation errors.
     */

    List<IDOMNode> children = new ArrayList<>();
    IDOMNode parent = target.getParent();
    Enumeration enumeration = parent.getChildren();

    //remove all parent children
    while (enumeration.hasMoreElements()) {
        IDOMNode node = (IDOMNode) enumeration.nextElement();
        children.add(node);
        node.remove();
    }

    //add all import statements
    for (String importStatement : defaultImports) {
        IDOMImport importEclipseUML = domFactory.createImport();
        importEclipseUML.setName(importStatement);
        parent.addChild(importEclipseUML);
    }

    //add children back
    for (IDOMNode child : children) {
        parent.addChild(child);
    }

    /**
     * We go through all applied stereotypes and for each we go through each attribute, and create a class field.
     */

    for (Stereotype stereotype : umlCls.getAppliedStereotypes()) {

        //get all properties
        for (Property attribute : stereotype.getAllAttributes()) {

            String name = attribute.getName();
            Type type = attribute.getType();
            String typeName = type.getName();

            org.eclipse.uml2.uml.Package packageType = type.getPackage();
            /**
             * Create Java field/class variable for each attribute 
             */
            Namespace namespace = attribute.getNamespace();

            //create import statement for each added field 
            //            IDOMImport importStatement = domFactory.createImport();
            //            importStatement.setName(packageType+"."+typeName);
            //            
            //            target.addChild(importStatement);

            IDOMField field = domFactory.createField();
            field.setName(name);
            field.setFlags(Flags.AccPrivate);
            /**
             * In case the model is incomplete, we add the field with Object as type
             */
            if (typeName == null) {
                field.setType("Object");
                //add in the generated code a comment explaining why field type is Object
                field.setComment("/*Type for attribute \"" + name + "\" on stereotype \"" + stereotype
                        + "\" is null */");
                System.err.println(
                        "Type for attribute \"" + name + "\" on stereotype \"" + stereotype + "\" is null");
            } else {
                field.setType(typeName);
            }

            target.addChild(field);

            /**
             * Add setter/getter for the added field
             */
            IDOMMethod setter = domFactory.createMethod();
            /**
             * Capitalize the first letter of the variable name so we have nice camel-case 
             */
            setter.setName("set" + name.substring(0, 1).toUpperCase() + name.substring(1));
            setter.setFlags(Flags.AccPublic);
            setter.setReturnType("void");
            setter.addParameter(typeName, name);
            setter.setBody("{ \n this." + name + "=" + name + ";\n }");

            target.addChild(setter);

            IDOMMethod getter = domFactory.createMethod();
            getter.setName("get" + name.substring(0, 1).toUpperCase() + name.substring(1));
            getter.setFlags(Flags.AccPublic);
            getter.setReturnType(typeName);
            getter.setBody("{ \n return this." + name + ";\n }");

            target.addChild(getter);

        }

    }
    return target;
}

From source file:com.iw.plugins.spindle.wizards.project.ApplicationWizardPage.java

License:Mozilla Public License

protected void populateMethodSource(IType servletType, IProgressMonitor monitor) throws JavaModelException {
    String methodName = MessageUtil.getString("Tapestry.servlet.getAppMethodName");
    ICompilationUnit parentCU = servletType.getCompilationUnit();
    String gensource = parentCU.getSource();

    DOMFactory domFactory = new DOMFactory();
    IDOMCompilationUnit unit = domFactory.createCompilationUnit(gensource, servletType.getElementName());
    IDOMNode current = unit.getFirstChild();
    while (current.getNodeType() != IDOMNode.TYPE) {
        current = current.getNextNode();
    }//from w w  w. ja v  a  2 s  .  co m
    IDOMNode theType = current;
    IDOMMethod method = findMethod(theType, methodName);
    if (method == null) {
        method = domFactory.createMethod("protected String getApplicationSpecificationPath() {}");
        theType.addChild(method);
    }
    method.setBody(getAppMethodBody());

    String newContents = Utils.formatJavaCode(unit.getContents(), 0,
            StubUtility.getLineDelimiterUsed(parentCU));
    parentCU.getBuffer().setContents(newContents);
    parentCU.save(monitor, true);
}

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

License:Open Source License

protected IDOMNode insertClone(IDOMNode sourceNode) {
    IDOMNode targetNode = null;//from  w  ww  .j ava2s  .co 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.merge.java.facade.jdom.JDOMFacadeHelper.java

License:Open Source License

@Override
public boolean addChild(JNode node, JNode child) {
    if (child != null) {
        IDOMNode idomNode = getIDOMNode(node);
        IDOMNode idomChild = getIDOMNode(child);
        idomNode.addChild(idomChild);
        return true;
    } else {//w  ww .  ja v  a 2s. com
        return false;
    }
}