Example usage for org.w3c.dom Document createTextNode

List of usage examples for org.w3c.dom Document createTextNode

Introduction

In this page you can find the example usage for org.w3c.dom Document createTextNode.

Prototype

public Text createTextNode(String data);

Source Link

Document

Creates a Text node given the specified string.

Usage

From source file:de.crowdcode.movmvn.plugin.general.GeneralPlugin.java

private void createPomGroupArtifactVersion(Document doc, Element rootElement, String groupIdStr,
        String artifactIdStr, String versionStr) {
    Element groupId = doc.createElement("groupId");
    groupId.appendChild(doc.createTextNode(groupIdStr));
    rootElement.appendChild(groupId);/*  w ww  .j a  va 2s. com*/
    Element artifactId = doc.createElement("artifactId");
    artifactId.appendChild(doc.createTextNode(artifactIdStr));
    rootElement.appendChild(artifactId);
    Element version = doc.createElement("version");
    version.appendChild(doc.createTextNode(versionStr));
    rootElement.appendChild(version);
}

From source file:com.hangum.tadpole.engine.sql.util.QueryUtils.java

/**
 * query to xml//from w  w w  . jav a  2 s.  c  o  m
 * 
 * @param userDB
 * @param strQuery
 * @param listParam
 */
@SuppressWarnings("deprecation")
public static String selectToXML(final UserDBDAO userDB, final String strQuery, final List<Object> listParam)
        throws Exception {
    final StringWriter stWriter = new StringWriter();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    final Document doc = builder.newDocument();
    final Element results = doc.createElement("Results");
    doc.appendChild(results);

    SqlMapClient client = TadpoleSQLManager.getInstance(userDB);
    QueryRunner qr = new QueryRunner(client.getDataSource());
    qr.query(strQuery, listParam.toArray(), new ResultSetHandler<Object>() {

        @Override
        public Object handle(ResultSet rs) throws SQLException {
            ResultSetMetaData metaData = rs.getMetaData();

            while (rs.next()) {
                Element row = doc.createElement("Row");
                results.appendChild(row);
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    String columnName = metaData.getColumnName(i);
                    Object value = rs.getObject(i) == null ? "" : rs.getObject(i);
                    Element node = doc.createElement(columnName);
                    node.appendChild(doc.createTextNode(value.toString()));
                    row.appendChild(node);
                }
            }

            return stWriter.toString();
        }
    });

    DOMSource domSource = new DOMSource(doc);
    TransformerFactory tf = TransformerFactory.newInstance();
    tf.setAttribute("indent-number", 4);

    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");//"ISO-8859-1");
    StreamResult sr = new StreamResult(stWriter);
    transformer.transform(domSource, sr);

    return stWriter.toString();
}

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

public static Text createTextNode(Document doc, Element root, String text) {

    Text textNode;//from w  ww .  j a  va2  s. com

    if (text != null) {
        textNode = doc.createTextNode(text);
        root.appendChild(textNode);
    } else {
        textNode = doc.createTextNode("");
        root.appendChild(textNode);
    }

    return textNode;
}

From source file:com.bstek.dorado.idesupport.resolver.RobotResolver.java

protected void assembleError(Document document, Element responseElement, Exception error) {
    String message = error.getMessage();
    if (message == null) {
        message = error.getClass().getSimpleName();
    }/*from w  w  w  .  j a v  a 2 s .  com*/
    Element messageElement = document.createElement("Message");
    messageElement.appendChild(document.createTextNode(message));
    responseElement.appendChild(messageElement);

    Element stackTraceElement = document.createElement("StackTrace");
    responseElement.appendChild(stackTraceElement);

    StackTraceElement[] stackTrace = error.getStackTrace();
    for (StackTraceElement ste : stackTrace) {
        Element element = document.createElement("Element");
        element.setAttribute("className", ste.getClassName());
        element.setAttribute("methodName", ste.getMethodName());
        element.setAttribute("fileName", ste.getFileName());
        element.setAttribute("lineNumber", String.valueOf(ste.getLineNumber()));
        stackTraceElement.appendChild(element);
    }
}

From source file:de.tudarmstadt.ukp.dkpro.core.io.mmax2.MMAXWriter.java

public String createMMAXFile(String mmaxFilename) throws MMAXWriterException {

    log.info("Writing Basedata");
    File basedataFile = new File(basedataPath, mmaxFilename + ".xml");
    basedata.save(basedataFile);//from   ww w .j  a  v  a 2  s . c  o  m

    Document doc = createXML();
    Element toplevel = doc.createElement("mmax_project");

    Element elem = doc.createElement("words");
    elem.appendChild(doc.createTextNode(mmaxFilename + ".xml"));
    toplevel.appendChild(elem);

    File mmaxFile = new File(projectPath, mmaxFilename + ".mmax");
    doc.appendChild(toplevel);
    saveXML(doc, mmaxFile, null, null);

    // load the current discourse
    loadDiscourse(mmaxFile.getPath());

    return basedataFile.getName();
}

From source file:com.fiorano.openesb.application.common.Param.java

/**
 * This method converts the specified <code>Document</code> object to
 * the specified <code>Node</code> object.
 *
 * @param document the Document object//  ww w . ja v  a2  s. co  m
 * @return the element node with the corresponding text node value.
 * @since Tifosi2.0
 */
public Node toJXMLString(Document document) {
    Node root0 = document.createElement("Param");

    ((Element) root0).setAttribute("name", m_paramName);

    Node pcData = document.createTextNode(m_paramValue);

    root0.appendChild(pcData);
    return root0;
}

From source file:com.adaptris.core.services.jdbc.XmlPayloadTranslatorImpl.java

protected Node createTextNode(Document doc, String value, boolean isCDATA) {
    String munged = stripIllegalXmlChars() ? XmlHelper.stripIllegalXmlCharacters(value) : value;
    if (isCDATA) {
        return doc.createCDATASection(munged);
    }/*from   w w  w. j  a v a  2  s  . c  o  m*/
    return doc.createTextNode(munged);
}

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

public static Element createElementBeforeChild(Element root, Element child, String name, String text) {

    if (name == null) {
        throw new XMLToolException("Element name cannot be null!");
    } else if (name.trim().length() == 0) {
        throw new XMLToolException("Element name has to contain at least one character!");
    }/*from   w  w w.ja va2 s  .  c  o m*/

    Document doc = root.getOwnerDocument();
    Element elem = doc.createElement(name);
    if (text != null) {
        Text textNode = doc.createTextNode(text);
        elem.appendChild(textNode);
    }
    root.insertBefore(elem, child);

    return elem;
}

From source file:com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI.java

/**
 * Returns a node list for the specified Xpath and context node. When the result is just a string, a NodeList is 
 * constructed containing one node text containing this string.
 * //  ww  w  . j  a  v a  2s.c o m
 * @param contextNode
 * @param xpath
 * @return null is an error occurs
 * @throws TransformerException
 */
public NodeList selectNodeList(Node contextNode, String xpath) throws TransformerException {
    switch (xpathEngine) {
    case Xalan:
        checkContextNode(contextNode);
        XObject Xobj;

        Xobj = xpathApi.eval(contextNode, xpath);
        switch (Xobj.getType()) {
        case XObject.CLASS_NODESET:
            return (Xobj.nodelist());

        case XObject.CLASS_BOOLEAN:
        case XObject.CLASS_NUMBER:
        case XObject.CLASS_STRING:
            try {
                Document doc = XMLUtils.getDefaultDocumentBuilder().newDocument();
                Element root = (Element) doc.createElement("root");
                doc.appendChild(root);
                root.appendChild(doc.createTextNode(Xobj.str()));
                return (root.getChildNodes());
            } catch (Exception e) {
                return null;
            }
        }
        return null;
    }
    return new JXPathNodeList(selectList(contextNode, xpath));

}

From source file:com.alvexcore.share.ShareExtensionRegistry.java

public ExtensionUpdateInfo checkForUpdates(String extensionId, String shareId, Map<String, String> shareHashes,
        String shareVersion, String repoId, Map<String, String> repoHashes, String repoVersion,
        String licenseId) throws Exception {
    // search for extension
    DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance();
    xmlFact.setNamespaceAware(true);//from ww w  . j a v a  2  s. c  om
    xmlBuilder = xmlFact.newDocumentBuilder();

    // build query
    Document queryXML = xmlBuilder.newDocument();
    Element rootElement = queryXML.createElement("extension");
    queryXML.appendChild(rootElement);
    Element el = queryXML.createElement("license-id");
    el.appendChild(queryXML.createTextNode(licenseId));
    rootElement.appendChild(el);
    el = queryXML.createElement("id");
    el.appendChild(queryXML.createTextNode(extensionId));
    rootElement.appendChild(el);
    el = queryXML.createElement("share-version");
    el.appendChild(queryXML.createTextNode(shareVersion));
    rootElement.appendChild(el);
    el = queryXML.createElement("repo-version");
    el.appendChild(queryXML.createTextNode(repoVersion));
    rootElement.appendChild(el);
    el = queryXML.createElement("share-id");
    el.appendChild(queryXML.createTextNode(shareId));
    rootElement.appendChild(el);
    el = queryXML.createElement("repo-id");
    el.appendChild(queryXML.createTextNode(repoId));
    rootElement.appendChild(el);
    el = queryXML.createElement("share-files");
    rootElement.appendChild(el);
    for (String fileName : shareHashes.keySet()) {
        Element fileElem = queryXML.createElement("file");
        fileElem.setAttribute("md5hash", shareHashes.get(fileName));
        fileElem.appendChild(queryXML.createTextNode(fileName));
        el.appendChild(fileElem);
    }
    el = queryXML.createElement("repo-files");
    rootElement.appendChild(el);
    for (String fileName : repoHashes.keySet()) {
        Element fileElem = queryXML.createElement("file");
        fileElem.setAttribute("md5hash", repoHashes.get(fileName));
        fileElem.appendChild(queryXML.createTextNode(fileName));
        el.appendChild(fileElem);
    }
    // query server
    try {
        URL url = new URL(
                "https://update.alvexhq.com:443/alfresco/s/api/alvex/extension/" + extensionId + "/update");
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        // disable host verification
        conn.setHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String arg0, SSLSession arg1) {
                return true;
            }
        });
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.connect();
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        TransformerFactory.newInstance().newTransformer().transform(new DOMSource(queryXML),
                new StreamResult(wr));
        wr.close();
        // get response
        Document responseXML = xmlBuilder.parse(conn.getInputStream());
        XPath xpath = XPathFactory.newInstance().newXPath();

        // get version
        String repoLatestVersion = ((Node) xpath.evaluate("/extension/repo-version/text()", responseXML,
                XPathConstants.NODE)).getNodeValue();
        String shareLatestVersion = ((Node) xpath.evaluate("/extension/share-version/text()", responseXML,
                XPathConstants.NODE)).getNodeValue();
        NodeList nl = (NodeList) xpath.evaluate("/extension/repo-files/file", responseXML,
                XPathConstants.NODESET);
        Map<String, Boolean> repoFiles = new HashMap<String, Boolean>(nl.getLength());
        for (int i = 0; i < nl.getLength(); i++)
            repoFiles.put(nl.item(i).getTextContent(),
                    "ok".equals(nl.item(i).getAttributes().getNamedItem("status").getNodeValue()));
        nl = (NodeList) xpath.evaluate("/extension/share-files/file", responseXML, XPathConstants.NODESET);
        Map<String, Boolean> shareFiles = new HashMap<String, Boolean>(nl.getLength());
        for (int i = 0; i < nl.getLength(); i++)
            shareFiles.put(nl.item(i).getTextContent(),
                    "ok".equals(nl.item(i).getAttributes().getNamedItem("status").getNodeValue()));
        String motd = ((Node) xpath.evaluate("/extension/motd/text()", responseXML, XPathConstants.NODE))
                .getNodeValue();
        return new ExtensionUpdateInfo(extensionId, repoVersion, shareVersion, repoLatestVersion,
                shareLatestVersion, repoFiles, shareFiles, motd);
    } catch (Exception e) {
        return new ExtensionUpdateInfo(extensionId, repoVersion, shareVersion, "", "",
                new HashMap<String, Boolean>(), new HashMap<String, Boolean>(), "");
    }
}