Example usage for org.dom4j Element add

List of usage examples for org.dom4j Element add

Introduction

In this page you can find the example usage for org.dom4j Element add.

Prototype

void add(Namespace namespace);

Source Link

Document

Adds the given Namespace to this element.

Usage

From source file:com.anite.maven.plugin.hivedoc.Dom4JRegistrySerializer.java

License:Apache License

private Element getRulesElement(ElementModel em) {
    Element rules = DocumentHelper.createElement("rules");

    for (Iterator i = em.getRules().iterator(); i.hasNext();) {
        Rule r = (Rule) i.next();/*from  ww w  .j  a v  a 2  s . com*/

        Element rule = null;

        if (r instanceof CreateObjectRule) {
            CreateObjectRule cor = (CreateObjectRule) r;
            rule = DocumentHelper.createElement("create-object");

            rule.addAttribute("class", cor.getClassName());
        } else if (r instanceof InvokeParentRule) {
            InvokeParentRule ipr = (InvokeParentRule) r;
            rule = DocumentHelper.createElement("invoke-parent");

            rule.addAttribute("method", ipr.getMethodName());
            if (ipr.getDepth() != 1)
                rule.addAttribute("depth", Integer.toString(ipr.getDepth()));
        } else if (r instanceof PushAttributeRule) {
            PushAttributeRule par = (PushAttributeRule) r;
            rule = DocumentHelper.createElement("push-attribute");

            rule.addAttribute("attribute", par.getAttributeName());
        } else if (r instanceof PushContentRule) {
            rule = DocumentHelper.createElement("push-content");
        } else if (r instanceof ReadAttributeRule) {
            ReadAttributeRule rar = (ReadAttributeRule) r;
            rule = DocumentHelper.createElement("read-attribute");

            rule.addAttribute("property", rar.getPropertyName());
            rule.addAttribute("attribute", rar.getAttributeName());
            if (!rar.getSkipIfNull())
                rule.addAttribute("skip-if-null", "false");
            if (rar.getTranslator() != null)
                rule.addAttribute("translator", rar.getTranslator());
        } else if (r instanceof ReadContentRule) {
            ReadContentRule rcr = (ReadContentRule) r;
            rule = DocumentHelper.createElement("read-content");

            rule.addAttribute("property", rcr.getPropertyName());
        } else if (r instanceof SetModuleRule) {
            SetModuleRule smr = (SetModuleRule) r;
            rule = DocumentHelper.createElement("set-module");

            rule.addAttribute("property", smr.getPropertyName());
        } else if (r instanceof SetParentRule) {
            SetParentRule spr = (SetParentRule) r;
            rule = DocumentHelper.createElement("set-parent");

            rule.addAttribute("property", spr.getPropertyName());
        } else if (r instanceof SetPropertyRule) {
            SetPropertyRule spr = (SetPropertyRule) r;
            rule = DocumentHelper.createElement("set-property");

            rule.addAttribute("property", spr.getPropertyName());
            rule.addAttribute("value", spr.getValue());
        } else if (r instanceof ConversionDescriptor) {
            ConversionDescriptor cd = (ConversionDescriptor) r;
            rule = DocumentHelper.createElement("conversion");

            rule.addAttribute("class", cd.getClassName());
            if (!cd.getParentMethodName().equals("addElement"))
                rule.addAttribute("parent-method", cd.getParentMethodName());

            for (Iterator j = cd.getAttributeMappings().iterator(); j.hasNext();) {
                AttributeMappingDescriptor amd = (AttributeMappingDescriptor) j.next();

                Element map = DocumentHelper.createElement("map");

                map.addAttribute("attribute", amd.getAttributeName());
                map.addAttribute("property", amd.getPropertyName());

                rule.add(map);
            }
        } else {
            rule = DocumentHelper.createElement("custom");

            rule.addAttribute("class", r.getClass().getName());
        }

        if (rule != null)
            rules.add(rule);
    }
    return rules;
}

From source file:com.anite.maven.plugin.hivedoc.Dom4JRegistrySerializer.java

License:Apache License

private Element getElementElement(ElementModel em) {
    Element element = DocumentHelper.createElement("element");
    element.addAttribute("name", em.getElementName());

    if (em.getAnnotation() != null) {
        element.addText(em.getAnnotation());
    }/*from  ww  w. j av  a  2 s.  co m*/

    for (Iterator i = em.getAttributeModels().iterator(); i.hasNext();) {
        AttributeModel am = (AttributeModel) i.next();

        Element attribute = getAttributeElement(am);

        element.add(attribute);
    }

    for (Iterator i = em.getElementModel().iterator(); i.hasNext();) {
        ElementModel nestedEm = (ElementModel) i.next();

        Element nestedElement = getElementElement(nestedEm);

        element.add(nestedElement);
    }

    if (!em.getRules().isEmpty()) {
        Element rules = getRulesElement(em);

        element.add(rules);
    }

    return element;
}

From source file:com.bullx.cacconfig.CACConfig.java

License:Open Source License

public Document getRequest() {
    Document doc = DocumentHelper.createDocument();
    Element request = doc.addElement("request");

    DOMElement configsData = getConfigs();
    request.add(configsData);
    return doc;// w w  w  . ja v  a2  s .c  o  m
}

From source file:com.bullx.cacdata.CACData.java

License:Open Source License

public Document getRequest() {
    Document doc = DocumentHelper.createDocument();
    Element request = doc.addElement("request");

    DOMElement monitorData = getMonitorData();
    request.add(monitorData);

    return doc;//from w w w .ja  v a2  s  . c o  m
}

From source file:com.chingo247.structureapi.plan.io.export.StructurePlanExporter.java

License:Open Source License

public void export(IStructurePlan plan, File destinationDirectory, String fileName, boolean prettyPrint)
        throws IOException, UnsupportedPlacementException {
    Preconditions.checkArgument(destinationDirectory.isDirectory());

    IPlacement placement = plan.getPlacement();
    if (!(placement instanceof IExportablePlacement)) {
        throw new UnsupportedPlacementException("Placement does not implement IWriteablePlacement");
    }/*from   w  w w . j a va2s .c  o m*/

    Document d = DocumentHelper.createDocument();

    Element root = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_ROOT_ELEMENT);
    d.add(root);

    Element nameElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_NAME_ELEMENT);
    nameElement.setText(plan.getName());
    root.add(nameElement);

    Element priceElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_PRICE_ELEMENT);
    priceElement.setText(String.valueOf(plan.getPrice()));
    root.add(priceElement);

    Element categoryElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_CATEGORY_ELEMENT);
    categoryElement.setText(plan.getCategory() == null ? "None" : plan.getCategory());
    root.add(categoryElement);

    Element descriptionElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_DESCRIPTION_ELEMENT);
    descriptionElement.setText(plan.getDescription() == null ? "None" : plan.getDescription());
    root.add(descriptionElement);

    Element placementElement = PlacementAPI.getInstance().handle((IExportablePlacement) plan.getPlacement());
    root.add(placementElement);

    //        if (plan instanceof SubStructuresPlan) {
    //            SubStructuresPlan ssp = (SubStructuresPlan) plan;
    //            
    //            Element substructuresElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_SUBSTRUCTURES);
    //            root.add(substructuresElement);
    //            
    //            for(Placement p : ssp.getSubPlacements()) {
    //                try {
    //                    Element e = PlacementAPI.getInstance().handle(p);
    //                    e.setName(StructurePlanXMLConstants.STRUCTURE_PLAN_SUBSTRUCTURE);
    //                    substructuresElement.add(e);
    //                } catch (PlacementException ex) {
    //                    System.err.println(ex.getMessage());
    //                }
    //            }
    //            
    //            int index = 0;
    //            for(StructurePlan p : ssp.getSubStructurePlans()) {
    //                File exportPlan = new File(destinationDirectory, p.getFile().getName() + "-" + index);
    //                
    //                try {
    //                    export(plan, destinationDirectory, exportPlan.getName(), prettyPrint);
    //                } catch (Exception e){
    //                    continue;
    //                }
    //                
    //                Element substructureElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_SUBSTRUCTURE);
    //                
    //                // TODO add position + direction
    //                
    //                Element typeElement = new BaseElement(PlacementXMLConstants.PLACEMENT_TYPE_ELEMENT);
    //                typeElement.setText(PlacementTypes.EMBEDDED);
    //                substructureElement.add(typeElement);
    //                
    //                Element pathElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_RELATIVE_PATH_ELEMENT);
    //                pathElement.setText(exportPlan.getName());
    //                substructureElement.add(pathElement);
    //                
    //                substructuresElement.add(substructureElement);
    //                
    //            }
    //            
    //        }

    OutputFormat format;
    if (prettyPrint) {
        format = OutputFormat.createPrettyPrint();
    } else {
        format = OutputFormat.createCompactFormat();
    }
    XMLWriter writer = new XMLWriter(new FileWriter(new File(destinationDirectory, fileName)), format);
    writer.write(d);
    writer.close();

}

From source file:com.chingo247.structureapi.plan.io.handlers.AbstractPlacementXMLHandler.java

License:Open Source License

@Override
public Element handle(T placement) {
    Element placementRoot = new BaseElement(PlacementXMLConstants.ROOT_ELEMENT);
    //        d.add(placementRoot);

    Vector v = placement.getOffset();
    if (!v.equals(Vector.ZERO)) { // Not equal to default
        Element xElement = new BaseElement(PlacementXMLConstants.X_ELEMENT);
        Element yElement = new BaseElement(PlacementXMLConstants.Y_ELEMENT);
        Element zElement = new BaseElement(PlacementXMLConstants.Z_ELEMENT);

        xElement.setText(String.valueOf(v.getBlockX()));
        yElement.setText(String.valueOf(v.getBlockY()));
        zElement.setText(String.valueOf(v.getBlockZ()));

        placementRoot.add(xElement);
        placementRoot.add(yElement);/*  w  ww.  j  av a 2  s. c om*/
        placementRoot.add(zElement);
    }

    if (placement instanceof RotationalPlacement) {
        int rotation = ((RotationalPlacement) placement).getRotation();
        Element directionElement = new BaseElement(PlacementXMLConstants.ROTATION_ELEMENT);
        directionElement.setText(String.valueOf(rotation));
        placementRoot.add(directionElement);
    }

    Element typeElement = new BaseElement(PlacementXMLConstants.TYPE_ELEMENT);
    typeElement.setText(placement.getTypeName());
    placementRoot.add(typeElement);

    return placementRoot;
}

From source file:com.chingo247.structureapi.plan.io.handlers.SchematicPlacementXMLHandler.java

License:Open Source License

@Override
public Element handle(SchematicPlacement placement) {
    Element placementRoot = super.handle(placement);

    // add the schematic
    Element schematicElement = new BaseElement(PlacementXMLConstants.SCHEMATIC_ELEMENT);
    schematicElement.setText(placement.getSchematic().getFile().getName());
    placementRoot.add(schematicElement);
    return placementRoot;
}

From source file:com.chingo247.structureapi.plan.overview.StructureOverview.java

License:Open Source License

@Override
public Element asElement() {
    Element root = new BaseElement(Elements.STRUCTURE_OVERVIEW);
    // Set X//  w  w  w  .j a  v a 2  s .  co  m
    Element xElement = new BaseElement(Elements.X);
    xElement.setText(String.valueOf(x));
    // Set Y
    Element yElement = new BaseElement(Elements.Y);
    yElement.setText(String.valueOf(y));
    // Set Z
    Element zElement = new BaseElement(Elements.Z);
    zElement.setText(String.valueOf(z));
    // Add nodes
    root.add(xElement);
    root.add(yElement);
    root.add(zElement);
    return root;
}

From source file:com.chingo247.structureapi.plan.PlanGenerator.java

License:Open Source License

private static void generatePlanFromSchematic(File file, File rootDirectory) throws IOException {
    String name = FilenameUtils.getBaseName(file.getName());
    File directory = file.getParentFile();

    Document d = DocumentHelper.createDocument();
    Element root = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_ROOT_ELEMENT);
    d.add(root);//from   w ww  .  jav  a 2 s . c om

    Element nameElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_NAME_ELEMENT);
    nameElement.setText(name);
    root.add(nameElement);

    Element priceElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_PRICE_ELEMENT);
    priceElement.setText("0");
    root.add(priceElement);

    Element description = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_DESCRIPTION_ELEMENT);
    description.setText("None");
    root.add(description);

    Element categoryElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_CATEGORY_ELEMENT);
    String category = rootDirectory.getName().equals(directory.getName()) ? "Default" : directory.getName();
    categoryElement.setText(category);
    root.add(categoryElement);

    Element placementElment = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_PLACEMENT);

    Element typeElement = new BaseElement(PlacementXMLConstants.TYPE_ELEMENT);
    typeElement.setText(PlacementTypes.SCHEMATIC);

    Element schematicElement = new BaseElement(PlacementXMLConstants.SCHEMATIC_ELEMENT);
    schematicElement.setText(file.getName());

    placementElment.add(typeElement);
    placementElment.add(schematicElement);

    root.add(placementElment);

    File planFile = new File(directory, name + ".xml");
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(new FileWriter(planFile), format);
    writer.write(d);
    writer.close();
}

From source file:com.chingo247.structureapi.plan.worldguard.StructureRegionFlag.java

License:Open Source License

@Override
public Element asElement() {
    Element element = new BaseElement(Elements.REGIONFLAG);

    Element flagName = new BaseElement(Elements.NAME);
    flagName.setText(flag.getName());/* www.  j a  va  2 s .  co m*/

    Element flagValue = new BaseElement(Elements.VALUE);
    flagValue.setText(String.valueOf(value));

    element.add(flagName);
    element.add(flagValue);

    return element;
}