View Javadoc
1   package net.sf.cobol2j.examples;
2   
3   import net.sf.cb2xml.CobolPreprocessor;
4   import net.sf.cb2xml.CopyBookAnalyzer;
5   import net.sf.cb2xml.DebugLexer;
6   import net.sf.cb2xml.sablecc.lexer.Lexer;
7   import net.sf.cb2xml.sablecc.node.Start;
8   import net.sf.cb2xml.sablecc.parser.Parser;
9   import net.sf.cb2xml.sablecc.parser.ParserException;
10  import net.sf.cb2xml.util.XmlUtils;
11  
12  import org.apache.xml.serialize.OutputFormat;
13  import org.apache.xml.serialize.XMLSerializer;
14  
15  import org.w3c.dom.Document;
16  
17  import java.io.File;
18  import java.io.FileOutputStream;
19  import java.io.PushbackReader;
20  import java.io.StringReader;
21  
22  import javax.xml.bind.JAXBContext;
23  import javax.xml.bind.Marshaller;
24  import javax.xml.parsers.DocumentBuilder;
25  import javax.xml.parsers.DocumentBuilderFactory;
26  import javax.xml.transform.Transformer;
27  import javax.xml.transform.TransformerFactory;
28  import javax.xml.transform.dom.DOMResult;
29  import javax.xml.transform.dom.DOMSource;
30  import javax.xml.transform.stream.StreamSource;
31  
32  
33  public class Cb2xc2j {
34      private static final String transletname = "cb2xml2cobol2j";
35  
36      public static void main(String[] args) throws Exception {
37          // COBOL copybook file to CB2XML xml document
38          String preProcessed = null;
39          Document cb2xmldoc = null;
40          Lexer lexer = null;
41          File file = new File(args[0]);
42          preProcessed = CobolPreprocessor.preProcess(file);
43  
44          StringReader sr = new StringReader(preProcessed);
45          PushbackReader pbr = new PushbackReader(sr, 1000);
46          lexer = new Lexer(pbr);
47  
48          Parser parser = new Parser(lexer);
49          Start ast = parser.parse();
50          CopyBookAnalyzer copyBookAnalyzer = new CopyBookAnalyzer(file.getName(),
51                  parser);
52          ast.apply(copyBookAnalyzer);
53          cb2xmldoc = copyBookAnalyzer.getDocument();
54  
55          // CB2XML xml document to COBOL2J xc2j document conversion
56          System.setProperty("javax.xml.transform.TransformerFactory",
57              "org.apache.xalan.xsltc.trax.TransformerFactoryImpl");
58  
59          TransformerFactory xformFactory = TransformerFactory.newInstance();
60          xformFactory.setAttribute("use-classpath", Boolean.TRUE);
61          xformFactory.setAttribute("package-name", "net.sf.cobol2j.translets");
62  
63          Transformer transformer = xformFactory.newTransformer(new StreamSource(
64                      transletname));
65          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
66          DocumentBuilder docbuilder = factory.newDocumentBuilder();
67          Document xc2jdoc = docbuilder.newDocument();
68          DOMResult res = new DOMResult(xc2jdoc);
69          transformer.transform(new DOMSource(cb2xmldoc), res);
70  
71          // COBOL2J xc2j document serialization
72          OutputFormat format = new OutputFormat(xc2jdoc);
73          format.setLineWidth(120);
74          format.setIndenting(true);
75          format.setIndent(2);
76  
77          XMLSerializer serializer = new XMLSerializer(System.out, format);
78          serializer.serialize(xc2jdoc);
79      }
80  }