Search in sources :

Example 36 with DOMSource

use of javax.xml.transform.dom.DOMSource in project jersey by jersey.

the class WadlResourceTest method extractWadlAsDocument.

/**
     * Extracts WADL as {@link Document} from given {@link Response}.
     *
     * @param response The response to extract {@code Document} from.
     * @return The extracted {@code Document}.
     * @throws ParserConfigurationException In case of parser configuration issues.
     * @throws SAXException                 In case of parsing issues.
     * @throws IOException                  In case of IO error.
     */
static Document extractWadlAsDocument(final Response response) throws ParserConfigurationException, SAXException, IOException {
    assertEquals(200, response.getStatus());
    final File tmpFile = response.readEntity(File.class);
    final DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
    bf.setNamespaceAware(true);
    bf.setValidating(false);
    if (!SaxHelper.isXdkDocumentBuilderFactory(bf)) {
        bf.setXIncludeAware(false);
    }
    final DocumentBuilder b = bf.newDocumentBuilder();
    final Document d = b.parse(tmpFile);
    Wadl5Test.printSource(new DOMSource(d));
    return d;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Document(org.w3c.dom.Document) File(java.io.File)

Example 37 with DOMSource

use of javax.xml.transform.dom.DOMSource 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 38 with DOMSource

use of javax.xml.transform.dom.DOMSource in project AndroidAsync by koush.

the class DocumentBody method prepare.

private void prepare() {
    if (bout != null)
        return;
    try {
        DOMSource source = new DOMSource(document);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        bout = new ByteArrayOutputStream();
        OutputStreamWriter writer = new OutputStreamWriter(bout, Charsets.UTF_8);
        StreamResult result = new StreamResult(writer);
        transformer.transform(source, result);
        writer.flush();
    } catch (Exception e) {
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 39 with DOMSource

use of javax.xml.transform.dom.DOMSource 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 40 with DOMSource

use of javax.xml.transform.dom.DOMSource in project jop by jop-devel.

the class XmlBuilder method writeDom.

/**
	 * @param dom
	 * @param s
	 * @throws TransformerFactoryConfigurationError
	 * @throws XmlSerializationException
	 */
public static void writeDom(Document dom, Writer s) throws TransformerFactoryConfigurationError, XmlSerializationException {
    DOMSource domSource = new DOMSource(dom);
    StreamResult streamResult = new StreamResult(s);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer;
    try {
        serializer = tf.newTransformer();
        serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
        serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://www.it.uu.se/research/group/darts/uppaal/flat-1_1.dtd");
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.transform(domSource, streamResult);
    } catch (Exception e) {
        throw new XmlSerializationException("Error in domToString()", e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

DOMSource (javax.xml.transform.dom.DOMSource)392 StreamResult (javax.xml.transform.stream.StreamResult)231 Transformer (javax.xml.transform.Transformer)204 Document (org.w3c.dom.Document)161 TransformerFactory (javax.xml.transform.TransformerFactory)112 TransformerException (javax.xml.transform.TransformerException)107 DocumentBuilder (javax.xml.parsers.DocumentBuilder)102 StringWriter (java.io.StringWriter)97 IOException (java.io.IOException)93 Element (org.w3c.dom.Element)81 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)77 Source (javax.xml.transform.Source)67 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)62 SAXException (org.xml.sax.SAXException)56 File (java.io.File)55 InputSource (org.xml.sax.InputSource)50 StreamSource (javax.xml.transform.stream.StreamSource)47 Node (org.w3c.dom.Node)45 InputStream (java.io.InputStream)35 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)35