Example usage for org.w3c.dom Text setNodeValue

List of usage examples for org.w3c.dom Text setNodeValue

Introduction

In this page you can find the example usage for org.w3c.dom Text setNodeValue.

Prototype

public void setNodeValue(String nodeValue) throws DOMException;

Source Link

Document

The value of this node, depending on its type; see the table above.

Usage

From source file:org.openmrs.web.controller.report.CohortReportFormController.java

/**
 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
 *      org.springframework.validation.BindException)
 */// ww w  .  j  a  v  a2 s  . c o  m
@Override
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object commandObj,
        BindException errors) throws Exception {
    CommandObject command = (CommandObject) commandObj;

    // do simpleframework serialization of everything but 'rows', and add those via handcoded xml, since
    // serializing them is not reversible

    ReportSchema rs = new ReportSchema();
    rs.setReportSchemaId(command.getReportId());
    rs.setName(command.getName());
    rs.setDescription(command.getDescription());
    rs.setReportParameters(command.getParameters());
    rs.setDataSetDefinitions(new ArrayList<DataSetDefinition>());
    Serializer serializer = OpenmrsUtil.getSerializer();
    StringWriter sw = new StringWriter();
    serializer.write(rs, sw);

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(false);
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document xml = db.parse(new InputSource(
            new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + sw.toString())));
    Node node = findChild(xml, "reportSchema");
    node = findChild(node, "dataSets");
    Element dsd = xml.createElement("dataSetDefinition");
    dsd.setAttribute("name", "cohorts");
    dsd.setAttribute("class", "org.openmrs.report.CohortDataSetDefinition");
    node.appendChild(dsd);
    Element strategies = xml.createElement("strategies");
    strategies.setAttribute("class", "java.util.LinkedHashMap");
    dsd.appendChild(strategies);
    Element descriptions = xml.createElement("descriptions");
    descriptions.setAttribute("class", "java.util.LinkedHashMap");
    dsd.appendChild(descriptions);
    for (CohortReportRow row : command.getRows()) {
        if (StringUtils.hasText(row.getQuery())) {
            Element entry = xml.createElement("entry");
            strategies.appendChild(entry);
            Element nameEl = xml.createElement("string");
            Text val = xml.createTextNode(row.getName());
            val.setNodeValue(row.getName());
            nameEl.appendChild(val);
            entry.appendChild(nameEl);
            Element cohort = xml.createElement("cohort");
            entry.appendChild(cohort);
            cohort.setAttribute("class", "org.openmrs.reporting.PatientSearch");
            Element strategyEl = xml.createElement("specification");
            val = xml.createTextNode(row.getQuery());
            val.setNodeValue(row.getQuery());
            strategyEl.appendChild(val);
            cohort.appendChild(strategyEl);
        }
        if (StringUtils.hasText(row.getDescription())) {
            Element entry = xml.createElement("entry");
            descriptions.appendChild(entry);
            Element el = xml.createElement("string");
            Text val = xml.createTextNode(row.getName());
            val.setNodeValue(row.getName());
            el.appendChild(val);
            entry.appendChild(el);
            el = xml.createElement("string");
            val = xml.createTextNode(row.getDescription());
            val.setNodeValue(row.getDescription());
            el.appendChild(val);
            entry.appendChild(el);
        }
    }

    // now turn this into an xml string
    System.setProperty("javax.xml.transform.TransformerFactory",
            "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans = transfac.newTransformer();
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    trans.setOutputProperty(OutputKeys.INDENT, "yes");
    trans.setOutputProperty(OutputKeys.METHOD, "xml");
    StringWriter out = new StringWriter();
    StreamResult result = new StreamResult(out);
    DOMSource source = new DOMSource(xml);
    trans.transform(source, result);
    String schemaXml = out.toString();

    ReportSchemaXml rsx = new ReportSchemaXml();
    rsx.populateFromReportSchema(rs);
    rsx.setXml(schemaXml);
    rsx.updateXmlFromAttributes();
    rsx.setUuid(request.getParameter("parentUUID"));

    ReportService rptSvc = (ReportService) Context.getService(ReportService.class);
    if (rsx.getReportSchemaId() != null) {
        rptSvc.saveReportSchemaXml(rsx);
    } else {
        rptSvc.saveReportSchemaXml(rsx);
    }

    return new ModelAndView(new RedirectView(getSuccessView() + "?reportId=" + rsx.getReportSchemaId()));
}

From source file:com.netspective.sparx.util.xml.XmlSource.java

public void replaceNodeMacros(Node inNode, Set nodeNames, Map variables) {
    if (!variables.containsKey("this"))
        variables.put("this", inNode);

    NamedNodeMap attrs = inNode.getAttributes();
    if (attrs != null && attrs.getLength() > 0) {
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attr = attrs.item(i);
            if (nodeNames.contains(attr.getNodeName())) {
                String nodeValue = attr.getNodeValue();
                String replaced = replaceExpressions(nodeValue, variables);
                if (nodeValue != replaced)
                    attr.setNodeValue(replaced);
            }/*from w w  w  .jav  a 2 s. c  o m*/
        }
    }

    NodeList children = inNode.getChildNodes();
    for (int c = 0; c < children.getLength(); c++) {
        Node node = children.item(c);
        if (node.getNodeType() == Node.ELEMENT_NODE && nodeNames.contains(node.getNodeName())) {
            Text textNode = (Text) node.getFirstChild();
            String nodeValue = textNode.getNodeValue();
            String replaced = replaceExpressions(nodeValue, variables);
            if (nodeValue != replaced)
                textNode.setNodeValue(replaced);
        }
    }
}

From source file:org.sakaiproject.tool.assessment.qti.asi.ASIBaseClass.java

/**
 *
 *
 * @param xpath// w w w .  j a v a2 s .  c o  m
 * @param fieldlabel
 */
protected void createFieldentry(String xpath, String fieldlabel) {
    if (log.isDebugEnabled()) {
        log.debug("createFieldentry(String " + xpath + ", String " + fieldlabel + ")");
    }

    try {
        List qtimetadataNodes = this.selectNodes(xpath);
        if (qtimetadataNodes.size() > 0) {
            Node qtimetadataNode = (Node) qtimetadataNodes.get(0);
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document newDocument = db.newDocument();

            Element qtimetadataField = newDocument.createElement(QTIConstantStrings.QTIMETADATAFIELD);
            Element fieldlabelElement = newDocument.createElement(QTIConstantStrings.FIELDLABEL);
            Element fieldentryElement = newDocument.createElement(QTIConstantStrings.FIELDENTRY);

            Text fieldlabelText = newDocument.createTextNode(QTIConstantStrings.FIELDLABEL);
            fieldlabelText.setNodeValue(fieldlabel);
            fieldlabelElement.appendChild(fieldlabelText);

            Text fieldentryText = newDocument.createTextNode(QTIConstantStrings.FIELDENTRY);
            fieldentryElement.appendChild(fieldentryText);

            Node importedFLE = qtimetadataField.getOwnerDocument().importNode(fieldlabelElement, true);
            Node importedFEE = qtimetadataField.getOwnerDocument().importNode(fieldentryElement, true);
            qtimetadataField.appendChild(importedFLE);
            qtimetadataField.appendChild(importedFEE);
            Node importedField = qtimetadataNode.getOwnerDocument().importNode(qtimetadataField, true);
            qtimetadataNode.appendChild(importedField);
        }
    } catch (ParserConfigurationException pce) {
        log.error("Exception thrown from createFieldentry()" + pce.getMessage(), pce);
        pce.printStackTrace();
    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
    }
}

From source file:org.sakaiproject.tool.assessment.qti.helper.AuthoringXml.java

/**
 * perform Update on this object//w  w w  . j a  v a2s .co  m
 * Based on method originally in XmlStringBuffer
 * @author rashmi
 * @author casong
 * @author Ed Smiley esmiley@stanford.edu changed method signatures used Document
 * @param document Document
 * @param xpath :- xpath and
 * @param value :-  Value of xpath
 *
 * @return modified Document
 * @throws DOMException DOCUMENTATION PENDING
 * @throws Exception DOCUMENTATION PENDING
 */
public Document update(Document document, String xpath, String value) throws DOMException, Exception {
    if (log.isDebugEnabled()) {
        log.debug("update(String " + xpath + ", String " + value + ")");
    }

    try {
        Element newElement = null;
        Attr newAttribute = null;
        List newElementList = this.selectNodes(document, xpath);
        int aIndex = xpath.indexOf("@");
        int size = newElementList.size();
        if (size > 1) {
            log.warn("UPDATING MORE THAN ONE ELEMENT");
        }

        if ((aIndex == -1) && (size != 0)) {
            for (int i = 0; i < size; i++) {
                newElement = (Element) newElementList.get(i);
                Node childNode = newElement.getFirstChild();

                if (childNode == null) {
                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                    DocumentBuilder db = dbf.newDocumentBuilder();
                    Document core = db.newDocument();
                    Text newElementText = core.createTextNode(newElement.getNodeName());
                    newElementText.setNodeValue(value);
                    Text clonedText = (Text) newElement.getOwnerDocument().importNode(newElementText, true);
                    newElement.appendChild(clonedText);
                } else {
                    CharacterData newElementText = (CharacterData) newElement.getFirstChild();
                    newElementText.setNodeValue(value);
                }
            }
        }

        if ((aIndex != -1) && (size != 0)) {
            newAttribute = (Attr) newElementList.set(0, null);
            if (newAttribute != null) {
                newAttribute.setValue(value);
            }
        }
    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
    }

    return document;
}

From source file:org.sakaiproject.tool.assessment.qti.util.XmlStringBuffer.java

/**
 * perform Update on this object/*from w  w w.j av a2 s .  c om*/
 *
 * @param xpath :- xpath and
 * @param value :-  Value of xpath
 *
 * @return XmlStringBuffer
 *
 * @throws DOMException
 * @throws Exception
 */

// Rashmi Aug 19th changed by Pamela on Sept 10th.
// Rashmi - replacing updateJDOM as on Sep 15
public final XmlStringBuffer update(String xpath, String value) throws DOMException, Exception {
    if (log.isDebugEnabled()) {
        log.debug("update(String " + xpath + ", String " + value + ")");
    }

    try {
        Element newElement = null;
        Attr newAttribute = null;
        List newElementList = this.selectNodes(xpath);
        //only look at the last part of the path
        int aIndex = xpath.lastIndexOf("/");
        if (aIndex == -1) {
            aIndex = 0;
        }
        aIndex = xpath.indexOf("@", aIndex);
        int size = newElementList.size();
        if (size > 1) {
            log.info("UPDATING MORE THAN ONE ELEMENT");
        }

        if ((aIndex == -1) && (size != 0)) {
            for (int i = 0; i < size; i++) {
                newElement = (Element) newElementList.get(i);
                Node childNode = newElement.getFirstChild();

                if (childNode == null) {
                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                    DocumentBuilder db = dbf.newDocumentBuilder();
                    Document document = db.newDocument();
                    Text newElementText = document.createTextNode(newElement.getNodeName());
                    newElementText.setNodeValue(value);
                    Text clonedText = (Text) newElement.getOwnerDocument().importNode(newElementText, true);
                    newElement.appendChild(clonedText);
                } else {
                    CharacterData newElementText = (CharacterData) newElement.getFirstChild();
                    newElementText.setNodeValue(value);
                }
            }
        }

        if ((aIndex != -1) && (size != 0)) {
            newAttribute = (Attr) newElementList.set(0, null);
            if (newAttribute != null) {
                newAttribute.setValue(value);
            }
        }
    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
    }

    return this;
}