From: Lukasz Rekucki Date: Thu, 14 Jan 2010 22:28:48 +0000 (+0100) Subject: Pure Java runner added. X-Git-Url: https://git.mdrn.pl/wl2pdf.git/commitdiff_plain/aa5c31831c1052ad02ca99ca6533bc18c6b8a3cb?ds=sidebyside Pure Java runner added. --- diff --git a/README b/README index 250e23f..47b3d98 100755 --- a/README +++ b/README @@ -30,13 +30,34 @@ - Apache FOP (at least version 0.95, trunk is recommended) - - Hyphenation is done via FOP, so read a section on it in their docs. + - XSLT 2.0 processor: Saxon9 + + - Hyphenation is done via FOP, so read a section on it in their docs. + + - Some unicode-capable font, like DejaVu. + + + CONFIGURING + ----------- + + 1) Compile FOP if needed. + 2) Generate font metrics (refer to FOP Documentation on that) + 3) Make a "fop-config.xml" out of the supplied example and place it in this directory. - - Some unicode-capable font, like DejaVu. - + + RUNNING + ------- + + With Jython: + + jython wl2pdf + + With Java: + java org.lqsoft.wlml.WL2PDF - INSTALLING - ---------- + (Rember to place FOP, it's libraries and Saxon under your classpath). + + \ No newline at end of file diff --git a/jsrc/org/lqsoft/wlml/WL2PDF.java b/jsrc/org/lqsoft/wlml/WL2PDF.java new file mode 100755 index 0000000..7e84df6 --- /dev/null +++ b/jsrc/org/lqsoft/wlml/WL2PDF.java @@ -0,0 +1,106 @@ +package org.lqsoft.wlml; + +import java.io.*; + +import javax.xml.transform.*; +import javax.xml.transform.sax.SAXResult; +import javax.xml.transform.sax.TransformerHandler; +import javax.xml.transform.stream.*; +import javax.xml.transform.*; + +import org.apache.fop.fo.ValidationException; + +import net.sf.saxon.TransformerFactoryImpl; +import net.sf.saxon.trans.XPathException; + +import org.apache.fop.apps.*; +import org.xml.sax.SAXException; + +public class WL2PDF +{ + + private Templates wl2fo_tmplt; + private Templates norm_tmplt; + private FopFactory fop_factory; + private TransformerFactoryImpl xfrm_factory; + + public WL2PDF(String homePath, String fopConfigPath) + throws TransformerConfigurationException, SAXException, IOException + { + this.fop_factory = FopFactory.newInstance(); + this.fop_factory.setUserConfig(new File(fopConfigPath)); + + this.xfrm_factory = new TransformerFactoryImpl(); + + this.wl2fo_tmplt = xfrm_factory.newTemplates( new StreamSource( + new File(homePath, "xslt/wl2fo.xslt") ) ); + + this.norm_tmplt = xfrm_factory.newTemplates( new StreamSource( + new File(homePath, "xslt/normalize.xslt") ) ); + } + + public void process(InputStream ins, OutputStream outs) + throws FOPException, IOException, TransformerException + { + FOUserAgent agent = this.fop_factory.newFOUserAgent(); + Fop fop = this.fop_factory.newFop(MimeConstants.MIME_PDF, agent, outs); + + TransformerHandler normalize_xfrm = this.xfrm_factory.newTransformerHandler(this.norm_tmplt); + TransformerHandler wl2fo_xfrm = xfrm_factory.newTransformerHandler(wl2fo_tmplt); + normalize_xfrm.setResult(new SAXResult(wl2fo_xfrm)); + wl2fo_xfrm.setResult(new SAXResult(fop.getDefaultHandler())); + + ByteArrayOutputStream filtered = new ByteArrayOutputStream(); + + BufferedReader text_input = new BufferedReader( new InputStreamReader(ins) ); + String line = null; + + while( (line = text_input.readLine()) != null ) + { + if( line.endsWith("/") ) { + filtered.write(line.substring(0, line.length()-1).getBytes()); + filtered.write("
\n".getBytes()); + } + else { + filtered.write(line.getBytes()); + filtered.write("\n".getBytes()); + } + } + + ins = new ByteArrayInputStream(filtered.toByteArray()); + Transformer xfrm = xfrm_factory.newTransformer(); + xfrm.transform( new StreamSource(ins), new SAXResult(normalize_xfrm)); + } + + /** + * @param args + */ + public static void main(String[] args) + { + System.out.println("WLML To PDF converter. Copyright © Łukasz Rekucki under GPLv3 License."); + + File file = new File(args[1]); + String filename = file.getName(); + String[] base_and_ext = filename.split("\\.", -1); + File output = new File(file.getParent(), base_and_ext[0] + ".pdf"); + + System.out.printf("%s -> %s\n", filename, output.getPath()); + + try + { + WL2PDF app = new WL2PDF(".", args[0]); + InputStream _if = new FileInputStream(file); + OutputStream _of = new FileOutputStream(output); + + app.process(_if, _of); + + _of.close(); + _if.close(); + System.out.print("Success"); + } + catch(Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/wl2pdf.py b/wl2pdf.py index 4373adf..e193b4d 100755 --- a/wl2pdf.py +++ b/wl2pdf.py @@ -39,8 +39,7 @@ from net.sf.saxon.trans import XPathException from org.apache.fop.apps import *; -RUNTIME_PATH = os.path.abspath(os.path.join(\ - os.path.dirname(__file__), '..', 'runtime')) +RUNTIME_PATH = os.path.abspath(os.path.dirname(__file__)) CONFIG_PATH = os.path.join(RUNTIME_PATH, 'fop-config.xml') @@ -110,7 +109,7 @@ def print_usage(): Usage: book2pdf.py file [file...]""" if __name__ == '__main__': - print "WLML To PDF converter (c) 2009 Łukasz Rekucki. GPLv3 License." + print "WLML To PDF converter. Copyright © Łukasz Rekucki under GPLv3 License." if len(sys.argv) == 1: print_usage()