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:at.gv.egovernment.moa.id.auth.validator.parep.client.szrgw.SZRGWClient.java

public Document buildGetIdentityLinkRequest(String PEPSIdentifier, String PEPSFirstname, String PEPSFamilyname,
        String PEPSDateOfBirth, String signature, String representative, String represented,
        String mandateContent) throws SZRGWClientException {

    String SZRGW_NS = "http://reference.e-government.gv.at/namespace/szrgw/20070807#";
    try {//from w w  w  .j  a  v a 2  s .c  o m
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.newDocument();

        Element getIdentityLink = doc.createElementNS(SZRGW_NS, "szrgw:GetIdentityLinkRequest");
        getIdentityLink.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:szrgw", SZRGW_NS);
        doc.appendChild(getIdentityLink);

        if ((PEPSIdentifier != null) || (PEPSFirstname != null) || (PEPSFamilyname != null)
                || (PEPSDateOfBirth != null)) {

            Element pepsDataElem = doc.createElementNS(SZRGW_NS, "szrgw:PEPSData");
            getIdentityLink.appendChild(pepsDataElem);

            if (PEPSIdentifier != null) {
                Element elem = doc.createElementNS(SZRGW_NS, "szrgw:Identifier");
                pepsDataElem.appendChild(elem);
                Text text = doc.createTextNode(PEPSIdentifier);
                elem.appendChild(text);
            }
            if (PEPSFirstname != null) {
                Element elem = doc.createElementNS(SZRGW_NS, "szrgw:Firstname");
                pepsDataElem.appendChild(elem);
                Text text = doc.createTextNode(PEPSFirstname);
                elem.appendChild(text);
            }

            if (PEPSFamilyname != null) {
                Element elem = doc.createElementNS(SZRGW_NS, "szrgw:Familyname");
                pepsDataElem.appendChild(elem);
                Text text = doc.createTextNode(PEPSFamilyname);
                elem.appendChild(text);
            }

            if (PEPSDateOfBirth != null) {
                Element elem = doc.createElementNS(SZRGW_NS, "szrgw:DateOfBirth");
                pepsDataElem.appendChild(elem);
                Text text = doc.createTextNode(PEPSDateOfBirth);
                elem.appendChild(text);
            }

            if (representative != null) {
                Element elem = doc.createElementNS(SZRGW_NS, "szrgw:Representative");
                pepsDataElem.appendChild(elem);
                Text text = doc.createTextNode(representative);
                elem.appendChild(text);
            }

            if (represented != null) {
                Element elem = doc.createElementNS(SZRGW_NS, "szrgw:Represented");
                pepsDataElem.appendChild(elem);
                Text text = doc.createTextNode(represented);
                elem.appendChild(text);
            }

            if (mandateContent != null) {
                Element elem = doc.createElementNS(SZRGW_NS, "szrgw:MandateContent");
                pepsDataElem.appendChild(elem);
                Text text = doc.createTextNode(mandateContent);
                elem.appendChild(text);
            }
        }

        if (signature == null)
            throw new SZRGWClientException("Signature element must not be null!");
        else {
            Element sig = doc.createElementNS(SZRGW_NS, "szrgw:Signature");
            Element base64content = doc.createElementNS(SZRGW_NS, "szrgw:Base64Content");
            sig.appendChild(base64content);
            getIdentityLink.appendChild(sig);
            Text text = doc.createTextNode(signature);
            base64content.appendChild(text);
        }

        if (representative != null && represented != null && mandateContent != null) {
            Element mis = doc.createElementNS(SZRGW_NS, "szrgw:MIS");
            Element filters = doc.createElementNS(SZRGW_NS, "szrgw:Filters");
            mis.appendChild(filters);
            Element target = doc.createElementNS(SZRGW_NS, "szrgw:Target");
            mis.appendChild(target);
            Element friendlyName = doc.createElementNS(SZRGW_NS, "szrgw:OAFriendlyName");
            mis.appendChild(friendlyName);
            getIdentityLink.appendChild(mis);

            //            TODO fetch data from oa params
            //             String moasessionid = req.getParameter(MOAIDAuthConstants.PARAM_SESSIONID);
            //             moasessionid = StringEscapeUtils.escapeHtml(moasessionid);
            //              AuthenticationSession moasession = AuthenticationSessionStoreage.getSession(moasessionid);
            //             OAAuthParameter oaParam = AuthConfigurationProvider.getInstance().getOnlineApplicationParameter(moasession.getPublicOAURLPrefix());
            //             if (oaParam == null)
            //                   throw new AuthenticationException("auth.00", new Object[] { moasession.getPublicOAURLPrefix() });
            //              Text text = doc.createTextNode(oaParam.getFriendlyName());
        }

        return doc;
    } catch (ParserConfigurationException e) {
        throw new SZRGWClientException(e);
    } /*catch (CertificateEncodingException e) {
       throw new SZRGWClientException(e);
      }*/

}

From source file:chat.viska.xmpp.plugins.webrtc.WebRtcPlugin.java

@Nonnull
public Completable closeSession(@Nonnull final Jid recipient, @Nonnull final String id) {
    final Document iq = Stanza.getIqTemplate(Stanza.IqType.SET, UUID.randomUUID().toString(),
            getSession().getNegotiatedJid(), recipient);
    final Element webrtcElement = (Element) iq.getDocumentElement()
            .appendChild(iq.createElementNS(XMLNS, "webrtc"));
    webrtcElement.setAttribute("id", id);
    webrtcElement.setAttribute("action", "close");
    return this.context.sendIq(new XmlWrapperStanza(iq)).getResponse().toSingle().toCompletable();
}

From source file:com.enonic.esl.xml.XMLTool.java

/**
 * Create a new document with a top element in a specific namespace.
 *
 * @param namespaceURI  The namespace URI.
 * @param qualifiedName The name of the top element.
 * @param docType       DocumentType//from  w  w  w  .  ja v a2s .  c  om
 * @return Document
 */
private static Document createDocument(String namespaceURI, String qualifiedName, DocumentType docType) {
    Document doc = createDocument();

    if (docType != null) {
        doc.appendChild(docType);
    }
    if (namespaceURI != null) {
        doc.appendChild(doc.createElementNS(namespaceURI, qualifiedName));
    } else {
        doc.appendChild(doc.createElement(qualifiedName));
    }

    return doc;
}

From source file:org.openremote.beehive.configuration.www.UsersAPI.java

private void writeConfig(Document document, Element rootElement, Account account) {
    Element configElement = document.createElementNS(OPENREMOTE_NAMESPACE, "config");
    rootElement.appendChild(configElement);

    Collection<ControllerConfiguration> configurations = account.getControllerConfigurations();
    for (ControllerConfiguration configuration : configurations) {
        if (!"rules.editor".equals(configuration.getName())) {
            Element propertyElement = document.createElementNS(OPENREMOTE_NAMESPACE, "property");
            configElement.appendChild(propertyElement);
            propertyElement.setAttribute("name", configuration.getName());
            propertyElement.setAttribute("value", configuration.getValue());
        }/*  w w w. ja  v  a 2  s . c  om*/
    }

}

From source file:chat.viska.xmpp.plugins.webrtc.WebRtcPlugin.java

@Nonnull
public Completable sendSdp(@Nonnull final Jid recipient, @Nonnull final String id,
        @Nonnull final SessionDescription sdp, final boolean creating) {
    final Document iq = Stanza.getIqTemplate(Stanza.IqType.SET, UUID.randomUUID().toString(),
            getSession().getNegotiatedJid(), recipient);
    final Element webrtcElement = (Element) iq.getDocumentElement()
            .appendChild(iq.createElementNS(XMLNS, "webrtc"));
    webrtcElement.setAttribute("id", id);
    if (creating) {
        webrtcElement.setAttribute("action", "create");
    }//from www  .j  a va2s. c om
    final Element sdpElement = (Element) webrtcElement.appendChild(iq.createElement("sdp"));
    if (sdp.type != null) {
        sdpElement.setAttribute("type", sdp.type.canonicalForm());
    }
    for (String line : sdp.description.split(REGEX_LINE_BREAK)) {
        final Node node = sdpElement.appendChild(iq.createElement("line"));
        node.setTextContent(line);
    }
    return this.context.sendIq(new XmlWrapperStanza(iq)).getResponse().toSingle().toCompletable();
}

From source file:de.ingrid.interfaces.csw.server.impl.GenericServer.java

@Override
public Document process(GetRecordsRequest request) throws CSWException {
    try {//from   w w w.java 2  s  .  c  o  m
        CSWQuery query = request.getQuery();
        CSWRecordResults result = this.searcher.search(query);

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        DOMImplementation domImpl = docBuilder.getDOMImplementation();
        Document doc = domImpl.createDocument(RESPONSE_NAMESPACE, "csw:GetRecordsResponse", null);

        Element searchStatus = doc.createElementNS(RESPONSE_NAMESPACE, "csw:SearchStatus");

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        df.setTimeZone(TimeZone.getTimeZone("ThreeLetterISO8601TimeZone"));
        searchStatus.setAttribute("timestamp", df.format(new Date()));
        doc.getDocumentElement().appendChild(searchStatus);

        Element searchResults = doc.createElementNS(RESPONSE_NAMESPACE, "csw:SearchResults");
        searchResults.setAttribute("elementSet", request.getQuery().getElementSetName().name().toLowerCase());
        searchResults.setAttribute("numberOfRecordsMatched", String.valueOf(result.getTotalHits()));
        searchResults.setAttribute("numberOfRecordsReturned",
                String.valueOf((result.getResults() == null) ? 0 : result.getResults().size()));
        int nextRecord = query.getStartPosition()
                + ((result.getResults() == null) ? 0 : result.getResults().size());
        if (nextRecord > result.getTotalHits()) {
            nextRecord = 0;
        }
        searchResults.setAttribute("nextRecord", String.valueOf(nextRecord));
        doc.getDocumentElement().appendChild(searchResults);

        if (query.getResultType() == ResultType.RESULTS && result.getResults() != null) {
            for (CSWRecord record : result.getResults()) {
                Node recordNode = record.getDocument().getFirstChild();
                doc.adoptNode(recordNode);
                searchResults.appendChild(recordNode);
            }
        }
        return doc;
    } catch (CSWException ex) {
        log.error("An error occured processing GetRecordsRequest", ex);
        throw ex;
    } catch (Exception ex) {
        log.error("An error occured processing GetRecordsRequest", ex);
        throw new CSWException("An error occured processing GetRecordsRequest");
    }
}

From source file:com.centeractive.ws.builder.soap.XmlUtils.java

public static Document createDocument(QName element) {
    ensureDocumentBuilder();/*w w  w. j  av a  2 s.c  o m*/

    Document document = documentBuilder.newDocument();
    document.appendChild(document.createElementNS(element.getNamespaceURI(), element.getLocalPart()));
    return document;
}

From source file:org.openremote.beehive.configuration.www.UsersAPI.java

private void writeCommands(Document document, Element rootElement, Account account) {
    Element commandsElement = document.createElementNS(OPENREMOTE_NAMESPACE, "commands");
    rootElement.appendChild(commandsElement);

    Collection<Device> devices = account.getDevices();

    for (Device device : devices) {
        Collection<Command> commands = device.getCommands();
        for (Command command : commands) {
            Element commandElement = document.createElementNS(OPENREMOTE_NAMESPACE, "command");
            commandsElement.appendChild(commandElement);
            commandElement.setAttribute("id", Long.toString(command.getId()));
            commandElement.setAttribute("protocol", command.getProtocol().getType());

            Collection<ProtocolAttribute> attributes = command.getProtocol().getAttributes();
            for (ProtocolAttribute attribute : attributes) {
                Element propertyElement = document.createElementNS(OPENREMOTE_NAMESPACE, "property");
                commandElement.appendChild(propertyElement);
                propertyElement.setAttribute("name", attribute.getName());
                propertyElement.setAttribute("value", attribute.getValue());
            }/*from   w w  w.  j  ava 2  s  .c  o m*/
            Element propertyElement = document.createElementNS(OPENREMOTE_NAMESPACE, "property");
            commandElement.appendChild(propertyElement);
            propertyElement.setAttribute("name", "name");
            propertyElement.setAttribute("value", command.getName());

            propertyElement = document.createElementNS(OPENREMOTE_NAMESPACE, "property");
            commandElement.appendChild(propertyElement);
            propertyElement.setAttribute("name", "urn:openremote:device-command:device-name");
            propertyElement.setAttribute("value", command.getDevice().getName());

            propertyElement = document.createElementNS(OPENREMOTE_NAMESPACE, "property");
            commandElement.appendChild(propertyElement);
            propertyElement.setAttribute("name", "urn:openremote:device-command:device-id");
            propertyElement.setAttribute("value", Long.toString(command.getDevice().getId()));
        }
    }
}

From source file:es.gob.afirma.signers.ooxml.be.fedict.eid.applet.service.signer.ooxml.AbstractOOXMLSignatureService.java

private ZipOutputStream copyOOXMLContent(final String signatureZipEntryName,
        final OutputStream signedOOXMLOutputStream)
        throws IOException, ParserConfigurationException, SAXException, TransformerException {
    final ZipOutputStream zipOutputStream = new ZipOutputStream(signedOOXMLOutputStream);
    final ZipInputStream zipInputStream = new ZipInputStream(
            new ByteArrayInputStream(this.getOfficeOpenXMLDocument()));
    ZipEntry zipEntry;/*from  w  ww  . j  ava 2  s .  c om*/
    boolean hasOriginSigsRels = false;
    while (null != (zipEntry = zipInputStream.getNextEntry())) {
        zipOutputStream.putNextEntry(new ZipEntry(zipEntry.getName()));
        if ("[Content_Types].xml".equals(zipEntry.getName())) { //$NON-NLS-1$
            final Document contentTypesDocument = loadDocumentNoClose(zipInputStream);
            final Element typesElement = contentTypesDocument.getDocumentElement();

            // We need to add an Override element.
            final Element overrideElement = contentTypesDocument.createElementNS(
                    "http://schemas.openxmlformats.org/package/2006/content-types", "Override"); //$NON-NLS-1$ //$NON-NLS-2$
            overrideElement.setAttribute("PartName", "/" + signatureZipEntryName); //$NON-NLS-1$ //$NON-NLS-2$
            overrideElement.setAttribute("ContentType", //$NON-NLS-1$
                    "application/vnd.openxmlformats-package.digital-signature-xmlsignature+xml"); //$NON-NLS-1$
            typesElement.appendChild(overrideElement);

            final Element nsElement = contentTypesDocument.createElement("ns"); //$NON-NLS-1$
            nsElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:tns", //$NON-NLS-1$
                    "http://schemas.openxmlformats.org/package/2006/content-types"); //$NON-NLS-1$
            final NodeList nodeList = XPathAPI.selectNodeList(contentTypesDocument,
                    "/tns:Types/tns:Default[@Extension='sigs']", nsElement); //$NON-NLS-1$
            if (0 == nodeList.getLength()) {
                // Add Default element for 'sigs' extension.
                final Element defaultElement = contentTypesDocument.createElementNS(
                        "http://schemas.openxmlformats.org/package/2006/content-types", "Default"); //$NON-NLS-1$ //$NON-NLS-2$
                defaultElement.setAttribute("Extension", "sigs"); //$NON-NLS-1$ //$NON-NLS-2$
                defaultElement.setAttribute("ContentType", //$NON-NLS-1$
                        "application/vnd.openxmlformats-package.digital-signature-origin"); //$NON-NLS-1$
                typesElement.appendChild(defaultElement);
            }

            writeDocumentNoClosing(contentTypesDocument, zipOutputStream, false);
        } else if ("_rels/.rels".equals(zipEntry.getName())) { //$NON-NLS-1$
            final Document relsDocument = loadDocumentNoClose(zipInputStream);

            final Element nsElement = relsDocument.createElement("ns"); //$NON-NLS-1$
            nsElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:tns", RELATIONSHIPS_SCHEMA); //$NON-NLS-1$
            final NodeList nodeList = XPathAPI.selectNodeList(relsDocument,
                    "/tns:Relationships/tns:Relationship[@Type='http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/origin']", //$NON-NLS-1$
                    nsElement);
            if (0 == nodeList.getLength()) {
                final Element relationshipElement = relsDocument.createElementNS(RELATIONSHIPS_SCHEMA,
                        "Relationship"); //$NON-NLS-1$
                relationshipElement.setAttribute("Id", "rel-id-" + UUID.randomUUID().toString()); //$NON-NLS-1$ //$NON-NLS-2$
                relationshipElement.setAttribute("Type", //$NON-NLS-1$
                        "http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/origin"); //$NON-NLS-1$
                relationshipElement.setAttribute("Target", "_xmlsignatures/origin.sigs"); //$NON-NLS-1$ //$NON-NLS-2$

                relsDocument.getDocumentElement().appendChild(relationshipElement);
            }

            writeDocumentNoClosing(relsDocument, zipOutputStream, false);
        } else if (zipEntry.getName().startsWith("_xmlsignatures/_rels/") //$NON-NLS-1$
                && zipEntry.getName().endsWith(".rels")) { //$NON-NLS-1$

            hasOriginSigsRels = true;
            final Document originSignRelsDocument = loadDocumentNoClose(zipInputStream);

            final Element relationshipElement = originSignRelsDocument.createElementNS(RELATIONSHIPS_SCHEMA,
                    "Relationship"); //$NON-NLS-1$
            relationshipElement.setAttribute("Id", "rel-" + UUID.randomUUID().toString()); //$NON-NLS-1$ //$NON-NLS-2$
            relationshipElement.setAttribute("Type", //$NON-NLS-1$
                    "http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/signature"); //$NON-NLS-1$

            relationshipElement.setAttribute("Target", FilenameUtils.getName(signatureZipEntryName)); //$NON-NLS-1$
            originSignRelsDocument.getDocumentElement().appendChild(relationshipElement);

            writeDocumentNoClosing(originSignRelsDocument, zipOutputStream, false);
        } else {
            IOUtils.copy(zipInputStream, zipOutputStream);
        }
    }

    if (!hasOriginSigsRels) {
        // Add signature relationships document.
        addOriginSigsRels(signatureZipEntryName, zipOutputStream);
        addOriginSigs(zipOutputStream);
    }

    // Return.
    zipInputStream.close();
    return zipOutputStream;
}

From source file:com.verisign.epp.codec.signedMark.EPPEncodedSignedMark.java

/**
 * Sets all this instance's data in the given XML document
 * /*  w w w.j  av  a2  s.c  om*/
 * @param aDocument
 *        a DOM Document to attach data to.
 * @return The root element of this component.
 * @throws EPPEncodeException
 *         Thrown if any errors prevent encoding.
 */
public Element encode(Document aDocument) throws EPPEncodeException {
    cat.debug("EPPEncodedSignedMark.encode(Document): enter");

    if (aDocument == null) {
        throw new EPPEncodeException("aDocument is null" + " on in EPPSignedMark.encode(Document)");
    }

    Element root = aDocument.createElementNS(NS, NS_PREFIX + ":" + ELM_ENCODED_SIGNED_MARK_LOCALNAME);

    byte[] signedMarkXml = super.encode();

    String base64EncodedText = new String(Base64.encodeBase64(signedMarkXml, true));

    Text currVal = aDocument.createTextNode(base64EncodedText);
    root.appendChild(currVal);
    cat.debug("EPPEncodedSignedMark.encode(Document): exit - encoded");
    return root;
}