Example usage for org.dom4j Node ELEMENT_NODE

List of usage examples for org.dom4j Node ELEMENT_NODE

Introduction

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

Prototype

short ELEMENT_NODE

To view the source code for org.dom4j Node ELEMENT_NODE.

Click Source Link

Document

Matches Element nodes

Usage

From source file:com.cladonia.xml.helper.Helper.java

License:Open Source License

private void setElementInternal(Node baseNode, ElementInformation ei) {
    try {//  w ww . ja  v  a  2  s . c o  m
        XElement node = null;

        if (baseNode.getNodeType() == org.dom4j.Node.ATTRIBUTE_NODE) {
            node = (XElement) baseNode.getParent();
        } else if (baseNode.getNodeType() == org.dom4j.Node.ELEMENT_NODE) {
            node = (XElement) baseNode;
        }

        SchemaElement element = null;

        if (ei != null) {

            if (ei instanceof SchemaElement) {
                element = (SchemaElement) ei;
                attributeList.setEnabled(true);
                elementList.setEnabled(true);

                setElementDetails(node.getName(), node.getNamespace().getPrefix(), false);

                setNamespace(node.getNamespace().getURI());

                Vector elementAttributes = new Vector();
                Vector schemaAttributes = new Vector();

                Vector elementChildren = new Vector();
                Vector schemaChildren = new Vector();

                if (element != null) {
                    setText(type, element.getType());

                    Vector modAtts = element.getAttributes();
                    if (modAtts != null) {

                        for (int cnt = 0; cnt < modAtts.size(); ++cnt) {
                            SchemaAttribute sa = (SchemaAttribute) modAtts.get(cnt);
                            schemaAttributes.add(sa);
                        }

                    }

                    Vector modElements = element.getChildElements();
                    if (modElements != null) {

                        for (int cnt = 0; cnt < modElements.size(); ++cnt) {
                            SchemaElement sa = (SchemaElement) modElements.get(cnt);
                            schemaChildren.add(sa);
                        }

                    }

                } else {
                    setType(null);
                }

                for (int cnt = 0; cnt < node.attributeCount(); ++cnt) {

                    elementAttributes.add((XAttribute) node.attribute(cnt));
                }

                XElement[] childElements = node.getElements();
                for (int cnt = 0; cnt < childElements.length; ++cnt) {

                    elementChildren.add((XElement) childElements[cnt]);
                }

                //this.ementList.setElements( element.getElements());
                //this.elementList.setElements( newElementArray);
                //attributeList.setAttributes( element.getAttributes());
                //attributeList.setAttributes( newAttArray);
                attributeList.setAttributes(schemaAttributes, elementAttributes);
                elementList.setElements(schemaChildren, elementChildren);
            } else if (ei instanceof DTDElement) {
                DTDElement dtdElement = (DTDElement) ei;
                attributeList.setEnabled(true);
                elementList.setEnabled(true);

                setElementDetails(node.getName(), node.getNamespace().getPrefix(), false);

                setNamespace(node.getNamespace().getURI());

                Vector elementAttributes = new Vector();
                Vector schemaAttributes = new Vector();

                Vector elementChildren = new Vector();
                Vector schemaChildren = new Vector();

                if (dtdElement != null) {
                    setText(type, dtdElement.getType());

                    Vector modAtts = dtdElement.getAttributes();
                    if (modAtts != null) {

                        for (int cnt = 0; cnt < modAtts.size(); ++cnt) {
                            DTDAttribute sa = (DTDAttribute) modAtts.get(cnt);
                            schemaAttributes.add(sa);
                        }

                    }

                    Vector modElements = dtdElement.getChildElements();
                    if (modElements != null) {

                        for (int cnt = 0; cnt < modElements.size(); ++cnt) {
                            DTDElement sa = (DTDElement) modElements.get(cnt);
                            schemaChildren.add(sa);
                        }

                    }

                } else {
                    setType(null);
                }

                for (int cnt = 0; cnt < node.attributeCount(); ++cnt) {

                    elementAttributes.add((XAttribute) node.attribute(cnt));
                }

                XElement[] childElements = node.getElements();
                for (int cnt = 0; cnt < childElements.length; ++cnt) {

                    elementChildren.add((XElement) childElements[cnt]);
                }

                //this.ementList.setElements( element.getElements());
                //this.elementList.setElements( newElementArray);
                //attributeList.setAttributes( element.getAttributes());
                //attributeList.setAttributes( newAttArray);
                attributeList.setAttributes(schemaAttributes, elementAttributes);
                elementList.setElements(schemaChildren, elementChildren);
            } else if (ei instanceof RNGElement) {
                RNGElement rngElement = (RNGElement) ei;
                attributeList.setEnabled(true);
                elementList.setEnabled(true);

                setElementDetails(node.getName(), node.getNamespace().getPrefix(), false);

                setNamespace(node.getNamespace().getURI());

                Vector elementAttributes = new Vector();
                Vector schemaAttributes = new Vector();

                Vector elementChildren = new Vector();
                Vector schemaChildren = new Vector();

                if (rngElement != null) {
                    setText(type, rngElement.getType());

                    Vector modAtts = rngElement.getAttributes();
                    if (modAtts != null) {

                        for (int cnt = 0; cnt < modAtts.size(); ++cnt) {
                            RNGAttribute sa = (RNGAttribute) modAtts.get(cnt);
                            schemaAttributes.add(sa);
                        }

                    }

                    Vector modElements = rngElement.getChildElements();
                    if (modElements != null) {

                        for (int cnt = 0; cnt < modElements.size(); ++cnt) {
                            RNGElement sa = (RNGElement) modElements.get(cnt);
                            schemaChildren.add(sa);
                        }

                    }

                } else {
                    setType(null);
                }

                for (int cnt = 0; cnt < node.attributeCount(); ++cnt) {

                    elementAttributes.add((XAttribute) node.attribute(cnt));
                }

                XElement[] childElements = node.getElements();
                for (int cnt = 0; cnt < childElements.length; ++cnt) {

                    elementChildren.add((XElement) childElements[cnt]);
                }

                //this.ementList.setElements( element.getElements());
                //this.elementList.setElements( newElementArray);
                //attributeList.setAttributes( element.getAttributes());
                //attributeList.setAttributes( newAttArray);
                attributeList.setAttributes(schemaAttributes, elementAttributes);
                elementList.setElements(schemaChildren, elementChildren);
            } else {

            }
        } else {
            //System.out.println("one is null");
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.cladonia.xml.XMLFormatter.java

License:Mozilla Public License

public void startElement(String namespaceURI, String localName, String qName, Attributes attributes)
        throws SAXException {
    if (DEBUG)//from w w  w.  j  a va2s  . co m
        System.out.println("XMLFormatter.startElement( " + namespaceURI + ", " + localName + ", " + qName + ", "
                + attributes + ")");
    try {
        writePrintln();
        indent();
        writer.write("<");
        writer.write(qName);
        writeNamespaces();
        writeAttributes(attributes);
        writer.write(">");
        ++indentLevel;
        lastOutputNodeType = Node.ELEMENT_NODE;

        super.startElement(namespaceURI, localName, qName, attributes);
    } catch (IOException e) {
        handleException(e);
    }
}

From source file:com.cladonia.xml.XMLFormatter.java

License:Mozilla Public License

public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
    if (DEBUG)/*ww w.  j  a  v  a2 s. co  m*/
        System.out.println("XMLFormatter.endElement( " + namespaceURI + ", " + localName + ", " + qName + ")");
    try {
        --indentLevel;
        if (lastOutputNodeType == Node.ELEMENT_NODE) {
            writePrintln();
            indent();
        }

        // XXXX: need to determine this using a stack and checking for
        // content / children
        boolean hadContent = true;
        if (hadContent) {
            writeClose(qName);
        } else {
            writeEmptyElementClose(qName);
        }
        lastOutputNodeType = Node.ELEMENT_NODE;

        super.endElement(namespaceURI, localName, qName);
    } catch (IOException e) {
        handleException(e);
    }
}

From source file:com.cladonia.xml.XMLFormatter.java

License:Mozilla Public License

protected void writeElement(Element element) throws IOException {
    if (DEBUG)//from   ww w .  ja  va 2s .c om
        System.out.println("XMLFormatter.writeElement( " + element + ")");
    //      if ( indentMixed) {
    //         super.writeElement( element);
    //      } else {
    int size = element.nodeCount();
    String qualifiedName = element.getQualifiedName();

    boolean hasElement = false;
    boolean hasText = false;

    // first test to see if this element has mixed content, 
    // if whitespace is significant ...
    for (int i = 0; i < size; i++) {
        Node node = element.node(i);

        if (node instanceof Element) {
            hasElement = true;
        } else if (node instanceof Text) {
            String text = node.getText();

            if (text != null && text.trim().length() > 0) {
                hasText = true;
            }
        }
    }

    Attribute space = element.attribute("space");
    boolean preserveSpace = false;

    if (space != null) {
        String prefix = space.getNamespacePrefix();
        String value = space.getValue();
        //         System.out.println( "prefix = "+prefix+" value = "+value);
        if (prefix != null && "xml".equals(prefix) && "preserve".equals(value)) {
            preserveSpace = true;
        }
    }

    writePrintln();
    indent();

    writer.write("<");
    writer.write(qualifiedName);

    int previouslyDeclaredNamespaces = namespaceStack.size();

    Namespace ns = element.getNamespace();

    if (isNamespaceDeclaration(ns)) {
        namespaceStack.push(ns);
        writeNamespace(ns);
    }

    // Print out additional namespace declarations
    for (int i = 0; i < size; i++) {
        Node node = element.node(i);

        if (node instanceof Namespace) {
            Namespace additional = (Namespace) node;

            if (isNamespaceDeclaration(additional)) {
                namespaceStack.push(additional);
                writeNamespace(additional);
            }
        }
    }

    writeAttributes(element);

    lastOutputNodeType = Node.ELEMENT_NODE;

    if (size <= 0) {
        writeEmptyElementClose(qualifiedName);
    } else {
        writer.write(">");

        if (!hasElement && !preserveSpace) { // text only
            // we have at least one text node so lets assume
            // that its non-empty
            //            System.out.println( "writeElementContent (Text) ...");
            boolean previousWrapText = wrapText;
            wrapText = true;
            writeElementContent(element);
            wrapText = previousWrapText;
        } else if (preserveMixedContent && hasElement && hasText) { // preserve space
            // Mixed content
            //                System.out.println( "writeMixedElementContent ...");
            Node lastNode = writeMixedElementContent(element);

        } else if (preserveSpace) { // preserve space
            // Mixed content
            //                System.out.println( "writePreserveElementContent ...");
            Node lastNode = writePreservedElementContent(element);

        } else { // hasElement && !hasText
            //               System.out.println( "writeElementContent (Element) ...");

            boolean previousWrapText = wrapText;
            wrapText = true;
            ++indentLevel;
            writeElementContent(element);
            --indentLevel;
            wrapText = previousWrapText;

            writePrintln();
            indent();
        }

        writer.write("</");
        writer.write(qualifiedName);
        writer.write(">");
        //         writePrintln();
    }

    // remove declared namespaceStack from stack
    while (namespaceStack.size() > previouslyDeclaredNamespaces) {
        namespaceStack.pop();
    }

    lastOutputNodeType = Node.ELEMENT_NODE;
    //      }
}

From source file:com.cladonia.xml.XMLFormatter.java

License:Mozilla Public License

protected void writeMixedElement(Element element) throws IOException {
    if (DEBUG)//w  w w. ja  v a  2 s .  c  o m
        System.out.println("XMLFormatter.writeMixedElement( " + element + ")");

    int size = element.nodeCount();
    String qualifiedName = element.getQualifiedName();

    boolean previousTrim = format.isTrimText();
    boolean previousWrapText = wrapText;

    if (isPreserveMixedContent()) {
        format.setTrimText(false);
        wrapText = false;
    } else {
        wrapText = true;
    }

    writer.write("<");
    writer.write(qualifiedName);

    int previouslyDeclaredNamespaces = namespaceStack.size();
    Namespace ns = element.getNamespace();

    if (isNamespaceDeclaration(ns)) {
        namespaceStack.push(ns);
        writeNamespace(ns);
    }

    // Print out additional namespace declarations
    boolean textOnly = true;
    for (int i = 0; i < size; i++) {
        Node node = element.node(i);
        if (node instanceof Namespace) {
            Namespace additional = (Namespace) node;
            if (isNamespaceDeclaration(additional)) {
                namespaceStack.push(additional);
                writeNamespace(additional);
            }
        } else if (node instanceof Element) {
            textOnly = false;
        }
    }

    writeAttributes(element);

    lastOutputNodeType = Node.ELEMENT_NODE;

    if (size <= 0) {
        writeEmptyElementClose(qualifiedName);
        //           writePrintln();
    } else {
        writer.write(">");
        writeMixedElementContent(element);
        writer.write("</");
        writer.write(qualifiedName);
        writer.write(">");
    }

    // remove declared namespaceStack from stack
    while (namespaceStack.size() > previouslyDeclaredNamespaces) {
        namespaceStack.pop();
    }

    lastOutputNodeType = Node.ELEMENT_NODE;

    format.setTrimText(previousTrim);
    wrapText = previousWrapText;
}

From source file:com.cladonia.xml.XMLFormatter.java

License:Mozilla Public License

protected void writePreservedElement(Element element) throws IOException {
    if (DEBUG)/*from  w  ww.j a  va2  s  . c o  m*/
        System.out.println("XMLFormatter.writePreservedElement( " + element + ")");
    int size = element.nodeCount();
    String qualifiedName = element.getQualifiedName();

    boolean previousTrim = format.isTrimText();
    boolean previousWrapText = wrapText;

    format.setTrimText(false);
    wrapText = false;

    boolean hasElement = false;
    boolean hasText = false;

    // first test to see if this element has mixed content, 
    // if whitespace is significant ...
    for (int i = 0; i < size; i++) {
        Node node = element.node(i);

        if (node instanceof Element) {
            hasElement = true;
        } else if (node instanceof Text) {
            String text = node.getText();

            if (text != null && text.trim().length() > 0) {
                hasText = true;
            }
        }
    }

    Attribute space = element.attribute("space");
    boolean defaultSpace = false;

    if (space != null) {
        String prefix = space.getNamespacePrefix();
        String value = space.getValue();
        //            System.out.println( "prefix = "+prefix+" value = "+value);
        if (prefix != null && "xml".equals(prefix) && "default".equals(value)) {
            defaultSpace = true;
        }
    }

    writer.write("<");
    writer.write(qualifiedName);

    int previouslyDeclaredNamespaces = namespaceStack.size();
    Namespace ns = element.getNamespace();

    if (isNamespaceDeclaration(ns)) {
        namespaceStack.push(ns);
        writeNamespace(ns);
    }

    // Print out additional namespace declarations
    for (int i = 0; i < size; i++) {
        Node node = element.node(i);
        if (node instanceof Namespace) {
            Namespace additional = (Namespace) node;
            if (isNamespaceDeclaration(additional)) {
                namespaceStack.push(additional);
                writeNamespace(additional);
            }
        }
    }

    writeAttributes(element);

    lastOutputNodeType = Node.ELEMENT_NODE;

    if (size <= 0) {
        writeEmptyElementClose(qualifiedName);
    } else {
        writer.write(">");

        if (preserveMixedContent && hasElement && hasText) { // mixed content
            //                System.out.println( "writeMixedElementContent ...");

            Node lastNode = writeMixedElementContent(element);
        } else if (!defaultSpace) { // preserve space
            //                System.out.println( "writePreservedElementContent ...");

            Node lastNode = writePreservedElementContent(element);
        } else {
            //                System.out.println( "writeElementContent ...");

            format.setTrimText(isTrimText());
            boolean prevWrapText = wrapText;
            wrapText = true;
            writeElementContent(element);
            wrapText = prevWrapText;
            format.setTrimText(false);
        }

        writer.write("</");
        writer.write(qualifiedName);
        writer.write(">");
    }

    // remove declared namespaceStack from stack
    while (namespaceStack.size() > previouslyDeclaredNamespaces) {
        namespaceStack.pop();
    }

    lastOutputNodeType = Node.ELEMENT_NODE;

    format.setTrimText(previousTrim);
    wrapText = previousWrapText;
}

From source file:com.cladonia.xml.XMLFormatter.java

License:Mozilla Public License

protected void writeString(String text) throws IOException {
    if (text != null && text.length() > 0) {
        if (format.isTrimText()) {
            boolean first = true;
            StringTokenizer tokenizer = new StringTokenizer(text);

            while (tokenizer.hasMoreTokens()) {
                String token = tokenizer.nextToken();

                if (wrapText && lineLength != -1
                        && ((((LineWriter) writer).getColumn() + token.length()) > lineLength)) {
                    indentLevel++;/*  w w w.j a va2 s  .com*/
                    writePrintln();
                    indent();
                    indentLevel--;

                    if (first) {
                        first = false;
                    }
                } else if (first) {
                    first = false;

                    if (lastOutputNodeType == Node.TEXT_NODE) {
                        writer.write(" ");
                    } else if (format.isPadText() && lastOutputNodeType == Node.ELEMENT_NODE) {
                        writer.write(" ");
                    }
                } else {
                    writer.write(" ");
                }

                writer.write(token);
                lastOutputNodeType = Node.TEXT_NODE;
            }
        } else {
            lastOutputNodeType = Node.TEXT_NODE;
            writer.write(text);
        }
    }
}

From source file:com.cladonia.xml.XMLFormatter.java

License:Mozilla Public License

protected void writeMixedNode(Node node) throws IOException {
    if (DEBUG)/*  w w w .  jav  a2s .  com*/
        System.out.println("XMLFormatter.writeMixedNode( " + node + ")");
    int nodeType = node.getNodeType();
    switch (nodeType) {
    case Node.ELEMENT_NODE:
        writeMixedElement((Element) node);
        break;
    case Node.ATTRIBUTE_NODE:
        writeAttribute((Attribute) node);
        break;
    case Node.TEXT_NODE:
        writeString(node.getText());
        //write((Text) node);
        break;
    case Node.CDATA_SECTION_NODE:
        writeCDATA(node.getText());
        break;
    case Node.ENTITY_REFERENCE_NODE:
        writeEntity((Entity) node);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        writeProcessingInstruction((ProcessingInstruction) node);
        break;
    case Node.COMMENT_NODE:
        writeComment(node.getText());
        break;
    case Node.DOCUMENT_NODE:
        write((Document) node);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        writeDocType((DocumentType) node);
        break;
    case Node.NAMESPACE_NODE:
        // Will be output with attributes
        //write((Namespace) node);
        break;
    default:
        throw new IOException("Invalid node type: " + node);
    }
}

From source file:com.cladonia.xml.XMLFormatter.java

License:Mozilla Public License

protected void writePreservedNode(Node node) throws IOException {
    if (DEBUG)/*w w  w.ja v  a 2s.  co  m*/
        System.out.println("XMLFormatter.writeMixedNode( " + node + ")");
    int nodeType = node.getNodeType();
    switch (nodeType) {
    case Node.ELEMENT_NODE:
        writePreservedElement((Element) node);
        break;
    case Node.ATTRIBUTE_NODE:
        writeAttribute((Attribute) node);
        break;
    case Node.TEXT_NODE:
        writeString(node.getText());
        //write((Text) node);
        break;
    case Node.CDATA_SECTION_NODE:
        writeCDATA(node.getText());
        break;
    case Node.ENTITY_REFERENCE_NODE:
        writeEntity((Entity) node);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        writeProcessingInstruction((ProcessingInstruction) node);
        break;
    case Node.COMMENT_NODE:
        writeComment(node.getText());
        break;
    case Node.DOCUMENT_NODE:
        write((Document) node);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        writeDocType((DocumentType) node);
        break;
    case Node.NAMESPACE_NODE:
        // Will be output with attributes
        //write((Namespace) node);
        break;
    default:
        throw new IOException("Invalid node type: " + node);
    }
}

From source file:com.eurelis.opencms.workflows.workflows.A_OSWorkflowManager.java

License:Open Source License

/**
 * Get the OpenCMS RFS OSWorkflow configuration file and update it with the given filepath.
 * //from  w w w . j  av a2 s .  co m
 * @param listOfWorkflowsFilepath
 *            the path of the file containing the list of available workflow descriptions
 * @return the path in RFS of the updated file
 * @throws DocumentException
 *             this exception is thrown if an error occurs during the parsing of the document
 * @throws IOException
 *             this exception is thrown if a problem occurs during overwriting of the config file
 */
private String updateOSWorkflowConfigFile(String listOfWorkflowsFilepath)
        throws CmsException, DocumentException, IOException {

    // get file path
    String configFilePath = this.getWebINFPath() + ModuleSharedVariables.SYSTEM_FILE_SEPARATOR
            + OSWORKFLOWCONFIGFILE_RFSFILEPATH;

    File listOfWorkflowsFile = new File(listOfWorkflowsFilepath);
    // Load Jdom parser
    SAXReader reader = new SAXReader();
    Document document = reader.read(configFilePath);
    Node propertyNode = document.selectSingleNode(OSWORKFLOWCONFIGFILE_PROPERTYTOUPDATE_XPATH);
    if (propertyNode != null) {
        if (propertyNode.getNodeType() == Node.ELEMENT_NODE) {
            // convert Node into element
            Element propertyElement = (Element) propertyNode;

            // update the Attribute
            Attribute valueAttribute = propertyElement
                    .attribute(OSWORKFLOWCONFIGFILE_PROPERTYTOUPDATE_ATTRIBUTNAME);

            valueAttribute.setValue(listOfWorkflowsFile.toURI().toString());

        } else {
            LOGGER.debug("the node with Xpath " + OSWORKFLOWCONFIGFILE_PROPERTYTOUPDATE_XPATH + " in the file "
                    + configFilePath + " doesn't correspond to an element");
        }

    } else {
        Node parentNode = document.selectSingleNode(OSWORKFLOWCONFIGFILE_PROPERTYTOUPDATE_PARENT_XPATH);
        if (parentNode != null) {

            if (parentNode.getNodeType() == Node.ELEMENT_NODE) {
                // convert Node into element
                Element parentElement = (Element) parentNode;

                // add new property
                Element propertyElement = parentElement.addElement("property");

                // add attributs
                propertyElement.addAttribute("key", "resource");
                propertyElement.addAttribute("value", listOfWorkflowsFile.toURI().toString());

            } else {
                LOGGER.debug("the node with Xpath " + OSWORKFLOWCONFIGFILE_PROPERTYTOUPDATE_XPATH
                        + " in the file " + configFilePath + " doesn't correspond to an element");
            }

        } else {
            LOGGER.debug("the node with Xpath " + OSWORKFLOWCONFIGFILE_PROPERTYTOUPDATE_PARENT_XPATH
                    + " in the file " + configFilePath + " has not been found.");
        }
    }

    /*
     * Get a string of the resulting file
     */

    // creating of a buffer that will collect result
    ByteArrayOutputStream xmlContent = new ByteArrayOutputStream();

    // Pretty print the document to xmlContent
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(xmlContent, format);
    writer.write(document);
    writer.flush();
    writer.close();

    // get the config file content as a String
    String documentContent = new String(xmlContent.toByteArray());

    /*
     * Overwrite the config file
     */
    FileWriter.writeFile(configFilePath, documentContent);

    return configFilePath;
}