Example usage for org.dom4j Element nodeIterator

List of usage examples for org.dom4j Element nodeIterator

Introduction

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

Prototype

Iterator<Node> nodeIterator();

Source Link

Document

Returns an iterator through the content nodes of this branch

Usage

From source file:architecture.common.xml.XmlProperties.java

License:Apache License

/**
 * Sets a property to an array of values. Multiple values matching the same
 * property is mapped to an XML file as multiple elements containing each
 * value. For example, using the name "foo.bar.prop", and the value string
 * array containing {"some value", "other value", "last value"} would
 * produce the following XML://from w  w w  . j a v  a  2 s. c o  m
 * 
 * <pre>
 * &lt;foo&gt;
 *     &lt;bar&gt;
 *         &lt;prop&gt;some value&lt;/prop&gt;
 *         &lt;prop&gt;other value&lt;/prop&gt;
 *         &lt;prop&gt;last value&lt;/prop&gt;
 *     &lt;/bar&gt;
 * &lt;/foo&gt;
 * </pre>
 *
 * @param name
 *            the name of the property.
 * @param values
 *            the values for the property (can be empty but not null).
 */
public void setProperties(String name, List<String> values) {
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy,
    // stopping one short.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        // If we don't find this part of the property in the XML heirarchy
        // we add it as a new node
        if (element.element(propName[i]) == null) {
            element.addElement(propName[i]);
        }
        element = element.element(propName[i]);
    }
    String childName = propName[propName.length - 1];
    // We found matching property, clear all children.
    List<Element> toRemove = new ArrayList<Element>();
    Iterator iter = element.elementIterator(childName);
    while (iter.hasNext()) {
        toRemove.add((Element) iter.next());
    }
    for (iter = toRemove.iterator(); iter.hasNext();) {
        element.remove((Element) iter.next());
    }
    // Add the new children.
    for (String value : values) {
        Element childElement = element.addElement(childName);
        if (value.startsWith("<![CDATA[")) {
            Iterator it = childElement.nodeIterator();
            while (it.hasNext()) {
                Node node = (Node) it.next();
                if (node instanceof CDATA) {
                    childElement.remove(node);
                    break;
                }
            }
            childElement.addCDATA(value.substring(9, value.length() - 3));
        } else {
            childElement.setText(StringEscapeUtils.escapeXml(value));
        }
    }
    saveProperties();

    // Generate event.
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("value", values);

    // PropertyEventDispatcher.dispatchEvent(name,
    // PropertyEventDispatcher.EventType.xml_property_set, params);

}

From source file:architecture.common.xml.XmlProperties.java

License:Apache License

/**
 * Sets the value of the specified property. If the property doesn't
 * currently exist, it will be automatically created.
 *
 * @param name// www .j a v  a 2  s.  c om
 *            the name of the property to set.
 * @param value
 *            the new value for the property.
 */
public synchronized void setProperty(String name, String value) {
    if (!StringEscapeUtils.escapeXml(name).equals(name)) {
        throw new IllegalArgumentException(L10NUtils.getMessage("002155"));
    }
    if (name == null) {
        return;
    }
    if (value == null) {
        value = "";
    }

    // Set cache correctly with prop name and value.
    propertyCache.put(name, value);

    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy.
    Element element = document.getRootElement();
    for (String aPropName : propName) {
        // If we don't find this part of the property in the XML heirarchy
        // we add it as a new node
        if (element.element(aPropName) == null) {
            element.addElement(aPropName);
        }
        element = element.element(aPropName);
    }
    // Set the value of the property in this node.
    if (value.startsWith("<![CDATA[")) {
        Iterator it = element.nodeIterator();
        while (it.hasNext()) {
            Node node = (Node) it.next();
            if (node instanceof CDATA) {
                element.remove(node);
                break;
            }
        }
        element.addCDATA(value.substring(9, value.length() - 3));
    } else {
        element.setText(value);
    }
    // Write the XML properties to disk
    saveProperties();

    // Generate event.
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("value", value);

    // PropertyEventDispatcher.dispatchEvent(name,
    // PropertyEventDispatcher.EventType.xml_property_set, params);
}

From source file:architecture.ee.util.xml.XmlProperties.java

License:Apache License

/**
 * Sets a property to an array of values. Multiple values matching the same
 * property is mapped to an XML file as multiple elements containing each
 * value. For example, using the name "foo.bar.prop", and the value string
 * array containing {"some value", "other value", "last value"} would
 * produce the following XML:/*from   w w  w .j  a v a  2  s .  com*/
 * 
 * <pre>
 * &lt;foo&gt;
 *     &lt;bar&gt;
 *         &lt;prop&gt;some value&lt;/prop&gt;
 *         &lt;prop&gt;other value&lt;/prop&gt;
 *         &lt;prop&gt;last value&lt;/prop&gt;
 *     &lt;/bar&gt;
 * &lt;/foo&gt;
 * </pre>
 *
 * @param name
 *            the name of the property.
 * @param values
 *            the values for the property (can be empty but not null).
 */
public void setProperties(String name, List<String> values) {
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy,
    // stopping one short.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        // If we don't find this part of the property in the XML heirarchy
        // we add it as a new node
        if (element.element(propName[i]) == null) {
            element.addElement(propName[i]);
        }
        element = element.element(propName[i]);
    }
    String childName = propName[propName.length - 1];
    // We found matching property, clear all children.
    List<Element> toRemove = new ArrayList<Element>();
    Iterator iter = element.elementIterator(childName);
    while (iter.hasNext()) {
        toRemove.add((Element) iter.next());
    }
    for (iter = toRemove.iterator(); iter.hasNext();) {
        element.remove((Element) iter.next());
    }
    // Add the new children.
    for (String value : values) {
        Element childElement = element.addElement(childName);
        if (value.startsWith("<![CDATA[")) {
            Iterator it = childElement.nodeIterator();
            while (it.hasNext()) {
                Node node = (Node) it.next();
                if (node instanceof CDATA) {
                    childElement.remove(node);
                    break;
                }
            }
            childElement.addCDATA(value.substring(9, value.length() - 3));
        } else {
            childElement.setText(XmlEscapers.xmlContentEscaper().escape(value));// StringEscapeUtils.escapeXml(value));
        }
    }
    saveProperties();

    // Generate event.
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("value", values);

    // PropertyEventDispatcher.dispatchEvent(name,
    // PropertyEventDispatcher.EventType.xml_property_set, params);

}

From source file:architecture.ee.util.xml.XmlProperties.java

License:Apache License

/**
 * Sets the value of the specified property. If the property doesn't
 * currently exist, it will be automatically created.
 *
 * @param name//from  ww  w. java2s  .  c  o  m
 *            the name of the property to set.
 * @param value
 *            the new value for the property.
 */
public synchronized void setProperty(String name, String value) {

    if (!XmlEscapers.xmlContentEscaper().escape(name).equals(name)) {
        throw new IllegalArgumentException();// L10NUtils.getMessage("002155"));
    }
    if (name == null) {
        return;
    }
    if (value == null) {
        value = "";
    }

    // Set cache correctly with prop name and value.
    propertyCache.put(name, value);

    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy.
    Element element = document.getRootElement();
    for (String aPropName : propName) {
        // If we don't find this part of the property in the XML heirarchy
        // we add it as a new node
        if (element.element(aPropName) == null) {
            element.addElement(aPropName);
        }
        element = element.element(aPropName);
    }
    // Set the value of the property in this node.
    if (value.startsWith("<![CDATA[")) {
        Iterator it = element.nodeIterator();
        while (it.hasNext()) {
            Node node = (Node) it.next();
            if (node instanceof CDATA) {
                element.remove(node);
                break;
            }
        }
        element.addCDATA(value.substring(9, value.length() - 3));
    } else {
        element.setText(value);
    }
    // Write the XML properties to disk
    saveProperties();

    // Generate event.
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("value", value);

    // PropertyEventDispatcher.dispatchEvent(name,
    // PropertyEventDispatcher.EventType.xml_property_set, params);
}

From source file:at.kc.tugraz.ss.serv.dataimport.impl.fct.op.SSDataImportAchsoFct.java

License:Apache License

public static List<SSi5CloudAchsoVideo> getVideoObjs(final SSUri userUri, final String vidXML)
        throws Exception {

    try {/*w w  w  . j ava2 s  .c  o  m*/
        final Document document = DocumentHelper.parseText(vidXML);
        final Element rootElement = document.getRootElement();
        final Iterator vidIterator = rootElement.elementIterator();
        final List<SSi5CloudAchsoVideo> videos = new ArrayList<>();
        Iterator vidContentIterator;
        Element vid;
        Element vidContent;
        String title;
        String video_uri;
        String creator;
        Long created_at;
        List<String> annotations;
        List<String> keywords;
        SSi5CloudAchsoVideo videoObj;

        while (vidIterator.hasNext()) {

            vid = (Element) vidIterator.next();
            vidContentIterator = vid.elementIterator();
            title = null;
            video_uri = null;
            creator = null;
            created_at = null;
            annotations = new ArrayList<>();
            keywords = new ArrayList<>();

            while (vidContentIterator.hasNext()) {

                vidContent = (Element) vidContentIterator.next();

                switch (SSI5CloudAchsoVideoMetaDataE.get(vidContent.getName())) {

                case title:
                    title = (String) vidContent.getData();
                    break;
                case video_uri:
                    video_uri = (String) vidContent.getData();
                    break;
                case creator:
                    creator = (String) vidContent.getData();
                    break;
                case created_at:
                    created_at = getCreationTime((String) vidContent.getData());
                    break;
                case keywords:
                    keywords = getKeywords(vidContent.nodeIterator());
                    break;
                case annotations:
                    annotations = getAnnotations(vidContent.nodeIterator());
                    break;
                }
            }

            try {
                videoObj = SSi5CloudAchsoVideo.get(SSUri.get(video_uri), SSLabel.get(title),
                        SSLabel.get(creator), keywords, annotations);

                videoObj.creationTime = created_at;

                videos.add(videoObj);

            } catch (Exception error) {
                SSLogU.warn("couldnt add vid: " + video_uri);
            }
        }

        return videos;
    } catch (Exception error) {
        SSServErrReg.regErrThrow(error);
        return null;
    }
}

From source file:ch.javasoft.xml.config.XmlPrint.java

License:BSD License

/**
 * Print the given element using the given print writer and initial 
 * indention//from  ww  w  .  j a  va  2s  .c  o m
 * @param elem      the xml element
 * @param indention   the initial indention
 * @param writer   the print writer to use for the output
 */
@SuppressWarnings("unchecked")
public void print(Element elem, String indention, PrintWriter writer) {
    writer.print(indention + "<" + elem.getName());

    Iterator<Attribute> itAtt = elem.attributeIterator();
    Iterator<Element> itElem = elem.elementIterator();

    if (elem.hasMixedContent() || (elem.hasContent() && !itElem.hasNext())) {
        Iterator<Node> it = elem.nodeIterator();
        while (it.hasNext()) {
            Node node = it.next();
            if (node instanceof CharacterData) {
                if (!(node instanceof Comment) && node.getText().trim().length() != 0) {
                    throw new IllegalArgumentException(
                            "text content not supported: \"" + node.getText() + "\"");
                }
            } else if (!(node instanceof Element || node instanceof Attribute)) {
                throw new IllegalArgumentException("only attributes and elements are supported");
            }
        }
    }
    while (itAtt.hasNext()) {
        Attribute att = itAtt.next();
        final String attName = att.getName();
        final String attValue = att.getValue();
        writer.print(" " + attName + "=\"" + escapeAttributeValue(attValue) + "\"");
    }
    if (!itElem.hasNext()) {
        writer.println("/>");
    } else {
        writer.println(">");
        while (itElem.hasNext()) {
            print(itElem.next(), indention + getIndention(), writer);
        }
        writer.println(indention + "</" + elem.getName() + ">");
    }
    writer.flush();
}

From source file:cn.b2b.index.product.create.ProductDataFromDB.java

private String getBrand(String xml) {
    Document doc;//from  www.j ava  2s .c om
    try {
        doc = DocumentHelper.parseText(xml);

        Element rootNode = doc.getRootElement();
        Iterator iter = rootNode.nodeIterator();
        while (iter.hasNext()) {
            Element recordEle = (Element) iter.next();
            String title = recordEle.getName();
            if (title.equals("P1")) {
                return recordEle.getText();
            }
            return "";
        }
    } catch (DocumentException e) {
        // e.printStackTrace();
    }
    return "";
}

From source file:cn.b2b.index.product.create.ProductDataFromDB.java

private String getItemByXml(String xml, String nodeName) {
    Document doc;/*from   ww  w.j  a  v a2  s . c  om*/
    try {
        doc = DocumentHelper.parseText(xml);

        Element rootNode = doc.getRootElement();
        Iterator iter = rootNode.nodeIterator();
        while (iter.hasNext()) {
            Element recordEle = (Element) iter.next();
            String title = recordEle.getName();
            if (title.equals(nodeName)) {
                return recordEle.getText();
            }
            return "";
        }
    } catch (DocumentException e) {
        // e.printStackTrace();
    }
    return "";
}

From source file:cn.com.iscs.base.util.XMLProperties.java

License:Open Source License

/**
 * Sets a property to an array of values. Multiple values matching the same
 * property is mapped to an XML file as multiple elements containing each
 * value. For example, using the name "foo.bar.prop", and the value string
 * array containing {"some value", "other value", "last value"} would produce
 * the following XML://from   w w  w  .  j a va2  s. c o  m
 * 
 * <pre>
 * &lt;foo&gt;
 *     &lt;bar&gt;
 *         &lt;prop&gt;some value&lt;/prop&gt;
 *         &lt;prop&gt;other value&lt;/prop&gt;
 *         &lt;prop&gt;last value&lt;/prop&gt;
 *     &lt;/bar&gt;
 * &lt;/foo&gt;
 * </pre>
 * 
 * @param name
 *          the name of the property.
 * @param values
 *          the values for the property (can be empty but not null).
 */
public void setProperties(String name, List<String> values) {
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy,
    // stopping one short.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        // If we don't find this part of the property in the XML heirarchy
        // we add it as a new node
        if (element.element(propName[i]) == null) {
            element.addElement(propName[i]);
        }
        element = element.element(propName[i]);
    }
    String childName = propName[propName.length - 1];
    // We found matching property, clear all children.
    List<Element> toRemove = new ArrayList<Element>();
    Iterator iter = element.elementIterator(childName);
    while (iter.hasNext()) {
        toRemove.add((Element) iter.next());
    }
    for (iter = toRemove.iterator(); iter.hasNext();) {
        element.remove((Element) iter.next());
    }
    // Add the new children.
    for (String value : values) {
        Element childElement = element.addElement(childName);
        if (value.startsWith("<![CDATA[")) {
            Iterator it = childElement.nodeIterator();
            while (it.hasNext()) {
                Node node = (Node) it.next();
                if (node instanceof CDATA) {
                    childElement.remove(node);
                    break;
                }
            }
            childElement.addCDATA(value.substring(9, value.length() - 3));
        } else {
            childElement.setText(StringEscapeUtils.escapeXml(value));
        }
    }
    saveProperties();

    // Generate event.
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("value", values);
    //    PropertyEventDispatcher.dispatchEvent(name, PropertyEventDispatcher.EventType.xml_property_set, params);
}

From source file:cn.com.iscs.base.util.XMLProperties.java

License:Open Source License

/**
 * Sets the value of the specified property. If the property doesn't currently
 * exist, it will be automatically created.
 * /*from   w  ww  . j  a  va 2s  .co m*/
 * @param name
 *          the name of the property to set.
 * @param value
 *          the new value for the property.
 */
public synchronized void setProperty(String name, String value) {
    if (!StringEscapeUtils.escapeXml(name).equals(name)) {
        throw new IllegalArgumentException("Property name cannot contain XML entities.");
    }
    if (name == null) {
        return;
    }
    if (value == null) {
        value = "";
    }

    // Set cache correctly with prop name and value.
    propertyCache.put(name, value);

    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy.
    Element element = document.getRootElement();
    for (String aPropName : propName) {
        // If we don't find this part of the property in the XML heirarchy
        // we add it as a new node
        if (element.element(aPropName) == null) {
            element.addElement(aPropName);
        }
        element = element.element(aPropName);
    }
    // Set the value of the property in this node.
    if (value.startsWith("<![CDATA[")) {
        Iterator it = element.nodeIterator();
        while (it.hasNext()) {
            Node node = (Node) it.next();
            if (node instanceof CDATA) {
                element.remove(node);
                break;
            }
        }
        element.addCDATA(value.substring(9, value.length() - 3));
    } else {
        element.setText(value);
    }
    // Write the XML properties to disk
    saveProperties();

    // Generate event.
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("value", value);
    //    PropertyEventDispatcher.dispatchEvent(name, PropertyEventDispatcher.EventType.xml_property_set, params);
}