Example usage for org.dom4j Element getNamespaceForURI

List of usage examples for org.dom4j Element getNamespaceForURI

Introduction

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

Prototype

Namespace getNamespaceForURI(String uri);

Source Link

Document

Returns the Namespace which is mapped to the given URI or null if it could not be found.

Usage

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

License:Open Source License

/**
* Sets the Schema Location attribute on the root element
*
* @param document The Exchanger document
* @param schemaType The schema type (either schemaLocation or noNamespaceSchemaLocation)
* @param namespace The namespace/* w  w w .j  a va 2s  . c  o  m*/
* @param schemaURL The URL or the schema
* 
*/
public static void setSchemaLocation(ExchangerDocument document, String schemaType, String namespace,
        String schemaURL) {
    schemaURL = URLUtilities.encodeURL(schemaURL);

    XDocument xdoc = document.getDocument();
    Element root = xdoc.getRootElement();

    if (schemaType.equals(SCHEMALOCATION)) {
        Attribute attrNoNS = root.attribute(NOSCHEMALOCATION);
        if (attrNoNS != null) {
            root.remove(attrNoNS);
        }

        // need to set both namspace and url
        Attribute attr = root.attribute(SCHEMALOCATION);
        if (attr == null) {
            // does the schema instance already exist
            Namespace ns = root.getNamespaceForURI(SCHEMAINSTANCE);
            if (ns != null) {
                String schemaInstancePrefix = ns.getPrefix();
                StringBuffer name = new StringBuffer();
                name.append(schemaInstancePrefix);
                name.append(":");
                name.append(SCHEMALOCATION);

                StringBuffer value = new StringBuffer();
                value.append(namespace);
                value.append(" ");
                value.append(schemaURL);

                root.addAttribute(name.toString(), value.toString());
            } else {
                root.addNamespace("xsi", SCHEMAINSTANCE);

                StringBuffer name = new StringBuffer();
                name.append("xsi");
                name.append(":");
                name.append(SCHEMALOCATION);

                StringBuffer value = new StringBuffer();
                value.append(namespace);
                value.append(" ");
                value.append(schemaURL);

                root.addAttribute(name.toString(), value.toString());
            }
        } else {
            String attrValue = attr.getValue();

            // break up all the namespace and url pairs
            ArrayList stringValues = new ArrayList();

            StringTokenizer st = new StringTokenizer(attrValue);
            while (st.hasMoreTokens()) {
                stringValues.add(st.nextToken());
            }

            // update existing attribute, Note: it may have multiple attribute pairs
            StringBuffer value = new StringBuffer();
            value.append(namespace);
            value.append(" ");
            value.append(schemaURL);

            //need to start at the third value (i.e we only replace the first namespace-url pair)
            for (int i = 2; i < stringValues.size(); i++) {
                value.append(" ");
                value.append((String) stringValues.get(i));
            }

            attr.setValue(value.toString());
        }
    } else {
        // is of type "no schema location"
        Attribute attrSchema = root.attribute(SCHEMALOCATION);
        if (attrSchema != null) {
            root.remove(attrSchema);
        }

        // just need to set the url
        Attribute attr = root.attribute(NOSCHEMALOCATION);
        if (attr == null) {
            // does the schema instance already exist
            Namespace ns = root.getNamespaceForURI(SCHEMAINSTANCE);
            if (ns != null) {
                String schemaInstancePrefix = ns.getPrefix();
                StringBuffer name = new StringBuffer();
                name.append(schemaInstancePrefix);
                name.append(":");
                name.append(NOSCHEMALOCATION);

                root.addAttribute(name.toString(), schemaURL);
            } else {
                root.addNamespace("xsi", SCHEMAINSTANCE);

                StringBuffer name = new StringBuffer();
                name.append("xsi");
                name.append(":");
                name.append(NOSCHEMALOCATION);

                root.addAttribute(name.toString(), schemaURL);
            }
        } else {
            // update existing attribute
            attr.setValue(schemaURL);
        }
    }
}

From source file:itensil.repository.web.ShellServlet.java

License:Open Source License

/**
 *  /getProps/* ww  w  .ja v a  2s  .  co m*/
 *
 * Get node properties
 *
 */
@ContentType("text/xml")
public void webGetProps(HttpServletRequest request, HttpServletResponse response) throws Exception {

    String uri = request.getParameter("uri");
    if (!(Check.isEmpty(uri) || uri.length() < 2)) {

        RepositoryHelper.beginTransaction();
        RepositoryHelper.useReadOnly();
        uri = RepositoryHelper.resolveUri(uri);
        MutableRepositoryNode node = getNode(uri);
        NodeProperties props = node.getProperties(new DefaultNodeVersion());
        Document doc = DocumentHelper.createDocument();
        Element root = doc.addElement("properties");
        Namespace davNs = DocumentHelper.createNamespace(PropertyHelper.DEFAULT_PREFIX,
                PropertyHelper.DEFAULT_QNAMESPACE);
        root.add(davNs);
        root.add(DocumentHelper.createNamespace(PropertyHelper.ITENSIL_PREFIX,
                PropertyHelper.ITENSIL_QNAMESPACE));
        root.add(DocumentHelper.createNamespace(PropertyHelper.ITENSIL_ENTITY_PREFIX,
                PropertyHelper.ITENSIL_ENTITY_QNAMESPACE));
        root.addAttribute("uri", node.getUri());
        root.addElement(new org.dom4j.QName("displayname", davNs)).addText(UriHelper.name(uri));
        if (node.isCollection()) {
            root.addElement(new org.dom4j.QName("collection", davNs));
        }
        int nsCount = 0;
        if (props != null) {
            for (Map.Entry<QName, String> prop : props.getPropertyMap().entrySet()) {
                Namespace ns = root.getNamespaceForURI(prop.getKey().getNamespaceURI());
                if (ns == null) {
                    nsCount++;
                    root.addNamespace(prop.getKey().getNamespaceURI(), "ns" + nsCount);
                }
                Element pElem = root.addElement(new org.dom4j.QName(prop.getKey().getLocalPart(), ns));
                String val = prop.getValue();
                if (val != null)
                    pElem.addText(val);
            }
        }
        RepositoryHelper.commitTransaction();
        ServletUtil.setExpired(response);
        doc.write(response.getWriter());
    } else {
        throw new NotFoundException("[blank]");
    }
}

From source file:itensil.repository.web.ShellServlet.java

License:Open Source License

/**
 *  /setProps/*from ww w  .  j  a v a  2 s .  c o  m*/
 *
 * Set node properties
 *
 */
@ContentType("text/xml")
public void webSetProps(HttpServletRequest request, HttpServletResponse response) throws Exception {

    Document reqDoc = XMLDocument.readStream(request.getInputStream());
    Element root = reqDoc.getRootElement();
    String uri = root.attributeValue("uri");
    if (!(Check.isEmpty(uri) || uri.length() < 2)) {

        RepositoryHelper.beginTransaction();
        uri = RepositoryHelper.resolveUri(uri);
        MutableRepositoryNode node = getNode(uri);
        String name = UriHelper.name(node.getUri());

        Namespace davNs = root.getNamespaceForURI(PropertyHelper.DEFAULT_QNAMESPACE);

        // check rename
        String reqName = root.elementTextTrim(new org.dom4j.QName("displayname", davNs));
        if (!Check.isEmpty(reqName)) {
            if (!name.equals(reqName)) {
                String parUri = UriHelper.getParent(node.getUri());
                node.move(UriHelper.absoluteUri(parUri, UriHelper.filterName(reqName)));
            }
        }

        Namespace irNs = root.getNamespaceForURI(PropertyHelper.ITENSIL_QNAMESPACE);
        Element desc = root.element(new org.dom4j.QName("description", irNs));
        NodeProperties props = node.getProperties(new DefaultNodeVersion());
        if (props == null) {
            props = new DefaultNodeProperties(new DefaultNodeVersion());
        }
        props.setValue(PropertyHelper.itensilQName("description"), desc != null ? desc.getTextTrim() : "");
        node.setProperties(props);

        RepositoryHelper.commitTransaction();
        response.getWriter().print("<ok/>");

    } else {
        throw new NotFoundException("[blank]");
    }
}

From source file:org.intalio.tempo.workflow.fds.dispatches.EscalateDispatcher.java

License:Open Source License

public Document dispatchRequest(Document request) throws InvalidInputFormatException {
    Namespace ns = new Namespace(NS_PREFIX, NS_URI);
    Element rootElement = request.getRootElement();
    userProcessNamespace = rootElement.getNamespaceURI();
    userProcessPrefix = rootElement.getNamespacePrefix();
    List nodes = DocumentHelper.createXPath("//*").selectNodes(request);
    for (int i = 0; i < nodes.size(); ++i) {
        Element element = (Element) nodes.get(i);
        element.remove(element.getNamespaceForURI(userProcessNamespace));
        element.setQName(new QName(element.getName(), ns));
    }//  www. j a  v a2 s . co  m

    rootElement.setQName(new QName("escalateTaskRequest", ns));
    // TODO: fix this in VC!
    return request;
}

From source file:org.intalio.tempo.workflow.fds.dispatches.EscalateDispatcher.java

License:Open Source License

public Document dispatchResponse(Document response) throws InvalidInputFormatException {
    // TODO: process the TMP response
    Namespace ns = new Namespace(userProcessPrefix, userProcessNamespace);
    response.getRootElement().setName("escalateResponse");
    List nodes = DocumentHelper.createXPath("//*").selectNodes(response);
    for (int i = 0; i < nodes.size(); ++i) {
        Element element = (Element) nodes.get(i);
        element.remove(element.getNamespaceForURI(NS_URI));
        element.setQName(new QName(element.getName(), ns));
    }// ww w.j a  v  a2 s . c o  m
    return response;
}

From source file:org.intalio.tempo.workflow.fds.dispatches.NotifyDispatcher.java

License:Open Source License

public Document dispatchRequest(Document request) throws InvalidInputFormatException {
    Element rootElement = request.getRootElement();
    userProcessNamespace = rootElement.getNamespaceURI();

    Namespace ns = new Namespace("tms", TMS_NS);
    rootElement.setQName(new QName("createTaskRequest", ns));

    Element metadataElement = rootElement.element("metadata");
    metadataElement.setQName(new QName("metadata", ns));
    metadataElement.detach();/*from  www .  j  a  v  a 2s. co  m*/

    Element taskElement = rootElement.addElement("task");
    taskElement.setQName(new QName("task", ns));

    taskElement.add(metadataElement);
    if (metadataElement.selectSingleNode("taskId") == null) {
        Element taskIdElement = metadataElement.addElement(new QName("taskId", ns));
        taskIdElement.setText(generateUID());
    }
    if (metadataElement.selectSingleNode("taskType") == null) {
        Element taskTypeElement = metadataElement.addElement(new QName("taskType", ns));
        taskTypeElement.setText("NOTIFICATION");
    }

    Element inputElement = rootElement.element("input");
    inputElement.setQName(new QName("input", ns));
    //inputElement.addNamespace("fe", userProcessNamespace);
    inputElement.detach();
    taskElement.add(inputElement);

    //TODO remove from TMS. Not needed
    rootElement.addElement("participantToken");

    /*
     * Now, change the namespace the
     * input, to TMS_NS.
     */

    XPath xpath = DocumentHelper.createXPath("/tms:createTaskRequest/tms:task/tms:input//*");
    HashMap map = MessageConstants._nsMap;
    map.put("tms", TMS_NS);
    xpath.setNamespaceURIs(MessageConstants._nsMap);
    List allTaskInputElements = xpath.selectNodes(request);

    xpath = DocumentHelper.createXPath("//*");
    List allBody = xpath.selectNodes(request);
    int size = allBody.size();
    LOG.debug(allTaskInputElements.size() + ":" + size);
    for (int i = 0; i < size; ++i) {
        Node node = (Node) allBody.get(i);
        if (!allTaskInputElements.contains(node)) {
            Element element = (Element) node;
            element.remove(element.getNamespaceForURI(userProcessNamespace));
            element.setQName(new QName(element.getName(), ns));
        }
    }

    return request;
}