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);
}
}
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.");
}
}
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;
}
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();
}
}
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();
}
Aggregations