Browse Source

Take over InputHandler code from maintenance branch.

Makes the org.apache.fop.tools.xslt package superfluous.
(#20398)
Submitted by: Glen Mazza <glenmazza@yahoo.com>


git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@196490 13f79535-47bb-0310-9956-ffa450edef68
pull/30/head
Jeremias Maerki 21 years ago
parent
commit
13a04c0b42

+ 2
- 1
src/java/org/apache/fop/apps/CommandLineOptions.java View File

@@ -420,8 +420,9 @@ public class CommandLineOptions {
/**
* Get the input handler.
* @return the input handler
* @throws FOPException if creating the InputHandler fails
*/
public InputHandler getInputHandler() {
public InputHandler getInputHandler() throws FOPException {
switch (inputmode) {
case FO_INPUT:
return new FOInputHandler(fofile);

+ 8
- 0
src/java/org/apache/fop/apps/FOInputHandler.java View File

@@ -100,5 +100,13 @@ public class FOInputHandler extends InputHandler {
return super.createParser();
}

/**
* @see org.apache.fop.apps.InputHandler#run(Driver)
*/
public void run(Driver driver) throws FOPException {
throw new FOPException("not implemented: FOInputHandler.run(Driver)");
}


}


+ 8
- 0
src/java/org/apache/fop/apps/InputHandler.java View File

@@ -129,5 +129,13 @@ public abstract class InputHandler {
throw new FOPException("Coudn't create XMLReader", pce);
}
}
/**
* Runs this InputHandler through the Driver.
* @param driver Driver instance to use
* @throws FOPException if processing this InputHandler fails
*/
public abstract void run(Driver driver) throws FOPException;
}


+ 77
- 13
src/java/org/apache/fop/apps/TraxInputHandler.java View File

@@ -54,6 +54,8 @@ package org.apache.fop.apps;
import java.io.File;

// Imported TraX classes
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;
@@ -71,23 +73,66 @@ import org.xml.sax.XMLFilter;
*/
public class TraxInputHandler extends InputHandler {

private File xmlfile, xsltfile;
private Transformer transformer;
private StreamSource xmlSource;
private Source xsltSource;

/**
* Main constructor
* Constructor with files as input.
* @param xmlfile XML file
* @param xsltfile XSLT file
* @throws FOPException if initializing the Transformer fails
*/
public TraxInputHandler(File xmlfile, File xsltfile) {
this.xmlfile = xmlfile;
this.xsltfile = xsltfile;
public TraxInputHandler(File xmlfile, File xsltfile) throws FOPException {
this.xmlSource = new StreamSource(xmlfile);
this.xsltSource = new StreamSource(xsltfile);
initTransformer();
}

/**
* Constructor with URIs/URLs as input.
* @param xmlURL XML URL
* @param xsltURL XSLT URL
* @throws FOPException if initializing the Transformer fails
*/
public TraxInputHandler(String xmlURL, String xsltURL) throws FOPException {
this.xmlSource = new StreamSource(xmlURL);
this.xsltSource = new StreamSource(xsltURL);
initTransformer();
}

/**
* Constructor with InputSources as input.
* @param xmlSource XML InputSource
* @param xsltSource XSLT InputSource
* @throws FOPException if initializing the Transformer fails
*/
public TraxInputHandler(InputSource xmlSource, InputSource xsltSource)
throws FOPException {
this.xmlSource = new StreamSource(xmlSource.getByteStream(),
xmlSource.getSystemId());
this.xsltSource = new StreamSource(xsltSource.getByteStream(),
xsltSource.getSystemId());
initTransformer();
}
private void initTransformer() throws FOPException {
try {
this.transformer =
TransformerFactory.newInstance().newTransformer(xsltSource);
} catch (Exception ex) {
throw new FOPException(ex);
}
}

/**
* @see org.apache.fop.apps.InputHandler#getInputSource()
*/
public InputSource getInputSource() {
return fileInputSource(xmlfile);
InputSource is = new InputSource();
is.setByteStream(xmlSource.getInputStream());
is.setSystemId(xmlSource.getSystemId());
return is;
}

/**
@@ -96,7 +141,7 @@ public class TraxInputHandler extends InputHandler {
* @see org.apache.fop.apps.InputHandler#getParser()
*/
public XMLReader getParser() throws FOPException {
return this.getXMLFilter(xmlfile, xsltfile);
return getXMLFilter(xsltSource);
}

/**
@@ -104,14 +149,12 @@ public class TraxInputHandler extends InputHandler {
* then can be used in a chain with the XMLReader passed to Driver. This way
* during the conversion of the xml file + xslt stylesheet the resulting
* data is fed into Fop. This should help to avoid memory problems
* @param xmlfile The xmlfile containing the text data
* @param xsltfile An xslt stylesheet
* @param xsltSource An xslt stylesheet
* @return an XMLFilter which can be chained together with other
* XMLReaders or XMLFilters
* @throws FOPException if setting up the XMLFilter fails
*/
public static XMLFilter getXMLFilter(File xmlfile,
File xsltfile) throws FOPException {
public static XMLFilter getXMLFilter(Source xsltSource) throws FOPException {
try {
// Instantiate a TransformerFactory.
TransformerFactory tFactory = TransformerFactory.newInstance();
@@ -124,7 +167,7 @@ public class TraxInputHandler extends InputHandler {
((SAXTransformerFactory)tFactory);
// Create an XMLFilter for each stylesheet.
XMLFilter xmlfilter =
saxTFactory.newXMLFilter(new StreamSource(xsltfile));
saxTFactory.newXMLFilter(xsltSource);

// Create an XMLReader.
XMLReader parser = createParser();
@@ -148,5 +191,26 @@ public class TraxInputHandler extends InputHandler {
}
}

}
/**
* @see org.apache.fop.apps.InputHandler#run(Driver)
*/
public void run(Driver driver) throws FOPException {
try {
transformer.transform(xmlSource,
new SAXResult(driver.getContentHandler()));
} catch (Exception ex) {
throw new FOPException(ex);
}
}

/**
* Sets an XSLT parameter.
* @param name the name of the parameter
* @param value the value of the parameter
*/
public void setParameter(String name, Object value) {
transformer.setParameter(name, value);
}


}

+ 47
- 94
src/java/org/apache/fop/apps/XSLTInputHandler.java View File

@@ -52,131 +52,84 @@ package org.apache.fop.apps;

// Imported java.io classes
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

// Imported SAX classes
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

// FOP
import org.apache.fop.tools.xslt.XSLTransform;

/**
* XSLTInputHandler basically takes an XML file and transforms it with an XSLT
* file and the resulting XSL-FO document is input for FOP.
* (todo) add URL constructor
*/
public class XSLTInputHandler extends InputHandler {

private File xmlfile, xsltfile;
private boolean useOldTransform = false;
private boolean gotParser = false;
private TraxInputHandler traxInputHandler;

/**
* Main constructor
* Constructor for files as input
* @param xmlfile XML file
* @param xsltfile XSLT file
* @throws FOPException if initializing the Transformer fails
*/
public XSLTInputHandler(File xmlfile, File xsltfile) {
this.xmlfile = xmlfile;
this.xsltfile = xsltfile;
public XSLTInputHandler(File xmlfile, File xsltfile) throws FOPException {
this.traxInputHandler = new TraxInputHandler(xmlfile, xsltfile);
}

/**
* @see org.apache.fop.apps.InputHandler#getInputSource()
* Constructor with URIs/URLs as input.
* @param xmlURL XML URL
* @param xsltURL XSLT URL
* @throws FOPException if initializing the Transformer fails
*/
public InputSource getInputSource() {
if (!gotParser) {
throw new IllegalStateException("The method getParser() must be "
+ "called and the parser used when using XSLTInputHandler");
}
if (useOldTransform) {
try {
java.io.Writer writer;
java.io.Reader reader;
File tmpFile = null;

// create a Writer
// the following is an ugly hack to allow processing of larger files
// if xml file size is larger than 500 kb write the fo:file to disk
if ((xmlfile.length()) > 500000) {
tmpFile = new File(xmlfile.getName() + ".fo.tmp");
writer = new java.io.FileWriter(tmpFile);
} else {
writer = new java.io.StringWriter();
}

XSLTransform.transform(xmlfile.getCanonicalPath(),
xsltfile.getCanonicalPath(), writer);

writer.flush();
writer.close();
public XSLTInputHandler(String xmlURL, String xsltURL) throws FOPException {
traxInputHandler = new TraxInputHandler(xmlURL, xsltURL);
}

if (tmpFile != null) {
reader = new java.io.FileReader(tmpFile);
} else {
// create a input source containing the xsl:fo file which can be fed to Fop
reader = new java.io.StringReader(writer.toString());
}
return new InputSource(reader);
} catch (Exception ex) {
ex.printStackTrace();
/**(todo) do proper logging of exceptions */
return null;
}
} else {
return fileInputSource(xmlfile);
}
/**
* Constructor with InputSources as input.
* @param xmlSource XML InputSource
* @param xsltSource XSLT InputSource
* @throws FOPException if initializing the Transformer fails
*/
public XSLTInputHandler(InputSource xmlSource, InputSource xsltSource)
throws FOPException {
traxInputHandler = new TraxInputHandler(xmlSource, xsltSource);
}

/**
* Get the InputSource.
* @return the InputSource
* @deprecated Use TraxInputHandler run(Driver driver) instead.
*/
public InputSource getInputSource() {
return traxInputHandler.getInputSource();
}

/**
* This looks to see if the Trax api is supported and uses that to
* get an XMLFilter. Otherwise, it falls back to using DOM documents
* @return the created <code>XMLReader</code>
* @throws FOPException if getting the parser fails
* Get the parser, actually an XML filter.
* @see org.apache.fop.apps.InputHandler#getParser()
* @deprecated Use TraxInputHandler run(Driver driver) instead.
*/
public XMLReader getParser() throws FOPException {
gotParser = true;
return traxInputHandler.getParser();
}

XMLReader result = null;
try {
// try trax first
Class transformer =
Class.forName("javax.xml.transform.Transformer");
transformer =
Class.forName("org.apache.fop.apps.TraxInputHandler");
Class[] argTypes = {
File.class, File.class
};
Method getFilterMethod = transformer.getMethod("getXMLFilter",
argTypes);
File[] args = {
xmlfile, xsltfile
};
Object obj = getFilterMethod.invoke(null, args);
if (obj instanceof XMLReader) {
result = (XMLReader)obj;
}
} catch (ClassNotFoundException ex) {
throw new FOPException(ex);
} catch (InvocationTargetException ex) {
throw new FOPException(ex);
} catch (IllegalAccessException ex) {
throw new FOPException(ex);
} catch (NoSuchMethodException ex) {
throw new FOPException(ex);
}
// otherwise, use DOM documents via our XSLTransform tool class old style
if (result == null) {
useOldTransform = true;
result = createParser();
}
return result;
/**
* @see org.apache.fop.apps.InputHandler#run(Driver)
*/
public void run(Driver driver) throws FOPException {
traxInputHandler.run(driver);
}

/**
* Sets an XSLT parameter.
* @param name the name of the parameter
* @param value the value of the parameter
*/
public void setParameter(String name, Object value) {
traxInputHandler.setParameter(name, value);
}

}



Loading…
Cancel
Save