Example usage for org.w3c.dom ProcessingInstruction setData

List of usage examples for org.w3c.dom ProcessingInstruction setData

Introduction

In this page you can find the example usage for org.w3c.dom ProcessingInstruction setData.

Prototype

public void setData(String data) throws DOMException;

Source Link

Document

The content of this processing instruction.

Usage

From source file:org.openmrs.module.formentry.PublishInfoPath.java

private static void prepareTemplate(File tempDir, String fileName, String solutionVersion, String publishUrl,
        String namespace) {/*from  www.  j  av  a  2 s.  c  om*/
    File file = new File(tempDir, fileName);
    if (file == null) {
        log.warn("Missing file: '" + fileName + "'");
        return;
    }
    if (log.isDebugEnabled())
        log.debug("Preparing template: " + file.getAbsolutePath());
    Document doc = null;
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.parse(file);

        // set namespace
        String tag = "form";
        Element elem = getSingleElement(doc, tag);
        if (elem == null) {
            log.warn("Could not locate " + tag + " element in " + file.getName());
            return;
        }
        elem.setAttribute("xmlns:openmrs", namespace);

        Node root = doc.getDocumentElement().getParentNode();
        NodeList children = root.getChildNodes();
        log.debug("Scanning for processing instructions");
        for (int i = 0; i < children.getLength(); i++) {
            Node node = children.item(i);
            if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE
                    && node.getNodeName().equals("mso-infoPathSolution")) {
                ProcessingInstruction pi = (ProcessingInstruction) node;
                String data = pi.getData();
                if (log.isDebugEnabled())
                    log.debug("  found: " + data);
                data = data.replaceAll("(\\bsolutionVersion\\s*=\\s*\")[^\"]+\"",
                        "$1" + solutionVersion + "\"");
                data = data.replaceAll("(\\bhref\\s*=\\s*\")[^\"]+\"", "$1" + publishUrl + "\"");
                if (log.isDebugEnabled())
                    log.debug("  replacing with: " + data);
                pi.setData(data);
            }
        }

    } catch (Exception e) {
        log.error("Trouble with file: " + fileName + " " + solutionVersion + " " + publishUrl, e);
    }
    writeXml(doc, file.getAbsolutePath());
}