Example usage for org.dom4j Namespace getPrefix

List of usage examples for org.dom4j Namespace getPrefix

Introduction

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

Prototype

public String getPrefix() 

Source Link

Document

DOCUMENT ME!

Usage

From source file:com.zimbra.soap.JaxbUtil.java

License:Open Source License

/**
 * Use namespace inheritance in preference to prefixes
 * @param elem//from w  ww  .j  a v a2s.c  om
 * @param defaultNs
 */
private static void removeNamespacePrefixes(org.dom4j.Element elem) {
    Namespace elemNs = elem.getNamespace();
    if (elemNs != null) {
        if (!Strings.isNullOrEmpty(elemNs.getPrefix())) {
            Namespace newNs = Namespace.get(elemNs.getURI());
            org.dom4j.QName newQName = new org.dom4j.QName(elem.getName(), newNs);
            elem.setQName(newQName);
        }
    }
    Iterator<?> elemIter = elem.elementIterator();
    while (elemIter.hasNext()) {
        JaxbUtil.removeNamespacePrefixes((org.dom4j.Element) elemIter.next());
    }
}

From source file:cz.fi.muni.xkremser.editor.server.newObject.Namespaces.java

License:Open Source License

private static Map<String, String> initPrefixUriMap(Map<String, Namespace> prefixNamespaceMap) {
    Map<String, String> prefixUriMap = new HashMap<String, String>();
    for (Namespace ns : prefixNamespaceMap.values()) {
        prefixUriMap.put(ns.getPrefix(), ns.getURI());
    }/* w  w  w  .j a v a  2  s .  c o  m*/
    return Collections.unmodifiableMap(prefixUriMap);
}

From source file:eu.planets_project.pp.plato.services.characterisation.xcl.Comparator.java

License:Open Source License

/**
 * Generates a PCR for all <param>mappedProperties</param> and their metrics.
 * Note: All possible metrics are included.
 * /*from  ww w. j a  v  a  2  s.  co m*/
 * @param mappedProperties
 * @return
 */
private String generateConfig(Set<XCLObjectProperty> mappedProperties) {
    Document doc = DocumentHelper.createDocument();

    Element root = doc.addElement("pcRequest");

    Namespace xsi = new Namespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    root.add(xsi);
    root.addAttribute(xsi.getPrefix() + ":schemaLocation",
            "http://www.planets-project.eu/xcl/schemas/xcl data/pp5/schemas/pcr/pcr.xsd");
    Namespace xcl = new Namespace("", "http://www.planets-project.eu/xcl/schemas/xcl");
    root.add(xcl);
    Element compSet = root.addElement("compSet", xcl.getURI());
    compSet.addElement("source").addAttribute("name", "samplerecord");
    compSet.addElement("target").addAttribute("name", "experimentresult");
    /*
             Element root = doc.addElement("plans");
            Namespace xsi = new Namespace("xsi",  "http://www.w3.org/2001/XMLSchema-instance");
            
            root.add(xsi);
            root.addAttribute(xsi.getPrefix()+":noNamespaceSchemaLocation", "http://www.ifs.tuwien.ac.at/dp/plato/schemas/plato-1.9.xsd");
            
     */
    for (XCLObjectProperty objectProperty : mappedProperties) {
        Element prop = compSet.addElement("property").addAttribute("id", objectProperty.getPropertyId())
                .addAttribute("name", objectProperty.getName());
        for (Metric metric : objectProperty.getMetrics()) {
            prop.addElement("metric").addAttribute("id", metric.getMetricId()).addAttribute("name",
                    metric.getName());
        }
    }
    return doc.asXML();
}

From source file:net.sf.saxon.dom4j.NodeWrapper.java

License:Mozilla Public License

/**
* Output all namespace nodes associated with this element. Does nothing if
* the node is not an element.//ww  w  .jav a  2s.  com
* @param out The relevant outputter
* @param includeAncestors True if namespaces declared on ancestor elements must
* be output; false if it is known that these are already on the result tree
*/

public void outputNamespaceNodes(Receiver out, boolean includeAncestors) throws XPathException {
    if (nodeKind == Type.ELEMENT) {
        NamePool pool = docWrapper.getNamePool();
        AxisIterator enm = iterateAxis(Axis.NAMESPACE);
        while (true) {
            NodeWrapper wrapper = (NodeWrapper) enm.next();
            if (wrapper == null) {
                break;
            }
            if (!includeAncestors && !wrapper.getParent().isSameNodeInfo(this)) {
                break;
            }
            Namespace ns = (Namespace) wrapper.node;
            int nscode = pool.allocateNamespaceCode(ns.getPrefix(), ns.getURI());
            out.namespace(nscode, 0);
        }
    }
}

From source file:net.sf.saxon.dom4j.NodeWrapper.java

License:Mozilla Public License

/**
 * Get all namespace undeclarations and undeclarations defined on this element.
 *
 * @param buffer If this is non-null, and the result array fits in this buffer, then the result
 *               may overwrite the contents of this array, to avoid the cost of allocating a new array on the heap.
 * @return An array of integers representing the namespace declarations and undeclarations present on
 *         this element. For a node other than an element, return null. Otherwise, the returned array is a
 *         sequence of namespace codes, whose meaning may be interpreted by reference to the name pool. The
 *         top half word of each namespace code represents the prefix, the bottom half represents the URI.
 *         If the bottom half is zero, then this is a namespace undeclaration rather than a declaration.
 *         The XML namespace is never included in the list. If the supplied array is larger than required,
 *         then the first unused entry will be set to -1.
 *         <p/>/*from w  w  w. j av a 2  s . c o m*/
 *         <p>For a node other than an element, the method returns null.</p>
 */
public int[] getDeclaredNamespaces(int[] buffer) {
    if (node instanceof Element) {
        final Element elem = (Element) node;
        final List namespaces = elem.declaredNamespaces();

        if (namespaces == null || namespaces.size() == 0) {
            return EMPTY_NAMESPACE_LIST;
        }
        final int count = namespaces.size();
        if (count == 0) {
            return EMPTY_NAMESPACE_LIST;
        } else {
            int[] result = (count > buffer.length ? new int[count] : buffer);
            NamePool pool = getNamePool();
            int n = 0;
            for (Iterator i = namespaces.iterator(); i.hasNext();) {
                final Namespace namespace = (Namespace) i.next();
                final String prefix = namespace.getPrefix();
                final String uri = namespace.getURI();

                result[n++] = pool.allocateNamespaceCode(prefix, uri);
            }
            if (count < result.length) {
                result[count] = -1;
            }
            return result;
        }
    } else {
        return null;
    }
}

From source file:net.sf.saxon.option.dom4j.NodeWrapper.java

License:Mozilla Public License

/**
 * Get all namespace undeclarations and undeclarations defined on this element.
 *
 * @param buffer If this is non-null, and the result array fits in this buffer, then the result
 *               may overwrite the contents of this array, to avoid the cost of allocating a new array on the heap.
 * @return An array of integers representing the namespace declarations and undeclarations present on
 *         this element. For a node other than an element, return null. Otherwise, the returned array is a
 *         sequence of namespace codes, whose meaning may be interpreted by reference to the name pool. The
 *         top half word of each namespace code represents the prefix, the bottom half represents the URI.
 *         If the bottom half is zero, then this is a namespace undeclaration rather than a declaration.
 *         The XML namespace is never included in the list. If the supplied array is larger than required,
 *         then the first unused entry will be set to -1.
 *         <p/>/*from   w w w. j  a va  2 s. c o m*/
 *         <p>For a node other than an element, the method returns null.</p>
 */
public NamespaceBinding[] getDeclaredNamespaces(NamespaceBinding[] buffer) {
    if (node instanceof Element) {
        final Element elem = (Element) node;
        final List namespaces = elem.declaredNamespaces();

        if (namespaces == null || namespaces.isEmpty()) {
            return NamespaceBinding.EMPTY_ARRAY;
        }
        final int count = namespaces.size();
        if (count == 0) {
            return NamespaceBinding.EMPTY_ARRAY;
        } else {
            NamespaceBinding[] result = (buffer == null || count > buffer.length ? new NamespaceBinding[count]
                    : buffer);
            int n = 0;
            for (Iterator i = namespaces.iterator(); i.hasNext();) {
                final Namespace namespace = (Namespace) i.next();
                final String prefix = namespace.getPrefix();
                final String uri = namespace.getURI();

                result[n++] = new NamespaceBinding(prefix, uri);
            }
            if (count < result.length) {
                result[count] = null;
            }
            return result;
        }
    } else {
        return null;
    }
}

From source file:org.jboss.rusheye.CommandCrawl.java

License:Open Source License

private void addDocumentRoot() {
    ns = Namespace.get(RushEye.NAMESPACE_VISUAL_SUITE);

    Element root = document.addElement(QName.get("visual-suite", ns));

    Namespace xsi = Namespace.get("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    QName schemaLocation = QName.get("schemaLocation", xsi);

    root.addNamespace("", ns.getURI());
    root.addNamespace(xsi.getPrefix(), xsi.getURI());
    root.addAttribute(schemaLocation, ns.getURI() + " " + RushEye.SCHEMA_LOCATION_VISUAL_SUITE);

    Element globalConfiguration = root.addElement(QName.get("global-configuration", ns));
    addSuiteListener(globalConfiguration);
    addRetrievers(globalConfiguration);//w w w. j ava 2  s .com
    addPerception(globalConfiguration);
    addMasksByType(base, globalConfiguration);
    addTests(base, root);
}

From source file:org.openadaptor.auxil.convertor.map.DocumentMapFacade.java

License:Open Source License

/**
 * Extract namespace information from the root element of the document. Apply the default
 * prefix to the default namespace if there is one.
 * @param document/*from w  w w  .  j a  v  a 2s . co m*/
 */
private void cacheNamespaces(Document document) {
    nsMap = new HashMap();
    Namespace ns = document.getRootElement().getNamespace();
    nsMap.put(getDefaultNamespacePrefix(), ns.getURI());
    log.debug("Default Namespace: prefix= [" + getDefaultNamespacePrefix() + "] URI= [" + ns.getURI() + "]");

    List additionalNamespaces = document.getRootElement().additionalNamespaces();
    Iterator nsIter = additionalNamespaces.iterator();
    while (nsIter.hasNext()) {
        Namespace nextNS = (Namespace) nsIter.next();
        nsMap.put(nextNS.getPrefix(), nextNS.getURI());
        log.debug("Next Namespace: prefix= [" + nextNS.getPrefix() + "] URI= [" + nextNS.getURI() + "]");
    }
}

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

License:Open Source License

/**
**  This will add a new namespace to the current available stack.
**
** @param ns Namespace to add.//  w  w  w  .  j  av a2  s  . c  o  m
**/
public void push(Namespace ns) {
    prefixes.push(ns.getPrefix());
    uris.push(ns.getURI());
}

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

License:Open Source License

/**
* <p>/*  w  w w. j a  va  2s  .  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();
    }
}