private InputHandler createInputHandler() throws IllegalArgumentException {
switch (inputmode) {
case FO_INPUT:
- return new FOFileHandler(fofile);
+ return new InputHandler(fofile);
case XSLT_INPUT:
- return new XSLTInputHandler(xmlfile, xsltfile, xsltParams);
+ return new InputHandler(xmlfile, xsltfile, xsltParams);
default:
throw new IllegalArgumentException("Error creating InputHandler object.");
}
+++ /dev/null
-/*
- * Copyright 1999-2004 The Apache Software Foundation.
- *
- * Licensed 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.apps;
-
-// Java
-import java.io.File;
-
-// JAXP
-import javax.xml.transform.Result;
-import javax.xml.transform.Source;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.sax.SAXResult;
-import javax.xml.transform.stream.StreamSource;
-
-// Imported SAX classes
-import org.xml.sax.InputSource;
-
-/**
- * Manages input if it is an XSL-FO file.
- */
-public class FOFileHandler extends InputHandler {
- private StreamSource fofile = null;
-
- /**
- * Create a FOFileHandler for a file.
- * @param fofile the file to read the FO document.
- */
- public FOFileHandler(File fofile) {
- this.fofile = new StreamSource(fofile);
-
- try {
- baseURL =
- new File(fofile.getAbsolutePath()).getParentFile().toURL().toExternalForm();
- } catch (Exception e) {
- baseURL = "";
- }
- }
-
- /**
- * @see org.apache.fop.apps.InputHandler#render(Fop)
- */
- public void render(Fop fop) throws FOPException {
-
- // temporary until baseURL removed from inputHandler objects
- if (fop.getUserAgent().getBaseURL() == null) {
- fop.getUserAgent().setBaseURL(getBaseURL());
- }
-
- try {
- // Setup JAXP using identity transformer (no stylesheet here)
- TransformerFactory factory = TransformerFactory.newInstance();
- Transformer transformer = factory.newTransformer();
-
- // Resulting SAX events (the generated FO) must be piped through to FOP
- Result res = new SAXResult(fop.getDefaultHandler());
-
- // Start XSLT transformation and FOP processing
- transformer.transform(fofile, res);
-
- } catch (Exception e) {
- throw new FOPException(e);
- }
- }
-}
package org.apache.fop.apps;
+// Imported java.io classes
+import java.io.File;
+import java.util.Vector;
+
+// Imported TraX classes
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.sax.SAXResult;
+import javax.xml.transform.stream.StreamSource;
+
/**
- * Abstract super class for input handlers.
+ * Class for handling files input from command line
+ * either with XML and XSLT files (and optionally xsl
+ * parameters) or FO File input alone
*/
-public abstract class InputHandler {
-
- protected String baseURL = null;
+public class InputHandler {
+ private File sourcefile = null; // either FO or XML/XSLT usage
+ private File stylesheet = null; // for XML/XSLT usage
+ private Vector xsltParams = null; // for XML/XSLT usage
/**
- * Get the base URL associated with this input source
- * @return the input source
+ * Constructor for XML->XSLT->FO input
+ * @param xmlfile XML file
+ * @param xsltfile XSLT file
+ * @param params Vector of command-line parameters (name, value,
+ * name, value, ...) for XSL stylesheet, null if none
+ */
+ public InputHandler(File xmlfile, File xsltfile, Vector params) {
+ sourcefile = xmlfile;
+ stylesheet = xsltfile;
+ xsltParams = params;
+ }
+
+ /**
+ * Constructor for FO input
+ * @param fofile the file to read the FO document.
*/
- public String getBaseURL() {
- return baseURL;
+ public InputHandler(File fofile) {
+ sourcefile = fofile;
}
/**
* @param fop -- Fop object
* @throws FOPException in case of an error during processing
*/
- public void render(Fop fop) throws FOPException {}
+ public void render(Fop fop) throws FOPException {
+ // if base URL was not explicitly set in FOUserAgent, obtain here
+ if (fop.getUserAgent().getBaseURL() == null) {
+ String baseURL = null;
+
+ try {
+ baseURL =
+ new File(sourcefile.getAbsolutePath()).
+ getParentFile().toURL().toExternalForm();
+ } catch (Exception e) {
+ baseURL = "";
+ }
+ fop.getUserAgent().setBaseURL(baseURL);
+ }
+
+ try {
+ // Setup XSLT
+ TransformerFactory factory = TransformerFactory.newInstance();
+ Transformer transformer;
+
+ if (stylesheet == null) { // FO Input
+ transformer = factory.newTransformer();
+ } else { // XML/XSLT input
+ transformer = factory.newTransformer(new StreamSource(
+ stylesheet));
+
+ // Set the value of parameters, if any, defined for stylesheet
+ if (xsltParams != null) {
+ for (int i = 0; i < xsltParams.size(); i += 2) {
+ transformer.setParameter((String) xsltParams.elementAt(i),
+ (String) xsltParams.elementAt(i + 1));
+ }
+ }
+ }
+
+ // Create a SAXSource from the input Source file
+ Source src = new StreamSource(sourcefile);
+
+ // Resulting SAX events (the generated FO) must be piped through to FOP
+ Result res = new SAXResult(fop.getDefaultHandler());
+
+ // Start XSLT transformation and FOP processing
+ transformer.transform(src, res);
+
+ } catch (Exception e) {
+ throw new FOPException(e);
+ }
+ }
}
+++ /dev/null
-/*
- * Copyright 1999-2004 The Apache Software Foundation.
- *
- * Licensed 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.apps;
-
-// Imported java.io classes
-import java.io.File;
-import java.util.Vector;
-
-// Imported TraX classes
-import javax.xml.transform.Result;
-import javax.xml.transform.Source;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.sax.SAXResult;
-import javax.xml.transform.stream.StreamSource;
-
-// Imported SAX classes
-import org.xml.sax.InputSource;
-
-/**
- * XSLTInputHandler basically takes an XML file and transforms it with an XSLT
- * file and the resulting XSL-FO document is input for FOP.
- */
-public class XSLTInputHandler extends InputHandler {
- private StreamSource xmlSource;
- private Source xsltSource;
- private Vector xsltParams = null;
-
- /**
- * Constructor for files as input
- * @param xmlfile XML file
- * @param xsltfile XSLT file
- * @param params Vector of command-line parameters (name, value,
- * name, value, ...) for XSL stylesheet, null if none
- * @throws FOPException if initializing the Transformer fails
- */
- public XSLTInputHandler(File xmlfile, File xsltfile, Vector params) {
- this.xmlSource = new StreamSource(xmlfile);
- this.xsltSource = new StreamSource(xsltfile);
- try {
- baseURL =
- new File(xmlfile.getAbsolutePath()).getParentFile().toURL().toExternalForm();
- } catch (Exception e) {
- baseURL = "";
- }
- xsltParams = params;
- }
-
- /**
- * @see org.apache.fop.apps.InputHandler#render(Fop)
- */
- public void render(Fop fop)
- throws FOPException {
-
- // temporary until baseURL removed from inputHandler objects
- if (fop.getUserAgent().getBaseURL() == null) {
- fop.getUserAgent().setBaseURL(getBaseURL());
- }
-
- try {
- // Setup XSLT
- TransformerFactory factory = TransformerFactory.newInstance();
- Transformer transformer = factory.newTransformer(xsltSource);
-
- // Set the value of parameters, if any, defined for stylesheet
- if (xsltParams != null) {
- for (int i = 0; i < xsltParams.size(); i += 2) {
- transformer.setParameter((String) xsltParams.elementAt(i),
- (String) xsltParams.elementAt(i + 1));
- }
- }
-
- // Resulting SAX events (the generated FO) must be piped through to FOP
- Result res = new SAXResult(fop.getDefaultHandler());
-
- // Start XSLT transformation and FOP processing
- transformer.transform(xmlSource, res);
-
- } catch (Exception e) {
- throw new FOPException(e);
- }
- }
-}
protected Map layoutDimension = null;
/** During input FO validation, certain FO's are not valid as
- child nodes if this FO is a descendant of an Out Of Line
+ child nodes if they would be a descendant of an Out Of Line
Formatting Object as defined in specification.
See Section 6.2 of 1.0/1.2 spec for more information.
*/
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.fop.apps.Fop;
-import org.apache.fop.apps.FOFileHandler;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.InputHandler;
-import org.apache.fop.apps.XSLTInputHandler;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
InputHandler inputHandler = null;
if (xsl == null) {
- inputHandler = new FOFileHandler(xmlFile);
+ inputHandler = new InputHandler(xmlFile);
} else {
- inputHandler = new XSLTInputHandler(xmlFile,
- new File(baseDir + "/"
- + xsl), null);
+ inputHandler = new InputHandler(xmlFile,
+ new File(baseDir + "/"
+ + xsl), null);
}
FOUserAgent userAgent = new FOUserAgent();
// FOP
import org.apache.fop.apps.InputHandler;
-import org.apache.fop.apps.FOFileHandler;
import org.apache.fop.fo.Constants;
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
private void render(File foFile, File outFile,
int renderer) throws FOPException {
- InputHandler inputHandler = new FOFileHandler(foFile);
+ InputHandler inputHandler = new InputHandler(foFile);
OutputStream out = null;
try {
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.InputHandler;
-import org.apache.fop.apps.XSLTInputHandler;
import org.w3c.dom.Document;
/**
Fop fop = new Fop(Fop.RENDER_PDF);
fop.setOutputStream(baout);
- InputHandler handler = new XSLTInputHandler(xmlFile, xsltFile, null);
+ InputHandler handler = new InputHandler(xmlFile, xsltFile, null);
handler.render(fop);
assertTrue("Generated PDF has zero length", baout.size() > 0);