Pure Java runner added.
[wl2pdf.git] / jsrc / org / lqsoft / wlml / WL2PDF.java
1 package org.lqsoft.wlml;
2
3 import java.io.*;
4
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.*;
10
11 import org.apache.fop.fo.ValidationException;
12
13 import net.sf.saxon.TransformerFactoryImpl;
14 import net.sf.saxon.trans.XPathException;
15
16 import org.apache.fop.apps.*;
17 import org.xml.sax.SAXException;
18
19 public class WL2PDF 
20 {
21         
22         private Templates wl2fo_tmplt;
23         private Templates norm_tmplt;
24         private FopFactory fop_factory;
25         private TransformerFactoryImpl xfrm_factory;
26         
27         public WL2PDF(String homePath, String fopConfigPath) 
28                 throws TransformerConfigurationException, SAXException, IOException
29         {               
30                 this.fop_factory = FopFactory.newInstance();
31                 this.fop_factory.setUserConfig(new File(fopConfigPath));
32                 
33                 this.xfrm_factory = new TransformerFactoryImpl();
34                                 
35                 this.wl2fo_tmplt = xfrm_factory.newTemplates( new StreamSource(
36                                 new File(homePath, "xslt/wl2fo.xslt") ) );
37                 
38                 this.norm_tmplt = xfrm_factory.newTemplates( new StreamSource(
39                                 new File(homePath, "xslt/normalize.xslt") ) );  
40         }
41         
42         public void process(InputStream ins, OutputStream outs) 
43                 throws FOPException, IOException, TransformerException 
44         {
45                 FOUserAgent agent = this.fop_factory.newFOUserAgent();
46             Fop fop = this.fop_factory.newFop(MimeConstants.MIME_PDF, agent, outs);
47             
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()));
52             
53             ByteArrayOutputStream filtered = new ByteArrayOutputStream();
54             
55             BufferedReader text_input = new BufferedReader( new InputStreamReader(ins) );
56             String line = null;
57
58             while( (line = text_input.readLine()) != null ) 
59             {
60                 if( line.endsWith("/") ) {
61                         filtered.write(line.substring(0, line.length()-1).getBytes());
62                         filtered.write("<br />\n".getBytes());
63                 }
64                 else {
65                         filtered.write(line.getBytes());
66                         filtered.write("\n".getBytes());
67                 }
68             }                   
69
70             ins = new ByteArrayInputStream(filtered.toByteArray());         
71             Transformer xfrm = xfrm_factory.newTransformer();       
72             xfrm.transform( new StreamSource(ins), new SAXResult(normalize_xfrm));              
73         }
74
75         /**
76          * @param args
77          */
78         public static void main(String[] args) 
79         {
80                 System.out.println("WLML To PDF converter. Copyright © Łukasz Rekucki under GPLv3 License.");
81                 
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");
86                 
87                 System.out.printf("%s -> %s\n", filename, output.getPath());
88                                         
89                 try 
90                 {                       
91                         WL2PDF app = new WL2PDF(".", args[0]);
92                         InputStream _if = new FileInputStream(file);
93                         OutputStream _of = new FileOutputStream(output);
94                         
95                         app.process(_if, _of);
96                         
97                         _of.close();
98                         _if.close();
99                         System.out.print("Success");
100                 }
101                 catch(Exception e) {
102                         e.printStackTrace();
103                 }                       
104         }
105
106 }