Example usage for org.dom4j Node getNodeType

List of usage examples for org.dom4j Node getNodeType

Introduction

In this page you can find the example usage for org.dom4j Node getNodeType.

Prototype

short getNodeType();

Source Link

Document

Returns the code according to the type of node.

Usage

From source file:org.orbeon.oxf.xml.dom4j.NonLazyUserDataElement.java

License:Open Source License

/**
 * @return A clone.  The clone will have parent == null but will have any necessary namespace
 *         declarations this element's ancestors.
 *//*from  w  w  w . j av  a2s  .  c o  m*/
public Object clone() {
    final NonLazyUserDataElement ret = cloneInternal();
    org.dom4j.Element anstr = getParent();
    done: if (anstr != null) {
        final NodeComparator nc = new NodeComparator();
        final java.util.TreeSet nsSet = new java.util.TreeSet(nc);

        do {
            final java.util.List sibs = anstr.content();
            for (final java.util.Iterator itr = sibs.iterator(); itr.hasNext();) {
                final org.dom4j.Node sib = (org.dom4j.Node) itr.next();
                if (sib.getNodeType() != org.dom4j.Node.NAMESPACE_NODE)
                    continue;
                nsSet.add(sib);
            }
            anstr = anstr.getParent();
        } while (anstr != null);
        if (nsSet.isEmpty())
            break done;
        for (final java.util.Iterator itr = nsSet.iterator(); itr.hasNext();) {
            final org.dom4j.Namespace ns = (org.dom4j.Namespace) itr.next();
            final String pfx = ns.getPrefix();
            if (ret.getNamespaceForPrefix(pfx) != null)
                continue;
            ret.add(ns);
        }
    }
    return ret;
}

From source file:org.orbeon.oxf.xml.dom4j.NonLazyUserDataElement.java

License:Open Source License

/**
 * If parent != null checks with ancestors and removes any redundant namespace declarations.
 */// ww w .  j a va  2s . co m
public void setParent(final org.dom4j.Element prnt) {
    super.setParent(prnt);
    done: if (prnt != null) {
        final org.dom4j.Namespace myNs = getNamespace();
        if (myNs != org.dom4j.Namespace.NO_NAMESPACE) {
            final String myPfx = myNs.getPrefix();
            final org.dom4j.Namespace prntNs = prnt.getNamespaceForPrefix(myPfx);
            if (myPfx.equals(prntNs)) {
                final String myNm = myNs.getName();
                final org.dom4j.QName newNm = new org.dom4j.QName(myNm);
                setQName(newNm);
            }
        }
        if (content == null)
            break done;
        for (final java.util.Iterator itr = content.iterator(); itr.hasNext();) {
            final org.dom4j.Node chld = (org.dom4j.Node) itr.next();
            if (chld.getNodeType() != org.dom4j.Node.NAMESPACE_NODE)
                continue;

            final org.dom4j.Namespace ns = (org.dom4j.Namespace) chld;
            final String pfx = ns.getPrefix();

            final org.dom4j.Namespace prntNs = prnt.getNamespaceForPrefix(pfx);
            if (ns.equals(prntNs))
                itr.remove();
        }
    }
}

From source file:org.orbeon.saxon.dom4j.NodeWrapper.java

License:Open Source License

protected static NodeWrapper makeWrapperImpl(Object node, DocumentWrapper docWrapper, NodeWrapper parent,
        int index) {

    NodeWrapper wrapper;/*from  w ww  .  ja v a  2 s  .com*/
    final Node dom4jNode = (Node) node;
    switch (dom4jNode.getNodeType()) {
    case Type.DOCUMENT:
        return docWrapper;
    case Type.ELEMENT:
    case Type.ATTRIBUTE:
    case Type.COMMENT:
    case Type.PROCESSING_INSTRUCTION:
    case Type.NAMESPACE:
    case Type.TEXT:
        wrapper = new NodeWrapper(node, parent, index);
        wrapper.nodeKind = dom4jNode.getNodeType();
        break;
    case 4: // dom4j CDATA
        wrapper = new NodeWrapper(node, parent, index);
        wrapper.nodeKind = Type.TEXT;
        break;
    default:
        throw new IllegalArgumentException(
                "Bad node type in dom4j: " + node.getClass() + " instance " + node.toString());
    }

    wrapper.docWrapper = docWrapper;
    return wrapper;
}

From source file:org.orbeon.saxon.dom4j.TypedNodeWrapper.java

License:Open Source License

static NodeWrapper makeTypedWrapper(Object node, DocumentWrapper docWrapper, NodeWrapper parent, int index) {
    NodeWrapper wrapper;/*from w w w .  ja va  2 s.co  m*/
    final Node dom4jNode = (Node) node;
    switch (dom4jNode.getNodeType()) {
    case Type.DOCUMENT:
        return docWrapper;
    case Type.ELEMENT:
    case Type.ATTRIBUTE:
    case Type.COMMENT:
    case Type.PROCESSING_INSTRUCTION:
    case Type.NAMESPACE:
    case Type.TEXT:
        wrapper = new TypedNodeWrapper(node, parent, index);
        wrapper.nodeKind = dom4jNode.getNodeType();
        break;
    case 4: // dom4j CDATA
        wrapper = new TypedNodeWrapper(node, parent, index);
        wrapper.nodeKind = Type.TEXT;
        break;
    default:
        throw new IllegalArgumentException(
                "Bad node type in dom4j: " + node.getClass() + " instance " + node.toString());
    }

    wrapper.docWrapper = docWrapper;
    return wrapper;
}

From source file:org.pentaho.platform.config.PentahoXml.java

License:Open Source License

public List<AclEntry> getDefaultAcls() {
    List<AclEntry> aclEntries = new ArrayList<>();
    List<Node> nodes = document.selectNodes(ACL_ENTRY_XPATH); //$NON-NLS-1$ //$NON-NLS-2$
    for (Node node : nodes) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;

            AclEntry aclEntry = new AclEntry();
            aclEntry.setPrincipalName(element.attributeValue("role"));
            aclEntry.setPermission(element.attributeValue("acl"));
            aclEntries.add(aclEntry);/* w w w . ja va  2s .c om*/
        }
    }
    return aclEntries;
}

From source file:org.pentaho.platform.config.SystemSettingsConfiguration.java

License:Open Source License

private void addElementsToProperties(List elements, Properties props, String parentPath) {

    for (java.lang.Object o : elements) {
        Node ele = (Node) o;
        if (ele.getNodeType() != 1) { // text
            continue;
        }/*w  w  w .  ja  v  a  2  s . com*/
        String contents = ele.getText().trim();

        String newParentPath = "";

        if (!StringUtils.isEmpty(parentPath)) {
            newParentPath = parentPath + ".";
        }
        newParentPath += ele.getName();

        if (!StringUtils.isEmpty(contents)) {
            props.setProperty(newParentPath, contents);
        }
        if (ele instanceof Element) {
            List children = ((Element) ele).content();

            addElementsToProperties(children, props, newParentPath);
        }

    }
}

From source file:org.xwiki.tool.xar.XWikiXMLWriter.java

License:Open Source License

@Override
protected void writeNodeText(Node node) throws IOException {
    if (this.useFormat && node.getText().trim().length() == 0) {
        // Check if parent node contains non text nodes
        boolean containsNonTextNode = false;
        for (Object object : node.getParent().content()) {
            Node objectNode = (Node) object;
            if (objectNode.getNodeType() != Node.TEXT_NODE) {
                containsNonTextNode = true;
                break;
            }/*  w  w w . j av a  2  s. c om*/
        }
        if (containsNonTextNode) {
            // Don't do anything, i.e. don't print the current text node
        } else {
            super.writeNodeText(node);
        }
    } else {
        super.writeNodeText(node);
    }
}

From source file:org.zenonpagetemplates.onePhaseImpl.PageTemplateImpl.java

License:Open Source License

@SuppressWarnings({ "unchecked" })
private void defaultContent(Element element, ContentHandler contentHandler, LexicalHandler lexicalHandler,
        EvaluationHelper evaluationHelper, Stack<Map<String, Slot>> slotStack)
        throws SAXException, PageTemplateException, IOException, EvaluationException {
    // Use default template content
    for (Iterator<Node> i = element.nodeIterator(); i.hasNext();) {
        Node node = i.next();
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            processElement((Element) node, contentHandler, lexicalHandler, evaluationHelper, slotStack);
            break;

        case Node.TEXT_NODE:
            char[] text = node.getText().toCharArray();
            contentHandler.characters(text, 0, text.length);
            break;

        case Node.COMMENT_NODE:
            char[] comment = node.getText().toCharArray();
            lexicalHandler.comment(comment, 0, comment.length);
            break;

        case Node.CDATA_SECTION_NODE:
            lexicalHandler.startCDATA();
            char[] cdata = node.getText().toCharArray();
            contentHandler.characters(cdata, 0, cdata.length);
            lexicalHandler.endCDATA();// w ww  .  j  a  v a2 s  . c om
            break;

        case Node.NAMESPACE_NODE:
            Namespace declared = (Namespace) node;
            //System.err.println( "Declared namespace: " + declared.getPrefix() + ":" + declared.getURI() );
            this.namespaces.put(declared.getPrefix(), declared.getURI());
            //if ( declared.getURI().equals( TAL_NAMESPACE_URI ) ) {
            //    this.talNamespacePrefix = declared.getPrefix();
            //} 
            //else if (declared.getURI().equals( METAL_NAMESPACE_URI ) ) {
            //    this.metalNamespacePrefix = declared.getPrefix();
            //}
            break;

        case Node.ATTRIBUTE_NODE:
            // Already handled
            break;

        case Node.DOCUMENT_TYPE_NODE:
        case Node.ENTITY_REFERENCE_NODE:
        case Node.PROCESSING_INSTRUCTION_NODE:
        default:
            //System.err.println( "WARNING: Node type not supported: " + node.getNodeTypeName() );       
        }
    }
}

From source file:org.zenonpagetemplates.twoPhasesImpl.ZPTDocumentFactory.java

License:Open Source License

@SuppressWarnings({ "unchecked" })
static private void mapContent(Element element, ZPTElement zptElement, ZPTDocument zptDocument,
        Stack<Map<String, Slot>> slotStack) throws SAXException, PageTemplateException, IOException {

    // Use default template content
    for (Iterator<Node> i = element.nodeIterator(); i.hasNext();) {
        Node node = i.next();
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            zptElement.addContent(getNewZPTElement((Element) node, zptDocument, slotStack));
            break;

        case Node.TEXT_NODE:
            zptElement.addContent(new TextNode(node.getText()));
            break;

        case Node.CDATA_SECTION_NODE:
            zptElement.addContent(new CDATANode(node.getText()));
            break;

        case Node.NAMESPACE_NODE: // Already handled
            /*//from  w  w w . j  av  a2s .  c  o m
             Namespace declared = (Namespace)node;
             if (zptDocument.isNamespaceToDeclare(declared)){
                zptDocument.addNamespace(declared);
             } else {
                zptElement.addNamespaceStaticAttribute(declared);
             }
             break;*/

        case Node.ATTRIBUTE_NODE: // Already handled
        case Node.COMMENT_NODE: // Remove all comments
        case Node.DOCUMENT_TYPE_NODE:
        case Node.ENTITY_REFERENCE_NODE:
        case Node.PROCESSING_INSTRUCTION_NODE:
        default:
            // Nothing to do
        }
    }
}

From source file:org.zenonpagetemplates.twoPhasesImpl.ZPTDocumentFactory.java

License:Open Source License

@SuppressWarnings("unchecked")
static private void mapNamespaces(Element element, ZPTElement zptElement, ZPTDocument zptDocument)
        throws PageTemplateException {

    for (Iterator<Attribute> i = element.nodeIterator(); i.hasNext();) {
        Node node = i.next();
        if (node.getNodeType() == Node.NAMESPACE_NODE) {
            Namespace declared = (Namespace) node;
            if (zptDocument.isNamespaceToDeclare(declared)) {
                zptDocument.addNamespace(declared);
            } else {
                zptElement.addNamespaceStaticAttribute(declared);
            }/*  ww  w  . ja v a 2s  . c om*/
        }
    }
}