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:ro.bmocanu.tests.ws.springws.server.ProductServiceDomPayloadEndpoint.java

/**
 * {@inheritDoc}/*from   w w w.j a v  a  2  s.  c o m*/
 */
@Override
protected Element invokeInternal(Element requestElement, Document responseDocument) throws Exception {
    NodeList nodeList = requestElement.getChildNodes();
    String requestText = nodeList.item(1).getTextContent();
    long id = Long.parseLong(requestText);
    System.out.println("Client asked for product with ID=" + id);

    Product product = ProductManager.singleInstance.getProductById(id);

    Element responseElement = responseDocument.createElementNS("http://test.ro/product-service",
            "GetProductByIdResponse");
    Element productElement = responseDocument.createElementNS("http://test.ro/product-service", "Product");
    productElement.setAttribute("id", product.getId() + "");
    productElement.setAttribute("name", product.getName());
    productElement.setAttribute("descriptions", product.getDescription());
    productElement.setAttribute("received", product.getReceived().toString());

    responseElement.appendChild(productElement);

    return responseElement;
}

From source file:net.sf.jabref.logic.msbib.MSBibDatabase.java

public Document getDOM() {
    Document document = null;
    try {/*from w w w .  ja va2  s.  c o m*/
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder documentBuilder = factory.newDocumentBuilder();
        document = documentBuilder.newDocument();

        Element rootNode = document.createElementNS(NAMESPACE, PREFIX + "Sources");
        rootNode.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", NAMESPACE);
        rootNode.setAttributeNS("http://www.w3.org/2000/xmlns/",
                "xmlns:" + PREFIX.substring(0, PREFIX.length() - 1), NAMESPACE);
        rootNode.setAttribute("SelectedStyle", "");

        for (MSBibEntry entry : entries) {
            Node node = entry.getDOM(document);
            rootNode.appendChild(node);
        }

        document.appendChild(rootNode);
    } catch (ParserConfigurationException e) {
        LOGGER.warn("Could not build XML document", e);
    }

    return document;
}

From source file:com.msopentech.odatajclient.engine.data.json.JSONEntryDeserializer.java

/**
 * {@inheritDoc }/*from ww  w .j  a  v  a2 s. c o  m*/
 */
@Override
public JSONEntry deserialize(final JsonParser parser, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    final ObjectNode tree = (ObjectNode) parser.getCodec().readTree(parser);

    if (tree.has(ODataConstants.JSON_VALUE) && tree.get(ODataConstants.JSON_VALUE).isArray()) {
        throw new JsonParseException("Expected OData Entity, found EntitySet", parser.getCurrentLocation());
    }

    final boolean isMediaEntry = tree.hasNonNull(ODataConstants.JSON_MEDIAREAD_LINK)
            && tree.hasNonNull(ODataConstants.JSON_MEDIA_CONTENT_TYPE);

    final JSONEntry entry = new JSONEntry();

    if (tree.hasNonNull(ODataConstants.JSON_METADATA)) {
        entry.setMetadata(URI.create(tree.get(ODataConstants.JSON_METADATA).textValue()));
        tree.remove(ODataConstants.JSON_METADATA);
    }

    if (tree.hasNonNull(ODataConstants.JSON_MEDIA_ETAG)) {
        entry.setMediaETag(tree.get(ODataConstants.JSON_MEDIA_ETAG).textValue());
        tree.remove(ODataConstants.JSON_MEDIA_ETAG);
    }

    if (tree.hasNonNull(ODataConstants.JSON_ETAG)) {
        entry.setETag(tree.get(ODataConstants.JSON_ETAG).textValue());
        tree.remove(ODataConstants.JSON_ETAG);
    }

    if (tree.hasNonNull(ODataConstants.JSON_TYPE)) {
        entry.setType(tree.get(ODataConstants.JSON_TYPE).textValue());
        tree.remove(ODataConstants.JSON_TYPE);
    }

    if (tree.hasNonNull(ODataConstants.JSON_ID)) {
        entry.setId(tree.get(ODataConstants.JSON_ID).textValue());
        tree.remove(ODataConstants.JSON_ID);
    }

    if (tree.hasNonNull(ODataConstants.JSON_READ_LINK)) {
        final JSONLink link = new JSONLink();
        link.setRel(ODataConstants.SELF_LINK_REL);
        link.setHref(tree.get(ODataConstants.JSON_READ_LINK).textValue());
        entry.setSelfLink(link);

        tree.remove(ODataConstants.JSON_READ_LINK);
    }

    if (tree.hasNonNull(ODataConstants.JSON_EDIT_LINK)) {
        final JSONLink link = new JSONLink();
        link.setRel(ODataConstants.EDIT_LINK_REL);
        link.setHref(tree.get(ODataConstants.JSON_EDIT_LINK).textValue());
        entry.setEditLink(link);

        tree.remove(ODataConstants.JSON_EDIT_LINK);
    }

    if (tree.hasNonNull(ODataConstants.JSON_MEDIAREAD_LINK)) {
        entry.setMediaContentSource(tree.get(ODataConstants.JSON_MEDIAREAD_LINK).textValue());
        tree.remove(ODataConstants.JSON_MEDIAREAD_LINK);
    }
    if (tree.hasNonNull(ODataConstants.JSON_MEDIAEDIT_LINK)) {
        final JSONLink link = new JSONLink();
        link.setHref(tree.get(ODataConstants.JSON_MEDIAEDIT_LINK).textValue());
        entry.addMediaEditLink(link);

        tree.remove(ODataConstants.JSON_MEDIAEDIT_LINK);
    }
    if (tree.hasNonNull(ODataConstants.JSON_MEDIA_CONTENT_TYPE)) {
        entry.setMediaContentType(tree.get(ODataConstants.JSON_MEDIA_CONTENT_TYPE).textValue());
        tree.remove(ODataConstants.JSON_MEDIA_CONTENT_TYPE);
    }

    final Set<String> toRemove = new HashSet<String>();
    final Iterator<Map.Entry<String, JsonNode>> itor = tree.fields();
    while (itor.hasNext()) {
        final Map.Entry<String, JsonNode> field = itor.next();

        if (field.getKey().endsWith(ODataConstants.JSON_NAVIGATION_LINK_SUFFIX)
                || field.getKey().endsWith(ODataConstants.JSON_NAVIGATION_LINK_ODATA_4_SUFFIX)) {
            final JSONLink link = new JSONLink();
            link.setTitle(getTitle(field));
            link.setRel(ODataConstants.NAVIGATION_LINK_REL + getTitle(field));
            if (field.getValue().isValueNode()) {
                link.setHref(field.getValue().textValue());
                link.setType(ODataLinkType.ENTITY_NAVIGATION.toString());
            }
            // NOTE: this should be expected to happen, but it isn't - at least up to OData 4.0
            /* if (field.getValue().isArray()) {
             * link.setHref(field.getValue().asText());
             * link.setType(ODataLinkType.ENTITY_SET_NAVIGATION.toString());
             * } */
            entry.addNavigationLink(link);

            toRemove.add(field.getKey());
            toRemove.add(setInline(field.getKey(),
                    field.getKey().endsWith(ODataConstants.JSON_NAVIGATION_LINK_SUFFIX)
                            ? ODataConstants.JSON_NAVIGATION_LINK_SUFFIX
                            : ODataConstants.JSON_NAVIGATION_LINK_ODATA_4_SUFFIX,
                    tree, parser.getCodec(), link));
        } else if (field.getKey().endsWith(ODataConstants.JSON_ASSOCIATION_LINK_SUFFIX)) {
            final JSONLink link = new JSONLink();
            link.setTitle(getTitle(field));
            link.setRel(ODataConstants.ASSOCIATION_LINK_REL + getTitle(field));
            link.setHref(field.getValue().textValue());
            link.setType(ODataLinkType.ASSOCIATION.toString());
            entry.addAssociationLink(link);

            toRemove.add(field.getKey());
        } else if (field.getKey().endsWith(ODataConstants.JSON_MEDIAEDIT_LINK_SUFFIX)) {
            final JSONLink link = new JSONLink();
            link.setTitle(getTitle(field));
            link.setRel(ODataConstants.MEDIA_EDIT_LINK_REL + getTitle(field));
            link.setHref(field.getValue().textValue());
            link.setType(ODataLinkType.MEDIA_EDIT.toString());
            entry.addMediaEditLink(link);

            toRemove.add(field.getKey());
            toRemove.add(setInline(field.getKey(), ODataConstants.JSON_MEDIAEDIT_LINK_SUFFIX, tree,
                    parser.getCodec(), link));
        } else if (field.getKey().charAt(0) == '#') {
            final ODataOperation operation = new ODataOperation();
            operation.setMetadataAnchor(field.getKey());

            final ObjectNode opNode = (ObjectNode) tree.get(field.getKey());
            operation.setTitle(opNode.get(ODataConstants.ATTR_TITLE).asText());
            operation.setTarget(URI.create(opNode.get(ODataConstants.ATTR_TARGET).asText()));

            entry.addOperation(operation);

            toRemove.add(field.getKey());
        }
    }
    tree.remove(toRemove);

    try {
        final DocumentBuilder builder = ODataConstants.DOC_BUILDER_FACTORY.newDocumentBuilder();
        final Document document = builder.newDocument();

        final Element properties = document.createElementNS(ODataConstants.NS_METADATA,
                ODataConstants.ELEM_PROPERTIES);

        DOMTreeUtils.buildSubtree(properties, tree);

        if (isMediaEntry) {
            entry.setMediaEntryProperties(properties);
        } else {
            entry.setContent(properties);
        }
    } catch (ParserConfigurationException e) {
        throw new JsonParseException("Cannot build entry content", parser.getCurrentLocation(), e);
    }

    return entry;
}

From source file:edu.umd.cfar.lamp.viper.examples.textline.AttributeWrapperTextline.java

public Element getXMLFormatConfig(Document root, Node container) {
    String qualifier = ViperData.ViPER_DATA_QUALIFIER;
    String uri = ViperData.ViPER_DATA_URI;
    Element poss = root.createElementNS(uri, qualifier + "textlink");
    if (getTextLink() != null)
        poss.setAttribute("value", getTextLink().getAttrName());
    return poss;// www  .j ava 2  s.  com
}

From source file:gov.nij.bundles.intermediaries.ers.EntityResolutionServiceIntermediaryTest.java

private SoapHeader makeSoapHeader(Document doc, String namespace, String localName, String value) {
    Element messageId = doc.createElementNS(namespace, localName);
    messageId.setTextContent(value);// w  w w.  j a  v  a2 s  .co  m
    SoapHeader soapHeader = new SoapHeader(new QName(namespace, localName), messageId);
    return soapHeader;
}

From source file:com.amalto.core.history.accessor.ManyFieldAccessor.java

public void insert() {
    Document domDocument = document.asDOM();
    Element parentNode = (Element) parent.getNode();
    NodeList children = parentNode.getElementsByTagName(fieldName);
    Node refNode = children.item(index);
    Node node = domDocument.createElementNS(domDocument.getNamespaceURI(), fieldName);
    parentNode.insertBefore(node, refNode);
    cachedCollectionItemNode = (Element) node;
}

From source file:edu.umd.cfar.lamp.viper.examples.textline.AttributeWrapperTextline.java

/**
 * Creates an XML element from the given object o
 * // w  w  w  . j  a v a  2  s  . c o m
 * @return the XML serialization of o (a TextlineModel object)
 */
public Element getXMLFormat(Document root, Object o, Node container) {
    String qualifier = ViperData.ViPER_DATA_QUALIFIER;
    String uri = ViperData.ViPER_DATA_URI;
    Element el = root.createElementNS(uri, qualifier + "textline");
    TextlineModel tm = (TextlineModel) o;
    el.setAttribute(AT_X, String.valueOf(tm.getX()));
    el.setAttribute(AT_Y, String.valueOf(tm.getY()));
    el.setAttribute(AT_W, String.valueOf(tm.getWidth()));
    el.setAttribute(AT_H, String.valueOf(tm.getHeight()));
    el.setAttribute(AT_R, String.valueOf(tm.getRotation()));
    el.setAttribute(AT_OCC, String.valueOf(tm.getOcclusionsAsStr()));
    el.setAttribute(AT_OFF, String.valueOf(tm.getWordOffsetsAsStr()));
    el.setAttribute(AT_TXT, String.valueOf(tm.getText(null)));
    return el;
}

From source file:be.fedict.eid.applet.service.signer.ooxml.RelationshipTransformService.java

@Override
public void marshalParams(XMLStructure parent, XMLCryptoContext context) throws MarshalException {
    LOG.debug("marshallParams(parent,context)");
    DOMStructure domParent = (DOMStructure) parent;
    Node parentNode = domParent.getNode();
    Element parentElement = (Element) parentNode;
    parentElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:mdssi",
            "http://schemas.openxmlformats.org/package/2006/digital-signature");
    Document document = parentNode.getOwnerDocument();
    for (String sourceId : this.sourceIds) {
        Element relationshipReferenceElement = document.createElementNS(
                "http://schemas.openxmlformats.org/package/2006/digital-signature",
                "mdssi:RelationshipReference");
        relationshipReferenceElement.setAttribute("SourceId", sourceId);
        parentElement.appendChild(relationshipReferenceElement);
    }/*from w ww .j  ava 2  s.  c  om*/
    for (String sourceType : this.sourceTypes) {
        Element relationshipsGroupReferenceElement = document.createElementNS(
                "http://schemas.openxmlformats.org/package/2006/digital-signature",
                "mdssi:RelationshipsGroupReference");
        relationshipsGroupReferenceElement.setAttribute("SourceType", sourceType);
        parentElement.appendChild(relationshipsGroupReferenceElement);
    }
}

From source file:at.gv.egovernment.moa.id.auth.validator.parep.client.szrgw.SZRGWClient.java

private Element getSOAPBody() throws SZRGWClientException {
    Document doc_;
    try {/*from  w  ww  .  ja  v a2 s .c  o m*/
        doc_ = ParepUtils.createEmptyDocument();
        Element root = doc_.createElementNS(SOAPConstants.SOAP_ENV_NS,
                SOAPConstants.SOAP_ENV_PREFIX + SOAPConstants.ENVELOPE);
        doc_.appendChild(root);

        root.setAttribute("xmlns" + SOAPConstants.SOAP_ENV_POSTFIX, SOAPConstants.SOAP_ENV_NS);
        //root.setAttribute(SOAPConstants.SOAP_ENV_PREFIX + SOAPConstants.ENCODING_STYLE, SOAPConstants.SOAP_ENV_ENCODING_STYLE);
        root.setAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
        root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");

        Element body = doc_.createElementNS(SOAPConstants.SOAP_ENV_NS,
                SOAPConstants.SOAP_ENV_PREFIX + SOAPConstants.BODY);
        root.appendChild(body);

        return body;
    } catch (SZRGWClientException e) {
        throw new SZRGWClientException(e);
    }

}

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

@Nonnull
public Completable sendIceCandidates(@Nonnull final Jid recipient, @Nonnull final String id,
        @Nonnull final Collection<IceCandidate> candidates) {
    final Document iq = Stanza.getIqTemplate(Stanza.IqType.SET, UUID.randomUUID().toString(),
            getSession().getNegotiatedJid(), recipient);
    final Element webrtc = (Element) iq.getDocumentElement().appendChild(iq.createElementNS(XMLNS, "webrtc"));
    webrtc.setAttribute("id", id);
    for (IceCandidate candidate : candidates) {
        final Element candidateElement = (Element) webrtc.appendChild(iq.createElement("ice-candidate"));
        candidateElement.setAttribute("sdpMLineIndex", Integer.toString(candidate.sdpMLineIndex));
        if (StringUtils.isNotBlank(candidate.sdpMid)) {
            candidateElement.setAttribute("sdpMid", candidate.sdpMid);
        }/*from  w  w w . j a va2s  .com*/
        if (StringUtils.isNotBlank(candidate.sdp)) {
            final Element sdpElement = (Element) candidateElement.appendChild(iq.createElement("sdp"));
            for (String line : candidate.sdp.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();
}