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.axis2.saaj.SOAPFactoryTest.java

@Validated
@Test//from  ww w .  j  a  v  a2  s .c o m
public void testCreateElement2() {
    try {
        SOAPFactory sf = SOAPFactory.newInstance();
        //SOAPFactory sf = SOAPFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
        if (sf == null) {
            fail("could not create SOAPFactory object");
        }
        log.info("Create a DOMElement");
        DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dbfactory.newDocumentBuilder();
        Document document = builder.newDocument();
        Element de = document.createElementNS("http://MyNamespace.org/", "MyTag");
        //Calling SOAPFactory.createElement(org.w3c.dom.Element)
        SOAPElement se = sf.createElement(de);
        if (!de.getNodeName().equals(se.getNodeName()) || !de.getNamespaceURI().equals(se.getNamespaceURI())) {
            //Node names are not equal
            fail("Got: <URI=" + se.getNamespaceURI() + ", PREFIX=" + se.getPrefix() + ", NAME="
                    + se.getNodeName() + ">" + "Expected: <URI=" + de.getNamespaceURI() + ", PREFIX="
                    + de.getPrefix() + ", NAME=" + de.getNodeName() + ">");
        }
    } catch (Exception e) {
        fail("Exception: " + e);
    }
}

From source file:org.apache.camel.component.xmlsecurity.api.XAdESSignatureProperties.java

protected Element createDigSigElement(String localName, Document doc, String prefixForXmlSignatureNamespace) {
    Element el = doc.createElementNS("http://www.w3.org/2000/09/xmldsig#", localName);
    if (prefixForXmlSignatureNamespace != null && !prefixForXmlSignatureNamespace.isEmpty()) {
        el.setPrefix(prefixForXmlSignatureNamespace);
    }//  w w w.jav a 2s  . c  o m
    return el;
}

From source file:org.apache.camel.component.xmlsecurity.api.XAdESSignatureProperties.java

protected Element createElement(String localName, Document doc, Input input) {

    Element el = doc.createElementNS(findNamespace(input.getMessage()), localName);
    String p = findPrefix(input.getMessage());
    if (p != null && !p.isEmpty()) {
        el.setPrefix(p);//from w ww . j  av a  2  s  . c  o m
    }
    return el;
}

From source file:org.apache.cocoon.forms.binding.TempRepeaterJXPathBinding.java

public void doSave(Widget frmModel, JXPathContext jctx) throws BindingException {
    // (See comment in doLoad about type checking and throwing a meaningful exception.)
    Repeater repeater = (Repeater) selectWidget(frmModel, this.repeaterId);

    // Perform shortcut binding if the repeater is empty
    // and the deleteIfEmpty config option is selected.
    if (repeater.getSize() == 0 && this.deleteIfEmpty) {
        // Delete all of the old data for this repeater.
        jctx.removeAll(this.repeaterPath);

        // Otherwise perform the normal save binding.
    } else {/*w w w.  java 2 s . co m*/

        // Narrow to the repeater context, creating the path if it did not exist.
        JXPathContext repeaterContext = jctx.getRelativeContext(jctx.createPath(this.repeaterPath));

        // Start by deleting all of the old row data.
        repeaterContext.removeAll(this.rowPath);

        // Verify that repeater is not empty and has an insert row binding.
        if (repeater.getSize() > 0) {
            if (this.insertRowBinding != null) {

                //register the factory!
                //this.insertRowBinding.saveFormToModel(repeater, repeaterContext);

                // Iterate through the repeater rows.
                for (int i = 0; i < repeater.getSize(); i++) {

                    // Narrow to the repeater row context.
                    Pointer rowPointer = repeaterContext.getPointer(this.rowPathInsert);
                    JXPathContext rowContext = repeaterContext.getRelativeContext(rowPointer);

                    // Variables used for virtual rows.
                    // They are initialized here just to keep the compiler happy. 
                    Node rowNode = null;
                    Node virtualNode = null;

                    // If virtual rows are requested, create a temporary node and
                    // narrow the context to this initially empty new virtual row.
                    if (virtualRows == true) {
                        rowNode = (Node) rowContext.getContextBean();
                        Document document = rowNode.getOwnerDocument();
                        virtualNode = document.createElementNS(null, "virtual");
                        Node fakeDocElement = document.getDocumentElement().cloneNode(false);
                        fakeDocElement.appendChild(virtualNode);
                        rowContext = JXPathContext.newContext(repeaterContext, fakeDocElement);
                        rowContext = rowContext.getRelativeContext(rowContext.getPointer("virtual"));
                    }

                    // Perform the insert row binding
                    this.insertRowBinding.saveFormToModel(repeater, rowContext);

                    // Perform the save row binding.
                    this.rowBinding.saveFormToModel(repeater.getRow(i), rowContext);

                    // If virtual rows are requested, finish by appending the
                    // children of the virtual row to the real context node.
                    if (virtualRows == true) {
                        NodeList list = virtualNode.getChildNodes();
                        int count = list.getLength();
                        for (int j = 0; j < count; j++) {
                            // The list shrinks when a child is appended to the context
                            // node, so we always reference the first child in the list.
                            rowNode.appendChild(list.item(0));
                        }
                    }
                    getLogger().debug("bound new row");
                }
            } else {
                getLogger().warn("TempRepeaterBinding has detected rows to insert, "
                        + "but misses the <on-insert-row> binding to do it.");
            }
        }
    }
}

From source file:org.apache.cocoon.util.jxpath.DOMFactory.java

private void addDOMElement(Node parent, int index, String tag) {
    int pos = tag.indexOf(':');
    String prefix = null;// w  w  w. j a v  a 2s  .c om
    if (pos != -1) {
        prefix = tag.substring(0, pos);
    }
    String uri = null;

    Node child = parent.getFirstChild();
    int count = 0;
    while (child != null) {
        if (child.getNodeName().equals(tag)) {
            count++;
        }
        child = child.getNextSibling();
    }

    Document doc = parent.getOwnerDocument();

    if (doc != null) {
        uri = getNamespaceURI((Element) parent, prefix);
    } else {
        if (parent instanceof Document) {
            doc = (Document) parent;
            if (prefix != null) {
                throw new RuntimeException("Cannot map non-null prefix " + "when creating a document element");
            }
        } else { // Shouldn't happen (must be a DocumentType object)
            throw new RuntimeException("Node of class " + parent.getClass().getName()
                    + " has null owner document " + "but is not a Document");
        }

    }

    // Keep inserting new elements until we have index + 1 of them
    while (count <= index) {
        Node newElement = doc.createElementNS(uri, tag);
        parent.appendChild(newElement);
        count++;
    }
}

From source file:org.apache.cocoon.webapps.session.context.RequestSessionContext.java

/**
 * Build attributes XML/*  w  w  w .ja  va  2 s  . co  m*/
 */
private void buildMiscXML(Element root) {
    Document doc = root.getOwnerDocument();

    Element node;

    node = doc.createElementNS(null, "characterEncoding");
    node.appendChild(this.createTextNode(doc, this.request.getCharacterEncoding()));
    root.appendChild(node);
    node = doc.createElementNS(null, "contentLength");
    node.appendChild(this.createTextNode(doc, "" + this.request.getContentLength()));
    root.appendChild(node);
    node = doc.createElementNS(null, "contentType");
    node.appendChild(this.createTextNode(doc, this.request.getContentType()));
    root.appendChild(node);
    node = doc.createElementNS(null, "protocol");
    node.appendChild(this.createTextNode(doc, this.request.getProtocol()));
    root.appendChild(node);
    node = doc.createElementNS(null, "remoteAddress");
    node.appendChild(this.createTextNode(doc, this.request.getRemoteAddr()));
    root.appendChild(node);
    node = doc.createElementNS(null, "remoteHost");
    node.appendChild(this.createTextNode(doc, this.request.getRemoteHost()));
    root.appendChild(node);
    node = doc.createElementNS(null, "scheme");
    node.appendChild(this.createTextNode(doc, this.request.getScheme()));
    root.appendChild(node);
    node = doc.createElementNS(null, "serverName");
    node.appendChild(this.createTextNode(doc, this.request.getServerName()));
    root.appendChild(node);
    node = doc.createElementNS(null, "serverPort");
    node.appendChild(this.createTextNode(doc, "" + this.request.getServerPort()));
    root.appendChild(node);
    node = doc.createElementNS(null, "method");
    node.appendChild(this.createTextNode(doc, this.request.getMethod()));
    root.appendChild(node);
    node = doc.createElementNS(null, "contextPath");
    node.appendChild(this.createTextNode(doc, this.request.getContextPath()));
    root.appendChild(node);
    node = doc.createElementNS(null, "pathInfo");
    node.appendChild(this.createTextNode(doc, this.request.getPathInfo()));
    root.appendChild(node);
    node = doc.createElementNS(null, "pathTranslated");
    node.appendChild(this.createTextNode(doc, this.request.getPathTranslated()));
    root.appendChild(node);
    node = doc.createElementNS(null, "remoteUser");
    node.appendChild(this.createTextNode(doc, this.request.getRemoteUser()));
    root.appendChild(node);
    node = doc.createElementNS(null, "requestedSessionId");
    node.appendChild(this.createTextNode(doc, this.request.getRequestedSessionId()));
    root.appendChild(node);
    node = doc.createElementNS(null, "requestURI");
    node.appendChild(this.createTextNode(doc, this.request.getRequestURI()));
    root.appendChild(node);
    node = doc.createElementNS(null, "servletPath");
    node.appendChild(this.createTextNode(doc, this.request.getServletPath()));
    root.appendChild(node);
    node = doc.createElementNS(null, "isRequestedSessionIdFromCookie");
    node.appendChild(
            doc.createTextNode(BooleanUtils.toStringTrueFalse(this.request.isRequestedSessionIdFromCookie())));
    root.appendChild(node);
    node = doc.createElementNS(null, "isRequestedSessionIdFromURL");
    node.appendChild(
            doc.createTextNode(BooleanUtils.toStringTrueFalse(this.request.isRequestedSessionIdFromURL())));
    root.appendChild(node);
    node = doc.createElementNS(null, "isRequestedSessionIdValid");
    node.appendChild(
            doc.createTextNode(BooleanUtils.toStringTrueFalse(this.request.isRequestedSessionIdValid())));
    root.appendChild(node);
}

From source file:org.apache.cocoon.webapps.session.context.RequestSessionContext.java

/**
 * Build attributes XML/*w  ww .  j av a 2  s  . c o  m*/
 */
private void buildAttributesXML(Element root) throws ProcessingException {
    Document doc = root.getOwnerDocument();
    Element attrElement = doc.createElementNS(null, "attributes");
    String attrName;
    Element attr;

    root.appendChild(attrElement);
    Enumeration all = this.request.getAttributeNames();
    while (all.hasMoreElements() == true) {
        attrName = (String) all.nextElement();
        try {
            attr = doc.createElementNS(null, attrName);
            attrElement.appendChild(attr);
            DOMUtil.valueOf(attr, this.request.getAttribute(attrName));
        } catch (DOMException de) {
            // Some request attributes have names that are invalid as element names.
            // Example : "FOM JavaScript GLOBAL SCOPE/file://my/path/to/flow/script.js"
            this.logger.info("RequestSessionContext: Cannot create XML element from request attribute '"
                    + attrName + "' : " + de.getMessage());
        }
    }
}

From source file:org.apache.cocoon.webapps.session.context.RequestSessionContext.java

/**
 * Build cookies XML//from ww w .j a v  a  2 s .  co m
 */
private void buildCookiesXML(Element root) {
    Document doc = root.getOwnerDocument();

    Element cookiesElement = doc.createElementNS(null, "cookies");
    root.appendChild(cookiesElement);

    Cookie[] cookies = this.request.getCookies();
    if (cookies != null) {
        Cookie current;
        Element node;
        Element parent;
        for (int i = 0; i < cookies.length; i++) {
            current = cookies[i];
            parent = doc.createElementNS(null, "cookie");
            parent.setAttributeNS(null, "name", current.getName());
            cookiesElement.appendChild(parent);
            node = doc.createElementNS(null, "comment");
            node.appendChild(this.createTextNode(doc, current.getComment()));
            parent.appendChild(node);
            node = doc.createElementNS(null, "domain");
            node.appendChild(this.createTextNode(doc, current.getDomain()));
            parent.appendChild(node);
            node = doc.createElementNS(null, "maxAge");
            node.appendChild(this.createTextNode(doc, "" + current.getMaxAge()));
            parent.appendChild(node);
            node = doc.createElementNS(null, "name");
            node.appendChild(this.createTextNode(doc, current.getName()));
            parent.appendChild(node);
            node = doc.createElementNS(null, "path");
            node.appendChild(this.createTextNode(doc, current.getPath()));
            parent.appendChild(node);
            node = doc.createElementNS(null, "secure");
            node.appendChild(doc.createTextNode(BooleanUtils.toStringTrueFalse(current.getSecure())));
            parent.appendChild(node);
            node = doc.createElementNS(null, "value");
            node.appendChild(this.createTextNode(doc, current.getValue()));
            parent.appendChild(node);
            node = doc.createElementNS(null, "version");
            node.appendChild(this.createTextNode(doc, "" + current.getVersion()));
            parent.appendChild(node);
        }
    }
}

From source file:org.apache.cocoon.webapps.session.context.RequestSessionContext.java

/**
 * Build headers XML/* ww  w  .  j a  va 2  s.co m*/
 */
private void buildHeadersXML(Element root) {
    Document doc = root.getOwnerDocument();
    Element headersElement = doc.createElementNS(null, "headers");
    String headerName;
    Element header;

    root.appendChild(headersElement);
    Enumeration all = this.request.getHeaderNames();
    while (all.hasMoreElements() == true) {
        headerName = (String) all.nextElement();
        try {
            header = doc.createElementNS(null, headerName);
            headersElement.appendChild(header);
            header.appendChild(this.createTextNode(doc, this.request.getHeader(headerName)));
        } catch (Exception ignore) {
            // if the header name is not a valid element name, we simply ignore it
        }
    }
}

From source file:org.apache.cocoon.webapps.session.context.RequestSessionContext.java

/**
 * Build parameter XML/*from   ww w. j  a  v  a2s  .c  om*/
 */
private void buildParameterXML(Element root, SAXParser parser) {
    Document doc = root.getOwnerDocument();
    // include all parameters
    // process "/parameter" and "/parametervalues" at the same time
    Element parameterElement = doc.createElementNS(null, "parameter");
    Element parameterValuesElement = doc.createElementNS(null, "parametervalues");
    root.appendChild(parameterElement);
    root.appendChild(parameterValuesElement);
    String parameterName = null;
    Enumeration pars = this.request.getParameterNames();
    Element parameter;
    Element element;
    Node valueNode;
    String[] values;
    String parValue;

    element = doc.createElementNS(CIncludeTransformer.CINCLUDE_NAMESPACE_URI, PARAMETERS_ELEMENT);
    element.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:cinclude",
            CIncludeTransformer.CINCLUDE_NAMESPACE_URI);
    parameterValuesElement.appendChild(element);
    parameterValuesElement = element;

    while (pars.hasMoreElements() == true) {
        parameterName = (String) pars.nextElement();
        values = this.request.getParameterValues(parameterName);

        for (int i = 0; i < values.length; i++) {

            // this is a fast test, if the parameter value contains xml!
            parValue = values[i].trim();
            if (parValue.length() > 0 && parValue.charAt(0) == '<') {
                try {
                    valueNode = DOMUtil.getDocumentFragment(parser, new StringReader(parValue));
                    valueNode = doc.importNode(valueNode, true);
                } catch (Exception noXMLException) {
                    valueNode = doc.createTextNode(parValue);
                }
            } else {
                valueNode = doc.createTextNode(parValue);
            }
            // create "/parameter" entry for first value
            if (i == 0) {
                try {
                    parameter = doc.createElementNS(null, parameterName);
                    parameter.appendChild(valueNode);
                    parameterElement.appendChild(parameter);
                } catch (Exception local) {
                    // the exception is ignored and only this parameters is ignored
                }
            }

            try {
                // create "/parametervalues" entry
                element = doc.createElementNS(CIncludeTransformer.CINCLUDE_NAMESPACE_URI, PARAMETER_ELEMENT);
                parameterValuesElement.appendChild(element);
                parameter = element;
                element = doc.createElementNS(CIncludeTransformer.CINCLUDE_NAMESPACE_URI, NAME_ELEMENT);
                parameter.appendChild(element);
                element.appendChild(doc.createTextNode(parameterName));
                element = doc.createElementNS(CIncludeTransformer.CINCLUDE_NAMESPACE_URI, VALUE_ELEMENT);
                parameter.appendChild(element);
                element.appendChild(valueNode.cloneNode(true));
            } catch (Exception local) {
                // the exception is ignored and only this parameters is ignored
            }
        }
    }
    // and now the query string
    element = doc.createElementNS(null, "querystring");
    root.appendChild(element);
    String value = request.getQueryString();
    if (value != null) {
        element.appendChild(doc.createTextNode('?' + value));
    }
}