Search in sources :

Example 91 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project generator by mybatis.

the class XmlFileMergerJaxp method getMergedSource.

public static String getMergedSource(InputSource newFile, InputSource existingFile, String existingFileName) throws IOException, SAXException, ParserConfigurationException, ShellException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setExpandEntityReferences(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setEntityResolver(new NullEntityResolver());
    Document existingDocument = builder.parse(existingFile);
    Document newDocument = builder.parse(newFile);
    DocumentType newDocType = newDocument.getDoctype();
    DocumentType existingDocType = existingDocument.getDoctype();
    if (!newDocType.getName().equals(existingDocType.getName())) {
        throw new ShellException(getString(//$NON-NLS-1$
        "Warning.12", existingFileName));
    }
    Element existingRootElement = existingDocument.getDocumentElement();
    Element newRootElement = newDocument.getDocumentElement();
    // reconcile the root element attributes -
    // take all attributes from the new element and add to the existing
    // element
    // remove all attributes from the existing root element
    NamedNodeMap attributes = existingRootElement.getAttributes();
    int attributeCount = attributes.getLength();
    for (int i = attributeCount - 1; i >= 0; i--) {
        Node node = attributes.item(i);
        existingRootElement.removeAttribute(node.getNodeName());
    }
    // add attributes from the new root node to the old root node
    attributes = newRootElement.getAttributes();
    attributeCount = attributes.getLength();
    for (int i = 0; i < attributeCount; i++) {
        Node node = attributes.item(i);
        existingRootElement.setAttribute(node.getNodeName(), node.getNodeValue());
    }
    // remove the old generated elements and any
    // white space before the old nodes
    List<Node> nodesToDelete = new ArrayList<Node>();
    NodeList children = existingRootElement.getChildNodes();
    int length = children.getLength();
    for (int i = 0; i < length; i++) {
        Node node = children.item(i);
        if (isGeneratedNode(node)) {
            nodesToDelete.add(node);
        } else if (isWhiteSpace(node) && isGeneratedNode(children.item(i + 1))) {
            nodesToDelete.add(node);
        }
    }
    for (Node node : nodesToDelete) {
        existingRootElement.removeChild(node);
    }
    // add the new generated elements
    children = newRootElement.getChildNodes();
    length = children.getLength();
    Node firstChild = existingRootElement.getFirstChild();
    for (int i = 0; i < length; i++) {
        Node node = children.item(i);
        // don't add the last node if it is only white space
        if (i == length - 1 && isWhiteSpace(node)) {
            break;
        }
        Node newNode = existingDocument.importNode(node, true);
        if (firstChild == null) {
            existingRootElement.appendChild(newNode);
        } else {
            existingRootElement.insertBefore(newNode, firstChild);
        }
    }
    // pretty print the result
    return prettyPrint(existingDocument);
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NamedNodeMap(org.w3c.dom.NamedNodeMap) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) DocumentType(org.w3c.dom.DocumentType) Document(org.w3c.dom.Document) ShellException(org.mybatis.generator.exception.ShellException) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Example 92 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project openhab1-addons by openhab.

the class NotifyMyAndroid method parseResponse.

private static String parseResponse(String response) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = factory.newDocumentBuilder();
    InputSource inStream = new InputSource();
    inStream.setCharacterStream(new StringReader(response));
    Document doc = db.parse(inStream);
    Element root = doc.getDocumentElement();
    String lastError = null;
    if (root.getTagName().equals("nma")) {
        Node item = root.getFirstChild();
        String childName = item.getNodeName();
        if (!childName.equals("success")) {
            lastError = item.getFirstChild().getNodeValue();
        }
    }
    return lastError;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

Example 93 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project openhab1-addons by openhab.

the class FrontierSiliconRadioApiResult method getXmlDocFromString.

/**
     * converts the string we got from the radio to a parsable XML document
     * 
     * @param xmlString
     *            the XML string read from the radio
     * @return the parsed XML document
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
private Document getXmlDocFromString(String xmlString) throws ParserConfigurationException, SAXException, IOException {
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = factory.newDocumentBuilder();
    final Document xmlDocument = builder.parse(new InputSource(new StringReader(xmlString)));
    return xmlDocument;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

Example 94 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project openhab1-addons by openhab.

the class XMLUtil method loadXMLFromString.

/**
     * Loads a string into a xml document object.
     * 
     * @param xml
     * @return
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
public static Document loadXMLFromString(String xml) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xml));
    Document doc = builder.parse(is);
    return doc;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

Example 95 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project openhab1-addons by openhab.

the class OWServerBinding method getVariable.

String getVariable(String response, String romId, String name) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    // Get the DOM Builder
    DocumentBuilder builder = null;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        logger.error("Error parsing OWServer XML response " + e.getMessage());
    }
    // Load and Parse the XML document
    // document contains the complete XML as a Tree.
    Document document = null;
    try {
        InputSource is = new InputSource(new StringReader(response));
        document = builder.parse(is);
    } catch (SAXException e) {
        logger.error("Error reading OWServer XML response " + e.getMessage());
    } catch (IOException e) {
        logger.error("Error reading OWServer XML response " + e.getMessage());
    }
    // Iterating through the nodes and extracting the data.
    NodeList nodeList = document.getDocumentElement().getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeName().startsWith("owd_")) {
            boolean romMatch = false;
            NodeList childNodes = node.getChildNodes();
            for (int j = 0; j < childNodes.getLength(); j++) {
                Node cNode = childNodes.item(j);
                // Identifying the child tag of employee encountered.
                if (cNode instanceof Element) {
                    String content = cNode.getLastChild().getTextContent().trim();
                    if (cNode.getNodeName().equals("ROMId") & content.equals(romId)) {
                        romMatch = true;
                    }
                    String nname = cNode.getNodeName();
                    if (nname.equals(name) & romMatch == true) {
                        return content;
                    }
                }
            }
        }
    }
    return null;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)759 DocumentBuilder (javax.xml.parsers.DocumentBuilder)622 Document (org.w3c.dom.Document)526 Element (org.w3c.dom.Element)244 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)232 NodeList (org.w3c.dom.NodeList)209 IOException (java.io.IOException)197 InputSource (org.xml.sax.InputSource)193 SAXException (org.xml.sax.SAXException)173 Node (org.w3c.dom.Node)145 StringReader (java.io.StringReader)142 Test (org.junit.Test)117 File (java.io.File)86 DOMSource (javax.xml.transform.dom.DOMSource)77 ByteArrayInputStream (java.io.ByteArrayInputStream)72 InputStream (java.io.InputStream)63 StreamResult (javax.xml.transform.stream.StreamResult)50 ArrayList (java.util.ArrayList)47 SAXParseException (org.xml.sax.SAXParseException)46 Transformer (javax.xml.transform.Transformer)44