Example usage for org.w3c.dom Document createElementNS

List of usage examples for org.w3c.dom Document createElementNS

Introduction

In this page you can find the example usage for org.w3c.dom Document createElementNS.

Prototype

public Element createElementNS(String namespaceURI, String qualifiedName) throws DOMException;

Source Link

Document

Creates an element of the given qualified name and namespace URI.

Usage

From source file:org.apache.geode.management.internal.configuration.utils.XmlUtilsJUnitTest.java

/**
 * Test method for {@link XmlUtils#changeNamespace(Node, String, String)}.
 *//*from   w ww  .j a  v  a 2s . c  o  m*/
@Test
public void testChangeNamespace() throws Exception {
    Document doc = XmlUtils.getDocumentBuilder().newDocument();
    Element root = doc.createElement("root");
    root = (Element) doc.appendChild(root);
    final Element child = doc.createElement("child");
    root.appendChild(child);
    final String ns2 = "urn:namespace2";
    final Element childWithNamespace = doc.createElementNS(ns2, "childWithNamespace");
    root.appendChild(childWithNamespace);
    root.appendChild(doc.createTextNode("some text"));

    assertEquals(null, root.getNamespaceURI());
    assertEquals(null, child.getNamespaceURI());
    assertEquals(ns2, childWithNamespace.getNamespaceURI());

    final String ns1 = "urn:namespace1";
    root = (Element) XmlUtils.changeNamespace(root, XMLConstants.NULL_NS_URI, ns1);

    assertEquals(ns1, root.getNamespaceURI());
    assertEquals(ns1, root.getElementsByTagName("child").item(0).getNamespaceURI());
    assertEquals(ns2, root.getElementsByTagName("childWithNamespace").item(0).getNamespaceURI());
}

From source file:org.apache.jackrabbit.spi2dav.RepositoryServiceImpl.java

private DavProperty<List<XmlSerializable>> createRegisterNodeTypesProperty(SessionInfo sessionInfo,
        QNodeTypeDefinition[] nodeTypeDefinitions, final boolean allowUpdate) throws IOException {
    // create xml elements for both cnd and allow update value.
    List<XmlSerializable> val = new ArrayList<XmlSerializable>();

    StringWriter sw = new StringWriter();
    CompactNodeTypeDefWriter writer = new CompactNodeTypeDefWriter(sw, new NamespaceResolverImpl(sessionInfo),
            true);/*  w w w . j  a  va  2s  . co  m*/
    writer.write(Arrays.asList(nodeTypeDefinitions));
    writer.close();

    final String cnd = sw.toString();
    val.add(new XmlSerializable() {
        public Element toXml(Document document) {
            Element cndElem = document.createElementNS(JcrRemotingConstants.NS_URI,
                    JcrRemotingConstants.NS_PREFIX + ":" + JcrRemotingConstants.XML_CND);
            DomUtil.setText(cndElem, cnd);
            return cndElem;
        }
    });
    val.add(new XmlSerializable() {
        public Element toXml(Document document) {
            Element allowElem = document.createElementNS(JcrRemotingConstants.NS_URI,
                    JcrRemotingConstants.NS_PREFIX + ":" + JcrRemotingConstants.XML_ALLOWUPDATE);
            DomUtil.setText(allowElem, Boolean.toString(allowUpdate));
            return allowElem;
        }
    });

    return new DefaultDavProperty<List<XmlSerializable>>(JcrRemotingConstants.JCR_NODETYPES_CND_LN, val,
            ItemResourceConstants.NAMESPACE, false);
}

From source file:org.apache.jackrabbit.spi2dav.RepositoryServiceImpl.java

private DavProperty<List<XmlSerializable>> createUnRegisterNodeTypesProperty(SessionInfo sessionInfo,
        Name[] nodeTypeNames) throws IOException, RepositoryException {
    NamePathResolver resolver = getNamePathResolver(sessionInfo);
    List<XmlSerializable> val = new ArrayList<XmlSerializable>();
    for (Name ntName : nodeTypeNames) {
        final String jcrName = resolver.getJCRName(ntName);
        val.add(new XmlSerializable() {
            public Element toXml(Document document) {
                Element ntNameElem = document.createElementNS(JcrRemotingConstants.NS_URI,
                        JcrRemotingConstants.NS_PREFIX + ":" + JcrRemotingConstants.XML_NODETYPENAME);
                org.w3c.dom.Text txt = document.createTextNode(jcrName);
                ntNameElem.appendChild(txt);
                return ntNameElem;
            }//ww w .  ja v  a 2s  .co m
        });
    }
    return new DefaultDavProperty<List<XmlSerializable>>(JcrRemotingConstants.JCR_NODETYPES_CND_LN, val,
            ItemResourceConstants.NAMESPACE, false);
}

From source file:org.apache.jackrabbit.spi2dav.RepositoryServiceImpl.java

private static DavProperty<List<XmlSerializable>> createNamespaceProperty(
        final Map<String, String> namespaces) {
    // convert the specified namespace to a xml-serializable value
    List<XmlSerializable> val = new ArrayList<XmlSerializable>();
    for (final String prefix : namespaces.keySet()) {
        val.add(new XmlSerializable() {

            public Element toXml(Document document) {
                Element nsElem = document.createElementNS(JcrRemotingConstants.NS_URI,
                        JcrRemotingConstants.NS_PREFIX + ":" + JcrRemotingConstants.XML_NAMESPACE);
                Element prefixElem = document.createElementNS(JcrRemotingConstants.NS_URI,
                        JcrRemotingConstants.NS_PREFIX + ":" + JcrRemotingConstants.XML_PREFIX);
                org.w3c.dom.Text txt = document.createTextNode(prefix);
                prefixElem.appendChild(txt);

                final String uri = namespaces.get(prefix);
                Element uriElem = document.createElementNS(JcrRemotingConstants.NS_URI,
                        JcrRemotingConstants.NS_PREFIX + ":" + JcrRemotingConstants.XML_URI);
                org.w3c.dom.Text txt2 = document.createTextNode(uri);
                uriElem.appendChild(txt2);

                nsElem.appendChild(prefixElem);
                nsElem.appendChild(uriElem);

                return nsElem;
            }// w  w  w  . j  a  v a2 s  . co  m
        });
    }
    return new DefaultDavProperty<List<XmlSerializable>>(JcrRemotingConstants.JCR_NAMESPACES_LN, val,
            ItemResourceConstants.NAMESPACE, false);
}

From source file:org.apache.ode.axis2.hooks.SessionInHandler.java

public InvocationResponse invoke(MessageContext messageContext) throws AxisFault {
    SOAPHeader header = messageContext.getEnvelope().getHeader();
    if (header != null) {
        if (__log.isDebugEnabled())
            __log.debug("Found a header in incoming message, checking if there are endpoints there.");
        // Checking if a session identifier has been provided for a stateful endpoint
        OMElement wsaToSession = header.getFirstChildWithName(new QName(Namespaces.ODE_SESSION_NS, "session"));
        if (wsaToSession == null) {
            wsaToSession = header.getFirstChildWithName(new QName(Namespaces.INTALIO_SESSION_NS, "session"));
        }//from w  ww. j  a v a 2  s  . com
        if (wsaToSession != null) {
            // Building an endpoint supposed to target the right instance
            Document doc = DOMUtils.newDocument();
            Element serviceEpr = doc.createElementNS(Namespaces.WS_ADDRESSING_NS, "EndpointReference");
            Element intSessionId = doc.createElementNS(Namespaces.INTALIO_SESSION_NS, "session");
            Element odeSessionId = doc.createElementNS(Namespaces.ODE_SESSION_NS, "session");
            doc.appendChild(serviceEpr);
            serviceEpr.appendChild(intSessionId);
            serviceEpr.appendChild(odeSessionId);
            intSessionId.setTextContent(wsaToSession.getText());
            odeSessionId.setTextContent(wsaToSession.getText());
            if (__log.isDebugEnabled())
                __log.debug(
                        "A TO endpoint has been found in the header with session: " + wsaToSession.getText());

            // Did the client provide an address too?
            OMElement wsaToAddress = header.getFirstChildWithName(new QName(Namespaces.WS_ADDRESSING_NS, "To"));
            if (wsaToAddress != null) {
                Element addressElmt = doc.createElementNS(Namespaces.WS_ADDRESSING_NS, "Address");
                addressElmt.setTextContent(wsaToAddress.getText());
                serviceEpr.appendChild(addressElmt);
            }
            if (__log.isDebugEnabled())
                __log.debug("Constructed a TO endpoint: " + DOMUtils.domToString(serviceEpr));
            messageContext.setProperty(ODEService.TARGET_SESSION_ENDPOINT, serviceEpr);
        }

        // Seeing if there's a callback, in case our client would be stateful as well
        OMElement callback = header.getFirstChildWithName(new QName(Namespaces.ODE_SESSION_NS, "callback"));
        if (callback == null) {
            callback = header.getFirstChildWithName(new QName(Namespaces.INTALIO_SESSION_NS, "callback"));
        }
        if (callback != null) {
            OMElement callbackSession = callback
                    .getFirstChildWithName(new QName(Namespaces.ODE_SESSION_NS, "session"));
            if (callbackSession == null) {
                callbackSession = callback
                        .getFirstChildWithName(new QName(Namespaces.INTALIO_SESSION_NS, "session"));
            }
            if (callbackSession != null) {
                // Building an endpoint that represents our client (we're supposed to call him later on)
                Document doc = DOMUtils.newDocument();
                Element serviceEpr = doc.createElementNS(Namespaces.WS_ADDRESSING_NS, "EndpointReference");
                Element intSessionId = doc.createElementNS(Namespaces.INTALIO_SESSION_NS, "session");
                Element odeSessionId = doc.createElementNS(Namespaces.ODE_SESSION_NS, "session");
                doc.appendChild(serviceEpr);
                serviceEpr.appendChild(intSessionId);
                serviceEpr.appendChild(odeSessionId);
                intSessionId.setTextContent(callbackSession.getText());
                odeSessionId.setTextContent(callbackSession.getText());
                if (__log.isDebugEnabled())
                    __log.debug("A CALLBACK endpoint has been found in the header with session: "
                            + callbackSession.getText());

                // Did the client give his address as well?
                OMElement wsaToAddress = callback
                        .getFirstChildWithName(new QName(Namespaces.WS_ADDRESSING_NS, "Address"));
                if (wsaToAddress != null) {
                    Element addressElmt = doc.createElementNS(Namespaces.WS_ADDRESSING_NS, "Address");
                    addressElmt.setTextContent(wsaToAddress.getText());
                    serviceEpr.appendChild(addressElmt);
                }
                if (__log.isDebugEnabled())
                    __log.debug("Constructed a CALLBACK endpoint: " + DOMUtils.domToString(serviceEpr));
                messageContext.setProperty(ODEService.CALLBACK_SESSION_ENDPOINT, serviceEpr);
            }
        }
    }
    return InvocationResponse.CONTINUE;
}

From source file:org.apache.ode.axis2.httpbinding.HttpClientHelper.java

/**
 * Convert a <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1">HTTP status line</a> into an xml element like this:
 * <p/>/*w  w w .j  a  v  a 2 s. com*/
 * < Status-line>
 * < HTTP-Version>HTTP/1.1< /HTTP-Version>
 * < Status-Code>200< /Status-Code>
 * < Reason-Phrase>Success - The action was successfully received, understood, and accepted< /Reason-Phrase>
 * < /Status-line></br>
 *
 * @param statusLine - the {@link org.apache.commons.httpclient.StatusLine} instance to be converted
 * @param doc        - the document to use to create new nodes
 * @return an Element
 */
public static Element statusLineToElement(Document doc, StatusLine statusLine) {
    Element statusLineEl = doc.createElementNS(null, "Status-Line");
    Element versionEl = doc.createElementNS(null, "HTTP-Version");
    Element codeEl = doc.createElementNS(null, "Status-Code");
    Element reasonEl = doc.createElementNS(null, "Reason-Phrase");

    // wiring
    doc.appendChild(statusLineEl);
    statusLineEl.appendChild(versionEl);
    statusLineEl.appendChild(codeEl);
    statusLineEl.appendChild(reasonEl);

    // values
    versionEl.setTextContent(statusLine.getHttpVersion());
    codeEl.setTextContent(String.valueOf(statusLine.getStatusCode()));
    reasonEl.setTextContent(statusLine.getReasonPhrase());

    return statusLineEl;
}

From source file:org.apache.ode.axis2.httpbinding.HttpClientHelper.java

/**
 * @param method/*  ww w  .j a  v a 2 s.co m*/
 * @param bodyIsXml if true the body will be parsed as xml else the body will be inserted as string
 * @return
 * @throws IOException
 */
public static Element prepareDetailsElement(HttpMethod method, boolean bodyIsXml) throws IOException {
    Document doc = DOMUtils.newDocument();
    Element detailsEl = doc.createElementNS(null, "details");
    Element statusLineEl = statusLineToElement(doc, method.getStatusLine());
    detailsEl.appendChild(statusLineEl);

    // set the body if any
    final InputStream bodyAsStream = method.getResponseBodyAsStream();
    if (bodyAsStream != null) {
        Element bodyEl = doc.createElementNS(null, "responseBody");
        detailsEl.appendChild(bodyEl);
        // first, try to parse the body as xml
        // if it fails, put it as string in the body element
        boolean exceptionDuringParsing = false;
        if (bodyIsXml) {
            try {
                Element parsedBodyEl = DOMUtils.parse(bodyAsStream).getDocumentElement();
                bodyEl.appendChild(parsedBodyEl);
            } catch (Exception e) {
                String errmsg = "Unable to parse the response body as xml. Body will be inserted as string.";
                if (log.isDebugEnabled())
                    log.debug(errmsg, e);
                exceptionDuringParsing = true;
            }
        }
        if (!bodyIsXml || exceptionDuringParsing) {
            bodyEl.setTextContent(method.getResponseBodyAsString());
        }
    }
    return detailsEl;
}

From source file:org.apache.ode.axis2.httpbinding.HttpHelper.java

/**
 * Convert a <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1">HTTP status line</a> into an xml element like this:
 * <p/>/*w w w .j a va  2s  . c  o  m*/
 * < Status-line>
 * < HTTP-Version>HTTP/1.1< /HTTP-Version>
 * < Status-Code>200< /Status-Code>
 * < Reason-Phrase>Success - The action was successfully received, understood, and accepted< /Reason-Phrase>
 * < /Status-line></br>
 *
 * @param statusLine - the {@link org.apache.commons.httpclient.StatusLine} instance to be converted
 * @param doc        - the document to use to create new nodes
 * @return an Element
 */
public static Element statusLineToElement(Document doc, StatusLine statusLine) {
    Element statusLineEl = doc.createElementNS(null, "Status-Line");
    Element versionEl = doc.createElementNS(null, "HTTP-Version");
    Element codeEl = doc.createElementNS(null, "Status-Code");
    Element reasonEl = doc.createElementNS(null, "Reason-Phrase");
    Element originalEl = doc.createElementNS(null, "original");

    // wiring
    doc.appendChild(statusLineEl);
    statusLineEl.appendChild(versionEl);
    statusLineEl.appendChild(codeEl);
    statusLineEl.appendChild(reasonEl);
    statusLineEl.appendChild(originalEl);

    // values
    versionEl.setTextContent(statusLine.getHttpVersion());
    codeEl.setTextContent(String.valueOf(statusLine.getStatusCode()));
    reasonEl.setTextContent(statusLine.getReasonPhrase());
    // the line as received, not parsed
    originalEl.setTextContent(statusLine.toString());

    return statusLineEl;
}

From source file:org.apache.ode.axis2.httpbinding.HttpHelper.java

/**
 * Build a "details" element that looks like this:
 *
 * @param method//w w w.j ava  2  s.c o m
 * @return
 * @throws IOException
 */
public static Element prepareDetailsElement(HttpMethod method) {
    Header h = method.getResponseHeader("Content-Type");
    String receivedType = h != null ? h.getValue() : null;
    boolean bodyIsXml = receivedType != null && HttpUtils.isXml(receivedType);

    Document doc = DOMUtils.newDocument();
    Element detailsEl = doc.createElementNS(null, "details");
    Element statusLineEl = statusLineToElement(doc, method.getStatusLine());
    detailsEl.appendChild(statusLineEl);

    // set the body if any
    try {
        final String body = method.getResponseBodyAsString();
        if (StringUtils.isNotEmpty(body)) {
            Element bodyEl = doc.createElementNS(null, "responseBody");
            detailsEl.appendChild(bodyEl);
            // first, try to parse the body as xml
            // if it fails, put it as string in the body element
            boolean exceptionDuringParsing = false;
            if (bodyIsXml) {
                try {
                    Element parsedBodyEl = DOMUtils.stringToDOM(body);
                    bodyEl.appendChild(doc.importNode(parsedBodyEl, true));
                } catch (Exception e) {
                    String errmsg = "Unable to parse the response body as xml. Body will be inserted as string.";
                    if (log.isDebugEnabled())
                        log.debug(errmsg, e);
                    exceptionDuringParsing = true;
                }
            }
            if (!bodyIsXml || exceptionDuringParsing) {
                bodyEl.setTextContent(body);
            }
        }
    } catch (IOException e) {
        if (log.isWarnEnabled())
            log.warn("Exception while loading response body", e);
    }
    return detailsEl;
}

From source file:org.apache.ode.axis2.httpbinding.HttpMethodConverter.java

/**
 * Create the element to be associated with this part into the {@link org.apache.ode.bpel.iapi.Message}.
 * <br/>An element named with the part name will be returned. the content of this element depends on the part.
 * <p/>If the part has a non-null element name, a new element will be created and named accordingly then the text value is inserted in this new element.
 * <br/>else the given text content is simply set on the part element.
 *
 * @param part//from   www . ja  v a 2 s  .  c o m
 * @param textContent
 * @return an element named with the part name will be returned
 */
public Element createPartElement(Part part, String textContent) {
    Document doc = DOMUtils.newDocument();
    Element partElement = doc.createElementNS(null, part.getName());
    if (part.getElementName() != null) {
        Element element = doc.createElementNS(part.getElementName().getNamespaceURI(),
                part.getElementName().getLocalPart());
        element.setTextContent(textContent);
        partElement.appendChild(element);
    } else {
        partElement.setTextContent(textContent);
    }
    return partElement;
}