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.google.visualization.datasource.render.HtmlRenderer.java

/**
 * Appends <html>, <head>, <title>, and <body> elements to the document.
 *
 * @param document The containing document.
 *
 * @return The <body> element.
 *///from   ww w .ja v a  2s. c  o m
private static Element appendHeadAndBody(Document document) {
    Element htmlElement = document.createElement("html");
    document.appendChild(htmlElement);
    Element headElement = document.createElement("head");
    htmlElement.appendChild(headElement);
    Element titleElement = document.createElement("title");
    titleElement.setTextContent("Google Visualization");
    headElement.appendChild(titleElement);
    Element bodyElement = document.createElement("body");
    htmlElement.appendChild(bodyElement);
    return bodyElement;
}

From source file:com.google.visualization.datasource.render.HtmlRenderer.java

/**
 * Renders a simple html for the given responseStatus.
 *
 * @param responseStatus The response message.
 *
 * @return A simple html for the given responseStatus.
 *///from w  ww.  j  ava  2  s.c om
public static CharSequence renderHtmlError(ResponseStatus responseStatus) {
    // Get the responseStatus details.
    StatusType status = responseStatus.getStatusType();
    ReasonType reason = responseStatus.getReasonType();
    String detailedMessage = responseStatus.getDescription();

    // Create an xml document with head and an empty body.
    Document document = createDocument();
    Element bodyElement = appendHeadAndBody(document);

    // Populate the xml document.
    Element oopsElement = document.createElement("h3");
    oopsElement.setTextContent("Oops, an error occured.");
    bodyElement.appendChild(oopsElement);

    if (status != null) {
        String text = "Status: " + status.lowerCaseString();
        appendSimpleText(document, bodyElement, text);
    }

    if (reason != null) {
        String text = "Reason: " + reason.getMessageForReasonType(null);
        appendSimpleText(document, bodyElement, text);
    }

    if (detailedMessage != null) {
        String text = "Description: " + sanitizeDetailedMessage(detailedMessage);
        appendSimpleText(document, bodyElement, text);
    }

    return transformDocumentToHtmlString(document);
}

From source file:Main.java

static void generateXMLIntervalSampleCoarse(double time, long tracks, String hardware, boolean finalStats) {
    Element element;
    Element root = doc.getDocumentElement();

    Element me;//w  w  w  . j ava 2  s  .c  o m
    if (finalStats)
        me = doc.createElement("FinalSample");
    else
        me = doc.createElement("Sample");
    root.appendChild(me);
    root = me;

    element = doc.createElement("Timestamp");
    element.setTextContent(Double.toString(time));
    root.appendChild(element);

    element = doc.createElement("Hardware");
    element.setTextContent(hardware);
    root.appendChild(element);

    // if (transactions > 0) {
    element = doc.createElement("DataAvailable");
    element.setTextContent("True");
    root.appendChild(element);

    element = doc.createElement("LaidTracks");
    element.setTextContent(Long.toString(tracks));
    root.appendChild(element);

}

From source file:com.nortal.jroad.util.SOAPUtil.java

/**
 * Adds a new element to an existing element
 *
 * @param element The parent {@link Element}, which the child will be added to.
 * @param id Tag name of the new {@link Element}
 * @param type xsi type of the new {@link Element}
 * @param value Text value of the new {@link Element}
 * @return//  ww w. j  a  v a 2 s. c  om
 * @throws SOAPException
 */
public static Element addElement(Element element, String id, String type, String value) throws SOAPException {
    Element child = element.getOwnerDocument().createElement(id);

    if (value != null) {
        child.setTextContent(value);
    }

    SOAPUtil.addTypeAttribute(child, type);

    element.appendChild(child);
    return child;
}

From source file:com.google.visualization.datasource.render.HtmlRenderer.java

/**
 * Generates an HTML string representation of a data table.
 * //from  w w  w.  ja v a 2  s. c o m
 * @param dataTable The data table to render.
 * @param locale The locale. If null, uses the default from
 *     {@code LocaleUtil#getDefaultLocale}.
 *
 * @return The char sequence with the html string.
 */
public static CharSequence renderDataTable(DataTable dataTable, ULocale locale) {
    // Create an xml document with head and an empty body.
    Document document = createDocument();
    Element bodyElement = appendHeadAndBody(document);

    // Populate the xml document.
    Element tableElement = document.createElement("table");
    bodyElement.appendChild(tableElement);
    tableElement.setAttribute("border", "1");
    tableElement.setAttribute("cellpadding", "2");
    tableElement.setAttribute("cellspacing", "0");

    // Labels tr element.
    List<ColumnDescription> columnDescriptions = dataTable.getColumnDescriptions();
    Element trElement = document.createElement("tr");
    trElement.setAttribute("style", "font-weight: bold; background-color: #aaa;");
    for (ColumnDescription columnDescription : columnDescriptions) {
        Element tdElement = document.createElement("td");
        tdElement.setTextContent(columnDescription.getLabel());
        trElement.appendChild(tdElement);
    }
    tableElement.appendChild(trElement);

    Map<ValueType, ValueFormatter> formatters = ValueFormatter.createDefaultFormatters(locale);
    // Table tr elements.
    int rowCount = 0;
    for (TableRow row : dataTable.getRows()) {
        rowCount++;
        trElement = document.createElement("tr");
        String backgroundColor = (rowCount % 2 != 0) ? "#f0f0f0" : "#ffffff";
        trElement.setAttribute("style", "background-color: " + backgroundColor);

        List<TableCell> cells = row.getCells();
        for (int c = 0; c < cells.size(); c++) {
            ValueType valueType = columnDescriptions.get(c).getType();
            TableCell cell = cells.get(c);
            String cellFormattedText = cell.getFormattedValue();
            if (cellFormattedText == null) {
                cellFormattedText = formatters.get(cell.getType()).format(cell.getValue());
            }

            Element tdElement = document.createElement("td");
            if (cell.isNull()) {
                tdElement.setTextContent("\u00a0");
            } else {
                switch (valueType) {
                case NUMBER:
                    tdElement.setAttribute("align", "right");
                    tdElement.setTextContent(cellFormattedText);
                    break;
                case BOOLEAN:
                    BooleanValue booleanValue = (BooleanValue) cell.getValue();
                    tdElement.setAttribute("align", "center");
                    if (booleanValue.getValue()) {
                        tdElement.setTextContent("\u2714"); // Check mark.
                    } else {
                        tdElement.setTextContent("\u2717"); // X mark.
                    }
                    break;
                default:
                    if (StringUtils.isEmpty(cellFormattedText)) {
                        tdElement.setTextContent("\u00a0"); // nbsp.
                    } else {
                        tdElement.setTextContent(cellFormattedText);
                    }
                }
            }
            trElement.appendChild(tdElement);
        }
        tableElement.appendChild(trElement);
    }
    bodyElement.appendChild(tableElement);

    // Warnings:
    for (Warning warning : dataTable.getWarnings()) {
        bodyElement.appendChild(document.createElement("br"));
        bodyElement.appendChild(document.createElement("br"));
        Element messageElement = document.createElement("div");
        messageElement.setTextContent(
                warning.getReasonType().getMessageForReasonType() + ". " + warning.getMessage());
        bodyElement.appendChild(messageElement);
    }

    return transformDocumentToHtmlString(document);
}

From source file:Main.java

public static Element addTextElement(Node parent, String name, String value, Attr[] attrs) {
    Element element;
    if (parent instanceof Document) {
        element = ((Document) parent).createElement(name);
    } else {/*  w w w .j  ava 2s.c o m*/
        element = parent.getOwnerDocument().createElement(name);
    }

    if (attrs != null && attrs.length > 0) {
        for (Attr attr : attrs) {
            element.setAttributeNode(attr);
        }
    }

    if (value != null) {
        element.setTextContent(value);
    }
    parent.appendChild(element);
    return element;
}

From source file:apps.ParsedPost.java

private static String createYahooAnswersQuestion(ParsedPost parentPost, ParsedPost post)
        throws ParserConfigurationException, TransformerException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    // root elements
    Document doc = docBuilder.newDocument();
    Element rootElement = doc.createElement("document");

    doc.appendChild(rootElement);// w  w w . j  a  v  a2s  .  c o  m

    Element uri = doc.createElement("uri");
    uri.setTextContent(parentPost.mId);
    rootElement.appendChild(uri);

    Element subject = doc.createElement("subject");
    subject.setTextContent(parentPost.mTitle);
    rootElement.appendChild(subject);

    Element content = doc.createElement("content");
    content.setTextContent(parentPost.mBody);
    rootElement.appendChild(content);

    Element bestanswer = doc.createElement("bestanswer");
    bestanswer.setTextContent(post.mBody);
    rootElement.appendChild(bestanswer);

    Element answer_item = doc.createElement("answer_item");
    answer_item.setTextContent(post.mBody);
    Element nbestanswers = doc.createElement("nbestanswers");
    nbestanswers.appendChild(answer_item);
    rootElement.appendChild(nbestanswers);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);

    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);

    transformer.transform(source, result);
    return "<vespaadd>" + xhlp.removeHeader(sw.toString()).replace("&", "&amp;") + "</vespaadd>\n";
}

From source file:net.sourceforge.dita4publishers.word2dita.Word2DitaValidationHelper.java

/**
 * @param commentsDom//from   w  w  w .  j a  va 2  s  .c o m
 * @param commentTemplate
 * @param messageText
 * @param commentId
 */
static void addCommentToComments(Document commentsDom, Element commentTemplate, String messageText,
        String commentId) {
    Element comment = (Element) commentsDom.importNode(commentTemplate, true);
    commentsDom.getDocumentElement().appendChild(comment);
    comment.setAttributeNS(wNs, "w:id", commentId);
    comment.setAttributeNS(wNs, "w:author", "XML Validator");
    comment.setAttributeNS(wNs, "w:initials", "XMLVal");
    comment.setAttributeNS(wNs, "w:date", timestampFormatter.format(Calendar.getInstance().getTime()));
    Element elem = DataUtil.getElementNS(comment, wNs, "p");
    NodeList nl = elem.getElementsByTagNameNS(wNs, "r");
    elem = (Element) nl.item(nl.getLength() - 1);
    Element text = DataUtil.getElementNS(elem, wNs, "t");
    text.setTextContent(messageText);
}

From source file:Main.java

static void generateXMLIntervalSample(double time, long commits, long transactions, long commitMemRefs,
        long totalMemRefs, String hardware, boolean finalStats) {
    Element element;
    Element root = doc.getDocumentElement();

    Element me;//from www . j  av a2 s.  c  om
    if (finalStats)
        me = doc.createElement("FinalSample");
    else
        me = doc.createElement("Sample");
    root.appendChild(me);
    root = me;

    element = doc.createElement("Timestamp");
    element.setTextContent(Double.toString(time));
    root.appendChild(element);

    element = doc.createElement("Hardware");
    element.setTextContent(hardware);
    root.appendChild(element);

    // if (transactions > 0) {
    element = doc.createElement("DataAvailable");
    element.setTextContent("True");
    root.appendChild(element);

    element = doc.createElement("Transactions");
    element.setTextContent(Long.toString(transactions));
    root.appendChild(element);

    element = doc.createElement("Commits");
    element.setTextContent(Long.toString(commits));
    root.appendChild(element);

    String pc;
    if (commits != 0 && transactions != 0) {
        pc = Long.toString(100 * commits / transactions);
    } else {
        pc = "0";
    }
    element = doc.createElement("PercentCommits");
    element.setTextContent(pc);
    root.appendChild(element);

    element = doc.createElement("MemRefs");
    element.setTextContent(Long.toString(totalMemRefs));
    root.appendChild(element);

    element = doc.createElement("CommitMemRefs");
    element.setTextContent(Long.toString(commitMemRefs));
    root.appendChild(element);

    String pm;
    if (commitMemRefs != 0 && totalMemRefs != 0) {
        pm = Long.toString(100 * commitMemRefs / totalMemRefs);
    } else {
        pm = "0";
    }
    element = doc.createElement("PercentCommitMemRefs");
    element.setTextContent(pm);
    root.appendChild(element);
}

From source file:com.amalto.core.storage.hibernate.DefaultStorageClassLoader.java

private static void setPropertyValue(Document document, String propertyName, String value)
        throws XPathExpressionException {
    XPathExpression compile = pathFactory
            .compile("hibernate-configuration/session-factory/property[@name='" + propertyName + "']"); //$NON-NLS-1$ //$NON-NLS-2$
    Node node = (Node) compile.evaluate(document, XPathConstants.NODE);
    if (node != null) {
        node.setTextContent(value);//from w  w  w  .  jav  a  2  s  .c  o m
    } else {
        XPathExpression parentNodeExpression = pathFactory.compile("hibernate-configuration/session-factory"); //$NON-NLS-1$
        Node parentNode = (Node) parentNodeExpression.evaluate(document, XPathConstants.NODE);
        // Create a new property element for this datasource-specified property (TMDM-4927).
        Element property = document.createElement("property"); //$NON-NLS-1$
        Attr propertyNameAttribute = document.createAttribute("name"); //$NON-NLS-1$
        property.setAttributeNode(propertyNameAttribute);
        propertyNameAttribute.setValue(propertyName);
        property.setTextContent(value);
        parentNode.appendChild(property);
    }
}