Example usage for org.dom4j Branch add

List of usage examples for org.dom4j Branch add

Introduction

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

Prototype

void add(ProcessingInstruction pi);

Source Link

Document

Adds the given ProcessingInstruction to this branch.

Usage

From source file:com.stratumsoft.xmlgen.SchemaTypeXmlGenerator.java

License:Open Source License

/**
 * Add the element to the parent branch. The min number of times to add it is determined by the element's minOccurs
 * value and the max no. of times to add it is determined by the minimum of the max repeating elements option
 * and the element's maxOccurs value//  ww  w.  ja  v a2s  .  co  m
 * <p/>
 * Note: the element may again be added multiple times based on its container minOccurs and maxOccurs values
 *
 * @param branch
 * @param elemToAdd
 * @param schElemOfElemToAdd
 */

private void addElement(Branch branch, Element elemToAdd, XmlSchemaElement schElemOfElemToAdd) {

    long minCount = schElemOfElemToAdd.getMinOccurs();
    long maxCount = schElemOfElemToAdd.getMaxOccurs();

    //determine how many times to add this element to the parent
    if (branch != null) {

        if (maxCount > 0) {
            long maxEls = getMaxElementsToGenerate(minCount, maxCount);

            for (long i = 1; i <= maxEls; i++) {
                if (i > minCount) { //anything > the min count but < max is optional
                    if (options.isGenCommentsForParticles()) {
                        branch.add(factory.createComment("optional"));
                    }
                }
                logger.trace("Adding dom4j element: {} to branch: {}", elemToAdd.getName(), branch.getName());
                branch.add(elemToAdd);

                elemToAdd = elemToAdd.createCopy(); //cannot add same element again, so create a copy

                //if id attr is present need to set a new value for it in the copied el
                Attribute idAttr = elemToAdd.attribute("id"); //NON-NLS
                if (idAttr != null) {
                    idAttr.setValue(SampleValueProvider.get(Constants.XSD_ID));
                }
            }
        }
    }

}

From source file:org.dentaku.gentaku.tools.cgen.visitor.PluginOutputVisitor.java

License:Apache License

/**
 * Standard visitor pattern entry point.
 *
 * @param mappingNode  Node that we are currently visiting in the mapping document
 * @param parentOutput Output node for document we are building.  Recursive calls gradually expand this
 * @param modelElement ModelElement from UML that we are currently working on
 * @param location/*from   ww  w .  ja v a2s .  c  o  m*/
 * @return true if we did something worth keeping
 */
public boolean visit(LocalDefaultElement mappingXSD, Branch parentOutput, ModelElement modelElement,
        String location) throws GenerationException {
    boolean result = false;
    if (!mappingXSD.getName().equals("element")) {
        result = iterateElements(mappingXSD, parentOutput, modelElement, location);
    } else {
        String ref = mappingXSD.attributeValue("ref");
        if (ref != null) {
            mappingXSD = (LocalDefaultElement) Util.selectSingleNode(xsdDoc,
                    "/xs:schema/xs:element[@name='" + ref + "']");
        }

        location = updateLocation(mappingXSD, location);

        if (location.equals("root")) {
            // root element is a special case because we always iterate it.  But is this the only occurence not caught
            // in the following loop?  Smells fishy.  todo This is also broken because the root element cannot have attributes
            Element newLocalNode = DocumentHelper.createElement(mappingXSD.attributeValue("name"));
            if (iterateElements(mappingXSD, newLocalNode, modelElement, location)) {
                parentOutput.add(newLocalNode);
                result = true;
            }
        } else {
            // iterate candidate ModelElements that match these criteria
            // the tag prefix we are looking for in tags named "prefix.tag"
            String prefix = mappingXSD.attributeValue("name");
            Collection c = findElementsForLocationAndPrefix(location, prefix, modelElement);
            for (Iterator elementIterator = c.iterator(); elementIterator.hasNext();) {
                ModelElementImpl element = (ModelElementImpl) elementIterator.next();
                if (element instanceof Namespace || element instanceof Feature) {
                    modelElement = element;
                } else {
                    throw new AssertionError("Please report this to Brian");
                }

                preGenerate(mappingXSD, parentOutput, modelElement);
                if (generate(mappingXSD, parentOutput, modelElement)) {
                    Element newLocalNode = DocumentHelper.createElement(mappingXSD.attributeValue("name"));
                    // @todo note that in older versions of this code, we used to keep elements that properly rendered, even if a node did not have
                    // any attributes that rendered.  This was removed organically in the process of tracking down problems, but it may be that with
                    // that problem solved that the old behavior is the correct behavior.  If it is, the special case noise when a root element is
                    // being rendered can probably be removed as well
                    if (iterateAttributes(mappingXSD, element, newLocalNode)) {
                        iterateElements(mappingXSD, newLocalNode, modelElement, location);
                        postGenerate(mappingXSD, parentOutput, modelElement, newLocalNode);
                        parentOutput.add(newLocalNode);
                        result = true;
                    }
                } else {
                    result = true;
                }
            }
        }
    }
    return result;
}

From source file:org.projectforge.framework.xstream.XmlObjectWriter.java

License:Open Source License

private void writeValue(final Branch branch, final Object obj, final String key, final String sValue,
        final boolean asAttribute, final boolean asCDATA) {
    if (sValue == null) {
        return;//w ww . j a  va 2 s .  c o m
    }
    if (asAttribute == true) {
        addAttribute((Element) branch, obj, key, sValue);
    } else if (asCDATA == true) {
        branch.addElement(key).add(new DefaultCDATA(sValue));
    } else {
        branch.addElement(key).setText(sValue);
    }
}

From source file:org.withinsea.izayoi.commons.dom.DOMUtils.java

License:Mozilla Public License

public static Node append(Branch parent, Node newNode)
        throws InvocationTargetException, IllegalAccessException {
    parent.add(newNode);
    return newNode;
}