Example usage for org.dom4j Element additionalNamespaces

List of usage examples for org.dom4j Element additionalNamespaces

Introduction

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

Prototype

List<Namespace> additionalNamespaces();

Source Link

Document

Returns any additional namespaces declarations for this element other than namespace returned via the #getNamespace() method.

Usage

From source file:com.cladonia.xml.xdiff.XmlElementNode.java

License:Open Source License

protected Line parseStartTag(Vector lines, Line current, Element elem) {

    boolean localInsertElement = false;
    boolean localDeleteElement = false;

    StyledElement styledElement = new StyledElement();

    if (insertElement || insideInsertChain(parent)) {
        localInsertElement = true;//from   w  ww . j  ava2  s . com
        styledElement.addString(INSERT_OPEN_BRACKET);
        currentColor = COLOR_GREEN;
    } else if (deleteElement || insideDeleteChain(parent)) {
        localDeleteElement = true;
        styledElement.addString(DELETE_OPEN_BRACKET);
        currentColor = Color.RED;
    } else if (merged || insideInsertMergeChain(getXmlElementNodeParent())) {
        styledElement.addString(MERGED_OPEN_BRACKET);
        currentColor = COLOR_MERGED;
    } else {
        styledElement.addString(OPEN_BRACKET);
        currentColor = Color.BLACK;
    }

    styledElement.addString(new ElementName(elem.getQualifiedName()));
    current.addStyledElement(styledElement);

    Namespace ns = elem.getNamespace();

    if (ns != null) {
        XElement parent = (XElement) elem.getParent();

        if (parent != null) {
            Namespace prev = parent.getNamespaceForPrefix(ns.getPrefix());

            if (prev == null || !ns.equals(prev)) {
                StyledElement sns = formatNamespace(ns);

                if (sns != null) {
                    if (current.length() + sns.length() + 1 > MAX_LINE_LENGTH) {
                        current = new Line();
                        lines.add(current);
                        current.addStyledString(TAB);
                    } else {
                        current.addStyledString(SPACE);
                    }

                    current.addStyledElement(sns);
                }
            }
        } else {
            StyledElement sns = formatNamespace(ns);

            if (sns != null) {
                if (current.length() + sns.length() + 1 > MAX_LINE_LENGTH) {
                    current = new Line();
                    lines.add(current);
                    current.addStyledString(TAB);
                } else {
                    current.addStyledString(SPACE);
                }

                current.addStyledElement(sns);
            }
        }
    }

    List namespaces = elem.additionalNamespaces();

    if (namespaces != null && namespaces.size() > 0) {
        Iterator iterator = namespaces.iterator();

        for (int i = 0; i < namespaces.size(); i++) {
            StyledElement sns = formatNamespace((Namespace) iterator.next());

            if (sns != null) {
                if (current.length() + sns.length() + 1 > MAX_LINE_LENGTH) {
                    current = new Line();
                    lines.add(current);
                    current.addStyledString(TAB);
                } else {
                    current.addStyledString(SPACE);
                }

                current.addStyledElement(sns);
            }
        }
    }

    List attributes = elem.attributes();

    if (attributes != null && attributes.size() > 0) {
        Iterator iterator = attributes.iterator();

        for (int i = 0; i < attributes.size(); i++) {
            StyledElement sa = formatAttribute((Attribute) iterator.next());

            if (current.length() + sa.length() + 1 > MAX_LINE_LENGTH) {
                current = new Line();
                lines.add(current);
                current.addStyledString(TAB);
            } else {
                current.addStyledString(SPACE);
            }

            current.addStyledElement(sa);
        }
    }

    if (!elem.hasContent() || hasPIorWhiteSpaceOnly((XElement) elem)) {
        if (updateElementFrom != null) {
            // content was blanked, don't add closing slash
        } else if (localInsertElement) {
            current.addStyledString(INSERT_SLASH);
        } else if (localDeleteElement) {
            current.addStyledString(DELETE_SLASH);
        } else if (merged || insideInsertMergeChain(getXmlElementNodeParent())) {
            current.addStyledString(MERGED_SLASH);
        } else {
            current.addStyledString(SLASH);
        }
    }

    if (localInsertElement) {
        current.addStyledString(INSERT_CLOSE_BRACKET);
    } else if (localDeleteElement) {
        current.addStyledString(DELETE_CLOSE_BRACKET);
    } else if (merged || insideInsertMergeChain(getXmlElementNodeParent())) {
        current.addStyledString(MERGED_CLOSE_BRACKET);
    } else {
        current.addStyledString(CLOSE_BRACKET);
    }

    currentColor = Color.BLACK;
    return current;
}

From source file:org.openxml4j.opc.signature.RelationshipTransform.java

License:Apache License

@SuppressWarnings("unchecked")
public static void RemoveAllNameSpacesExceptRelationship(Element elem) {

    // if the default namespace is not correct or if it has a prefix
    // fix it by setting to correct value without prefix
    if (elem.getNamespace().getStringValue() != PackageNamespaces.RELATIONSHIPS
            || elem.getNamespace().getPrefix() != "") {
        elem.setQName(new QName(elem.getName(),
                DocumentFactory.getInstance().createNamespace("", PackageNamespaces.RELATIONSHIPS)));
    }//from   w ww .  java2 s.  c  o  m

    // remove all additional namespace declarations
    List<Namespace> additionalNameSpaces = elem.additionalNamespaces();
    for (Namespace nms : additionalNameSpaces) {
        elem.remove(nms);
    }
}

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

License:Open Source License

/**
* <p>/*ww w .  j a  v  a  2  s  .co 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

/**
 ** Gets all the declared namespaces starting at the given element and accumulates the detected namespaces
 ** within the given namespace stack. The given namespace stack is also returned as the result.
 ** Please note, that this method has been added for the purpose that not always all given namespaces
 ** are serialized if they are already given for an ancestor.
 **/// w  w  w . j  a va 2s . c o  m
private TDOM4JNamespaceStack getParentNamespaces(Element element, TDOM4JNamespaceStack namespaces) {

    if (element == null)
        return namespaces;

    if (element.getParent() != null)
        namespaces = getParentNamespaces(element.getParent(), namespaces);

    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);
        // Put a new namespace declaratation into the namespace stack
        if (!ns.getURI().equals(uri)) {
            namespaces.push(ns);
        }
    }

    // Add additional namespace declarations if not given yet
    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);
        }
    }

    return namespaces;

}