View Javadoc
1   package net.sf.cobol2j.examples;
2   
3   import net.sf.cobol2j.FieldFormat;
4   import net.sf.cobol2j.FieldsGroup;
5   import net.sf.cobol2j.FieldsList;
6   import net.sf.cobol2j.FileFormat;
7   import net.sf.cobol2j.FileFormatException;
8   import net.sf.cobol2j.RecordFormat;
9   import net.sf.cobol2j.RecordParseException;
10  import net.sf.cobol2j.RecordSet;
11  import net.sf.cobol2j.RecordsMap;
12  
13  import org.apache.xml.serialize.OutputFormat;
14  import org.apache.xml.serialize.XMLSerializer;
15  
16  import org.w3c.dom.Document;
17  import org.w3c.dom.Element;
18  import org.w3c.dom.Node;
19  import org.w3c.dom.bootstrap.DOMImplementationRegistry;
20  
21  import java.io.FileInputStream;
22  import java.io.FileNotFoundException;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  
30  import javax.xml.bind.JAXBContext;
31  import javax.xml.bind.JAXBException;
32  import javax.xml.bind.Unmarshaller;
33  
34  
35  public class Dat2xml {
36      public static void main(String[] args)
37          throws FileNotFoundException, IOException, ClassNotFoundException,
38              InstantiationException, IllegalAccessException, FileFormatException,
39              JAXBException, RecordParseException {
40          String xc2jfilename = args[0];
41          JAXBContext context = JAXBContext.newInstance("net.sf.cobol2j");
42          Unmarshaller unmarshaller = context.createUnmarshaller();
43          Object o = unmarshaller.unmarshal(new FileInputStream(xc2jfilename));
44          FileFormat fF = (FileFormat) o;
45          RecordSet rset = new RecordSet(System.in, fF);
46          Document doc = DOMImplementationRegistry.newInstance()
47                                                  .getDOMImplementation("Core")
48                                                  .createDocument(null,
49                  "Dat2Xml", null);
50          Element docelement = doc.getDocumentElement();
51          Map recdefs = new RecordsMap(fF);
52  
53          while (rset.hasNext()) {
54              List fields = rset.next();
55              RecordFormat rF = (RecordFormat) recdefs.get("0");
56  
57              if (recdefs.size() > 1) {
58                  String first1 = fields.get(0).toString();
59                  rF = (RecordFormat) recdefs.get(first1);
60              }
61  
62              Element recelement = doc.createElement(rF.getCobolRecordName());
63              Iterator data = fields.listIterator();
64              createFields(data, rF, recelement);
65              docelement.appendChild(recelement);
66          }
67  
68          // Still ( 2006-01-27 ) the prettiest ( Xerces ) serialization
69          // Have to wait for inmplementation independence
70          OutputFormat format = new OutputFormat(doc);
71          format.setLineWidth(120);
72          format.setIndenting(true);
73          format.setIndent(2);
74  
75          XMLSerializer serializer = new XMLSerializer(System.out, format);
76          serializer.serialize(doc);
77      }
78  
79      private static void createFields(Iterator data, FieldsList recformat,
80          Element parentelement) {
81          Iterator def = recformat.getFieldFormatOrFieldsGroup().listIterator();
82  
83          while (def.hasNext()) {
84              Object u = def.next();
85  
86              if (u instanceof FieldFormat) {
87                  int occurs = 1;
88                  FieldFormat fF = (FieldFormat) u;
89                  String dependingon = fF.getDependingOn();
90  
91                  if (dependingon.length() > 0) {
92                      Node previous = parentelement.getLastChild()
93                                                   .getPreviousSibling();
94  
95                      while (previous != null) {
96                          if (previous.getNodeName() == dependingon) {
97                              Element e = (Element) previous;
98                              occurs = (Integer.parseInt(previous.getTextContent()));
99  
100                             break;
101                         } else {
102                             previous = previous.getPreviousSibling();
103                         }
104                     }
105                 } else {
106                     occurs = fF.getOccurs().intValue();
107                 }
108 
109                 while (occurs-- > 0) {
110                     Object o = data.next();
111                     Element fieldelement = parentelement.getOwnerDocument()
112                                                         .createElement(fF.getName());
113                     fieldelement.setTextContent(o.toString());
114                     parentelement.appendChild(fieldelement);
115                 }
116             } else if (u instanceof FieldsGroup) {
117                 int occurs = 1;
118                 FieldsGroup fG = (FieldsGroup) u;
119                 String dependingon = fG.getDependingOn();
120 
121                 if (dependingon.length() > 0) {
122                     Node previous = parentelement.getLastChild()
123                                                  .getPreviousSibling();
124 
125                     while (previous != null) {
126                         if (previous.getNodeName() == dependingon) {
127                             Element e = (Element) previous;
128                             occurs = (Integer.parseInt(previous.getTextContent()));
129 
130                             break;
131                         } else {
132                             previous = previous.getPreviousSibling();
133                         }
134                     }
135                 } else {
136                     occurs = fG.getOccurs().intValue();
137                 }
138 
139                 while (occurs-- > 0) {
140                     Element groupelement = parentelement.getOwnerDocument()
141                                                         .createElement(fG.getName());
142                     createFields(data, fG, groupelement);
143                     parentelement.appendChild(groupelement);
144                 }
145             }
146         }
147     }
148 }