Example usage for org.dom4j Namespace XML_NAMESPACE

List of usage examples for org.dom4j Namespace XML_NAMESPACE

Introduction

In this page you can find the example usage for org.dom4j Namespace XML_NAMESPACE.

Prototype

Namespace XML_NAMESPACE

To view the source code for org.dom4j Namespace XML_NAMESPACE.

Click Source Link

Document

XML Namespace

Usage

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

License:Apache License

/**
 * Writes the attributes of the given element
 *
 *//*from  w  w  w  . j a  va2s. c om*/
protected void writeAttributes(Element element) throws IOException {

    // I do not yet handle the case where the same prefix maps to
    // two different URIs. For attributes on the same element
    // this is illegal; but as yet we don't throw an exception
    // if someone tries to do this
    for (int i = 0, size = element.attributeCount(); i < size; i++) {
        Attribute attribute = element.attribute(i);
        Namespace ns = attribute.getNamespace();
        if (ns != null && ns != Namespace.NO_NAMESPACE && ns != Namespace.XML_NAMESPACE) {
            String prefix = ns.getPrefix();
            String uri = namespaceStack.getURI(prefix);
            if (!ns.getURI().equals(uri)) { // output a new namespace
                // declaration
                writeNamespace(ns);
                namespaceStack.push(ns);
            }
        }

        // If the attribute is a namespace declaration, check if we have
        // already
        // written that declaration elsewhere (if that's the case, it must
        // be
        // in the namespace stack
        String attName = attribute.getName();
        if (attName.startsWith("xmlns:")) {
            String prefix = attName.substring(6);
            if (namespaceStack.getNamespaceForPrefix(prefix) == null) {
                String uri = attribute.getValue();
                namespaceStack.push(prefix, uri);
                writeNamespace(prefix, uri);
            }
        } else if (attName.equals("xmlns")) {
            if (namespaceStack.getDefaultNamespace() == null) {
                String uri = attribute.getValue();
                namespaceStack.push(null, uri);
                writeNamespace(null, uri);
            }
        } else {
            char quote = format.getAttributeQuoteCharacter();
            writer.write(" ");
            writer.write(attribute.getQualifiedName());
            writer.write("=");
            writer.write(quote);
            writeEscapeAttributeEntities(attribute.getValue());
            writer.write(quote);
        }
    }
}

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

License:Apache License

protected boolean isNamespaceDeclaration(Namespace ns) {
    if (ns != null && ns != Namespace.XML_NAMESPACE) {
        String uri = ns.getURI();
        if (uri != null) {
            if (!namespaceStack.contains(ns)) {
                return true;

            }//  w  w  w.jav  a 2 s .c o  m
        }
    }
    return false;

}

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

License:Apache License

/** Writes the attributes of the given element
  *//  ww  w  .j ava 2 s  . co  m
  */
protected void writeAttributes(Element element) throws IOException {

    // I do not yet handle the case where the same prefix maps to
    // two different URIs. For attributes on the same element
    // this is illegal; but as yet we don't throw an exception
    // if someone tries to do this
    for (int i = 0, size = element.attributeCount(); i < size; i++) {
        Attribute attribute = element.attribute(i);
        Namespace ns = attribute.getNamespace();
        if (ns != null && ns != Namespace.NO_NAMESPACE && ns != Namespace.XML_NAMESPACE) {
            String prefix = ns.getPrefix();
            String uri = namespaceStack.getURI(prefix);
            if (!ns.getURI().equals(uri)) { // output a new namespace declaration
                writeNamespace(ns);
                namespaceStack.push(ns);
            }
        }

        // If the attribute is a namespace declaration, check if we have already
        // written that declaration elsewhere (if that's the case, it must be
        // in the namespace stack
        String attName = attribute.getName();
        if (attName.startsWith("xmlns:")) {
            String prefix = attName.substring(6);
            if (namespaceStack.getNamespaceForPrefix(prefix) == null) {
                String uri = attribute.getValue();
                namespaceStack.push(prefix, uri);
                writeNamespace(prefix, uri);
            }
        } else if (attName.equals("xmlns")) {
            if (namespaceStack.getDefaultNamespace() == null) {
                String uri = attribute.getValue();
                namespaceStack.push(null, uri);
                writeNamespace(null, uri);
            }
        } else {
            char quote = format.getAttributeQuoteCharacter();
            writer.write(" ");
            writer.write(attribute.getQualifiedName());
            writer.write("=");
            writer.write(quote);
            writeEscapeAttributeEntities(attribute.getValue());
            writer.write(quote);
        }
    }
}

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

License:Open Source License

protected void writeAttributes(Element element) throws IOException {

    // I do not yet handle the case where the same prefix maps to
    // two different URIs. For attributes on the same element
    // this is illegal; but as yet we don't throw an exception
    // if someone tries to do this
    for (int i = 0, size = element.attributeCount(); i < size; i++) {
        Attribute attribute = element.attribute(i);
        Namespace ns = attribute.getNamespace();
        if (ns != null && ns != Namespace.NO_NAMESPACE && ns != Namespace.XML_NAMESPACE) {
            String prefix = ns.getPrefix();
            String uri = namespaceStack.getURI(prefix);
            if (!ns.getURI().equals(uri)) { // output a new namespace declaration
                writeNamespace(ns);//from w ww.j  a v a  2s  . c  o  m
                namespaceStack.push(ns);
            }
        }

        writeAttribute(attribute);
    }
}

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

License:Open Source License

protected boolean isNamespaceDeclaration(Namespace ns) {
    if (ns != null && ns != Namespace.XML_NAMESPACE) {
        String uri = ns.getURI();
        if (uri != null) {
            if (!namespaceStack.contains(ns)) {
                return true;

            }/*from   w w w  .  j a va 2 s.  c o m*/
        }
    }
    return false;
}

From source file:org.firesoa.common.jxpath.model.dom4j.Dom4JNodePointer.java

License:Open Source License

public String getNamespaceURI(String prefix) {
    if (prefix.equals("xml")) {
        return Namespace.XML_NAMESPACE.getURI();
    }/*from  w  ww . j ava 2 s  .  c o m*/
    if (prefix.equals(Constants.DEFAULT_NS_PREFIX)) {//context??
        return getDefaultNamespaceURI();
    }
    Element element = null;
    if (node instanceof Document) {
        element = ((Document) node).getRootElement();
    }
    if (node instanceof Element) {
        element = (Element) node;
    }
    if (element == null) {
        return null;
    }
    Namespace ns = element.getNamespaceForPrefix(prefix);
    return ns == null ? null : ns.getURI();
}

From source file:org.firesoa.common.jxpath.model.dom4j.Dom4JNodePointer.java

License:Open Source License

/**
 * Get the language of this element./*from  w  ww .j  a va2 s  .  c  o  m*/
 * @return String language
 */
protected String getLanguage() {
    return findEnclosingAttribute(node, "lang", Namespace.XML_NAMESPACE);
}

From source file:org.firesoa.common.jxpath.model.dom4j.Dom4JNodePointer.java

License:Open Source License

public Object getValue() {
    if (node instanceof Document) {
        return node;
    }//w ww.j av a2 s .  c om
    if (node instanceof Element) {
        StringBuffer buf = new StringBuffer();
        for (NodeIterator children = childIterator(null, false, null); children
                .setPosition(children.getPosition() + 1);) {
            NodePointer ptr = children.getNodePointer();
            if (ptr.getImmediateNode() instanceof Element || ptr.getImmediateNode() instanceof Text) {
                buf.append(ptr.getValue());
            }
        }
        return buf.toString();
    }
    if (node instanceof Comment) {
        String text = ((Comment) node).getText();
        if (text != null) {
            text = text.trim();
        }
        return text;
    }
    String result = null;
    if (node instanceof Text) {
        result = ((Text) node).getText();
    }
    if (node instanceof ProcessingInstruction) {
        result = ((ProcessingInstruction) node).getStringValue();//TODO ?
    }
    boolean trim = !"preserve".equals(findEnclosingAttribute(node, "space", Namespace.XML_NAMESPACE));
    return result != null && trim ? result.trim() : result;

    //      if ((node instanceof org.dom4j.CharacterData
    //            || node instanceof Attribute || node instanceof DocumentType
    //            || node instanceof Entity || node instanceof ProcessingInstruction)) {
    //         return ((Node)node).getText();
    //      }else{
    //         if (node instanceof Document){
    //            ((Document)node).getText();
    //         }else if (node instanceof Element){
    //            Element elm = (Element)node;
    //            return elm.getText();
    //         }
    //      }
    //        return node.toString();
}

From source file:org.orbeon.oxf.processor.tamino.dom4j.TDOM4JXMLOutputter.java

License:Open Source License

/**
* <p>/* ww w.  java2s.c o m*/
* This will handle printing out an <code>{@link Element}</code>,
*   its <code>{@link Attribute}</code>s, and its value.
* </p>
*
* @param element <code>Element</code> to output.
* @param out <code>Writer</code> to write to.
* @param indent <code>int</code> level of indention.
* @param namespaces <code>List</code> stack of Namespaces in scope.
*/
protected void printElement(Element element, Writer out, int indentLevel, TDOM4JNamespaceStack namespaces)
        throws IOException {

    List mixedContent = element.elements();

    boolean empty = mixedContent.size() == 0;
    boolean stringOnly = !empty && mixedContent.size() == 1 && mixedContent.get(0) instanceof String;

    // Print beginning element tag
    /* maybe the doctype, xml declaration, and processing instructions
     should only break before and not after; then this check is
     unnecessary, or maybe the println should only come after and
     never before.  Then the output always ends with a newline */

    indent(out, indentLevel);

    // Print the beginning of the tag plus attributes and any
    // necessary namespace declarations
    out.write("<");
    out.write(element.getQualifiedName());
    int previouslyDeclaredNamespaces = namespaces.size();

    Namespace ns = element.getNamespace();

    // Add namespace decl only if it's not the XML namespace and it's
    // not the NO_NAMESPACE with the prefix "" not yet mapped
    // (we do output xmlns="" if the "" prefix was already used and we
    // need to reclaim it for the NO_NAMESPACE)
    if (ns != Namespace.XML_NAMESPACE && !(ns == Namespace.NO_NAMESPACE && namespaces.getURI("") == null)) {
        String prefix = ns.getPrefix();
        String uri = namespaces.getURI(prefix);
        if (!ns.getURI().equals(uri)) { // output a new namespace decl
            namespaces.push(ns);
            printNamespace(ns, out);
        }
    }

    // Print out additional namespace declarations
    List additionalNamespaces = element.additionalNamespaces();
    if (additionalNamespaces != null) {
        for (int i = 0; i < additionalNamespaces.size(); i++) {
            Namespace additional = (Namespace) additionalNamespaces.get(i);
            String prefix = additional.getPrefix();
            String uri = namespaces.getURI(prefix);
            if (!additional.getURI().equals(uri)) {
                namespaces.push(additional);
                printNamespace(additional, out);
            }
        }
    }

    printAttributes(element.attributes(), element, out, namespaces);

    // handle "" string same as empty
    if (stringOnly) {
        String elementText = trimText ? element.getTextTrim() : element.getText();
        if (elementText == null || elementText.equals("")) {
            empty = true;
        }
    }

    if (empty) {
        // Simply close up
        if (!expandEmptyElements) {
            out.write(" />");
        } else {
            out.write("></");
            out.write(element.getQualifiedName());
            out.write(">");
        }
        maybePrintln(out);
    } else {
        // we know it's not null or empty from above
        out.write(">");

        if (stringOnly) {
            // if string only, print content on same line as tags
            printElementContent(element, out, indentLevel, namespaces, mixedContent);
        } else {
            maybePrintln(out);
            printElementContent(element, out, indentLevel, namespaces, mixedContent);
            indent(out, indentLevel);
        }

        out.write("</");
        out.write(element.getQualifiedName());
        out.write(">");

        maybePrintln(out);
    }

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

From source file:org.orbeon.oxf.processor.tamino.dom4j.TDOM4JXMLOutputter.java

License:Open Source License

/**
* <p>/*www  .j  a  v  a 2s  . com*/
* This will handle printing out an <code>{@link Attribute}</code> list.
* </p>
*
* @param attributes <code>List</code> of Attribute objcts
* @param out <code>Writer</code> to write to
*/
protected void printAttributes(List attributes, Element parent, Writer out, TDOM4JNamespaceStack namespaces)
        throws IOException {

    // I do not yet handle the case where the same prefix maps to
    // two different URIs. For attributes on the same element
    // this is illegal; but as yet we don't throw an exception
    // if someone tries to do this
    Set prefixes = new HashSet();

    for (int i = 0, size = attributes.size(); i < size; i++) {
        Attribute attribute = (Attribute) attributes.get(i);
        Namespace ns = attribute.getNamespace();
        if (ns != Namespace.NO_NAMESPACE && ns != Namespace.XML_NAMESPACE) {
            String prefix = ns.getPrefix();
            String uri = namespaces.getURI(prefix);
            if (!ns.getURI().equals(uri)) { // output a new namespace decl
                printNamespace(ns, out);
                namespaces.push(ns);
            }
        }

        out.write(" ");
        out.write(attribute.getQualifiedName());
        out.write("=");

        out.write("\"");
        out.write(escapeAttributeEntities(attribute.getValue()));
        out.write("\"");
    }

}