Java Code Examples for org.jdom.Element#getAdditionalNamespaces()

The following examples show how to use org.jdom.Element#getAdditionalNamespaces() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JDOMNamespaceContext.java    From cxf with Apache License 2.0 6 votes vote down vote up
public static String rawGetPrefix(Element element, String namespaceURI) {
    if (element.getNamespaceURI().equals(namespaceURI)) {
        return element.getNamespacePrefix();
    }

    List<?> namespaces = element.getAdditionalNamespaces();

    for (Iterator<?> itr = namespaces.iterator(); itr.hasNext();) {
        Namespace ns = (Namespace)itr.next();

        if (ns.getURI().equals(namespaceURI)) {
            return ns.getPrefix();
        }
    }

    if (element.getParentElement() != null) {
        return rawGetPrefix(element.getParentElement(), namespaceURI);
    }
    return null;
}
 
Example 2
Source File: JDOMNamespaceContext.java    From cxf with Apache License 2.0 6 votes vote down vote up
static void rawGetPrefixes(Element element, String namespaceURI, List<String> prefixes) {
    if (element.getNamespaceURI().equals(namespaceURI)) {
        prefixes.add(element.getNamespacePrefix());
    }

    List<?> namespaces = element.getAdditionalNamespaces();

    for (Iterator<?> itr = namespaces.iterator(); itr.hasNext();) {
        Namespace ns = (Namespace)itr.next();

        if (ns.getURI().equals(namespaceURI)) {
            prefixes.add(ns.getPrefix());
        }
    }

    if (element.getParentElement() != null) {
        rawGetPrefixes(element.getParentElement(), namespaceURI, prefixes);
    }
}
 
Example 3
Source File: StaxSerializer.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void writeElement(Element e, XMLStreamWriter writer) throws XMLStreamException {
    // need to check if the namespace is declared before we write the
    // start element because that will put the namespace in the context.
    String elPrefix = e.getNamespacePrefix();
    String elUri = e.getNamespaceURI();

    String boundPrefix = writer.getPrefix(elUri);
    boolean writeElementNS = false;
    if (boundPrefix == null || !elPrefix.equals(boundPrefix)) {
        writeElementNS = true;
    }

    writer.writeStartElement(elPrefix, e.getName(), elUri);

    List<?> namespaces = e.getAdditionalNamespaces();
    for (Iterator<?> itr = namespaces.iterator(); itr.hasNext();) {
        Namespace ns = (Namespace)itr.next();

        String prefix = ns.getPrefix();
        String uri = ns.getURI();

        writer.setPrefix(prefix, uri);
        writer.writeNamespace(prefix, uri);

        if (elUri.equals(uri) && elPrefix.equals(prefix)) {
            writeElementNS = false;
        }
    }

    for (Iterator<?> itr = e.getAttributes().iterator(); itr.hasNext();) {
        Attribute attr = (Attribute)itr.next();
        String attPrefix = attr.getNamespacePrefix();
        String attUri = attr.getNamespaceURI();

        if (attUri == null || attUri.isEmpty()) {
            writer.writeAttribute(attr.getName(), attr.getValue());
        } else {
            writer.writeAttribute(attPrefix, attUri, attr.getName(), attr.getValue());

            if (!isDeclared(writer, attPrefix, attUri)) {
                if (elUri.equals(attUri) && elPrefix.equals(attPrefix)) {
                    if (writeElementNS) {
                        writer.setPrefix(attPrefix, attUri);
                        writer.writeNamespace(attPrefix, attUri);
                        writeElementNS = false;
                    }
                } else {
                    writer.setPrefix(attPrefix, attUri);
                    writer.writeNamespace(attPrefix, attUri);
                }
            }
        }
    }

    if (writeElementNS) {
        if (elPrefix == null || elPrefix.length() == 0) {
            writer.writeDefaultNamespace(elUri);
        } else {
            writer.writeNamespace(elPrefix, elUri);
        }
    }

    for (Iterator<?> itr = e.getContent().iterator(); itr.hasNext();) {
        Content n = (Content)itr.next();
        if (n instanceof CDATA) {
            writer.writeCData(n.getValue());
        } else if (n instanceof Text) {
            writer.writeCharacters(((Text)n).getText());
        } else if (n instanceof Element) {
            writeElement((Element)n, writer);
        } else if (n instanceof Comment) {
            writer.writeComment(n.getValue());
        } else if (n instanceof EntityRef) {
            // EntityRef ref = (EntityRef) n;
            // writer.writeEntityRef(ref.)
        }
    }

    writer.writeEndElement();
}