Search in sources :

Example 1 with Configuration

use of org.apache.avalon.framework.configuration.Configuration 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 2 with Configuration

use of org.apache.avalon.framework.configuration.Configuration in project xwiki-platform by xwiki.

the class FOPXSLFORenderer method extendConfiguration.

private void extendConfiguration(DefaultConfiguration writableConfiguration) {
    // Add XWiki fonts folder to the configuration.
    try {
        String fontsPath = this.environment.getResource(FONTS_PATH).getPath();
        XWikiContext xcontext = this.xcontextProvider.get();
        if (xcontext != null) {
            XWikiRequest request = xcontext.getRequest();
            if (request != null && request.getSession() != null) {
                fontsPath = request.getSession().getServletContext().getRealPath(FONTS_PATH);
            }
        }
        // <renderers>
        DefaultConfiguration renderersConfiguration = (DefaultConfiguration) writableConfiguration.getChild(RENDERERS, false);
        if (renderersConfiguration == null) {
            renderersConfiguration = new DefaultConfiguration(RENDERERS);
            writableConfiguration.addChild(renderersConfiguration);
        }
        // Ensure we have support for PDF rendering.
        // <renderer mime="application/pdf">
        DefaultConfiguration pdfRenderer = null;
        for (Configuration renderer : renderersConfiguration.getChildren()) {
            if (MIME_TYPE_PDF.equals(renderer.getAttribute(MIME))) {
                pdfRenderer = (DefaultConfiguration) renderer;
            }
        }
        if (pdfRenderer == null) {
            pdfRenderer = new DefaultConfiguration("renderer");
            pdfRenderer.setAttribute(MIME, MIME_TYPE_PDF);
            renderersConfiguration.addChild(pdfRenderer);
        }
        // <fonts>
        DefaultConfiguration fontsConfiguration = (DefaultConfiguration) pdfRenderer.getChild(FONTS, false);
        if (fontsConfiguration == null) {
            fontsConfiguration = new DefaultConfiguration(FONTS);
            pdfRenderer.addChild(fontsConfiguration);
        }
        // <directory>fontdirectory</directory>
        DefaultConfiguration directoryConfiguration = new DefaultConfiguration("directory");
        directoryConfiguration.setValue(fontsPath);
        fontsConfiguration.addChild(directoryConfiguration);
    } catch (Exception e) {
        this.logger.warn("Starting with 1.5, XWiki uses the WEB-INF/fonts/ directory as the font directory, " + "and it should contain the FreeFont (http://savannah.gnu.org/projects/freefont/) fonts. " + "FOP cannot access this directory. If this is an upgrade from a previous version, " + "make sure you also copy the WEB-INF/fonts directory from the new distribution package.");
    }
}
Also used : XWikiRequest(com.xpn.xwiki.web.XWikiRequest) DefaultConfiguration(org.apache.avalon.framework.configuration.DefaultConfiguration) Configuration(org.apache.avalon.framework.configuration.Configuration) XWikiContext(com.xpn.xwiki.XWikiContext) DefaultConfiguration(org.apache.avalon.framework.configuration.DefaultConfiguration) InitializationException(org.xwiki.component.phase.InitializationException) ConfigurationException(org.apache.avalon.framework.configuration.ConfigurationException)

Example 3 with Configuration

use of org.apache.avalon.framework.configuration.Configuration in project xwiki-platform by xwiki.

the class FOPXSLFORenderer method loadConfiguration.

private Configuration loadConfiguration() {
    Configuration configuration = null;
    try (InputStream fopConfigurationFile = FOPXSLFORenderer.class.getResourceAsStream("/fop-config.xml")) {
        if (fopConfigurationFile != null) {
            configuration = new DefaultConfigurationBuilder().build(fopConfigurationFile);
        }
    } catch (Exception e) {
        this.logger.warn("Wrong FOP configuration: " + ExceptionUtils.getRootCauseMessage(e));
    }
    configuration = maybeExtendConfiguration(configuration);
    return configuration;
}
Also used : DefaultConfigurationBuilder(org.apache.avalon.framework.configuration.DefaultConfigurationBuilder) DefaultConfiguration(org.apache.avalon.framework.configuration.DefaultConfiguration) Configuration(org.apache.avalon.framework.configuration.Configuration) InputStream(java.io.InputStream) InitializationException(org.xwiki.component.phase.InitializationException) ConfigurationException(org.apache.avalon.framework.configuration.ConfigurationException)

Example 4 with Configuration

use of org.apache.avalon.framework.configuration.Configuration in project opentheso by miledrousset.

the class ApacheFOP method test_basic.

public void test_basic() throws SAXException, IOException, TransformerConfigurationException, TransformerException, ConfigurationException, URISyntaxException {
    // Step 1: Construct a FopFactory by specifying a reference to the configuration file
    // (reuse if you plan to render multiple documents!)
    DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
    Configuration cfg = cfgBuilder.buildFromFile(new File("fop-config.xml"));
    URI baseURI = new URI("https://www.testUri.com");
    FopFactoryBuilder builder;
    builder = new FopFactoryBuilder(baseURI).setConfiguration(cfg);
    FopFactory fopFactory = builder.build();
    // Step 2: Set up output stream.
    // Note: Using BufferedOutputStream for performance reasons (helpful with FileOutputStreams).
    OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("test-fop.pdf")));
    try {
        // Step 3: Construct fop with desired output format
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
        // Step 4: Setup JAXP using identity transformer
        Source xslt = new StreamSource(new File("skos-alpha.xsl"));
        TransformerFactory factory = TransformerFactory.newInstance();
        // identity transformer
        Transformer transformer = factory.newTransformer(xslt);
        // Step 5: Setup input and output for XSLT transformation
        // Setup input stream
        Source src = new StreamSource(new File("test_unesco.rdf"));
        // Resulting SAX events (the generated FO) must be piped through to FOP
        Result res = new SAXResult(fop.getDefaultHandler());
        // Step 6: Start XSLT transformation and FOP processing
        transformer.transform(src, res);
    } finally {
        // Clean-up
        out.close();
    }
}
Also used : DefaultConfigurationBuilder(org.apache.avalon.framework.configuration.DefaultConfigurationBuilder) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) Configuration(org.apache.avalon.framework.configuration.Configuration) Fop(org.apache.fop.apps.Fop) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) StreamSource(javax.xml.transform.stream.StreamSource) FopFactory(org.apache.fop.apps.FopFactory) URI(java.net.URI) 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) SAXResult(javax.xml.transform.sax.SAXResult) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 5 with Configuration

use of org.apache.avalon.framework.configuration.Configuration in project xwiki-platform by xwiki.

the class FOPXSLFORenderer method initialize.

@Override
public void initialize() throws InitializationException {
    EnvironmentProfile environmentProfile = EnvironmentalProfileFactory.createDefault(new File(".").toURI(), this.resourceResolver);
    FopFactoryBuilder builder = new FopFactoryBuilder(environmentProfile);
    Configuration configuration = loadConfiguration();
    if (configuration != null) {
        builder.setConfiguration(configuration);
    }
    this.fopFactory = builder.build();
}
Also used : FopFactoryBuilder(org.apache.fop.apps.FopFactoryBuilder) DefaultConfiguration(org.apache.avalon.framework.configuration.DefaultConfiguration) Configuration(org.apache.avalon.framework.configuration.Configuration) EnvironmentProfile(org.apache.fop.apps.EnvironmentProfile) File(java.io.File)

Aggregations

Configuration (org.apache.avalon.framework.configuration.Configuration)5 File (java.io.File)3 ConfigurationException (org.apache.avalon.framework.configuration.ConfigurationException)3 DefaultConfiguration (org.apache.avalon.framework.configuration.DefaultConfiguration)3 DefaultConfigurationBuilder (org.apache.avalon.framework.configuration.DefaultConfigurationBuilder)3 Result (javax.xml.transform.Result)2 Source (javax.xml.transform.Source)2 Transformer (javax.xml.transform.Transformer)2 TransformerFactory (javax.xml.transform.TransformerFactory)2 SAXResult (javax.xml.transform.sax.SAXResult)2 StreamResult (javax.xml.transform.stream.StreamResult)2 StreamSource (javax.xml.transform.stream.StreamSource)2 Fop (org.apache.fop.apps.Fop)2 FopFactory (org.apache.fop.apps.FopFactory)2 FopFactoryBuilder (org.apache.fop.apps.FopFactoryBuilder)2 InitializationException (org.xwiki.component.phase.InitializationException)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiRequest (com.xpn.xwiki.web.XWikiRequest)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1