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:com.evolveum.midpoint.model.client.ModelClientUtil.java

public static Element createTextElement(QName qname, String value, Document doc) {
    Element element = doc.createElementNS(qname.getNamespaceURI(), qname.getLocalPart());
    element.setTextContent(value);/*from ww w.ja  va  2s . co m*/
    return element;
}

From source file:net.padaf.xmpbox.SaveMetadataHelper.java

/**
 * Prepare XMP Saving Put data necessary to make a well-formed XMP
 * /*w  ww.  java 2s  .c  o m*/
 * @param metadata
 *            metadata concerned by the serialization processing
 * @param intoXPacket
 *            true if Processing instruction must be embedded
 * @return The DOM Document which will represent the serialized metadata
 */
protected static Document prepareSaving(XMPMetadata metadata, boolean intoXPacket) {
    Document newdoc = (Document) metadata.getFuturOwner().cloneNode(true);
    if (intoXPacket) {
        ProcessingInstruction beginXPacket = newdoc.createProcessingInstruction("xpacket",
                "begin=\"" + metadata.getXpacketBegin() + "\" id=\"" + metadata.getXpacketId() + "\"");
        newdoc.appendChild(beginXPacket);
    }

    Element xmpMeta = newdoc.createElementNS("adobe:ns:meta/", "x:xmpmeta");
    xmpMeta.setAttributeNS(XMPSchema.NS_NAMESPACE, "xmlns:x", "adobe:ns:meta/");

    newdoc.appendChild(xmpMeta);
    Element elem = (Element) metadata.getContainerElement().cloneNode(true);
    newdoc.adoptNode(elem);
    xmpMeta.appendChild(elem);

    if (intoXPacket) {
        ProcessingInstruction endXPacket = newdoc.createProcessingInstruction("xpacket",
                metadata.getEndXPacket());
        newdoc.appendChild(endXPacket);
    }
    return newdoc;
}

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

private static void appendPoses(final Element parent, final Document document, final Iterator<JsonNode> itor) {
    while (itor.hasNext()) {
        final Iterator<JsonNode> lineItor = itor.next().elements();
        final Element pos = document.createElementNS(ODataConstants.NS_GML, ODataConstants.ELEM_POS);
        parent.appendChild(pos);//from  w  ww  .  ja  v  a 2  s  . c  o  m
        pos.appendChild(document.createTextNode(lineItor.next().asText() + " " + lineItor.next().asText()));
    }
}

From source file:com.hphoto.server.ApiServlet.java

private static Element addNode(Document doc, Node parent, String ns, String name, String text) {
    Element child = doc.createElementNS((String) NS_MAP.get(ns), ns + ":" + name);
    child.appendChild(doc.createTextNode(getLegalXml(text)));
    parent.appendChild(child);// w w w . java 2  s  . co m
    return child;
}

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

public static void serialize(final JsonGenerator jgen, final Element node, final String type)
        throws IOException {
    final EdmSimpleType edmSimpleType = EdmSimpleType.fromValue(type);

    if (edmSimpleType.equals(EdmSimpleType.GeographyCollection)
            || edmSimpleType.equals(EdmSimpleType.GeometryCollection)) {

        jgen.writeStringField(ODataConstants.ATTR_TYPE, EdmSimpleType.GeometryCollection.name());
    } else {/*from w w w  . j  a va 2 s  .  c  om*/
        final int yIdx = edmSimpleType.name().indexOf('y');
        final String itemType = edmSimpleType.name().substring(yIdx + 1);
        jgen.writeStringField(ODataConstants.ATTR_TYPE, itemType);
    }

    Element root = null;
    switch (edmSimpleType) {
    case GeographyPoint:
    case GeometryPoint:
        root = XMLUtils.getChildElements(node, ODataConstants.ELEM_POINT).get(0);

        jgen.writeArrayFieldStart(ODataConstants.JSON_COORDINATES);
        serializePoint(jgen, XMLUtils.getChildElements(root, ODataConstants.ELEM_POS).get(0));
        jgen.writeEndArray();
        break;

    case GeographyMultiPoint:
    case GeometryMultiPoint:
        root = XMLUtils.getChildElements(node, ODataConstants.ELEM_MULTIPOINT).get(0);

        jgen.writeArrayFieldStart(ODataConstants.JSON_COORDINATES);

        final List<Element> pMembs = XMLUtils.getChildElements(root, ODataConstants.ELEM_POINTMEMBERS);
        if (pMembs != null && !pMembs.isEmpty()) {
            for (Element point : XMLUtils.getChildElements(pMembs.get(0), ODataConstants.ELEM_POINT)) {
                jgen.writeStartArray();
                serializePoint(jgen, XMLUtils.getChildElements(point, ODataConstants.ELEM_POS).get(0));
                jgen.writeEndArray();
            }
        }

        jgen.writeEndArray();
        break;

    case GeographyLineString:
    case GeometryLineString:
        root = XMLUtils.getChildElements(node, ODataConstants.ELEM_LINESTRING).get(0);

        jgen.writeArrayFieldStart(ODataConstants.JSON_COORDINATES);
        serializeLineString(jgen, root);
        jgen.writeEndArray();
        break;

    case GeographyMultiLineString:
    case GeometryMultiLineString:
        root = XMLUtils.getChildElements(node, ODataConstants.ELEM_MULTILINESTRING).get(0);

        jgen.writeArrayFieldStart(ODataConstants.JSON_COORDINATES);

        final List<Element> lMembs = XMLUtils.getChildElements(root, ODataConstants.ELEM_LINESTRINGMEMBERS);
        if (lMembs != null && !lMembs.isEmpty()) {
            for (Element lineStr : XMLUtils.getChildElements(lMembs.get(0), ODataConstants.ELEM_LINESTRING)) {
                jgen.writeStartArray();
                serializeLineString(jgen, lineStr);
                jgen.writeEndArray();
            }
        }

        jgen.writeEndArray();
        break;

    case GeographyPolygon:
    case GeometryPolygon:
        root = XMLUtils.getChildElements(node, ODataConstants.ELEM_POLYGON).get(0);

        jgen.writeArrayFieldStart(ODataConstants.JSON_COORDINATES);
        serializePolygon(jgen, root);
        jgen.writeEndArray();
        break;

    case GeographyMultiPolygon:
    case GeometryMultiPolygon:
        root = XMLUtils.getChildElements(node, ODataConstants.ELEM_MULTIPOLYGON).get(0);

        jgen.writeArrayFieldStart(ODataConstants.JSON_COORDINATES);

        final List<Element> mpMembs = XMLUtils.getChildElements(root, ODataConstants.ELEM_SURFACEMEMBERS);
        if (mpMembs != null & !mpMembs.isEmpty()) {
            for (Element pol : XMLUtils.getChildElements(mpMembs.get(0), ODataConstants.ELEM_POLYGON)) {
                jgen.writeStartArray();
                serializePolygon(jgen, pol);
                jgen.writeEndArray();
            }
        }

        jgen.writeEndArray();
        break;

    case GeographyCollection:
    case GeometryCollection:
        root = XMLUtils.getChildElements(node, ODataConstants.ELEM_GEOCOLLECTION).get(0);

        final List<Element> cMembs = XMLUtils.getChildElements(root, ODataConstants.ELEM_GEOMEMBERS);
        if (cMembs != null && !cMembs.isEmpty()) {
            jgen.writeArrayFieldStart(ODataConstants.JSON_GEOMETRIES);

            for (Node geom : XMLUtils.getChildNodes(cMembs.get(0), Node.ELEMENT_NODE)) {
                try {
                    final DocumentBuilder builder = ODataConstants.DOC_BUILDER_FACTORY.newDocumentBuilder();
                    final Document doc = builder.newDocument();

                    final Element fakeParent = doc.createElementNS(ODataConstants.NS_DATASERVICES,
                            ODataConstants.PREFIX_DATASERVICES + "fake");
                    fakeParent.appendChild(doc.importNode(geom, true));

                    final EdmSimpleType callAsType = XMLUtils.simpleTypeForNode(
                            edmSimpleType == EdmSimpleType.GeographyCollection ? Geospatial.Dimension.GEOGRAPHY
                                    : Geospatial.Dimension.GEOMETRY,
                            geom);

                    jgen.writeStartObject();
                    serialize(jgen, fakeParent, callAsType.toString());
                    jgen.writeEndObject();
                } catch (Exception e) {
                    LOG.warn("While serializing {}", XMLUtils.getSimpleName(geom), e);
                }
            }

            jgen.writeEndArray();
        }
        break;

    default:
    }

    if (root != null) {
        serializeCrs(jgen, root);
    }
}

From source file:de.betterform.xml.xforms.ui.state.UIElementStateUtil.java

/**
 * Creates the state element./*from  www .java2 s. c  om*/
 *
 * @param parent the parent element to use.
 * @return the state element.
 */
public static Element createStateElement(Element parent) {
    Element state = DOMUtil.findFirstChildNS(parent, NamespaceConstants.BETTERFORM_NS,
            UIElementState.STATE_ELEMENT);
    if (state != null) {
        return state;
    }

    Document document = parent.getOwnerDocument();
    state = document.createElementNS(NamespaceConstants.BETTERFORM_NS,
            NamespaceConstants.BETTERFORM_PREFIX + ":" + UIElementState.STATE_ELEMENT);
    parent.appendChild(state);

    return state;
}

From source file:com.evolveum.midpoint.prism.util.PrismUtil.java

private static void fortifyNamespaceDeclarations(Element definitionElement, Element childElement) {
    Document doc = definitionElement.getOwnerDocument();
    NamedNodeMap attributes = childElement.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr attr = (Attr) attributes.item(i);
        if (DOMUtil.isNamespaceDefinition(attr)) {
            String prefix = DOMUtil.getNamespaceDeclarationPrefix(attr);
            String namespace = DOMUtil.getNamespaceDeclarationNamespace(attr);
            Element namespaceElement = doc.createElementNS(PrismConstants.A_NAMESPACE.getNamespaceURI(),
                    PrismConstants.A_NAMESPACE.getLocalPart());
            namespaceElement.setAttribute(PrismConstants.A_NAMESPACE_PREFIX, prefix);
            namespaceElement.setAttribute(PrismConstants.A_NAMESPACE_URL, namespace);
            definitionElement.insertBefore(namespaceElement, childElement);
        }/*w w w . ja  v a2 s. co m*/
    }
}

From source file:com.zimbra.cs.service.AutoDiscoverServlet.java

private static String createResponseDoc(String displayName, String email, String serviceUrl) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/* w  w  w.j  a  v  a 2 s  .  c  o m*/
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document xmlDoc = builder.newDocument();

    Element root = xmlDoc.createElementNS(NS, "Autodiscover");
    root.setAttribute("xmlns", NS);
    root.setAttribute("xmlns:xsi", XSI_NS);
    root.setAttribute("xmlns:xsd", XSD_NS);
    xmlDoc.appendChild(root);

    //Add the response element.
    Element response = xmlDoc.createElementNS(NS_MOBILE, "Response");
    root.appendChild(response);

    //Add culture to to response
    Element culture = xmlDoc.createElement("Culture");
    culture.appendChild(xmlDoc.createTextNode("en:en"));
    response.appendChild(culture);

    //User
    Element user = xmlDoc.createElement("User");
    Element displayNameElm = xmlDoc.createElement("DisplayName");
    displayNameElm.appendChild(xmlDoc.createTextNode(displayName));
    user.appendChild(displayNameElm);
    Element emailAddr = xmlDoc.createElement("EMailAddress");
    emailAddr.appendChild(xmlDoc.createTextNode(email));
    user.appendChild(emailAddr);
    response.appendChild(user);

    //Action
    Element action = xmlDoc.createElement("Action");
    Element settings = xmlDoc.createElement("Settings");
    Element server = xmlDoc.createElement("Server");

    Element type = xmlDoc.createElement("Type");
    type.appendChild(xmlDoc.createTextNode("MobileSync"));
    server.appendChild(type);

    Element url = xmlDoc.createElement("Url");
    url.appendChild(xmlDoc.createTextNode(serviceUrl));
    server.appendChild(url);

    Element name = xmlDoc.createElement("Name");
    name.appendChild(xmlDoc.createTextNode(serviceUrl));
    server.appendChild(name);

    settings.appendChild(server);
    action.appendChild(settings);
    response.appendChild(action);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    DOMSource source = new DOMSource(xmlDoc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    transformer.transform(source, result);
    writer.flush();
    String xml = writer.toString();
    writer.close();

    //manually generate xmlns for Autodiscover and Response element, this works
    //for testexchangeconnectivity.com, but iOS and Android don't like Response's xmlns
    //        StringBuilder str = new StringBuilder();
    //        str.append("<?xml version=\"1.0\"?>\n");
    //        str.append("<Autodiscover xmlns:xsd=\"").append(XSD_NS).append("\"");
    //        str.append(" xmlns:xsi=\"").append(XSI_NS).append("\"");
    //        str.append(" xmlns=\"").append(NS).append("\">\n");
    //        int respIndex = xml.indexOf("<Response>");
    //        str.append("<Response xmlns=\"").append(NS_MOBILE).append("\">");
    //        str.append(xml.substring(respIndex + "<Response>".length(), xml.length()));
    //        return str.toString();
    return "<?xml version=\"1.0\"?>\n" + xml;
}

From source file:com.zimbra.cs.service.AutoDiscoverServlet.java

private static String createResponseDocForEws(String displayName, String email, String serviceUrl, Account acct)
        throws Exception {

    Provisioning prov = Provisioning.getInstance();
    Server server = prov.getServer(acct);

    String cn = server.getCn();/*from  w  w w  .  ja va2  s  . c o m*/
    String name = server.getName();
    String acctId = acct.getId();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document xmlDoc = builder.newDocument();

    Element root = xmlDoc.createElementNS(NS, "Autodiscover");
    root.setAttribute("xmlns", NS);
    root.setAttribute("xmlns:xsi", XSI_NS);
    root.setAttribute("xmlns:xsd", XSD_NS);
    xmlDoc.appendChild(root);

    //Add the response element.
    Element response = xmlDoc.createElementNS(NS_OUTLOOK, "Response");
    root.appendChild(response);

    //User
    Element user = xmlDoc.createElement("User");
    Element displayNameElm = xmlDoc.createElement("DisplayName");
    displayNameElm.appendChild(xmlDoc.createTextNode(displayName));
    user.appendChild(displayNameElm);
    Element emailAddr = xmlDoc.createElement("EmailAddress");
    emailAddr.appendChild(xmlDoc.createTextNode(email));
    user.appendChild(emailAddr);

    Element depId = xmlDoc.createElement("DeploymentId");
    depId.appendChild(xmlDoc.createTextNode(acctId));
    user.appendChild(depId);

    response.appendChild(user);

    //Action
    Element account = xmlDoc.createElement("Account");
    Element acctType = xmlDoc.createElement("AccountType");
    acctType.appendChild(xmlDoc.createTextNode("email"));
    account.appendChild(acctType);
    response.appendChild(account);

    Element action = xmlDoc.createElement("Action");
    action.appendChild(xmlDoc.createTextNode("settings"));
    account.appendChild(action);

    Element protocol = xmlDoc.createElement("Protocol");
    account.appendChild(protocol);

    Element type = xmlDoc.createElement("Type");
    type.appendChild(xmlDoc.createTextNode("EXCH"));
    protocol.appendChild(type);

    Element ews = xmlDoc.createElement("EwsUrl");
    protocol.appendChild(ews);
    ews.appendChild(xmlDoc.createTextNode(serviceUrl));

    Element as = xmlDoc.createElement("ASUrl");
    protocol.appendChild(as);
    as.appendChild(xmlDoc.createTextNode(serviceUrl));

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    DOMSource source = new DOMSource(xmlDoc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    transformer.transform(source, result);
    writer.flush();
    String xml = writer.toString();
    writer.close();
    return "<?xml version=\"1.0\"?>\n" + xml;
}

From source file:com.evolveum.midpoint.testing.wstest.AbstractWebserviceTest.java

protected static Element createTextElement(QName qname, String value, Document doc) {
    Element element = doc.createElementNS(qname.getNamespaceURI(), qname.getLocalPart());
    element.setTextContent(value);/*from ww w  .  j  a va2  s.  c o m*/
    return element;
}