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:com.raspi.chatapp.util.internet.XmppManager.java

/**
 * sends an image message/*  w  ww . ja  v a2s  . c  om*/
 *
 * @param serverFile  the file on the server
 * @param description the description of the sent image
 * @param buddyJID    the Buddy to receive the message
 * @return true if sending was successful
 */
public boolean sendImageMessage(String serverFile, String description, String buddyJID, long id) {
    if (buddyJID.indexOf('@') == -1)
        buddyJID += "@" + service;
    ChatManager chatManager = ChatManager.getInstanceFor(connection);
    if (connection != null && connection.isConnected() && chatManager != null) {
        Chat chat = chatManager.createChat(buddyJID);
        try {
            //generate the message in order to set the type to image
            Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
            Element msg = doc.createElement("message");
            doc.appendChild(msg);
            msg.setAttribute("type", MessageHistory.TYPE_IMAGE);
            msg.setAttribute("id", String.valueOf(id));
            Element file = doc.createElement("file");
            msg.appendChild(file);
            file.setTextContent(serverFile);
            Element desc = doc.createElement("description");
            msg.appendChild(desc);
            desc.setTextContent(description);

            // create the string
            Transformer t = TransformerFactory.newInstance().newTransformer();
            StringWriter writer = new StringWriter();
            StreamResult r = new StreamResult(writer);
            t.transform(new DOMSource(doc), r);
            String message = writer.toString();

            // send the message
            chat.sendMessage(message);
            Log.d("DEBUG", "Success: Sent message");
            return true;
        } catch (Exception e) {
            Log.e("ERROR", "Couldn't send message.");
            Log.e("ERROR", e.toString());
            return false;
        }
    }
    Log.e("ERROR", "Sending failed: No connection.");
    return false;
}

From source file:net.wesjd.overcastmappacker.xml.DocumentHandler.java

public void set(Class<? extends ParentXMLModule> parentClass, Class<? extends XMLModule> moduleClass,
        String moduleValue, ContinuingMap<String, String> attributeMapping) {
    final XMLModule module = XMLModule.of(moduleClass);
    final Element moduleElement = (Element) document.getElementsByTagName(module.getTag()).item(0);

    if (moduleElement == null)
        add(parentClass, moduleClass, moduleValue, attributeMapping);
    else {/* w w w  .jav  a 2s . c om*/
        moduleElement.setTextContent(moduleValue);
        handleAttributes(module, moduleElement, attributeMapping);
    }
}

From source file:net.wesjd.overcastmappacker.xml.DocumentHandler.java

public Element add(Class<? extends ParentXMLModule> parentClass, Class<? extends XMLModule> moduleClass,
        String moduleValue, ContinuingMap<String, String> attributeMapping) {
    final ParentXMLModule parentModule = getParentModule(parentClass, moduleClass);
    final XMLModule module = XMLModule.of(moduleClass);

    final Element moduleElement = document.createElement(module.getTag());
    moduleElement.setTextContent(moduleValue);

    Node parentNode = document.getElementsByTagName(parentModule.getTag()).item(0);
    if (parentNode == null) {
        set(null, parentModule.getClass());
        parentNode = document.getElementsByTagName(parentModule.getTag()).item(0);
    }// w w w  .j  a  v a  2  s. c  o  m
    parentNode.appendChild(moduleElement);

    handleAttributes(module, moduleElement, attributeMapping);

    return moduleElement;
}

From source file:netinf.common.communication.MessageEncoderXML.java

private static void appendElementWithValue(Document xml, Node node, String name, String value) {
    Element element = xml.createElement(name);
    element.setTextContent(value);
    node.appendChild(element);/*from www  . ja  v a  2s . co  m*/
}

From source file:nl.b3p.kaartenbalie.core.server.monitoring.DataMonitoring.java

public static Element createElement(Document doc, String createElementName, String textContent) {
    Element tmpElement = doc.createElement(createElementName);
    tmpElement.setTextContent(textContent);
    return tmpElement;
}

From source file:nl.b3p.viewer.admin.stripes.GeoServiceActionBean.java

public Resolution generateSld() throws Exception {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);//from   ww w.  ja  va2 s.  co  m
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document sldDoc = db.newDocument();

    Element sldEl = sldDoc.createElementNS(NS_SLD, "StyledLayerDescriptor");
    sldDoc.appendChild(sldEl);
    sldEl.setAttributeNS(NS_SLD, "version", "1.0.0");
    sldEl.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:schemaLocation",
            "http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd");
    sldEl.setAttribute("xmlns:ogc", NS_OGC);
    sldEl.setAttribute("xmlns:gml", NS_GML);
    service.loadLayerTree();

    Queue<Layer> layerStack = new LinkedList();
    Layer l = service.getTopLayer();
    while (l != null) {
        layerStack.addAll(service.getLayerChildrenCache(l));

        if (l.getName() != null) {
            Element nlEl = sldDoc.createElementNS(NS_SLD, "NamedLayer");
            sldEl.appendChild(nlEl);
            String title = l.getTitleAlias() != null ? l.getTitleAlias() : l.getTitle();
            if (title != null) {
                nlEl.appendChild(sldDoc.createComment(" Layer '" + title + "' "));
            }
            Element nEl = sldDoc.createElementNS(NS_SLD, "Name");
            nEl.setTextContent(l.getName());
            nlEl.appendChild(nEl);

            if (l.getFeatureType() != null) {
                String protocol = "";
                if (l.getFeatureType().getFeatureSource() != null) {
                    protocol = " (protocol " + l.getFeatureType().getFeatureSource().getProtocol() + ")";
                }

                String ftComment = " This layer has a feature type" + protocol
                        + " you can use in a FeatureTypeConstraint element as follows:\n";
                ftComment += "            <LayerFeatureConstraints>\n";
                ftComment += "                <FeatureTypeConstraint>\n";
                ftComment += "                    <FeatureTypeName>" + l.getFeatureType().getTypeName()
                        + "</FeatureTypeName>\n";
                ftComment += "                    Add ogc:Filter or Extent element here. ";
                if (l.getFeatureType().getAttributes().isEmpty()) {
                    ftComment += " No feature type attributes are known.\n";
                } else {
                    ftComment += " You can use the following feature type attributes in ogc:PropertyName elements:\n";
                    for (AttributeDescriptor ad : l.getFeatureType().getAttributes()) {
                        ftComment += "                    <ogc:PropertyName>" + ad.getName()
                                + "</ogc:PropertyName>";
                        if (ad.getAlias() != null) {
                            ftComment += " (" + ad.getAlias() + ")";
                        }
                        if (ad.getType() != null) {
                            ftComment += " (type: " + ad.getType() + ")";
                        }
                        ftComment += "\n";
                    }
                }
                ftComment += "                </FeatureTypeConstraint>\n";
                ftComment += "            </LayerFeatureConstraints>\n";
                ftComment += "        ";
                nlEl.appendChild(sldDoc.createComment(ftComment));
            }

            nlEl.appendChild(sldDoc.createComment(" Add a UserStyle or NamedStyle element here "));
            String styleComment = " (no server-side named styles are known other than 'default') ";
            ClobElement styleDetail = l.getDetails().get(Layer.DETAIL_WMS_STYLES);
            if (styleDetail != null) {
                try {
                    JSONArray styles = new JSONArray(styleDetail.getValue());

                    if (styles.length() > 0) {
                        styleComment = " The following NamedStyles are available according to the capabilities: \n";

                        for (int i = 0; i < styles.length(); i++) {
                            JSONObject jStyle = styles.getJSONObject(i);

                            styleComment += "            <NamedStyle><Name>" + jStyle.getString("name")
                                    + "</Name></NamedStyle>";
                            if (jStyle.has("title")) {
                                styleComment += " (" + jStyle.getString("title") + ")";
                            }
                            styleComment += "\n";
                        }
                    }

                } catch (JSONException e) {
                }
                styleComment += "        ";
            }
            nlEl.appendChild(sldDoc.createComment(styleComment));
        }

        l = layerStack.poll();
    }

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

    DOMSource source = new DOMSource(sldDoc);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(bos);
    t.transform(source, result);
    generatedSld = new String(bos.toByteArray(), "UTF-8");

    // indent doesn't add newline after XML declaration
    generatedSld = generatedSld.replaceFirst("\"\\?><StyledLayerDescriptor", "\"?>\n<StyledLayerDescriptor");
    return new ForwardResolution(JSP_EDIT_SLD);
}

From source file:npm.modules.java

public void addsite(Document doc, String sitename) throws Exception {
    Element site = doc.createElement("site");
    Element name = doc.createElement("name");
    name.setTextContent(sitename);
    Element style = doc.createElement("style");
    style.setTextContent("../sites/styles/" + sitename + "/");
    Element script = doc.createElement("script");
    script.setTextContent("../sites/scripts/" + sitename + "/");
    Node sitesnode = doc.getElementsByTagName("sites").item(0);
    Element sites = (Element) sitesnode;
    site.appendChild(name);//w ww  .j av  a 2 s  . co m
    site.appendChild(style);
    site.appendChild(script);
    sites.appendChild(site);
    savexml(doc);
}

From source file:org.akaza.openclinica.ws.EventEndpoint.java

/**
 * Create Success Response/*  w ww  .  j a  v a  2s  . c o  m*/
 * 
 * @param confirmation
 * @return
 * @throws Exception
 */
private Element mapSuccessConfirmation(HashMap<String, String> confirmation) throws Exception {
    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
    Document document = docBuilder.newDocument();

    Element responseElement = document.createElementNS(NAMESPACE_URI_V1, "scheduleResponse");
    Element resultElement = document.createElementNS(NAMESPACE_URI_V1, "result");
    Element eventDefinitionOID = document.createElementNS(NAMESPACE_URI_V1, "eventDefinitionOID");
    Element studySubjectOID = document.createElementNS(NAMESPACE_URI_V1, "studySubjectOID");
    Element eventOrdinal = document.createElementNS(NAMESPACE_URI_V1, "studyEventOrdinal");
    resultElement.setTextContent(messages.getMessage("eventEndpoint.success", null, "Success", locale));
    eventDefinitionOID.setTextContent(confirmation.get("eventDefinitionOID"));
    studySubjectOID.setTextContent(confirmation.get("studySubjectOID"));
    eventOrdinal.setTextContent(confirmation.get("studyEventOrdinal"));
    responseElement.appendChild(resultElement);
    responseElement.appendChild(eventDefinitionOID);
    responseElement.appendChild(studySubjectOID);
    responseElement.appendChild(eventOrdinal);
    return responseElement;

}

From source file:org.akaza.openclinica.ws.EventEndpoint.java

/**
 * Create Error Response//from w ww.j av a  2s .  c om
 * 
 * @param confirmation
 * @return
 * @throws Exception
 */
private Element mapFailConfirmation(Errors errors) throws Exception {
    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
    Document document = docBuilder.newDocument();

    Element responseElement = document.createElementNS(NAMESPACE_URI_V1, "scheduleResponse");
    Element resultElement = document.createElementNS(NAMESPACE_URI_V1, "result");
    resultElement.setTextContent(messages.getMessage("eventEndpoint.fail", null, "Fail", locale));
    responseElement.appendChild(resultElement);
    for (ObjectError error : errors.getAllErrors()) {
        Element errorElement = document.createElementNS(NAMESPACE_URI_V1, "error");
        String theMessage = messages.getMessage(error.getCode(), error.getArguments(), locale);
        errorElement.setTextContent(theMessage);
        responseElement.appendChild(errorElement);
    }
    return responseElement;

}

From source file:org.apache.airavata.gfac.hadoop.handler.HadoopDeploymentHandler.java

private void addPropertyToConfiguration(Map.Entry<Object, Object> entry, Element configElement, Document doc) {
    Element property = doc.createElement("property");
    configElement.appendChild(property);

    Element nameEle = doc.createElement("name");
    nameEle.setTextContent(entry.getKey().toString());
    property.appendChild(nameEle);/*from www  .  j  ava 2s  .c o m*/

    Element valueEle = doc.createElement("value");
    valueEle.setTextContent(entry.getValue().toString());
    property.appendChild(valueEle);
}