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:org.onosproject.yang.serializers.xml.DefaultXmlWalker.java

License:Apache License

/**
 * Determine the type of an element.//  w w  w.  ja va  2s  .  com
 *
 * @param element to be analysed
 * @return type of the element
 */
private XmlNodeType getElementType(Element element) {
    Element newElement = element.createCopy();
    newElement.remove(element.getNamespace());
    return newElement.hasContent() && newElement.isTextOnly() ? TEXT_NODE : OBJECT_NODE;
}

From source file:org.onosproject.yang.serializers.xml.XmlSerializer.java

License:Apache License

@Override
public CompositeData decode(CompositeStream external, YangSerializerContext context) {

    try {/*from ww  w.  ja va  2s .co  m*/
        //parse XML input
        String xmlInput = addRootElementWithAnnotation(external.resourceData(),
                context.getProtocolAnnotations());
        Document document = DocumentHelper.parseText(xmlInput);

        // initialize all the required builders
        CompositeData.Builder cBuilder = DefaultCompositeData.builder();
        ResourceData.Builder rdBuilder = DefaultResourceData.builder();
        XmlWalker walker = new DefaultXmlWalker();
        XmlSerializerListener listener = new XmlSerializerListener();
        listener.cBuilder(cBuilder);

        String uri = external.resourceId();
        if (uri == null) {
            listener.dnBuilder(initializeDataNode(context));
            walker.walk(listener, document.getRootElement(), document.getRootElement());
            rdBuilder = rdBuilder.addDataNode(listener.dnBuilder().build());
        } else {
            /*
             * If URI is not null, then each first level elements is
             * converted to data node and added to list of data nodes in
             * resource data
             */
            ResourceId.Builder rIdBuilder = convertUriToRid(uri, context);
            Element rootElement = document.getRootElement();
            if (rootElement.hasContent() && !rootElement.isTextOnly()) {
                Iterator i = rootElement.elementIterator();
                while (i.hasNext()) {
                    Element childElement = (Element) i.next();
                    listener.dnBuilder(initializeDataNode(rIdBuilder));
                    walker.walk(listener, childElement, rootElement);
                    rdBuilder = rdBuilder.addDataNode(listener.dnBuilder().build());
                }
            }
            rdBuilder.resourceId(rIdBuilder.build());
        }
        return cBuilder.resourceData(rdBuilder.build()).build();
    } catch (DocumentException e) {
        throw new XmlSerializerException(e.getMessage());
    } catch (IOException e) {
        throw new XmlSerializerException(e.getMessage());
    }
}

From source file:org.onosproject.yms.app.ych.defaultcodecs.netconf.NetconfCodec.java

License:Apache License

/**
 * Returns the data root element based on the NETCONF operation parameter.
 *
 * @param rootElement root element of document tree to find the root node
 * @param opType      protocol operation being performed
 * @return the data root node element//from   www.ja v a  2  s  . c o  m
 */
public Element getDataRootElement(Element rootElement, YmsOperationType opType) {

    Element retElement = null;
    String elementName = rootElement.getName();
    try {
        validateOpType(elementName, opType);
        // If config tag name is found then set the root element node.
        if (DATA.equals(elementName) || CONFIG.equals(elementName) || FILTER.equals(elementName)) {
            return rootElement;
        }

        // If element has child node then traverse through the child node
        // by recursively calling getDataRootElement method.
        if (rootElement.hasContent() && !rootElement.isTextOnly()) {
            for (Iterator i = rootElement.elementIterator(); i.hasNext();) {
                Element childElement = (Element) i.next();
                retElement = getDataRootElement(childElement, opType);
            }
        }
    } catch (Exception e) {
        // TODO
    }

    return retElement;
}

From source file:org.onosproject.yms.app.ych.defaultcodecs.xml.DefaultXmlCodecWalker.java

License:Apache License

@Override
public void walk(XmlListener listener, Element element, Element rootElement) {
    try {/* ww w. j a va  2 s  .com*/
        Element newElement = element.createCopy();
        newElement.remove(element.getNamespace());

        listener.enterXmlElement(element, getElementType(newElement), rootElement);

        if (element.hasContent() && !element.isTextOnly()) {
            for (Iterator i = element.elementIterator(); i.hasNext();) {
                Element childElement = (Element) i.next();
                walk(listener, childElement, rootElement);
            }
        }

        listener.exitXmlElement(element, getElementType(element), rootElement);
    } catch (Exception e) {
        log.error("Exception occurred when walk xml element: {}", element);
    }
}

From source file:org.onosproject.yms.app.ych.defaultcodecs.xml.DefaultXmlCodecWalker.java

License:Apache License

/**
 * Determine the type of an element./*  ww w . j  a v  a  2 s  .com*/
 *
 * @param element to be analysed
 * @return type of the element
 */
private XmlNodeType getElementType(Element element) {
    return element.hasContent() && element.isTextOnly() ? TEXT_NODE : OBJECT_NODE;
}

From source file:org.snipsnap.snip.storage.SerializerSupport.java

License:Open Source License

/**
 * Produce a map with all elements and its values found below the top element.
 * @param el the root element//ww w .  j av a2s . co m
 * @return the map with names and values
 */
public Map getElementMap(Element el) {
    Map elements = new HashMap();
    Iterator childIterator = el.elementIterator();
    while (childIterator.hasNext()) {
        Element element = (Element) childIterator.next();
        if (element.isTextOnly()) {
            elements.put(element.getName(), notNull(element.getText()));
        } else {
            elements.put(element.getName(), element);
        }
    }
    return elements;
}