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.bluexml.side.Integration.alfresco.xforms.webscript.XmlBuilder.java

private static Element buildAssociation(Document doc, AssociationBean associationBean) {
    // association
    Element association = doc.createElement(ENTRY_ASSOCIATION_NODE);
    association.setAttribute(ENTRY_QUALIFIEDNAME, associationBean.getAssociationName());
    if (associationBean.getAssoType().equals(AssoType.Simple)) {
        association.setAttribute(ENTRY_ASSOCIATION_TYPE, ENTRY_ASSOCIATION_TYPE_SIMPLE);
    } else {/*from ww  w.  j  ava2  s.  c  o  m*/
        association.setAttribute(ENTRY_ASSOCIATION_TYPE, ENTRY_ASSOCIATION_TYPE_COMPOSITION);
    }
    // target
    String targetValue = associationBean.getTargetId();
    String targetLabel = associationBean.getTargetLabel();

    String targetNodeType = associationBean.getTargetQualifiedName();
    Element target = doc.createElement(ENTRY_ASSOCIATION_TARGET_NODE);
    target.setAttribute(ENTRY_QUALIFIEDNAME, targetNodeType);
    target.setTextContent(targetValue);
    if (StringUtils.trimToNull(targetLabel) != null) {
        target.setAttribute("label", targetLabel);
    }
    association.appendChild(target);

    return association;
}

From source file:cz.incad.kramerius.rest.api.k5.client.utils.SOLRUtils.java

public static Element value(Document doc, String attname, String val) {
    synchronized (doc) {
        Element strElm = doc.createElement("str");
        if (attname != null)
            strElm.setAttribute("name", attname);
        strElm.setTextContent(val);
        return strElm;
    }/*from ww  w .  ja v a  2s. co  m*/
}

From source file:cz.incad.kramerius.rest.api.k5.client.utils.SOLRUtils.java

public static Element value(Document doc, String attname, Integer val) {
    synchronized (doc) {
        Element strElm = doc.createElement("int");
        if (attname != null)
            strElm.setAttribute("name", attname);
        strElm.setTextContent("" + val);
        return strElm;
    }//from   w ww .j  a  va 2  s . c o  m
}

From source file:Main.java

/**
 * Given a certain parent {@link Element} and {@link Document}, append a
 * child {@link Element} with Tag Name (which cannot be null), Node
 * Attribute Name, Node Attribute Value (which both are null at the same
 * time or none is null at all), Node Value (which can be null).
 * /*ww  w  .  j a va 2 s .  c o m*/
 * @param nodeTagName
 *            Node Tag Name.
 * @param nodeAttributeName
 *            Node Attribute Name.
 * @param nodeAttributeValue
 *            Node Attribute Value.
 * @param nodeValue
 *            Node Value.
 * @param elementToBeApppendedTo
 *            Parent {@link Element} which the new created {@link Element}
 *            will be appended to.
 * @param document
 *            {@link Document} which everything belongs to.
 * 
 * @return Returns the created {@link Element}.
 */
private static Element appendNewElementToNode(String nodeTagName, String nodeAttributeName,
        String nodeAttributeValue, String nodeValue, Element elementToBeApppendedTo, Document document) {
    // create element and append it
    Element element = document.createElement(nodeTagName);
    if ((nodeAttributeName != null) && (nodeAttributeValue != null)) {
        element.setAttribute(nodeAttributeName, nodeAttributeValue);
    }
    if (nodeValue != null) {
        element.setTextContent(nodeValue);
    }
    elementToBeApppendedTo.appendChild(element);

    return element;
}

From source file:Main.java

public static Element beanToXml(Document document, Object obj)
        throws ParserConfigurationException, InvocationTargetException, IllegalAccessException {
    String name = obj.getClass().getName();
    Element rootElement = document.createElement(name);
    Method[] methods = obj.getClass().getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        String methodName = method.getName();
        String fieldName = analyzeFieldName(methodName);
        Element fieldElement = document.createElement(fieldName);
        if (methodName.startsWith("get") && !"getClass".equals(methodName)) {
            Object value = method.invoke(obj);
            if (value != null && !"".equals(value.toString())) {
                fieldElement.setTextContent(value.toString());
            } else {
                continue;
            }//  ww w.jav a  2  s  . c  o  m
        } else {
            continue;
        }
        rootElement.appendChild(fieldElement);
    }
    return rootElement;
}

From source file:com.photon.phresco.impl.WindowsApplicationProcessor.java

private static void createNewItemGroup(Document doc, List<ArtifactGroup> artifactGroups) {
    Element project = doc.getDocumentElement();
    Element itemGroup = doc.createElement(ITEMGROUP);
    for (ArtifactGroup artifactGroup : artifactGroups) {
        if (artifactGroup.getType().name().equals(Type.FEATURE.name())) {
            Element reference = doc.createElement(REFERENCE);
            reference.setAttribute(INCLUDE, artifactGroup.getName());
            Element hintPath = doc.createElement(HINTPATH);
            hintPath.setTextContent(DOUBLE_DOT + COMMON + File.separator + artifactGroup.getName() + DLL);
            reference.appendChild(hintPath);
            itemGroup.appendChild(reference);
        }//from  w  w w.  j  a v a2s.  c o  m
    }
    project.appendChild(itemGroup);
}

From source file:com.photon.phresco.impl.WindowsApplicationProcessor.java

private static void updateContent(Document doc, List<ArtifactGroup> artifactGroups, List<Node> itemGroup,
        String elementName) {//from   w w  w.ja v a2s. c o  m
    for (Node node : itemGroup) {
        NodeList childNodes = node.getChildNodes();
        for (int j = 0; j < childNodes.getLength(); j++) {
            Node item = childNodes.item(j);
            if (item.getNodeName().equals(elementName)) {
                Node parentNode = item.getParentNode();
                for (ArtifactGroup artifactGroup : artifactGroups) {
                    if (artifactGroup.getType().name().equals(Type.FEATURE.name())) {
                        Element content = doc.createElement(elementName);
                        if (elementName.equalsIgnoreCase(REFERENCE)) {
                            content.setAttribute(INCLUDE, artifactGroup.getName() + DLL);
                            Element hintPath = doc.createElement(HINTPATH);
                            hintPath.setTextContent(
                                    DOUBLE_DOT + COMMON + File.separator + artifactGroup.getName() + DLL);
                            content.appendChild(hintPath);
                        } else {
                            content.setAttribute(INCLUDE, artifactGroup.getName() + DLL);
                        }
                        parentNode.appendChild(content);
                    }
                }
                break;
            }
        }
    }
}

From source file:edu.kit.dama.mdm.content.util.DublinCoreHelper.java

/**
 * Create the Dublin Core document.// ww  w  .  j ava2  s .c om
 *
 * @param theObject The object to create the DC information for.
 * @param pCreator A custom creator stored as author/publisher in Dublin
 * Core. If not provided, the object's uploader is used if available.
 *
 * @return The Dublin Core Document.
 *
 * @throws ParserConfigurationException If creating the Dublin Core document
 * failed.
 */
public static Document createDublinCoreDocument(DigitalObject theObject, UserData pCreator)
        throws ParserConfigurationException {
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();

    Element root = doc.createElementNS("http://www.openarchives.org/OAI/2.0/oai_dc/", "oai_dc:dc");
    root.setAttribute("xmlns:dc", "http://purl.org/dc/elements/1.1/");
    root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    root.setAttribute("xsi:schemaLocation",
            "http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd");

    doc.appendChild(root);

    Element title = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:title");
    title.setTextContent(StringEscapeUtils.escapeXml11(theObject.getLabel()));
    root.appendChild(title);

    if (pCreator != null) {
        Element creator = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:creator");
        Element publisher = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:publisher");
        creator.setTextContent(StringEscapeUtils.escapeXml11(pCreator.getFullname()));
        publisher.setTextContent(StringEscapeUtils.escapeXml11(pCreator.getFullname()));
        root.appendChild(creator);
        root.appendChild(publisher);
    } else if (theObject.getUploader() != null) {
        Element creator = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:creator");
        Element publisher = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:publisher");
        creator.setTextContent(StringEscapeUtils.escapeXml11(theObject.getUploader().getFullname()));
        publisher.setTextContent(StringEscapeUtils.escapeXml11(theObject.getUploader().getFullname()));
        root.appendChild(creator);
        root.appendChild(publisher);
    }

    for (UserData experimenter : theObject.getExperimenters()) {
        //don't list uploader a second time here
        if (theObject.getUploader() == null || !experimenter.equals(theObject.getUploader())) {
            Element contributor = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:contributor");
            contributor.setTextContent(StringEscapeUtils.escapeXml11(experimenter.getFullname()));
            root.appendChild(contributor);
        }
    }

    if (theObject.getInvestigation() != null) {
        Element subject = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:subject");
        subject.setTextContent(StringEscapeUtils.escapeXml11(theObject.getInvestigation().getTopic()));
        root.appendChild(subject);
        if (theObject.getInvestigation().getDescription() != null) {
            Element description = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:description");
            description.setTextContent(
                    StringEscapeUtils.escapeXml11(theObject.getInvestigation().getDescription()));
            root.appendChild(description);
        }

        if (theObject.getInvestigation().getStudy() != null) {
            if (theObject.getInvestigation().getStudy().getLegalNote() != null) {
                Element rights = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:rights");
                rights.setTextContent(
                        StringEscapeUtils.escapeXml11(theObject.getInvestigation().getStudy().getLegalNote()));
                root.appendChild(rights);
            }
        }
    }

    if (theObject.getStartDate() != null) {
        Element date = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:date");
        date.setTextContent(df.format(theObject.getStartDate()));
        root.appendChild(date);
    }
    Element format = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:format");
    format.setTextContent("application/octet-stream");
    root.appendChild(format);
    Element type = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:type");
    type.setTextContent("Dataset");
    root.appendChild(type);
    Element identifier = doc.createElementNS("http://purl.org/dc/elements/1.1/", "dc:identifier");
    identifier.setTextContent(
            StringEscapeUtils.escapeXml11(theObject.getDigitalObjectId().getStringRepresentation()));
    root.appendChild(identifier);
    return doc;
}

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

/**
 * Appends a simple text line to the body of the document.
 *
 * @param document The containing document.
 * @param bodyElement The body of the document.
 * @param text The text to append./*from www.j av a  2  s  .c  o  m*/
 */
private static void appendSimpleText(Document document, Element bodyElement, String text) {
    Element statusElement = document.createElement("div");
    statusElement.setTextContent(text);
    bodyElement.appendChild(statusElement);
}

From source file:eu.scidipes.toolkits.palibrary.utils.XMLUtils.java

/**
 * Transforms a collection of {@link FormField} objects into a simple XML document
 * /*from   w  w w.  j  a va 2  s .c o m*/
 * @param fields
 *            a collection of form fields
 * @return an xml document representing the form fields, or null if the passed collection is null or empty, or all
 *         form fields have an empty value
 */
public static Node formFieldsToMetadata(final Collection<? extends FormField> fields) {
    if (fields != null && !fields.isEmpty()) {
        try {
            final Document xml;

            synchronized (documentBuilderFactory) {
                xml = documentBuilderFactory.newDocumentBuilder().newDocument();
            }

            xml.setXmlStandalone(true);
            final Element metaData = xml.createElement("metadata");
            int completedFieldCount = 0;

            for (final FormField field : fields) {
                if (!StringUtils.isEmpty(field.getValue())) {
                    completedFieldCount++;
                    final Element metaDataEntry = xml.createElement("metadataentry");
                    final Element entryName = xml.createElement("entryname");
                    final Element entryValue = xml.createElement("entryvalue");

                    entryName.setTextContent(field.getDisplayName());
                    entryValue.setTextContent(field.getValue());

                    metaDataEntry.appendChild(entryName);
                    metaDataEntry.appendChild(entryValue);
                    metaData.appendChild(metaDataEntry);
                }
            }

            if (completedFieldCount > 0) {
                xml.appendChild(metaData);
                return xml;
            } else {
                return null;
            }

        } catch (final ParserConfigurationException e) {
            LOG.error(e.getMessage(), e);
        }
    }
    return null;
}