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.evolveum.midpoint.util.DOMUtil.java

public static void setElementTextContent(Element element, String value) {
    checkValidXmlChars(value);/*from w  w  w  .  j  a  va  2 s. c  o  m*/
    element.setTextContent(value);
}

From source file:net.sf.dsig.xmldsig.XmldsigStrategy.java

@Override
public String signPlaintext(String plaintext, PrivateKey privateKey, X509Certificate[] certificateChain)
        throws Exception {
    Document contentDocument = builder.newDocument();

    Element valueElem = contentDocument.createElement("value");
    valueElem.setTextContent(plaintext);
    contentDocument.appendChild(valueElem);

    return signInternal(contentDocument, "plaintext", privateKey, certificateChain);
}

From source file:org.gvnix.dynamic.configuration.roo.addon.config.XpathElementsDynamicConfiguration.java

/**
 * {@inheritDoc}// w  w  w  .  ja  v a  2 s  .  co m
 */
@Override
public void write(DynPropertyList dynProps) {

    // Get the XML file path
    MutableFile file = getFile();
    if (file != null) {

        // Obtain the root element of the XML file
        Document doc = getXmlDocument(file);

        // Update the root element property values with dynamic properties
        for (DynProperty dynProp : dynProps) {

            // Obtain the element related to this dynamic property
            String xpath = getXpath() + XPATH_ARRAY_PREFIX + getKey() + XPATH_EQUALS_SYMBOL
                    + XPATH_STRING_DELIMITER + getKeyValue(dynProp.getKey()) + XPATH_STRING_DELIMITER
                    + XPATH_ARRAY_SUFIX;
            Element elem = XmlUtils.findFirstElement(xpath, doc.getDocumentElement());
            if (elem == null) {

                logger.log(Level.WARNING, "Element " + xpath + " to set value not exists on file");
            } else {

                // If target element exists, set new value
                Element value = XmlUtils.findFirstElement(getValue(), elem);
                if (value == null) {

                    logger.log(Level.WARNING, "Element  " + xpath + " to set value not exists on file");
                } else {

                    value.setTextContent(dynProp.getValue());
                }
            }
        }

        // Update the XML file
        XmlUtils.writeXml(file.getOutputStream(), doc);

    } else if (!dynProps.isEmpty()) {

        logger.log(Level.WARNING,
                "File " + getFilePath() + " not exists and there are dynamic properties to set it");
    }
}

From source file:gov.nij.bundles.intermediaries.ers.EntityResolutionServiceIntermediaryTest.java

private SoapHeader makeSoapHeader(Document doc, String namespace, String localName, String value) {
    Element messageId = doc.createElementNS(namespace, localName);
    messageId.setTextContent(value);
    SoapHeader soapHeader = new SoapHeader(new QName(namespace, localName), messageId);
    return soapHeader;
}

From source file:gool.generator.xml.XmlCodePrinter.java

/**
 * Translate GOOL node to XML Element./* w  w w.  j  av  a  2s  .  c om*/
 * 
 * @param node
 *            GOOL node
 * @param document
 *            XML Document
 * @return XML Element
 */
private Element NodeToElement(Object node, Document document) {
    // element create for return
    Element newElement = null;

    // check if the parameter does not cause trouble
    if (node == null || node.getClass().getName().equals("gool.generator.xml.XmlPlatform"))
        return null;
    if (node.getClass().isAssignableFrom(gool.ast.core.Node.class)) {
        return null;
    }
    if (node.getClass().getName().equals("gool.ast.core.ClassDef")) {
        if (classdefok)
            classdefok = false;
        else
            return null;
    }

    // Create the new Element
    newElement = document.createElement(node.getClass().getName().substring(9));

    // find every method to find every child node
    Method[] meths = node.getClass().getMethods();
    for (Method meth : meths) {
        Class<?> laCl = meth.getReturnType();
        // check if the method return type is a node.
        if (gool.ast.core.Node.class.isAssignableFrom(laCl)
                && (meth.getParameterTypes().length == 0) /* && nbNode<1000 */) {
            // Debug for recursion
            // nbNode++;
            // Log.d(laCl.getName() + "\n" + nbNode);
            try {
                gool.ast.core.Node newNode = (gool.ast.core.Node) meth.invoke(node);
                // detect recursion risk.
                boolean recursionRisk = (newNode == node) ? true : false;
                if (methexclude.containsKey(node.getClass().getName())) {
                    for (String exmeth : methexclude.get(node.getClass().getName())) {
                        if (newNode == null || meth.getName().equals(exmeth))
                            recursionRisk = true;
                    }
                }
                if (recursionRisk) {
                    Element newElement2 = document.createElement(node.getClass().getName().substring(9));
                    newElement2.setTextContent("recursion risk detected!");
                    newElement.appendChild(newElement2);
                } else {
                    Element el = NodeToElement(newNode, document);
                    if (el != null) {
                        el.setAttribute("getterName", meth.getName());
                        newElement.appendChild(el);
                    }
                }
            } catch (Exception e) {
                Log.e(e);
                System.exit(1);
            }
        }
        // if the method return node list.
        else if (java.util.List.class.isAssignableFrom(laCl)) {
            try {
                java.util.List<Object> listObj = (java.util.List<Object>) meth.invoke(node);
                for (Object o : listObj) {
                    Element el = NodeToElement(o, document);
                    if (el != null) {
                        el.setAttribute("getterName", meth.getName());
                        newElement.appendChild(el);
                    }
                }
            } catch (Exception e) {
                Log.e(e);
                System.exit(1);
            }

        }
        // generate XML attribute for getter
        else if (meth.getName().startsWith("get") && !meth.getName().equals("getCode")
                && (meth.getParameterTypes().length == 0)) {
            try {
                if (!attrexclude.contains(meth.getName()))
                    newElement.setAttribute(meth.getName().substring(3),
                            meth.invoke(node) == null ? "null" : meth.invoke(node).toString());
            } catch (Exception e) {
                Log.e(e);
                System.exit(1);
            }
        }
        // generate XML attribute for iser
        else if (meth.getName().startsWith("is") && (meth.getParameterTypes().length == 0)) {
            try {
                if (!attrexclude.contains(meth.getName()))
                    newElement.setAttribute(meth.getName().substring(2),
                            meth.invoke(node) == null ? "null" : meth.invoke(node).toString());
            } catch (Exception e) {
                Log.e(e);
                System.exit(1);
            }
        }
    }
    return newElement;
}

From source file:com.photon.phresco.service.impl.DependencyManagerImpl.java

private List<Element> configList(File pomFile, String moduleGroupId, String moduleArtifactId,
        String moduleVersion, Document doc) throws PhrescoException {
    if (isDebugEnabled) {
        LOGGER.debug("DependencyManagerImpl.configList:Entry");
        LOGGER.info("DependencyManagerImpl.configList",
                ServiceConstants.PATH_EQUALS_SLASH + pomFile.getPath() + "\"",
                "moduleGroupId=\"" + moduleGroupId + "\"", "moduleArtifactId=\"" + moduleArtifactId + "\"",
                "moduleVersion=\"" + moduleVersion + "\"");
    }//from w  w  w .j  a  va2  s. c om
    List<Element> configList = new ArrayList<Element>();
    Element groupId = doc.createElement("groupId");
    groupId.setTextContent(moduleGroupId);
    Element artifactId = doc.createElement("artifactId");
    artifactId.setTextContent(moduleArtifactId);
    Element version = doc.createElement("version");
    version.setTextContent(moduleVersion);
    Element eleType = doc.createElement("type");
    eleType.setTextContent("zip");
    Element overWrite = doc.createElement("overWrite");
    overWrite.setTextContent("false");
    Element outputDirectory = doc.createElement("outputDirectory");
    outputDirectory.setTextContent(artifactTypeMap.get(artifactType));
    configList.add(groupId);
    configList.add(artifactId);
    configList.add(version);
    configList.add(eleType);
    configList.add(overWrite);
    configList.add(outputDirectory);
    if (isDebugEnabled) {
        LOGGER.debug("DependencyManagerImpl.configList:Exit");
    }
    return configList;
}

From source file:com.hydroLibCreator.action.Creator.java

private Node createInstrumentNode(Document document, String audioFilename, String index) {

    String noExtentionName = audioFilename.substring(0, audioFilename.indexOf("."));

    Element id = document.createElement("id");
    id.setTextContent(index);

    Element name = document.createElement("name");
    name.setTextContent(noExtentionName);

    Element volume = document.createElement("volume");
    volume.setTextContent("1");

    Element isMuted = document.createElement("isMuted");
    isMuted.setTextContent("false");

    Element pan_L = document.createElement("pan_L");
    pan_L.setTextContent("1");

    Element pan_R = document.createElement("pan_R");
    pan_R.setTextContent("1");

    Element randomPitchFactor = document.createElement("randomPitchFactor");
    randomPitchFactor.setTextContent("0");

    Element gain = document.createElement("gain");
    gain.setTextContent("1");

    Element filterActive = document.createElement("filterActive");
    filterActive.setTextContent("true");

    Element filterCutoff = document.createElement("filterCutoff");
    filterCutoff.setTextContent("1");

    Element filterResonance = document.createElement("filterResonance");
    filterResonance.setTextContent("0");

    Element attack = document.createElement("Attack");
    attack.setTextContent("0");

    Element decay = document.createElement("Decay");
    decay.setTextContent("0");

    Element sustain = document.createElement("Sustain");
    sustain.setTextContent("1");

    Element release = document.createElement("Release");
    release.setTextContent("1000");

    Element muteGroup = document.createElement("muteGroup");
    muteGroup.setTextContent("-1");

    Element midiOutChannel = document.createElement("midiOutChannel");
    midiOutChannel.setTextContent("-1");

    Element midiOutNote = document.createElement("midiOutNote");
    midiOutNote.setTextContent("60");

    Element isStopNote = document.createElement("isStopNote");
    isStopNote.setTextContent("false");

    Element fx1Level = document.createElement("FX1Level");
    fx1Level.setTextContent("0");

    Element fx2Level = document.createElement("FX2Level");
    fx2Level.setTextContent("0");

    Element fx3Level = document.createElement("FX3Level");
    fx3Level.setTextContent("0");

    Element fx4Level = document.createElement("FX4Level");
    fx4Level.setTextContent("0");

    Node layer = createLayerNode(document, audioFilename);

    Element instrument = document.createElement("instrument");
    instrument.appendChild(id);/* www.  j  av  a 2  s. c  o m*/
    instrument.appendChild(name);
    instrument.appendChild(volume);
    instrument.appendChild(isMuted);
    instrument.appendChild(pan_L);
    instrument.appendChild(pan_R);
    instrument.appendChild(randomPitchFactor);
    instrument.appendChild(gain);
    instrument.appendChild(filterActive);
    instrument.appendChild(filterCutoff);
    instrument.appendChild(filterResonance);
    instrument.appendChild(attack);
    instrument.appendChild(decay);
    instrument.appendChild(sustain);
    instrument.appendChild(release);
    instrument.appendChild(muteGroup);
    instrument.appendChild(midiOutChannel);
    instrument.appendChild(midiOutNote);
    instrument.appendChild(isStopNote);
    instrument.appendChild(fx1Level);
    instrument.appendChild(fx2Level);
    instrument.appendChild(fx3Level);
    instrument.appendChild(fx4Level);
    instrument.appendChild(layer);

    return instrument;
}

From source file:org.gvnix.dynamic.configuration.roo.addon.PomManagerImpl.java

/**
 * Write a property into POM profile./*www . ja  v  a 2s. c  o m*/
 * <p>
 * No write property if null value.
 * </p>
 * 
 * @param doc Pom document
 * @param props Properties element
 * @return New property element
 */
protected void exportProperty(Document doc, Element props, DynProperty dynProp) {

    String key = dynProp.getKey().replace('/', '.').replace('[', '.').replace(']', '.').replace('@', '.')
            .replace(':', '.').replace('-', '.').replace('%', '.');
    while (key.startsWith(".")) {
        key = key.substring(1, key.length());
    }
    while (key.endsWith(".")) {
        key = key.substring(0, key.length() - 1);
    }
    while (key.contains("..")) {
        key = key.replace("..", ".");
    }

    // Create property if not empty (no write property if null value)
    if (dynProp.getValue() != null) {

        // <log4j.rootLogger>INFO, stdout</log4j.rootLogger>

        // Create a property element for this dynamic property
        Element prop = doc.createElement(key);
        props.appendChild(prop);

        // Store this dynamic property value in pom and replace
        // dynamic property value with a var
        prop.setTextContent(dynProp.getValue());
    }

    dynProp.setValue("${" + key + "}");
}

From source file:org.openmrs.module.xforms.aop.XformsLocationAdvisor.java

/**
 * Refreshes a location in a given xforms select1 node.
 * //  w ww .  j  av  a 2  s . co  m
 * @param operation the refresh operation.
 * @param locationSelect1Element the location select1 node.
 * @param location the location.
 * @param oldName the location name before editing.
 * @param doc the xforms document.
 * @param xform the xform object.
 * @param xformsService the xforms service.
 * @throws Exception
 */
private void refreshLocationWithId(RefreshOperation operation, Element locationSelect1Element,
        Location location, String oldName, Document doc, Xform xform, XformsService xformsService)
        throws Exception {
    String sLocationId = location.getLocationId().toString();

    if (operation == RefreshOperation.DELETE || operation == RefreshOperation.EDIT) {

        //Get all xf:item nodes.
        NodeList elements = locationSelect1Element
                .getElementsByTagName(XformBuilder.PREFIX_XFORMS + ":" + "item");

        boolean locationFound = false;

        //Look for an item node having an id attribute equal to the locationId.
        for (int index = 0; index < elements.getLength(); index++) {
            Element itemElement = (Element) elements.item(index);
            if (!sLocationId.equals(itemElement.getAttribute(XformBuilder.ATTRIBUTE_ID)))
                continue; //Not the location we are looking for.

            //If the location has been deleted, then remove their item node from the xforms document.
            if (operation == RefreshOperation.DELETE) {
                locationSelect1Element.removeChild(itemElement);
            } else {
                //New name for the location after editing.
                String newName = XformBuilder.getLocationName(location);

                //If name has not changed, then just do nothing.
                if (newName.equals(oldName))
                    return;

                //If the location name has been edited, then change the xf:label node text.
                NodeList labels = itemElement.getElementsByTagName(XformBuilder.PREFIX_XFORMS + ":" + "label");

                //If the existing xforms label is not the same as the previous location's name, then
                //do not change it, possibly the location wants the xforms value not to match the location's name.
                Element labelElement = (Element) labels.item(0);
                if (!oldName.equals(labelElement.getTextContent()))
                    return;

                labelElement.setTextContent(newName);
            }

            locationFound = true;
            break;
        }

        //select1 node does not have the location to delete or edit.
        if (!locationFound) {
            if (operation == RefreshOperation.DELETE)
                return;

            addNewLocationNode(doc, locationSelect1Element, location);
        }

    } else {

        //Older versions of openmrs call AOP advisors more than once hence resulting into duplicates
        //if this check is not performed.
        if (locationExists(locationSelect1Element, sLocationId))
            return;

        //Add new location
        addNewLocationNode(doc, locationSelect1Element, location);
    }

    xform.setXformXml(XformsUtil.doc2String(doc));
    xformsService.saveXform(xform);
}

From source file:org.openmrs.module.xforms.aop.XformsProviderAdvisor.java

/**
 * Refreshes a provider in a given xforms select1 node.
 * /* w  w  w . j  av  a2s .  co m*/
 * @param operation the refresh operation.
 * @param providerSelect1Element the provider select1 node.
 * @param user the provider.
 * @param oldName the provider name before editing.
 * @param doc the xforms document.
 * @param xform the xform object.
 * @param xformsService the xforms service.
 * @throws Exception
 */
private void refreshProviderWithId(RefreshOperation operation, Element providerSelect1Element, User user,
        String oldName, Document doc, Xform xform, XformsService xformsService) throws Exception {
    Integer personId = XformsUtil.getPersonId(user);
    String sPersonId = personId.toString();

    if (operation == RefreshOperation.DELETE || operation == RefreshOperation.EDIT) {

        //Get all xf:item nodes.
        NodeList elements = providerSelect1Element
                .getElementsByTagName(XformBuilder.PREFIX_XFORMS + ":" + "item");

        boolean providerFound = false;

        //Look for an item node having an id attribute equal to the userId.
        for (int index = 0; index < elements.getLength(); index++) {
            Element itemElement = (Element) elements.item(index);
            if (!sPersonId.equals(itemElement.getAttribute(XformBuilder.ATTRIBUTE_ID)))
                continue; //Not the provider we are looking for.

            //If the user has been deleted, then remove their item node from the xforms document.
            if (operation == RefreshOperation.DELETE) {
                providerSelect1Element.removeChild(itemElement);
            } else {
                //New name for the provider after editing.
                String newName = XformBuilder.getProviderName(user, personId);

                //If name has not changed, then just do nothing.
                if (newName.equals(oldName))
                    return;

                //If the user name has been edited, then change the xf:label node text.
                NodeList labels = itemElement.getElementsByTagName(XformBuilder.PREFIX_XFORMS + ":" + "label");

                //If the existing xforms label is not the same as the previous user's name, then
                //do not change it, possibly the user wants the xforms value not to match the user's name.
                Element labelElement = (Element) labels.item(0);
                if (!oldName.equals(labelElement.getTextContent()))
                    return;

                labelElement.setTextContent(newName);
            }

            providerFound = true;
            break;
        }

        //select1 node does not have the provider to delete or edit.
        if (!providerFound) {
            if (operation == RefreshOperation.DELETE)
                return;

            //This must be a person who has just got a provider role which he or she did not have before.
            addNewProviderNode(doc, providerSelect1Element, user, personId);
        }

    } else {

        //Older versions of openmrs call AOP advisors more than once hence resulting into duplicates
        //if this check is not performed.
        if (providerExists(providerSelect1Element, sPersonId))
            return;

        //Add new provider
        addNewProviderNode(doc, providerSelect1Element, user, personId);
    }

    xform.setXformXml(XformsUtil.doc2String(doc));
    xformsService.saveXform(xform);
}