1 package org.lqsoft.wlml;
5 import javax.xml.transform.*;
6 import javax.xml.transform.sax.SAXResult;
7 import javax.xml.transform.sax.TransformerHandler;
8 import javax.xml.transform.stream.*;
9 import javax.xml.transform.*;
11 import org.apache.fop.fo.ValidationException;
13 import net.sf.saxon.TransformerFactoryImpl;
14 import net.sf.saxon.trans.XPathException;
16 import org.apache.fop.apps.*;
17 import org.xml.sax.SAXException;
22 private Templates wl2fo_tmplt;
23 private Templates norm_tmplt;
24 private FopFactory fop_factory;
25 private TransformerFactoryImpl xfrm_factory;
27 public WL2PDF(String homePath, String fopConfigPath)
28 throws TransformerConfigurationException, SAXException, IOException
30 this.fop_factory = FopFactory.newInstance();
31 this.fop_factory.setUserConfig(new File(fopConfigPath));
33 this.xfrm_factory = new TransformerFactoryImpl();
35 this.wl2fo_tmplt = xfrm_factory.newTemplates( new StreamSource(
36 new File(homePath, "xslt/wl2fo.xslt") ) );
38 this.norm_tmplt = xfrm_factory.newTemplates( new StreamSource(
39 new File(homePath, "xslt/normalize.xslt") ) );
42 public void process(InputStream ins, OutputStream outs)
43 throws FOPException, IOException, TransformerException
45 FOUserAgent agent = this.fop_factory.newFOUserAgent();
46 Fop fop = this.fop_factory.newFop(MimeConstants.MIME_PDF, agent, outs);
48 TransformerHandler normalize_xfrm = this.xfrm_factory.newTransformerHandler(this.norm_tmplt);
49 TransformerHandler wl2fo_xfrm = xfrm_factory.newTransformerHandler(wl2fo_tmplt);
50 normalize_xfrm.setResult(new SAXResult(wl2fo_xfrm));
51 wl2fo_xfrm.setResult(new SAXResult(fop.getDefaultHandler()));
53 ByteArrayOutputStream filtered = new ByteArrayOutputStream();
55 BufferedReader text_input = new BufferedReader( new InputStreamReader(ins) );
58 while( (line = text_input.readLine()) != null )
60 if( line.endsWith("/") ) {
61 filtered.write(line.substring(0, line.length()-1).getBytes());
62 filtered.write("<br />\n".getBytes());
65 filtered.write(line.getBytes());
66 filtered.write("\n".getBytes());
70 ins = new ByteArrayInputStream(filtered.toByteArray());
71 Transformer xfrm = xfrm_factory.newTransformer();
72 xfrm.transform( new StreamSource(ins), new SAXResult(normalize_xfrm));
78 public static void main(String[] args)
80 System.out.println("WLML To PDF converter. Copyright © Łukasz Rekucki under GPLv3 License.");
82 File file = new File(args[1]);
83 String filename = file.getName();
84 String[] base_and_ext = filename.split("\\.", -1);
85 File output = new File(file.getParent(), base_and_ext[0] + ".pdf");
87 System.out.printf("%s -> %s\n", filename, output.getPath());
91 WL2PDF app = new WL2PDF(".", args[0]);
92 InputStream _if = new FileInputStream(file);
93 OutputStream _of = new FileOutputStream(output);
95 app.process(_if, _of);
99 System.out.print("Success");