Search in sources :

Example 46 with Result

use of javax.xml.transform.Result in project asciidoctor-fopub by asciidoctor.

the class InputHandler method renderTo.

/**
     * Generate a document, given an initialized Fop object
     * @param userAgent the user agent
     * @param outputFormat the output format to generate (MIME type, see MimeConstants)
     * @param out the output stream to write the generated output to (may be null if not applicable)
     * @throws FOPException in case of an error during processing
     */
public void renderTo(FOUserAgent userAgent, String outputFormat, OutputStream out) throws FOPException {
    String baseURL = null;
    try {
        baseURL = new File(sourcefile.getAbsolutePath()).getParentFile().toURI().toURL().toExternalForm();
    } catch (Exception e) {
        baseURL = "";
    }
    FopFactory factory = new FopFactoryBuilder(URI.create(baseURL)).build();
    Fop fop;
    if (out != null) {
        fop = factory.newFop(outputFormat, userAgent, out);
    } else {
        fop = factory.newFop(outputFormat, userAgent);
    }
    // Resulting SAX events (the generated FO) must be piped through to FOP
    Result res = new SAXResult(fop.getDefaultHandler());
    transformTo(res);
}
Also used : SAXResult(javax.xml.transform.sax.SAXResult) File(java.io.File) TransformerException(javax.xml.transform.TransformerException) FileNotFoundException(java.io.FileNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult)

Example 47 with Result

use of javax.xml.transform.Result in project gephi by gephi.

the class PresetUtils method savePreset.

public void savePreset(PreviewPreset preset) {
    int exist = -1;
    for (int i = 0; i < presets.size(); i++) {
        PreviewPreset p = presets.get(i);
        if (p.getName().equals(preset.getName())) {
            exist = i;
            break;
        }
    }
    if (exist == -1) {
        addPreset(preset);
    } else {
        presets.set(exist, preset);
    }
    try {
        //Create file if dont exist
        FileObject folder = FileUtil.getConfigFile("previewpresets");
        if (folder == null) {
            folder = FileUtil.getConfigRoot().createFolder("previewpresets");
        }
        FileObject presetFile = folder.getFileObject(preset.getName(), "xml");
        if (presetFile == null) {
            presetFile = folder.createData(preset.getName(), "xml");
        }
        //Create doc
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = factory.newDocumentBuilder();
        final Document document = documentBuilder.newDocument();
        document.setXmlVersion("1.0");
        document.setXmlStandalone(true);
        //Write doc
        writeXML(document, preset);
        //Write XML file
        Source source = new DOMSource(document);
        Result result = new StreamResult(FileUtil.toFile(presetFile));
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.transform(source, result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileObject(org.openide.filesystems.FileObject) Document(org.w3c.dom.Document) PreviewPreset(org.gephi.preview.api.PreviewPreset) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 48 with Result

use of javax.xml.transform.Result in project hazelcast by hazelcast.

the class AbstractXmlConfigHelper method schemaValidation.

protected void schemaValidation(Document doc) throws Exception {
    ArrayList<StreamSource> schemas = new ArrayList<StreamSource>();
    InputStream inputStream = null;
    String schemaLocation = doc.getDocumentElement().getAttribute("xsi:schemaLocation");
    schemaLocation = schemaLocation.replaceAll("^ +| +$| (?= )", "");
    // get every two pair. every pair includes namespace and uri
    String[] xsdLocations = schemaLocation.split("(?<!\\G\\S+)\\s");
    for (String xsdLocation : xsdLocations) {
        if (xsdLocation.isEmpty()) {
            continue;
        }
        String namespace = xsdLocation.split('[' + LINE_SEPARATOR + " ]+")[0];
        String uri = xsdLocation.split('[' + LINE_SEPARATOR + " ]+")[1];
        // if this is hazelcast namespace but location is different log only warning
        if (namespace.equals(xmlns) && !uri.endsWith(hazelcastSchemaLocation)) {
            LOGGER.warning("Name of the hazelcast schema location incorrect using default");
        }
        // if this is not hazelcast namespace then try to load from uri
        if (!namespace.equals(xmlns)) {
            inputStream = loadSchemaFile(uri);
            schemas.add(new StreamSource(inputStream));
        }
    }
    // include hazelcast schema
    schemas.add(new StreamSource(getClass().getClassLoader().getResourceAsStream(hazelcastSchemaLocation)));
    // document to InputStream conversion
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Source xmlSource = new DOMSource(doc);
    Result outputTarget = new StreamResult(outputStream);
    TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
    InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
    // schema validation
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemaFactory.newSchema(schemas.toArray(new Source[schemas.size()]));
    Validator validator = schema.newValidator();
    try {
        SAXSource source = new SAXSource(new InputSource(is));
        validator.validate(source);
    } catch (Exception e) {
        throw new InvalidConfigurationException(e.getMessage());
    } finally {
        for (StreamSource source : schemas) {
            closeResource(source.getInputStream());
        }
        closeResource(inputStream);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) ParseException(java.text.ParseException) NoSuchElementException(java.util.NoSuchElementException) HazelcastException(com.hazelcast.core.HazelcastException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) SAXSource(javax.xml.transform.sax.SAXSource) ByteArrayInputStream(java.io.ByteArrayInputStream) Validator(javax.xml.validation.Validator)

Example 49 with Result

use of javax.xml.transform.Result in project jOOQ by jOOQ.

the class XMLasDOMBinding method toString.

// ------------------------------------------------------------------------
// The following logic originates from jOOX
// ------------------------------------------------------------------------
/**
     * Transform an {@link Node} into a <code>String</code>.
     */
static final String toString(Node node) {
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        Source source = new DOMSource(node);
        Result target = new StreamResult(out);
        transformer.transform(source, target);
        return out.toString("UTF-8");
    } catch (Exception e) {
        return "[ ERROR IN toString() : " + e.getMessage() + " ]";
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) Source(javax.xml.transform.Source) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 50 with Result

use of javax.xml.transform.Result in project spring-framework by spring-projects.

the class SourceHttpMessageConverter method writeInternal.

@Override
protected void writeInternal(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    try {
        Result result = new StreamResult(outputMessage.getBody());
        transform(t, result);
    } catch (TransformerException ex) {
        throw new HttpMessageNotWritableException("Could not transform [" + t + "] to output message", ex);
    }
}
Also used : HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) StreamResult(javax.xml.transform.stream.StreamResult) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Aggregations

Result (javax.xml.transform.Result)80 StreamResult (javax.xml.transform.stream.StreamResult)72 Source (javax.xml.transform.Source)52 Transformer (javax.xml.transform.Transformer)52 DOMSource (javax.xml.transform.dom.DOMSource)42 TransformerFactory (javax.xml.transform.TransformerFactory)33 StringWriter (java.io.StringWriter)24 TransformerException (javax.xml.transform.TransformerException)21 IOException (java.io.IOException)20 StreamSource (javax.xml.transform.stream.StreamSource)20 SAXResult (javax.xml.transform.sax.SAXResult)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 File (java.io.File)15 Document (org.w3c.dom.Document)14 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)11 DOMResult (javax.xml.transform.dom.DOMResult)11 InputSource (org.xml.sax.InputSource)11 SAXException (org.xml.sax.SAXException)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9