diff options
author | Dirk-Willem van Gulik <dirkx@apache.org> | 1999-11-08 19:12:49 +0000 |
---|---|---|
committer | Dirk-Willem van Gulik <dirkx@apache.org> | 1999-11-08 19:12:49 +0000 |
commit | 10070e8383ff94f3f256e346b8c4a2a493533cfb (patch) | |
tree | 59080d7faae7c0bd9ff4e5a48f4df4394d468a02 /src/org/apache/fop/apps | |
parent | b510e7f15a798e944bb138993f2b586413adecbe (diff) | |
download | xmlgraphics-fop-10070e8383ff94f3f256e346b8c4a2a493533cfb.tar.gz xmlgraphics-fop-10070e8383ff94f3f256e346b8c4a2a493533cfb.zip |
Initial revision
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@193213 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/org/apache/fop/apps')
-rw-r--r-- | src/org/apache/fop/apps/CommandLine.java | 122 | ||||
-rw-r--r-- | src/org/apache/fop/apps/Driver.java | 215 | ||||
-rw-r--r-- | src/org/apache/fop/apps/ErrorHandler.java | 4 | ||||
-rw-r--r-- | src/org/apache/fop/apps/FOPException.java | 16 | ||||
-rw-r--r-- | src/org/apache/fop/apps/Makefile | 30 | ||||
-rw-r--r-- | src/org/apache/fop/apps/Version.java | 18 | ||||
-rw-r--r-- | src/org/apache/fop/apps/XTCommandLine.java | 78 | ||||
-rw-r--r-- | src/org/apache/fop/apps/package.html | 7 |
8 files changed, 490 insertions, 0 deletions
diff --git a/src/org/apache/fop/apps/CommandLine.java b/src/org/apache/fop/apps/CommandLine.java new file mode 100644 index 000000000..0786ba7aa --- /dev/null +++ b/src/org/apache/fop/apps/CommandLine.java @@ -0,0 +1,122 @@ +package org.apache.xml.fop.apps; + +// SAX +import org.xml.sax.Parser; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +// Java +import java.io.FileReader; +import java.io.File; +import java.io.FileWriter; +import java.io.PrintWriter; +import java.io.IOException; +import java.io.FileNotFoundException; +import java.net.URL; + +/** + * mainline class. + * + * Gets input and output filenames from the command line. + * Creates a SAX Parser (defaulting to XP). + * + */ +public class CommandLine { + + /** + * creates a SAX parser, using the value of org.xml.sax.parser + * defaulting to com.jclark.xml.sax.Driver + * + * @return the created SAX parser + */ + static Parser createParser() { + String parserClassName = + System.getProperty("org.xml.sax.parser"); + if (parserClassName == null) { + parserClassName = "com.jclark.xml.sax.Driver"; + } + System.err.println("using SAX parser " + parserClassName); + + try { + return (Parser) + Class.forName(parserClassName).newInstance(); + } catch (ClassNotFoundException e) { + System.err.println("Could not find " + parserClassName); + } catch (InstantiationException e) { + System.err.println("Could not instantiate " + + parserClassName); + } catch (IllegalAccessException e) { + System.err.println("Could not access " + parserClassName); + } catch (ClassCastException e) { + System.err.println(parserClassName + " is not a SAX driver"); + } + return null; + } + + /** + * create an InputSource from a file name + * + * @param filename the name of the file + * @return the InputSource created + */ + protected static InputSource fileInputSource(String filename) { + + /* this code adapted from James Clark's in XT */ + File file = new File(filename); + String path = file.getAbsolutePath(); + String fSep = System.getProperty("file.separator"); + if (fSep != null && fSep.length() == 1) + path = path.replace(fSep.charAt(0), '/'); + if (path.length() > 0 && path.charAt(0) != '/') + path = '/' + path; + try { + return new InputSource(new URL("file", null, + path).toString()); + } + catch (java.net.MalformedURLException e) { + throw new Error("unexpected MalformedURLException"); + } + } + + /** + * mainline method + * + * first command line argument is input file + * second command line argument is output file + * + * @param command line arguments + */ + public static void main(String[] args) { + String version = Version.getVersion(); + System.err.println(version); + + if (args.length != 2) { + System.err.println("usage: java " + + "org.apache.xml.fop.apps.CommandLine " + + "formatting-object-file pdf-file"); + System.exit(1); + } + + Parser parser = createParser(); + + if (parser == null) { + System.err.println("ERROR: Unable to create SAX parser"); + System.exit(1); + } + + try { + Driver driver = new Driver(); + driver.setRenderer("org.apache.xml.fop.render.pdf.PDFRenderer", version); + driver.addElementMapping("org.apache.xml.fop.fo.StandardElementMapping"); + driver.addElementMapping("org.apache.xml.fop.svg.SVGElementMapping"); + driver.setWriter(new PrintWriter(new FileWriter(args[1]))); + driver.buildFOTree(parser, fileInputSource(args[0])); + driver.format(); + driver.render(); + } catch (Exception e) { + System.err.println("FATAL ERROR: " + e.getMessage()); + System.exit(1); + } + } +} diff --git a/src/org/apache/fop/apps/Driver.java b/src/org/apache/fop/apps/Driver.java new file mode 100644 index 000000000..fc0ba13fd --- /dev/null +++ b/src/org/apache/fop/apps/Driver.java @@ -0,0 +1,215 @@ +package org.apache.xml.fop.apps; + +// FOP +import org.apache.xml.fop.fo.FOTreeBuilder; +import org.apache.xml.fop.fo.ElementMapping; +import org.apache.xml.fop.layout.AreaTree; +import org.apache.xml.fop.layout.FontInfo; +import org.apache.xml.fop.render.Renderer; + +// DOM +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Attr; + +// SAX +import org.xml.sax.DocumentHandler; +import org.xml.sax.InputSource; +import org.xml.sax.Parser; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributeListImpl; + +// Java +import java.io.PrintWriter; +import java.io.IOException; + +public class Driver { + + protected FOTreeBuilder treeBuilder; + protected AreaTree areaTree; + protected Renderer renderer; + protected PrintWriter writer; + + public Driver() { + this.treeBuilder = new FOTreeBuilder(); + } + + public void setRenderer(Renderer renderer) { + this.renderer = renderer; + } + + public void setRenderer(String rendererClassName, String producer) { + this.renderer = createRenderer(rendererClassName); + this.renderer.setProducer(producer); + } + + protected Renderer createRenderer(String rendererClassName) { + System.err.println("using renderer " + rendererClassName); + + try { + return (Renderer) + Class.forName(rendererClassName).newInstance(); + } catch (ClassNotFoundException e) { + System.err.println("Could not find " + rendererClassName); + } catch (InstantiationException e) { + System.err.println("Could not instantiate " + + rendererClassName); + } catch (IllegalAccessException e) { + System.err.println("Could not access " + rendererClassName); + } catch (ClassCastException e) { + System.err.println(rendererClassName + " is not a renderer"); + } + return null; + } + + public void addElementMapping(ElementMapping mapping) { + mapping.addToBuilder(this.treeBuilder); + } + + public void addElementMapping(String mappingClassName) { + createElementMapping(mappingClassName).addToBuilder(this.treeBuilder); + } + + protected ElementMapping createElementMapping(String mappingClassName) { + System.err.println("using element mapping " + mappingClassName); + + try { + return (ElementMapping) + Class.forName(mappingClassName).newInstance(); + } catch (ClassNotFoundException e) { + System.err.println("Could not find " + mappingClassName); + } catch (InstantiationException e) { + System.err.println("Could not instantiate " + + mappingClassName); + } catch (IllegalAccessException e) { + System.err.println("Could not access " + mappingClassName); + } catch (ClassCastException e) { + System.err.println(mappingClassName + " is not an element mapping"); + } + return null; + } + + public DocumentHandler getDocumentHandler() { + return this.treeBuilder; + } + + public void buildFOTree(Parser parser, InputSource source) + throws FOPException { + parser.setDocumentHandler(this.treeBuilder); + try { + parser.parse(source); + } catch (SAXException e) { + if (e.getException() instanceof FOPException) + throw (FOPException) e.getException(); + else + throw new FOPException(e.getMessage()); + } catch (IOException e) { + throw new FOPException(e.getMessage()); + } + } + + public void buildFOTree(Document document) + throws FOPException { + + /* most of this code is modified from John Cowan's */ + + Node currentNode; + AttributeListImpl currentAtts; + + /* temporary array for making Strings into character arrays */ + char[] array = null; + + currentAtts = new AttributeListImpl(); + + /* start at the document element */ + currentNode = document; + + try { + while (currentNode != null) { + switch (currentNode.getNodeType()) { + case Node.DOCUMENT_NODE: + this.treeBuilder.startDocument(); + break; + case Node.CDATA_SECTION_NODE: + case Node.TEXT_NODE: + String data = currentNode.getNodeValue(); + int datalen = data.length(); + if (array == null || array.length < datalen) { + /* if the array isn't big enough, make a new + one */ + array = new char[datalen]; + } + data.getChars(0, datalen, array, 0); + this.treeBuilder.characters(array, 0, datalen); + break; + case Node.PROCESSING_INSTRUCTION_NODE: + this.treeBuilder.processingInstruction( + currentNode.getNodeName(), + currentNode.getNodeValue()); + break; + case Node.ELEMENT_NODE: + NamedNodeMap map = currentNode.getAttributes(); + currentAtts.clear(); + for (int i = map.getLength() - 1; i >= 0; i--) { + Attr att = (Attr)(map.item(i)); + currentAtts.addAttribute(att.getName(), + "CDATA", + att.getValue()); + } + this.treeBuilder.startElement( + currentNode.getNodeName(), currentAtts); + break; + } + + Node nextNode = currentNode.getFirstChild(); + if (nextNode != null) { + currentNode = nextNode; + continue; + } + + while (currentNode != null) { + switch (currentNode.getNodeType()) { + case Node.DOCUMENT_NODE: + this.treeBuilder.endDocument(); + break; + case Node.ELEMENT_NODE: + this.treeBuilder.endElement( + currentNode.getNodeName()); + break; + } + + nextNode = currentNode.getNextSibling(); + if (nextNode != null) { + currentNode = nextNode; + break; + } + + currentNode = currentNode.getParentNode(); + } + } + } catch (SAXException e) { + throw new FOPException(e.getMessage()); + } + } + + public void setWriter(PrintWriter writer) { + this.writer = writer; + } + + public void format() + throws FOPException { + FontInfo fontInfo = new FontInfo(); + this.renderer.setupFontInfo(fontInfo); + + this.areaTree = new AreaTree(); + this.areaTree.setFontInfo(fontInfo); + + this.treeBuilder.format(areaTree); + } + + public void render() + throws IOException { + this.renderer.render(areaTree, this.writer); + } +} diff --git a/src/org/apache/fop/apps/ErrorHandler.java b/src/org/apache/fop/apps/ErrorHandler.java new file mode 100644 index 000000000..c2aaa277d --- /dev/null +++ b/src/org/apache/fop/apps/ErrorHandler.java @@ -0,0 +1,4 @@ +package org.apache.xml.fop.apps; + +/** not implemented yet */ +public interface ErrorHandler {} diff --git a/src/org/apache/fop/apps/FOPException.java b/src/org/apache/fop/apps/FOPException.java new file mode 100644 index 000000000..1bbbf051f --- /dev/null +++ b/src/org/apache/fop/apps/FOPException.java @@ -0,0 +1,16 @@ +package org.apache.xml.fop.apps; + +/** + * Exception thrown when FOP has a problem + */ +public class FOPException extends Exception { + + /** + * create a new FOP Exception + * + * @param message descriptive message + */ + public FOPException(String message) { + super(message); + } +} diff --git a/src/org/apache/fop/apps/Makefile b/src/org/apache/fop/apps/Makefile new file mode 100644 index 000000000..f1cd6126a --- /dev/null +++ b/src/org/apache/fop/apps/Makefile @@ -0,0 +1,30 @@ + + +BASEDIR:=../../../../.. +include $(BASEDIR)/Makefile.rules + +SUBDIRS= + +SOURCES=CommandLine.java \ + Driver.java \ + ErrorHandler.java \ + FOPException.java \ + Version.java \ + XTCommandLine.java + + + +CLASSES=$(SOURCES:.java=.class) + +all: $(CLASSES) allsubs + +clean: cleanme cleansubs + +cleanme: + rm -f *.class + +$(TARGETS:%=%subs): %subs : + for dir in $(SUBDIRS) ; do \ + (cd $$dir && pwd && $(MAKE) $(MFLAGS) $*) || exit 1 ; \ + done + diff --git a/src/org/apache/fop/apps/Version.java b/src/org/apache/fop/apps/Version.java new file mode 100644 index 000000000..bcc2c26e0 --- /dev/null +++ b/src/org/apache/fop/apps/Version.java @@ -0,0 +1,18 @@ +package org.apache.xml.fop.apps; + +/** + * class representing the version of FOP. + * + * added at the request of Stefano Mazzocchi for use by Cocoon. + */ +public class Version { + + /** + * get the version of FOP + * + * @return the version string + */ + public static String getVersion() { + return "FOP 0.12.0pre5"; + } +} diff --git a/src/org/apache/fop/apps/XTCommandLine.java b/src/org/apache/fop/apps/XTCommandLine.java new file mode 100644 index 000000000..bd273c45c --- /dev/null +++ b/src/org/apache/fop/apps/XTCommandLine.java @@ -0,0 +1,78 @@ +package org.apache.xml.fop.apps; + +import org.apache.xml.fop.render.pdf.PDFRenderer; +import org.apache.xml.fop.fo.StandardElementMapping; +import org.apache.xml.fop.svg.SVGElementMapping; + +// James Clark +import com.jclark.xsl.sax.XSLProcessor; +import com.jclark.xsl.sax.XSLProcessorImpl; + +// SAX +import org.xml.sax.Parser; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +// Java +import java.io.FileReader; +import java.io.FileWriter; +import java.io.PrintWriter; +import java.io.IOException; +import java.io.FileNotFoundException; +import java.net.URL; + +/** + * mainline class for full transformation (via XT) + formatting/rendering. + * + * gets input, stylesheet and output filenames from the command line + * creates an implementation of XSLProcessor, passing it the stylesheet + * treats XSLProcessor as SAXParser + * + */ +public class XTCommandLine extends CommandLine { + + /** + * mainline method. + * + * first command line argument is XML input file + * second command line argument is XSL stylesheet file + * third command line argument is outputfile + */ + public static void main(String[] args) { + String version = Version.getVersion(); + System.err.println(version); + + if (args.length != 3) { + System.err.println("usage: java org.apache.xml.fop.apps.XTCommandLine xml-file xsl-stylesheet pdf-file"); + System.exit(1); + } + + Parser parser = createParser(); + + if (parser == null) { + System.err.println("ERROR: Unable to create SAX parser"); + System.exit(1); + } + + XSLProcessor xslProcessor = new XSLProcessorImpl(); + xslProcessor.setParser(parser); + + try { + xslProcessor.loadStylesheet(fileInputSource(args[1])); + + Driver driver = new Driver(); + driver.setRenderer("org.apache.xml.fop.render.pdf.PDFRenderer", + version); + driver.addElementMapping("org.apache.xml.fop.fo.StandardElementMapping"); + driver.addElementMapping("org.apache.xml.fop.svg.SVGElementMapping"); + driver.setWriter(new PrintWriter(new FileWriter(args[2]))); + driver.buildFOTree(parser, fileInputSource(args[0])); + driver.format(); + driver.render(); + } catch (Exception e) { + System.err.println("FATAL ERROR: " + e.getMessage()); + System.exit(1); + } + } +} diff --git a/src/org/apache/fop/apps/package.html b/src/org/apache/fop/apps/package.html new file mode 100644 index 000000000..1832867a5 --- /dev/null +++ b/src/org/apache/fop/apps/package.html @@ -0,0 +1,7 @@ +<HTML> +<TITLE>org.apache.xml.fop.apps Package</TITLE> +<BODY> +<P>Application classes used for running FOP both on the command line and +embedded in other applications.</P> +</BODY> +</HTML>
\ No newline at end of file |