--- /dev/null
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+
+package org.apache.fop.threading;
+
+import org.apache.avalon.framework.logger.Logger;
+
+import org.apache.fop.events.Event;
+import org.apache.fop.events.EventFormatter;
+import org.apache.fop.events.EventListener;
+import org.apache.fop.events.model.EventSeverity;
+
+/**
+ * Redirects events to an Avalon logger.
+ */
+class AvalonAdapter implements EventListener {
+
+ private final Logger logger;
+ private String filename;
+
+ public AvalonAdapter(Logger logger, String filename) {
+ this.logger = logger;
+ this.filename = filename;
+ }
+
+ public void processEvent(Event event) {
+ String msg = EventFormatter.format(event);
+ EventSeverity severity = event.getSeverity();
+ if (severity == EventSeverity.INFO) {
+ //logger.info(filename + ": " + msg);
+ } else if (severity == EventSeverity.WARN) {
+ //logger.warn(filename + ": " + msg);
+ } else if (severity == EventSeverity.ERROR) {
+ logger.error(filename + ": " + msg);
+ } else if (severity == EventSeverity.FATAL) {
+ logger.fatalError(filename + ": " + msg);
+ } else {
+ assert false;
+ }
+ }
+
+}
\ No newline at end of file
private int threads;
private File outputDir;
private Configuration fopCfg;
- private FOProcessor foprocessor;
+ private Processor foprocessor;
private boolean writeToDevNull;
private int counter = 0;
for (int i = 0; i < entries.length; i++) {
this.taskList.add(new TaskDef(entries[i]));
}
- this.fopCfg = configuration.getChild("foprocessor");
+ this.fopCfg = configuration.getChild("processor");
}
/** {@inheritDoc} */
* Creates a new FOProcessor.
* @return the newly created instance
*/
- public FOProcessor createFOProcessor() {
+ public Processor createFOProcessor() {
try {
Class clazz = Class.forName(this.fopCfg.getAttribute("class",
"org.apache.fop.threading.FOProcessorImpl"));
- FOProcessor fop = (FOProcessor)clazz.newInstance();
+ Processor fop = (Processor)clazz.newInstance();
ContainerUtil.enableLogging(fop, getLogger());
ContainerUtil.configure(fop, this.fopCfg);
ContainerUtil.initialize(fop);
this.fo = cfg.getAttribute("fo", null);
if (this.fo == null) {
this.xml = cfg.getAttribute("xml");
- this.xslt = cfg.getAttribute("xslt");
- TransformerFactory factory = TransformerFactory.newInstance();
- Source xsltSource = new StreamSource(new File(xslt));
- try {
- this.templates = factory.newTemplates(xsltSource);
- } catch (TransformerConfigurationException tce) {
- throw new ConfigurationException("Invalid XSLT", tce);
+ this.xslt = cfg.getAttribute("xslt", null);
+ if (this.xslt != null) {
+ TransformerFactory factory = TransformerFactory.newInstance();
+ Source xsltSource = new StreamSource(new File(xslt));
+ try {
+ this.templates = factory.newTemplates(xsltSource);
+ } catch (TransformerConfigurationException tce) {
+ throw new ConfigurationException("Invalid XSLT", tce);
+ }
}
}
}
private TaskDef def;
private int num;
- private FOProcessor fop;
+ private Processor fop;
- public Task(TaskDef def, int num, FOProcessor fop) {
+ public Task(TaskDef def, int num, Processor fop) {
this.def = def;
this.num = num;
this.fop = fop;
+++ /dev/null
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/* $Id$ */
-
-package org.apache.fop.threading;
-
-import java.io.OutputStream;
-
-import javax.xml.transform.Source;
-import javax.xml.transform.Templates;
-
-/**
- * Represents an FO processor.
- */
-public interface FOProcessor {
-
- /**
- * Process a file.
- * @param src the Source for the FO or XML file
- * @param templates a JAXP Templates object for an XSLT transformation or null
- * @param out the OutputStream for the target file
- * @throws Exception if an error occurs
- */
- void process(Source src, Templates templates, OutputStream out)
- throws Exception;
-
- /**
- * Returns the target file extension for the configured output format.
- * @return the target file extension (for example ".pdf")
- */
- String getTargetFileExtension();
-}
\ No newline at end of file
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
-import org.apache.fop.events.Event;
-import org.apache.fop.events.EventFormatter;
-import org.apache.fop.events.EventListener;
-import org.apache.fop.events.model.EventSeverity;
/**
- * Default implementation of the FOProcessor interface using FOP.
+ * Default implementation of the {@link Processor} interface using FOP.
*/
public class FOProcessorImpl extends AbstractLogEnabled
- implements FOProcessor, Configurable, Initializable {
+ implements Processor, Configurable, Initializable {
private FopFactory fopFactory = FopFactory.newInstance();
private TransformerFactory factory = TransformerFactory.newInstance();
try {
URL url = new URL(src.getSystemId());
String filename = FilenameUtils.getName(url.getPath());
- foUserAgent.getEventBroadcaster().addEventListener(new AvalonAdapter(filename));
+ foUserAgent.getEventBroadcaster().addEventListener(
+ new AvalonAdapter(getLogger(), filename));
} catch (MalformedURLException mfue) {
throw new RuntimeException(mfue);
}
public String getTargetFileExtension() {
return this.fileExtension;
}
-
- private class AvalonAdapter implements EventListener {
-
- private String filename;
-
- public AvalonAdapter(String filename) {
- this.filename = filename;
- }
-
- public void processEvent(Event event) {
- String msg = EventFormatter.format(event);
- EventSeverity severity = event.getSeverity();
- if (severity == EventSeverity.INFO) {
- //getLogger().info(filename + ": " + msg);
- } else if (severity == EventSeverity.WARN) {
- //getLogger().warn(filename + ": " + msg);
- } else if (severity == EventSeverity.ERROR) {
- getLogger().error(filename + ": " + msg);
- } else if (severity == EventSeverity.FATAL) {
- getLogger().fatalError(filename + ": " + msg);
- } else {
- assert false;
- }
- }
-
- }
}
\ No newline at end of file
--- /dev/null
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+
+package org.apache.fop.threading;
+
+import java.io.OutputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.Templates;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.sax.SAXResult;
+import javax.xml.transform.stream.StreamResult;
+
+import org.xml.sax.ContentHandler;
+
+import org.apache.avalon.framework.activity.Initializable;
+import org.apache.avalon.framework.configuration.Configurable;
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.commons.io.FilenameUtils;
+
+import org.apache.fop.apps.FOPException;
+import org.apache.fop.apps.FOUserAgent;
+import org.apache.fop.apps.FopFactory;
+import org.apache.fop.apps.MimeConstants;
+import org.apache.fop.render.intermediate.IFDocumentHandler;
+import org.apache.fop.render.intermediate.IFException;
+import org.apache.fop.render.intermediate.IFParser;
+import org.apache.fop.render.intermediate.IFUtil;
+
+/**
+ * Implementation of the {@link Processor} interface that renders IF XML to a final output format.
+ */
+public class IFProcessorImpl extends AbstractLogEnabled
+ implements Processor, Configurable, Initializable {
+
+ private FopFactory fopFactory = FopFactory.newInstance();
+ private TransformerFactory factory = TransformerFactory.newInstance();
+ private String userconfig;
+ private String mime;
+ private String fileExtension;
+
+ /** {@inheritDoc} */
+ public void configure(Configuration configuration) throws ConfigurationException {
+ this.userconfig = configuration.getChild("userconfig").getValue(null);
+ this.mime = configuration.getChild("mime").getValue(MimeConstants.MIME_PDF);
+ this.fileExtension = configuration.getChild("extension").getValue(".pdf");
+ }
+
+ /** {@inheritDoc} */
+ public void initialize() throws Exception {
+ if (this.userconfig != null) {
+ getLogger().debug("Setting user config: " + userconfig);
+ fopFactory.setUserConfig(this.userconfig);
+ }
+ }
+
+ /** {@inheritDoc}
+ * @throws IFException */
+ public void process(Source src, Templates templates, OutputStream out)
+ throws org.apache.fop.apps.FOPException, java.io.IOException, IFException {
+ FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
+ foUserAgent.setBaseURL(src.getSystemId());
+ try {
+ URL url = new URL(src.getSystemId());
+ String filename = FilenameUtils.getName(url.getPath());
+ foUserAgent.getEventBroadcaster().addEventListener(
+ new AvalonAdapter(getLogger(), filename));
+ } catch (MalformedURLException mfue) {
+ throw new RuntimeException(mfue);
+ }
+
+ //Setup target handler
+ IFDocumentHandler targetHandler = fopFactory.getRendererFactory().createDocumentHandler(
+ foUserAgent, mime);
+
+ //Setup fonts
+ IFUtil.setupFonts(targetHandler);
+ targetHandler.setResult(new StreamResult(out));
+
+ try {
+ Transformer transformer;
+ if (templates == null) {
+ transformer = factory.newTransformer();
+ } else {
+ transformer = templates.newTransformer();
+ }
+ IFParser parser = new IFParser();
+ ContentHandler contentHandler = parser.getContentHandler(targetHandler, foUserAgent);
+ Result res = new SAXResult(contentHandler);
+ transformer.transform(src, res);
+ } catch (TransformerException e) {
+ throw new FOPException(e);
+ }
+ }
+
+ /** {@inheritDoc} */
+ public String getTargetFileExtension() {
+ return this.fileExtension;
+ }
+
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+
+package org.apache.fop.threading;
+
+import java.io.OutputStream;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.Templates;
+
+/**
+ * Represents a processor.
+ */
+public interface Processor {
+
+ /**
+ * Process a file.
+ * @param src the Source for the FO or XML file
+ * @param templates a JAXP Templates object for an XSLT transformation or null
+ * @param out the OutputStream for the target file
+ * @throws Exception if an error occurs
+ */
+ void process(Source src, Templates templates, OutputStream out)
+ throws Exception;
+
+ /**
+ * Returns the target file extension for the configured output format.
+ * @return the target file extension (for example ".pdf")
+ */
+ String getTargetFileExtension();
+}
\ No newline at end of file
<config prompt="false">
<threads>2</threads>
<output-dir>C:/Dev/FOP/temp/out</output-dir>
- <foprocessor class="org.apache.fop.threading.FOProcessorImpl">
+ <processor class="org.apache.fop.threading.FOProcessorImpl">
<!--
<userconfig>C:/Dev/FOP/temp/userconfig.xml</userconfig>
-->
<mime>application/pdf</mime>
<extension>.pdf</extension>
- </foprocessor>
+ </processor>
<tasks repeat="2">
<task fo="C:/Dev/FOP/temp/helloworld.fo"/>
<task xml="C:/Dev/FOP/temp/page-x-of-y.xml" xslt="C:/Dev/FOP/temp/page-x-of-y.xsl"/>