Search in sources :

Example 81 with StreamResult

use of javax.xml.transform.stream.StreamResult in project midpoint by Evolveum.

the class DomToSchemaProcessor method parseSchema.

private XSSchemaSet parseSchema(Element schema) throws SchemaException {
    // Make sure that the schema parser sees all the namespace declarations
    DOMUtil.fixNamespaceDeclarations(schema);
    XSSchemaSet xss = null;
    try {
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource source = new DOMSource(schema);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        StreamResult result = new StreamResult(out);
        trans.transform(source, result);
        XSOMParser parser = createSchemaParser();
        InputSource inSource = new InputSource(new ByteArrayInputStream(out.toByteArray()));
        // XXX: hack: it's here to make entity resolver work...
        inSource.setSystemId("SystemId");
        // XXX: end hack
        inSource.setEncoding("utf-8");
        parser.parse(inSource);
        xss = parser.getResult();
    } catch (SAXException e) {
        throw new SchemaException("XML error during XSD schema parsing: " + e.getMessage() + "(embedded exception " + e.getException() + ") in " + shortDescription, e);
    } catch (TransformerException e) {
        throw new SchemaException("XML transformer error during XSD schema parsing: " + e.getMessage() + "(locator: " + e.getLocator() + ", embedded exception:" + e.getException() + ") in " + shortDescription, e);
    } catch (RuntimeException e) {
        // This sometimes happens, e.g. NPEs in Saxon
        if (LOGGER.isErrorEnabled()) {
            LOGGER.error("Unexpected error {} during parsing of schema:\n{}", e.getMessage(), DOMUtil.serializeDOMToString(schema));
        }
        throw new SchemaException("XML error during XSD schema parsing: " + e.getMessage() + " in " + shortDescription, e);
    }
    return xss;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) XSOMParser(com.sun.xml.xsom.parser.XSOMParser) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SAXException(org.xml.sax.SAXException) ByteArrayInputStream(java.io.ByteArrayInputStream) TransformerException(javax.xml.transform.TransformerException)

Example 82 with StreamResult

use of javax.xml.transform.stream.StreamResult in project midpoint by Evolveum.

the class DOMUtil method printDom.

public static StringBuffer printDom(Node node, boolean indent, boolean omitXmlDeclaration) {
    StringWriter writer = new StringWriter();
    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans;
    try {
        trans = transfac.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new SystemException("Error in XML configuration: " + e.getMessage(), e);
    }
    trans.setOutputProperty(OutputKeys.INDENT, (indent ? "yes" : "no"));
    // XALAN-specific
    trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    trans.setParameter(OutputKeys.ENCODING, "utf-8");
    // Note: serialized XML does not contain xml declaration
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, (omitXmlDeclaration ? "yes" : "no"));
    DOMSource source = new DOMSource(node);
    try {
        trans.transform(source, new StreamResult(writer));
    } catch (TransformerException e) {
        throw new SystemException("Error in XML transformation: " + e.getMessage(), e);
    }
    return writer.getBuffer();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SystemException(com.evolveum.midpoint.util.exception.SystemException) StreamResult(javax.xml.transform.stream.StreamResult) TransformerException(javax.xml.transform.TransformerException)

Example 83 with StreamResult

use of javax.xml.transform.stream.StreamResult in project goci by EBISPOT.

the class ChromosomeRenderlet method render.

public void render(RenderletNexus nexus, C context, E entity) {
    int position = getPosition();
    int height = SVGCanvas.canvasHeight;
    int width = SVGCanvas.canvasWidth;
    double chromWidth = (double) width / 12;
    double chromHeight = (double) height / 2;
    double xCoordinate;
    double yCoordinate = 0;
    if (position < 12) {
        xCoordinate = position * chromWidth;
    } else {
        xCoordinate = (position - 12) * chromWidth;
        yCoordinate = (double) height / 2;
    }
    InputStream in = null;
    try {
        in = getSVGFile().openStream();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document chromSVG = db.parse(in);
        Element root = chromSVG.getDocumentElement();
        Element g = (Element) root.getElementsByTagName("g").item(0).cloneNode(true);
        setChromBands(g, nexus);
        StringBuilder builder = new StringBuilder();
        builder.append("translate(");
        builder.append(Double.toString(xCoordinate));
        builder.append(",");
        builder.append(Double.toString(yCoordinate));
        builder.append(")");
        g.setAttribute("transform", builder.toString());
        g.setAttribute("gwasname", getName());
        g.removeAttribute("title");
        SVGArea currentArea = new SVGArea(xCoordinate, yCoordinate, chromWidth, chromHeight, 0);
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        StringWriter buffer = new StringWriter();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(new DOMSource(g), new StreamResult(buffer));
        String str = buffer.toString();
        RenderingEvent<E> event = new RenderingEvent<E>(entity, str, currentArea, this);
        nexus.renderingEventOccurred(event);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException("Failed to read in template chromosome SVG from original resource", e);
    } catch (SAXException e) {
        throw new RuntimeException("Failed to read in template chromosome SVG from original resource", e);
    } catch (IOException e) {
        throw new RuntimeException("Failed to render chromosome SVG", e);
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException("Failed to render chromosome SVG", e);
    } catch (TransformerException e) {
        throw new RuntimeException("Failed to render chromosome SVG", e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            // tried our best!
            }
        }
    }
}
Also used : 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) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) RenderingEvent(uk.ac.ebi.spot.goci.pussycat.renderlet.RenderingEvent) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SVGArea(uk.ac.ebi.spot.goci.pussycat.layout.SVGArea) TransformerException(javax.xml.transform.TransformerException)

Example 84 with StreamResult

use of javax.xml.transform.stream.StreamResult in project jdk8u_jdk by JetBrains.

the class Test method toString.

private static String toString(Source response) throws TransformerException, IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(response, new StreamResult(bos));
    bos.close();
    return new String(bos.toByteArray());
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 85 with StreamResult

use of javax.xml.transform.stream.StreamResult in project jdk8u_jdk by JetBrains.

the class XSLTExFuncTest method testTemplatesEnableExtFunc.

/**
     * use Templates template = factory.newTemplates(new StreamSource( new
     * FileInputStream(xslFilename))); // Use the template to create a
     * transformer Transformer xformer = template.newTransformer();
     *
     * @param factory
     * @return
     */
/**
     * Security is enabled, use new feature: enableExtensionFunctions Use the
     * template to create a transformer
     */
public void testTemplatesEnableExtFunc() {
    Policy p = new SimplePolicy(new AllPermission());
    Policy.setPolicy(p);
    System.setSecurityManager(new SecurityManager());
    TransformerFactory factory = TransformerFactory.newInstance();
    /**
         * Use of the extension function 'http://exslt.org/strings:tokenize' is
         * not allowed when the secure processing feature is set to true.
         * Attempt to use the new property to enable extension function
         */
    boolean isExtensionSupported = enableExtensionFunction(factory);
    try {
        SAXSource xslSource = new SAXSource(new InputSource(xslFile));
        xslSource.setSystemId(xslFileId);
        Templates template = factory.newTemplates(xslSource);
        Transformer transformer = template.newTransformer();
        StringWriter stringResult = new StringWriter();
        Result result = new StreamResult(stringResult);
        transformer.transform(new SAXSource(new InputSource(xmlFile)), result);
        System.out.println("testTemplatesEnableExtFunc: OK");
    } catch (TransformerConfigurationException e) {
        fail(e.getMessage());
    } catch (TransformerException e) {
        fail(e.getMessage());
    } finally {
        System.setSecurityManager(null);
    }
}
Also used : Policy(java.security.Policy) InputSource(org.xml.sax.InputSource) StreamResult(javax.xml.transform.stream.StreamResult) StreamResult(javax.xml.transform.stream.StreamResult) SAXSource(javax.xml.transform.sax.SAXSource) StringWriter(java.io.StringWriter) AllPermission(java.security.AllPermission)

Aggregations

StreamResult (javax.xml.transform.stream.StreamResult)448 Transformer (javax.xml.transform.Transformer)267 DOMSource (javax.xml.transform.dom.DOMSource)234 StringWriter (java.io.StringWriter)206 TransformerFactory (javax.xml.transform.TransformerFactory)138 TransformerException (javax.xml.transform.TransformerException)125 Document (org.w3c.dom.Document)103 IOException (java.io.IOException)94 StreamSource (javax.xml.transform.stream.StreamSource)88 Source (javax.xml.transform.Source)74 Test (org.junit.Test)73 DocumentBuilder (javax.xml.parsers.DocumentBuilder)65 ByteArrayOutputStream (java.io.ByteArrayOutputStream)64 Element (org.w3c.dom.Element)59 File (java.io.File)58 Result (javax.xml.transform.Result)57 StringReader (java.io.StringReader)56 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)53 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)50 ByteArrayInputStream (java.io.ByteArrayInputStream)44