Example usage for org.w3c.dom Element setTextContent

List of usage examples for org.w3c.dom Element setTextContent

Introduction

In this page you can find the example usage for org.w3c.dom Element setTextContent.

Prototype

public void setTextContent(String textContent) throws DOMException;

Source Link

Document

This attribute returns the text content of this node and its descendants.

Usage

From source file:org.apache.hadoop.mapred.QueueConfigurationParser.java

/**
 * Construct an {@link Element} for a single queue, constructing the inner
 * queue <name/>, <properties/>, <state/> and the inner
 * <queue> elements recursively.
 * // w w w. ja v a2 s. co m
 * @param document
 * @param jqi
 * @return
 */
static Element getQueueElement(Document document, JobQueueInfo jqi) {

    // Queue
    Element q = document.createElement(QUEUE_TAG);

    // Queue-name
    Element qName = document.createElement(QUEUE_NAME_TAG);
    qName.setTextContent(getSimpleQueueName(jqi.getQueueName()));
    q.appendChild(qName);

    // Queue-properties
    Properties props = jqi.getProperties();
    Element propsElement = document.createElement(PROPERTIES_TAG);
    if (props != null) {
        Set<String> propList = props.stringPropertyNames();
        for (String prop : propList) {
            Element propertyElement = document.createElement(PROPERTY_TAG);
            propertyElement.setAttribute(KEY_TAG, prop);
            propertyElement.setAttribute(VALUE_TAG, (String) props.get(prop));
            propsElement.appendChild(propertyElement);
        }
    }
    q.appendChild(propsElement);

    // Queue-state
    String queueState = jqi.getState().getStateName();
    if (queueState != null && !queueState.equals(QueueState.UNDEFINED.getStateName())) {
        Element qStateElement = document.createElement(STATE_TAG);
        qStateElement.setTextContent(queueState);
        q.appendChild(qStateElement);
    }

    // Queue-children
    List<JobQueueInfo> children = jqi.getChildren();
    if (children != null) {
        for (JobQueueInfo child : children) {
            q.appendChild(getQueueElement(document, child));
        }
    }

    return q;
}

From source file:org.apache.hadoop.mapred.QueueManagerTestUtils.java

public static Element createQueue(Document doc, String name) {
    Element queue = doc.createElement("queue");
    Element nameNode = doc.createElement("name");
    nameNode.setTextContent(name);
    queue.appendChild(nameNode);/* w w  w .  ja  v a 2  s. c o  m*/
    return queue;
}

From source file:org.apache.hadoop.mapred.QueueManagerTestUtils.java

public static Element createAcls(Document doc, String aclName, String listNames) {
    Element acls = doc.createElement(aclName);
    acls.setTextContent(listNames);
    return acls;/*  www . j  a v  a 2 s .  c o  m*/
}

From source file:org.apache.hadoop.mapred.QueueManagerTestUtils.java

public static Element createState(Document doc, String state) {
    Element stateElement = doc.createElement("state");
    stateElement.setTextContent(state);
    return stateElement;
}

From source file:org.apache.hadoop.yarn.applications.ivic.ApplicationMaster.java

private static String generateVmSetting(ConnectDataBase con, ResultSet rs) {
    if (con == null) {
        con = new ConnectDataBase();
    }/*ww w .  ja v  a 2  s  .  c  o  m*/
    String xmlStr = null;
    String sql;
    ResultSet rSet;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;
    try {
        builder = dbf.newDocumentBuilder();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Document doc = builder.newDocument();

    Element root = doc.createElement("vNode");
    doc.appendChild(root); // 

    try {
        Element hostName = doc.createElement("Hostname");
        hostName.setTextContent(rs.getString("hostname"));
        root.appendChild(hostName);

        Element password = doc.createElement("Password");
        password.setTextContent(rs.getString("vnc_password"));
        root.appendChild(password);

        Element mem = doc.createElement("Mem");
        mem.setTextContent(rs.getString("mem_total"));
        root.appendChild(mem);

        Element vcpu = doc.createElement("vCPU");
        vcpu.setTextContent(rs.getString("vcpu"));
        root.appendChild(vcpu);
        // ******************************************
        // vm_temps?
        // ******************************************
        if (rs.getString("vm_temp_id") != null) {
            sql = "select url, os_type, os_distribution, os_release, os_kernel, os_packages from vm_temps where id = "
                    + rs.getString("vm_temp_id");
            ResultSet set = con.executeQuery(sql);
            while (set.next()) {
                Element vTemplateRef = doc.createElement("vTemplateRef");
                vTemplateRef.setTextContent(set.getString("url"));
                root.appendChild(vTemplateRef);

                Element os = doc.createElement("OS");
                root.appendChild(os);

                Element type = doc.createElement("Type");
                type.setTextContent(set.getString("os_type"));
                os.appendChild(type);

                Element distribution = doc.createElement("Distribution");
                distribution.setTextContent(set.getString("os_distribution"));
                os.appendChild(distribution);

                Element release = doc.createElement("Release");
                release.setTextContent(set.getString("os_release"));
                os.appendChild(release);

                Element kernel = doc.createElement("Kernel");
                kernel.setTextContent(set.getString("os_kernel"));
                os.appendChild(kernel);

                Element packages = doc.createElement("Packages");
                packages.setTextContent(set.getString("os_packages"));
                os.appendChild(packages);
            }
        } else {
            Element devType = doc.createElement("DevType");
            devType.setTextContent(rs.getString("disk_dev_type"));
            root.appendChild(devType);
        }

        // **************************************************
        // ?vm_id?vdisks?
        // TODO ?????
        // volumecdromvdisk??typeposition?
        // **************************************************
        int index = 0;
        sql = "select uuid, vdisk_type, img_type, base_id, size from vdisks where virtual_machine_id = "
                + rs.getString("id") + " order by img_type, position";
        rSet = con.executeQuery(sql);
        while (rSet.next()) {
            Element vdisk = doc.createElement("vDisk");
            vdisk.setAttribute("id", "\\\'" + index + "\\\'");
            root.appendChild(vdisk);

            Element uuid = doc.createElement("UUID");
            uuid.setTextContent(rSet.getString("uuid"));
            vdisk.appendChild(uuid);

            Element type = doc.createElement("Type");
            if (rSet.getString("vdisk_type").equals("volumn"))
                type.setTextContent(rSet.getString("img_type"));
            else
                type.setTextContent("cdrom");
            vdisk.appendChild(type);

            // TODO ?
            Element path = doc.createElement("Path");
            if (rSet.getString("vdisk_type").equals("volumn"))
                path.setTextContent("/var/lib/ivic/vstore/" + rSet.getString("uuid") + ".img");
            else
                path.setTextContent("/var/lib/ivic/vstore/" + rSet.getString("uuid") + ".iso");
            vdisk.appendChild(path);

            if (rs.getString("vm_temp_id") != null && rSet.getString("base_id") != null) {
                Element basePath = doc.createElement("BasePath");
                // TODO vdiskbase_id??baseuuid????vdisk?
                sql = "select * from vdisks where id = " + rSet.getString("base_id");
                ResultSet set = con.executeQuery(sql);
                while (set.next()) {
                    if (rSet.getString("vdisk_type").equals("volumn"))
                        basePath.setTextContent("/var/lib/ivic/vstore/" + set.getString("uuid") + ".img");
                    else
                        basePath.setTextContent("/var/lib/ivic/vstore/" + set.getString("uuid") + ".iso");
                }
                vdisk.appendChild(basePath);
            }

            if (rSet.getString("img_type").equals("raw") || rSet.getString("img_type").equals("rootfs")) {
                Element size = doc.createElement("Size");
                size.setTextContent(rSet.getString("size"));
                vdisk.appendChild(size);
            }
            index++;
        }

        sql = "select vnics.*, vnets.vswitch_id from vnics, vnets where vnics.virtual_machine_id = "
                + rs.getString("id") + " and vnics.vnet_id = vnets.id";
        rSet = con.executeQuery(sql);
        while (rSet.next()) {
            Element nic = doc.createElement("NIC");
            nic.setAttribute("id", "\\\'" + (rSet.getRow() - 1) + "\\\'");
            root.appendChild(nic);

            Element mac = doc.createElement("MAC");
            mac.setTextContent(rSet.getString("mac_address"));
            nic.appendChild(mac);

            Element addr = doc.createElement("Address");
            addr.setTextContent(rSet.getString("ip_address"));
            nic.appendChild(addr);

            Element netmask = doc.createElement("Netmask");
            netmask.setTextContent(rSet.getString("netmask"));
            nic.appendChild(netmask);

            Element gateway = doc.createElement("GateWay");
            gateway.setTextContent(rSet.getString("gateway"));
            nic.appendChild(gateway);

            Element dns = doc.createElement("DNS");
            dns.setTextContent("8.8.8.8");
            nic.appendChild(dns);

            Element vswitch = doc.createElement("vSwitchRef");
            if (rSet.getString("vswitch_id") != null) {
                sql = "select uuid from vswitches where id = " + rSet.getString("vswitch_id");
                ResultSet set = con.executeQuery(sql);
                while (set.next()) {
                    vswitch.setTextContent(set.getString(1));
                }
            }
            nic.appendChild(vswitch);

            Element type = doc.createElement("vnetType");
            type.setTextContent(rSet.getString("gateway"));
            nic.appendChild(type);
        }
    } catch (DOMException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t;
    try {
        t = tf.newTransformer();
        t.setOutputProperty("encoding", "UTF-8");//GBK?   
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        t.transform(new DOMSource(doc), new StreamResult(bos));
        xmlStr = bos.toString();
        return xmlStr;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return xmlStr;
}

From source file:org.apache.nifi.cluster.flow.impl.DataFlowDaoImpl.java

private Element createTextElement(final Document document, final String elementName, final String value) {
    final Element element = document.createElement(elementName);
    element.setTextContent(value);
    return element;
}

From source file:org.apache.nifi.minifi.bootstrap.util.ConfigTransformer.java

protected static void addTextElement(final Element element, final String name, final String value) {
    final Document doc = element.getOwnerDocument();
    final Element toAdd = doc.createElement(name);
    toAdd.setTextContent(value);
    element.appendChild(toAdd);// w w w .  j a  va  2s .c o  m
}

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. ja  va  2  s  . c o m*/
        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  .java 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");

    // 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 va  2 s. com*/
 * @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;
}