Search in sources :

Example 96 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project android-selector-intellij-plugin by importre.

the class AndroidSelectorDialog method createDrawableV21.

private void createDrawableV21(String filename, String color, String pressed) throws Exception {
    VirtualFile child = dir.findChild(drawableV21Dir);
    if (child == null) {
        child = dir.createChildDirectory(null, drawableV21Dir);
    }
    VirtualFile newXmlFile = child.findChild(filename);
    if (newXmlFile != null && newXmlFile.exists()) {
        newXmlFile.delete(null);
    }
    newXmlFile = child.createChildData(null, filename);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();
    Element root = doc.createElement("ripple");
    root.setAttributeNS(nsUri, "xmlns:android", androidUri);
    root.setAttribute("android:color", pressed);
    doc.appendChild(root);
    Element item = doc.createElement("item");
    item.setAttribute("android:drawable", color);
    root.appendChild(item);
    OutputStream os = newXmlFile.getOutputStream(null);
    PrintWriter out = new PrintWriter(os);
    StringWriter writer = new StringWriter();
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(INDENT_SPACE, "4");
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    out.println(writer.getBuffer().toString());
    out.close();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 97 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project languagetool by languagetool-org.

the class XMLValidator method mergeIntoSource.

private static Source mergeIntoSource(InputStream baseXmlStream, InputStream xmlStream) throws Exception {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setIgnoringComments(true);
    domFactory.setValidating(false);
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document baseDoc = builder.parse(baseXmlStream);
    Document ruleDoc = builder.parse(xmlStream);
    // Shall this be more generic, i.e. reuse not just unification ???
    NodeList unificationNodes = baseDoc.getElementsByTagName("unification");
    Node ruleNode = ruleDoc.getElementsByTagName("rules").item(0);
    Node firstChildRuleNode = ruleNode.getChildNodes().item(1);
    for (int i = 0; i < unificationNodes.getLength(); i++) {
        Node unificationNode = ruleDoc.importNode(unificationNodes.item(i), true);
        ruleNode.insertBefore(unificationNode, firstChildRuleNode);
    }
    return new DOMSource(ruleDoc);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document)

Example 98 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project languagetool by languagetool-org.

the class AfterTheDeadlineChecker method getDocument.

private Document getDocument(String xml) {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource inputSource = new InputSource(new StringReader(xml));
        return builder.parse(inputSource);
    } catch (Exception e) {
        throw new RuntimeException("Could not parse XML: " + xml);
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) XPathExpressionException(javax.xml.xpath.XPathExpressionException) IOException(java.io.IOException)

Example 99 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project jop by jop-devel.

the class XmlBuilder method createDom.

/**
	 * Create a DOM object.
	 * (guideline: http://totheriver.com/learn/xml/xmltutorial.html)
	 * 
	 * @return An XML Document (DOM)
	 * @throws XmlSerializationException 
	 */
public static Document createDom() throws XmlSerializationException {
    Document dom;
    //get an instance of factory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        //get an instance of builder
        DocumentBuilder db = dbf.newDocumentBuilder();
        //create an instance of DOM
        dom = db.newDocument();
    } catch (ParserConfigurationException pce) {
        throw new XmlSerializationException("Error while trying to instantiate DocumentBuilder", pce);
    }
    return dom;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document)

Example 100 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project jphp by jphp-compiler.

the class UIReader method read.

public Component read(InputStream inputStream) {
    try {
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        Document document = builder.parse(inputStream);
        Element root = document.getDocumentElement();
        return readElement(root);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Aggregations

DocumentBuilder (javax.xml.parsers.DocumentBuilder)883 Document (org.w3c.dom.Document)694 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)622 Element (org.w3c.dom.Element)339 IOException (java.io.IOException)276 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)276 NodeList (org.w3c.dom.NodeList)267 InputSource (org.xml.sax.InputSource)238 SAXException (org.xml.sax.SAXException)235 Node (org.w3c.dom.Node)199 StringReader (java.io.StringReader)167 Test (org.junit.Test)127 DOMSource (javax.xml.transform.dom.DOMSource)102 File (java.io.File)99 ByteArrayInputStream (java.io.ByteArrayInputStream)86 InputStream (java.io.InputStream)73 ArrayList (java.util.ArrayList)72 StreamResult (javax.xml.transform.stream.StreamResult)65 Transformer (javax.xml.transform.Transformer)59 SAXParseException (org.xml.sax.SAXParseException)56