Search in sources :

Example 81 with Source

use of javax.xml.transform.Source 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 82 with Source

use of javax.xml.transform.Source 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 83 with Source

use of javax.xml.transform.Source in project claw-compiler by C2SM-RCM.

the class Configuration method validate.

/**
   * Validate the configuration file with the XSD schema.
   *
   * @param xsdPath Path to the XSD schema.
   * @throws Exception If configuration file is not valid.
   */
private void validate(String xsdPath) throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source schemaFile = new StreamSource(new File(xsdPath));
    Schema schema = factory.newSchema(schemaFile);
    Validator validator = schema.newValidator();
    validator.validate(new DOMSource(_document));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) File(java.io.File) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator)

Example 84 with Source

use of javax.xml.transform.Source 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 85 with Source

use of javax.xml.transform.Source 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)

Aggregations

Source (javax.xml.transform.Source)238 StreamSource (javax.xml.transform.stream.StreamSource)161 DOMSource (javax.xml.transform.dom.DOMSource)108 Transformer (javax.xml.transform.Transformer)76 StreamResult (javax.xml.transform.stream.StreamResult)74 InputSource (org.xml.sax.InputSource)67 SAXSource (javax.xml.transform.sax.SAXSource)56 StringReader (java.io.StringReader)52 IOException (java.io.IOException)46 TransformerException (javax.xml.transform.TransformerException)45 Result (javax.xml.transform.Result)42 TransformerFactory (javax.xml.transform.TransformerFactory)41 Test (org.junit.Test)39 StringWriter (java.io.StringWriter)35 InputStream (java.io.InputStream)32 SAXException (org.xml.sax.SAXException)32 Schema (javax.xml.validation.Schema)29 Validator (javax.xml.validation.Validator)29 SchemaFactory (javax.xml.validation.SchemaFactory)28 ByteArrayInputStream (java.io.ByteArrayInputStream)26