Example usage for org.dom4j.tree BaseElement BaseElement

List of usage examples for org.dom4j.tree BaseElement BaseElement

Introduction

In this page you can find the example usage for org.dom4j.tree BaseElement BaseElement.

Prototype

public BaseElement(QName qname) 

Source Link

Usage

From source file:com.chingo247.structureapi.plan.document.DocumentPluginElement.java

public void setList(String listName, List<IStructurePlanElement> elements) {
    // Get the list Element
    Element e = (Element) pluginElement.selectSingleNode(listName);

    // If we found the element and the new value was null or an empty list.
    // Delete the element
    if (e != null && (elements == null || elements.isEmpty())) {
        e.detach();// w ww . j  ava  2  s.co  m
        return;
    }

    if (e == null) {
        pluginElement.add(new BaseElement(listName));
        e = (Element) pluginElement.selectSingleNode(listName);
    }

    e.clearContent();

    //
    List<Element> toElements = new LinkedList<>();
    for (IStructurePlanElement se : elements) {
        toElements.add(se.asElement());
    }
    e.setContent(toElements);
}

From source file:com.chingo247.structureapi.plan.document.DocumentPluginElement.java

public void setValue(String name, Object value) {
    // Get the list Element
    Element e = (Element) pluginElement.selectSingleNode(name);

    // Remove the element if value is null and the element exists
    if (value == null) {
        if (e != null) {
            e.detach();//from   ww w . j a  va  2  s  .c  om
        }
        return;
    }

    // If element doesnt exist, add it
    if (e == null) {
        pluginElement.add(new BaseElement(name));
        e = (Element) pluginElement.selectSingleNode(name);
    }
    e.setText(String.valueOf(value));
}

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 v  a2 s  .  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);/*from  w ww.  ja v a  2  s  . c  om*/
        placementRoot.add(yElement);
        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/*from w w  w.  j  a  v a2s .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  ww  w  .  j  a v a 2 s. c o  m*/

    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());/*from  w w w  .  java  2 s  . c  o  m*/

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

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

    return element;
}

From source file:com.doculibre.constellio.izpack.UsersToXmlFile.java

License:Open Source License

public static void run(AbstractUIProcessHandler handler, String[] args) {
    if (args.length != 3) {
        System.out.println("file login password");
        return;/*from   www .  j  a v a 2 s  . co m*/
    }

    String target = args[0];

    File xmlFile = new File(target);

    BufferedWriter writer;
    try {
        writer = new BufferedWriter(new FileWriter(xmlFile));
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    try {
        for (String line : Arrays.asList(emptyFileLines)) {
            writer.write(line + System.getProperty("line.separator"));
        }

        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    String login = args[1];

    String passwd = args[2];

    UsersToXmlFile elem = new UsersToXmlFile();

    ConstellioUser dataUser = elem.new ConstellioUser(login, passwd, null);
    dataUser.setFirstName("System");
    dataUser.setLastName("Administrator");
    dataUser.getRoles().add(Roles.ADMIN);

    Document xmlDocument;
    try {
        xmlDocument = new SAXReader().read(target);
        Element root = xmlDocument.getRootElement();

        BaseElement user = new BaseElement(USER);
        user.addAttribute(FIRST_NAME, dataUser.getFirstName());
        user.addAttribute(LAST_NAME, dataUser.getLastName());
        user.addAttribute(LOGIN, dataUser.getUsername());
        user.addAttribute(PASSWORD_HASH, dataUser.getPasswordHash());

        if (dataUser.getLocale() != null) {
            user.addAttribute(LOCALE, dataUser.getLocaleCode());
        }

        Set<String> constellioRoles = dataUser.getRoles();
        if (!constellioRoles.isEmpty()) {
            Element roles = user.addElement(ROLES);
            for (String constellioRole : constellioRoles) {
                Element role = roles.addElement(ROLE);
                role.addAttribute(VALUE, constellioRole);
            }
        }

        root.add(user);

        OutputFormat format = OutputFormat.createPrettyPrint();

        xmlFile = new File(target);
        // FIXME recrire la DTD
        // xmlDocument.addDocType(arg0, arg1, arg2)
        XMLWriter writer2 = new XMLWriter(new FileOutputStream(xmlFile), format);

        writer2.write(xmlDocument);
        writer2.close();
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.doculibre.constellio.services.SolrServicesImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//  w  ww  .j av  a2s .  c om
public void updateDismax(RecordCollection collection) {
    ensureCore(collection);

    Element dismaxElement = getDismaxElement(collection);
    Document solrConfigDocument = readSolrConfig(collection);
    Element root = solrConfigDocument.getRootElement();

    boolean defaultSearchFieldFound = false;
    // 1. keep only one requestHandler with name DISMAX_ATTRIBUTE_NAME
    for (Iterator<Element> it = root.elementIterator("requestHandler"); it.hasNext();) {
        Element currentRequestHandlerElement = it.next();
        String currentSearchComponentName = currentRequestHandlerElement.attribute("name").getText();
        if (currentSearchComponentName.equals(DISMAX_ATTRIBUTE_NAME)) {
            // first copy other fields that are not defaults if the
            // query was set as default
            if (!defaultSearchFieldFound) {
                Attribute defaultAttribute = currentRequestHandlerElement.attribute("default");
                if (defaultAttribute != null && defaultAttribute.getText().equals("true")) {
                    defaultSearchFieldFound = true;
                    defaultAttribute.setText("false");
                    List<Element> elements = currentRequestHandlerElement.elements();
                    for (Element element : elements) {
                        if (element.attribute("name") != null
                                && !element.attribute("name").getValue().equals("defaults")) {
                            BaseElement cloneElement = new BaseElement(element.getName());
                            cloneElement.appendAttributes(element);
                            cloneElement.appendContent(element);
                            dismaxElement.add(cloneElement);
                        }
                    }
                }
            }
            it.remove();
        }
    }
    if (!defaultSearchFieldFound) {
        // 2. add the parameters of the default RequestHandler to dismax
        // requestHandler (escape the parameter with name="defaults")
        for (Iterator<Element> it = root.elementIterator("requestHandler"); it.hasNext();) {
            Element currentRequestHandlerElement = it.next();
            Attribute defaultAttribute = currentRequestHandlerElement.attribute("default");
            if (defaultAttribute != null && defaultAttribute.getText().equals("true")) {
                defaultAttribute.setText("false");
                List<Element> elements = currentRequestHandlerElement.elements();
                for (Element element : elements) {
                    if (element.attribute("name") != null
                            && !element.attribute("name").getValue().equals("defaults")) {
                        BaseElement cloneElement = new BaseElement(element.getName());
                        cloneElement.appendAttributes(element);
                        cloneElement.appendContent(element);
                        dismaxElement.add(cloneElement);
                    }
                }
                break;
            }
        }
    }

    root.add(dismaxElement);

    writeSolrConfig(collection, solrConfigDocument);
    initCore(collection);
}