Example usage for org.dom4j Element isTextOnly

List of usage examples for org.dom4j Element isTextOnly

Introduction

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

Prototype

boolean isTextOnly();

Source Link

Document

Returns true if this Element has text only content.

Usage

From source file:com.heren.turtle.server.utils.XmlUtils.java

License:Open Source License

public static Map<String, Object> getMessageReceiveSample(String message) throws DocumentException {
    message = replaceWrongPart(message);
    Document document = DocumentHelper.parseText(message);
    Element rootElement = document.getRootElement();
    Element request = rootElement.element("request");
    List elements = request.elements();
    Map<String, Object> result = new HashMap<>();
    for (Object element : elements) {
        Element subElement = (Element) element;
        String name = subElement.getName();
        if (name.equalsIgnoreCase("item")) {
            List subElements = subElement.elements();
            for (Object subEle : subElements) {
                Element grantSubElement = (Element) subEle;
                result.put(grantSubElement.getName(), grantSubElement.getTextTrim());
            }/*w  ww . j  a  va  2  s.co  m*/
        } else if (subElement.isTextOnly()) {
            result.put(name, subElement.getTextTrim());
        }
    }
    return result;
}

From source file:com.heren.turtle.server.utils.XmlUtils.java

License:Open Source License

public static List<Map<String, Object>> getMessageReceiveSampleContainItems(String message)
        throws DocumentException {
    message = replaceWrongPart(message);
    Document document = DocumentHelper.parseText(message);
    Element rootElement = document.getRootElement();
    Element request = rootElement.element("request");
    Element items = request.element("items");
    List elements = items.elements();
    List<Map<String, Object>> resultList = new ArrayList<>();
    for (Object element : elements) {
        Map<String, Object> result = new HashMap<>();
        Element subElement = (Element) element;
        String name = subElement.getName();
        if (name.equalsIgnoreCase("item")) {
            List subElements = subElement.elements();
            for (Object subEle : subElements) {
                Element grantSubElement = (Element) subEle;
                result.put(grantSubElement.getName(), grantSubElement.getTextTrim());
            }/*from  w w w.  j  a  v  a  2s .  c  o m*/
        } else if (subElement.isTextOnly()) {
            result.put(name, subElement.getTextTrim());
        }
        resultList.add(result);
    }
    return resultList;
}

From source file:com.heren.turtle.server.utils.XmlUtils.java

License:Open Source License

/**
 * delete the label of xml that is not use
 *
 * @param message/*from  w  w w.j  av a  2  s.  c om*/
 * @return
 */
public static Map<String, Object> getMessageContainsMap(String message) throws DocumentException {
    message = replaceWrongPart(message);
    Document document = DocumentHelper.parseText(message);
    Element rootElement = document.getRootElement();
    Element request = rootElement.element("request");
    List elements = request.elements();
    Map<String, Object> result = new HashMap<>();
    for (Object element : elements) {
        Element subElement = (Element) element;
        if (subElement.isTextOnly()) {
            result.put(subElement.getName(), subElement.getTextTrim());
        } else {
            List<Map<String, String>> subList = new ArrayList<>();
            List subEle = subElement.elements();
            for (Object aSubEle : subEle) {
                Element itemElements = (Element) aSubEle;
                Map<String, String> subMap = new HashMap<>();
                List grandSubElements = itemElements.elements();
                for (Object grandSubElement : grandSubElements) {
                    Element grandItemElements = (Element) grandSubElement;
                    subMap.put(grandItemElements.getName().trim(), grandItemElements.getTextTrim());
                }
                subList.add(subMap);
            }
            result.put(subElement.getName(), subList);
        }
    }
    return result;
}

From source file:com.seer.datacruncher.utils.generic.StreamsUtils.java

License:Open Source License

/**
 * Recursive method that parses all xml childs.
 * /*  ww w.j a  v  a2  s  . co  m*/
 * @param elem - current dom element.
 * @param xmlTextNodes - text node list
 */
private static void getXmlChilds(Element elem, List<Element> xmlTextNodes) {
    for (@SuppressWarnings("unchecked")
    Iterator<Element> i = elem.elementIterator(); i.hasNext();) {
        Element el = i.next();
        if (el.isTextOnly()) {
            xmlTextNodes.add(el);
        } else {
            getXmlChilds(el, xmlTextNodes);
        }
    }
}

From source file:cz.muni.stanse.utils.xmlpatterns.XMLPattern.java

License:GNU General Public License

private static boolean matchingElements(final Element XMLpivot, final Element XMLelement,
        final XMLPatternVariablesAssignment varsAssignment) {
    if (XMLpivot.getName().equals("nested")) {
        final String elementName = XMLelement.getName();
        for (final Iterator<Attribute> j = XMLpivot.attributeIterator(); j.hasNext();)
            if (elementName.equals(j.next().getValue()))
                return false;
        return onNested(XMLpivot, XMLelement, varsAssignment);
    }/*w  w  w  .j  a  v a 2  s .c o  m*/
    if (XMLpivot.getName().equals("ignore"))
        return true;
    if (XMLpivot.getName().equals("var")) {
        if (!satisfyVarConstraints(XMLelement.getName(), XMLpivot.attribute("match"),
                XMLpivot.attribute("except")))
            return false;
        varsAssignment.put(XMLpivot.attribute("name").getValue(), XMLelement);
        return true;
    }

    if (!XMLpivot.getName().equals(XMLelement.getName()))
        return false;
    if (!matchingAttributes(XMLpivot.attributes(), XMLelement))
        return false;
    if (XMLpivot.isTextOnly() != XMLelement.isTextOnly())
        return false;
    if (XMLpivot.isTextOnly() && !XMLpivot.getText().equals(XMLelement.getText()))
        return false;

    final Iterator<Element> i = XMLpivot.elementIterator();
    final Iterator<Element> j = XMLelement.elementIterator();
    while (i.hasNext() && j.hasNext()) {
        final Element pivotNext = i.next();
        if (pivotNext.getName().equals("any"))
            return true;
        if (!matchingElements(pivotNext, j.next(), varsAssignment))
            return false;
    }
    if (i.hasNext() || j.hasNext())
        return false;

    return true;
}

From source file:dk.netarkivet.common.utils.XmlTree.java

License:Open Source License

/**
 * Returns true if the given node is a leaf (i.e. has only text).
 *
 * @param e A node to check//from  www.  java  2  s.  c  om
 * @return True if the given node is a leaf.
 */
private boolean elementIsLeaf(Element e) {
    return e.isTextOnly();
}

From source file:dk.netarkivet.deploy.EvaluateConfigFile.java

License:Open Source License

/**
 * Evaluates a element (has to called with the settings branch). Then tries to evaluate all the branches to the
 * element. The method is called recursively for the children of curElem.
 *
 * @param curElem The current element to evaluate. Null element represents in this context that no settings branch
 * exists for the current instance.//from   www  .j a  va 2 s . c  o  m
 */
@SuppressWarnings("unchecked")
private void evaluateElement(Element curElem) {
    // make sure to catch null-pointers
    if (curElem == null) {
        return;
    }
    List<Element> elList = curElem.elements();
    for (Element el : elList) {
        boolean valid = false;
        // get path
        String path = getSettingsPath(el);

        // check if path exists in any default setting.
        valid = existBranch(completeSettings, path.split(Constants.SLASH));

        if (valid) {
            if (!el.isTextOnly()) {
                evaluateElement(el);
            }
        } else {
            // Print out the 'illegal' branches.
            System.out.println("Branch in settings not found: " + path.replace(Constants.SLASH, Constants.DOT));
        }
    }
}

From source file:dk.netarkivet.deploy.XmlStructure.java

License:Open Source License

/**
 * Retrieves the content of a branch deep in tree structure.
 *
 * @param name Specifies the path in the tree (e.g. in HTML: GetSubChildValue("HTML", "HEAD", "TITLE") to get the
 * title of a HTML document)/*from  www .  j  ava2 s  .co  m*/
 * @return The content of the leaf. If it is not a leaf, the entire XML-branch is returned. Returns 'null' if the
 * path to the branch cannot be found.
 */
public String getSubChildValue(String... name) {
    ArgumentNotValid.checkNotNull(name, "String ...name");
    Element e = getSubChild(name);
    if (e != null) {
        if (e.isTextOnly()) {
            return e.getText().trim();
        } else {
            log.debug("Element is not text. The entire XML-branch is returned.");
            return e.asXML();
        }
    } else {
        return null;
    }
}

From source file:dk.netarkivet.deploy.XmlStructure.java

License:Open Source License

/**
 * Retrieves the content of a branch deep in tree structure.
 *
 * @param path Specifies the path in the tree (e.g. in HTML: GetSubChildValue("HTML", "HEAD", "TITLE") to get the
 * title of a HTML document)//from   w  w w . j  a v  a2 s  . co  m
 * @return The content of the leaf. If it is not a leaf, return null. Returns 'null' if the path to the branch
 * cannot be found.
 */
public String getLeafValue(String... path) {
    ArgumentNotValid.checkNotNull(path, "String ...name");
    Element e = getSubChild(path);
    if (e != null && e.isTextOnly()) {
        return e.getText().trim();
    } else {
        log.debug("Element is not text. Null returned.");
        return null;
    }
}

From source file:dk.netarkivet.deploy.XmlStructure.java

License:Open Source License

/**
 * The current tree will be overwritten by the overwriter tree. The new branches in overwriter will be added to the
 * current tree. For the leafs which are present in both overwriter and current, the value in the current-leaf will
 * be overwritten by the overwriter-leaf.
 * <p>/*from w ww  . j av  a2s  . co  m*/
 * The subtrees which exists in both the overwriter and the current tree, this function will be run recursively on
 * these subtrees.
 *
 * @param current The base element
 * @param overwriter The element to have its values overwrite the base element
 * @throws IllegalState If a leaf in current is about to be replaced by a tree
 */
@SuppressWarnings("unchecked")
private void overWriting(Element current, Element overwriter) throws IllegalState {
    ArgumentNotValid.checkNotNull(current, "Element current");
    ArgumentNotValid.checkNotNull(overwriter, "Element overwriter");
    // get the attributes to be overwritten
    List<Element> attributes = overwriter.elements();
    List<Element> addElements = new ArrayList<Element>();

    // add branch if it does not exists
    for (Element e : attributes) {
        // find corresponding attribute in current element
        List<Element> curElems = current.elements(e.getName());

        // if no such elements in current tree, add branch.
        if (curElems.isEmpty()) {
            addElements.add(e);
        } else {
            //
            List<Element> overElems = overwriter.elements(e.getName());

            // if the lists have a 1-1 ratio, then overwrite
            if (curElems.size() == 1 && overElems.size() == 1) {
                // only one branch, thus overwrite
                Element curE = curElems.get(0);
                // if leaf overwrite value, otherwise repeat for branches.
                if (curE.isTextOnly()) {
                    curE.setText(e.getText().trim()); // TODO Is this necessary
                } else {
                    overWriting(curE, e);
                }
            } else {
                // a different amount of current branches exist (not 0).
                // Therefore remove the branches in current tree,
                // and add replacements.
                for (Element curE : curElems) {
                    current.remove(curE);
                }
                // add only current branch, since the others will follow.
                addElements.add(e);
            }
        }
    }

    // add all the new branches to the current branch.
    for (Element e : addElements) {
        current.add(e.createCopy());
    }
}