Search in sources :

Example 21 with Result

use of javax.xml.transform.Result in project series-rest-api by 52North.

the class PDFReportGenerator method encodeAndWriteTo.

@Override
public void encodeAndWriteTo(DataCollection<QuantityData> data, OutputStream stream) throws IoParseException {
    try {
        generateOutput(data);
        DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
        Configuration cfg = cfgBuilder.build(document.newInputStream());
        FopFactory fopFactory = new FopFactoryBuilder(baseURI).setConfiguration(cfg).build();
        final String mimeType = MimeType.APPLICATION_PDF.getMimeType();
        Fop fop = fopFactory.newFop(mimeType, stream);
        //FopFactory fopFactory = FopFactory.newInstance(cfg);
        //Fop fop = fopFactory.newFop(APPLICATION_PDF.getMimeType(), stream);
        //FopFactory fopFactory = fopFactoryBuilder.build();
        //Fop fop = fopFactory.newFop(APPLICATION_PDF.getMimeType(), stream);
        // Create PDF via XSLT transformation
        TransformerFactory transFact = TransformerFactory.newInstance();
        StreamSource transformationRule = getTransforamtionRule();
        Transformer transformer = transFact.newTransformer(transformationRule);
        Source source = new StreamSource(document.newInputStream());
        Result result = new SAXResult(fop.getDefaultHandler());
        if (LOGGER.isDebugEnabled()) {
            try {
                File tempFile = File.createTempFile(TEMP_FILE_PREFIX, ".xml");
                StreamResult debugResult = new StreamResult(tempFile);
                transformer.transform(source, debugResult);
                String xslResult = XmlObject.Factory.parse(tempFile).xmlText();
                LOGGER.debug("xsl-fo input (locale '{}'): {}", i18n.getTwoDigitsLanguageCode(), xslResult);
            } catch (IOException | TransformerException | XmlException e) {
                LOGGER.error("Could not debug XSL result output!", e);
            }
        }
        // XXX debug, diagram is not embedded
        transformer.transform(source, result);
    } catch (FOPException e) {
        throw new IoParseException("Failed to create Formatting Object Processor (FOP)", e);
    } catch (SAXException | ConfigurationException | IOException e) {
        throw new IoParseException("Failed to read config for Formatting Object Processor (FOP)", e);
    } catch (TransformerConfigurationException e) {
        throw new IoParseException("Invalid transform configuration. Inspect xslt!", e);
    } catch (TransformerException e) {
        throw new IoParseException("Could not generate PDF report!", e);
    }
}
Also used : IoParseException(org.n52.io.IoParseException) DefaultConfigurationBuilder(org.apache.avalon.framework.configuration.DefaultConfigurationBuilder) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) Configuration(org.apache.avalon.framework.configuration.Configuration) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Fop(org.apache.fop.apps.Fop) StreamSource(javax.xml.transform.stream.StreamSource) FopFactory(org.apache.fop.apps.FopFactory) IOException(java.io.IOException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) SAXException(org.xml.sax.SAXException) FopFactoryBuilder(org.apache.fop.apps.FopFactoryBuilder) FOPException(org.apache.fop.apps.FOPException) SAXResult(javax.xml.transform.sax.SAXResult) ConfigurationException(org.apache.avalon.framework.configuration.ConfigurationException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) XmlException(org.apache.xmlbeans.XmlException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 22 with Result

use of javax.xml.transform.Result in project ACS by ACS-Community.

the class DumpXML method main.

/**
	 * @param args
	 */
public static void main(String[] args) throws Throwable {
    if (args.length != 1) {
        System.err.println("Usage: java " + DumpXML.class.getName() + " <xml file>");
        System.exit(1);
    }
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    // XercesJ 2.9.1 rejects this, it supports it by default
    //factory.setXIncludeAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document xmldoc = builder.parse(new InputSource(new FileReader(args[0])));
    Source src = new DOMSource(xmldoc);
    Result dest = new StreamResult(System.out);
    TransformerFactory tranFactory = TransformerFactory.newInstance();
    Transformer transformer = tranFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(src, dest);
}
Also used : InputSource(org.xml.sax.InputSource) 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) FileReader(java.io.FileReader) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 23 with Result

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

the class ModelExporter method doIt.

/**
	 * Process
	 * 
	 * @return info
	 */
protected String doIt() throws Exception {
    ExportHelper expHelper = new ExportHelper(getCtx(), p_AD_Client_ID);
    MEXPFormat exportFormat = new MEXPFormat(getCtx(), p_EXP_Format_ID, get_TrxName());
    File file = new File(p_FileName);
    Document doc = expHelper.exportRecord(exportFormat, "", MReplicationStrategy.REPLICATION_TABLE, X_AD_ReplicationTable.REPLICATIONTYPE_Merge, ModelValidator.TYPE_AFTER_CHANGE);
    // Save the document to the disk file
    TransformerFactory tranFactory = TransformerFactory.newInstance();
    //   tranFactory.setAttribute("indent-number", 4); //Adempiere-65 change    
    Transformer aTransformer = tranFactory.newTransformer();
    aTransformer.setOutputProperty(OutputKeys.METHOD, "xml");
    aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
    Source src = new DOMSource(doc);
    // =================================== Write to String
    Writer writer = new StringWriter();
    Result dest2 = new StreamResult(writer);
    aTransformer.transform(src, dest2);
    // =================================== Write to Disk
    try {
        Result dest = new StreamResult(file);
        aTransformer.transform(src, dest);
        writer.flush();
        writer.close();
    } catch (TransformerException ex) {
        ex.printStackTrace();
        throw ex;
    }
    return "Exported";
}
Also used : MEXPFormat(org.compiere.model.MEXPFormat) DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) StringWriter(java.io.StringWriter) File(java.io.File) StringWriter(java.io.StringWriter) Writer(java.io.Writer) TransformerException(javax.xml.transform.TransformerException)

Example 24 with Result

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

the class TopicExportProcessor method process.

/**
	 * 
	 */
public void process(Properties ctx, MEXPProcessor expProcessor, Document document, Trx trx) throws Exception {
    String host = expProcessor.getHost();
    int port = expProcessor.getPort();
    String account = expProcessor.getAccount();
    String password = expProcessor.getPasswordInfo();
    String protocol = null;
    String topicName = "";
    String clientID = null;
    String timeToLiveStr = null;
    int timeToLive = 10000;
    boolean isDeliveryModePersistent = true;
    // Read all processor parameters and set them!        
    X_EXP_ProcessorParameter[] processorParameters = expProcessor.getEXP_ProcessorParameters(trx.getTrxName());
    if (processorParameters != null && processorParameters.length > 0) {
        for (int i = 0; i < processorParameters.length; i++) {
            log.info("ProcesParameter          Value = " + processorParameters[i].getValue());
            log.info("ProcesParameter ParameterValue = " + processorParameters[i].getParameterValue());
            if (processorParameters[i].getValue().equals("topicName")) {
                topicName = processorParameters[i].getParameterValue();
            } else if (processorParameters[i].getValue().equals("protocol")) {
                protocol = processorParameters[i].getParameterValue();
            } else if (processorParameters[i].getValue().equals("clientID")) {
                clientID = processorParameters[i].getParameterValue();
            } else if (processorParameters[i].getValue().equals("timeToLive")) {
                timeToLiveStr = processorParameters[i].getParameterValue();
                timeToLive = Integer.parseInt(timeToLiveStr);
            } else if (processorParameters[i].getValue().equals("isDeliveryModePersistent")) {
                isDeliveryModePersistent = Boolean.parseBoolean(processorParameters[i].getParameterValue());
            } else {
            // Some other mandatory parameter here
            }
        }
    }
    if (topicName == null || topicName.length() == 0) {
        throw new Exception("Missing " + X_EXP_ProcessorParameter.Table_Name + " with key 'topicName'!");
    }
    if (protocol == null || protocol.length() == 0) {
        throw new Exception("Missing " + X_EXP_ProcessorParameter.Table_Name + " with key 'protocol'!");
    }
    if (clientID == null || clientID.length() == 0) {
        throw new Exception("Missing " + X_EXP_ProcessorParameter.Table_Name + " with key 'clientID'!");
    }
    if (timeToLiveStr == null || timeToLiveStr.length() == 0) {
        throw new Exception("Missing " + X_EXP_ProcessorParameter.Table_Name + " with key 'timeToLive'!");
    }
    // Construct Transformer Factory and Transformer
    TransformerFactory tranFactory = TransformerFactory.newInstance();
    String jVersion = System.getProperty("java.version");
    if (jVersion.startsWith("1.5.0"))
        tranFactory.setAttribute("indent-number", Integer.valueOf(1));
    Transformer aTransformer = tranFactory.newTransformer();
    aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
    Source src = new DOMSource(document);
    // =================================== Write to String
    Writer writer = new StringWriter();
    Result dest2 = new StreamResult(writer);
    aTransformer.transform(src, dest2);
    sendJMSMessage(host, port, writer.toString(), protocol, topicName, clientID, account, password, timeToLive, isDeliveryModePersistent);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) X_EXP_ProcessorParameter(org.compiere.model.X_EXP_ProcessorParameter) JMSException(javax.jms.JMSException) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) StringWriter(java.io.StringWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 25 with Result

use of javax.xml.transform.Result in project jackrabbit by apache.

the class ClientSession method exportDocumentView.

/**
     * Exports the XML document view of the specified repository location
     * to the given XML content handler. This method first requests the
     * raw XML data from the remote session, and then uses an identity
     * transformation to feed the data to the given XML content handler.
     * Possible IO and transformer exceptions are thrown as SAXExceptions.
     *
     * {@inheritDoc}
     */
public void exportDocumentView(String path, ContentHandler handler, boolean binaryAsLink, boolean noRecurse) throws SAXException, RepositoryException {
    try {
        byte[] xml = remote.exportDocumentView(path, binaryAsLink, noRecurse);
        Source source = new StreamSource(new ByteArrayInputStream(xml));
        Result result = new SAXResult(handler);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.transform(source, result);
    } catch (RemoteException ex) {
        throw new RemoteRepositoryException(ex);
    } catch (IOException ex) {
        throw new SAXException(ex);
    } catch (TransformerConfigurationException ex) {
        throw new SAXException(ex);
    } catch (TransformerException ex) {
        throw new SAXException(ex);
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) SAXException(org.xml.sax.SAXException) SAXResult(javax.xml.transform.sax.SAXResult) ByteArrayInputStream(java.io.ByteArrayInputStream) RemoteException(java.rmi.RemoteException) TransformerException(javax.xml.transform.TransformerException)

Aggregations

Result (javax.xml.transform.Result)68 StreamResult (javax.xml.transform.stream.StreamResult)60 Source (javax.xml.transform.Source)42 Transformer (javax.xml.transform.Transformer)42 DOMSource (javax.xml.transform.dom.DOMSource)33 TransformerFactory (javax.xml.transform.TransformerFactory)25 StringWriter (java.io.StringWriter)21 StreamSource (javax.xml.transform.stream.StreamSource)19 IOException (java.io.IOException)18 TransformerException (javax.xml.transform.TransformerException)17 SAXResult (javax.xml.transform.sax.SAXResult)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 File (java.io.File)11 Document (org.w3c.dom.Document)11 InputSource (org.xml.sax.InputSource)10 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)9 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)9 DOMResult (javax.xml.transform.dom.DOMResult)9 SAXException (org.xml.sax.SAXException)9