View Javadoc

1   package org.opensync.engine.server.adapter;
2   
3   import java.io.File;
4   import java.io.FileNotFoundException;
5   import java.io.IOException;
6   import java.io.StringReader;
7   
8   import javax.xml.parsers.ParserConfigurationException;
9   import javax.xml.parsers.SAXParser;
10  import javax.xml.parsers.SAXParserFactory;
11  
12  import org.dom4j.DocumentException;
13  import org.opensync.engine.util.FileHelper;
14  import org.xml.sax.Attributes;
15  import org.xml.sax.InputSource;
16  import org.xml.sax.SAXException;
17  
18  /***
19   * Title:         OpenSync
20   * Description:   This class implements the translation of XML files into
21   *                text files. The targeted text files may be tag separated values
22   *                or fixed size columns files.
23   *                A descriptor file describes the targeted text files.
24   */
25  
26  public class Xml2Txt extends Translator {
27  
28    /****/
29    public final static String NEWLINE = "\n";
30  
31    private DescriptorParser descriptorParser;
32    private StringBuffer elementcontent;
33    private StringBuffer destination;
34    private int fieldcounter;
35    private boolean inleaf;
36    int offset_size;
37  
38    private int nbRow = 0;
39  
40  
41  
42    /***
43     * @param	args
44     */
45    public static void main(String args[]) {
46      try {
47        if (args.length != 4) {
48          System.out.println("java Xml2Txt ['csv' | 'fixed'] [descriptor] [source] [destination]");
49          System.exit(1);
50        }
51  
52        Xml2Txt xmlAdaptor = new Xml2Txt();
53  
54        if (args[0].equals("csv"))
55          xmlAdaptor.toCSV = true;
56        else if (args[0].equals("fixed"))
57          xmlAdaptor.toCSV = false;
58        else {
59          System.out.println("java Xml2Txt ['csv' | 'fixed'] [descriptor] [source] [destination]");
60          System.out.println("-------------------^ first argument must be csv or fixed");
61          System.exit(1);
62        }
63  
64        xmlAdaptor.readDescriptor(args[1]);
65        xmlAdaptor.parseFile(args[2], args[3]);
66      }
67      catch (Exception ex) {
68        ex.printStackTrace();
69      }
70    }
71  
72    /***
73     * @param	descriptorFileName
74     * @exception	IOException
75     * @exception	FileNotFoundException
76     * @exception	DocumentException
77     * @exception	SAXException
78     * @exception	ParserConfigurationException
79     */
80    public void readDescriptor(String descriptorFileName)
81    throws ParserConfigurationException, SAXException,FileNotFoundException,IOException{
82      descriptorParser = new DescriptorParser(this);
83  
84      SAXParserFactory factory = SAXParserFactory.newInstance();
85      factory.setValidating(true);
86      SAXParser parser = factory.newSAXParser();
87      parser.parse( new InputSource(new File(descriptorFileName).toURL().toExternalForm()),
88                    descriptorParser);
89    }
90  
91    /***
92     * @param	sourceFileName
93     * @param	destinationFileName
94     * @exception	IOException
95     * @exception	SAXException
96     * @exception	ParserConfigurationException
97     */
98    public void parseFile(String sourceFileName, String destinationFileName)
99    throws ParserConfigurationException,SAXException,IOException{
100     destination = new StringBuffer();
101 
102     SAXParserFactory factory = SAXParserFactory.newInstance();
103     factory.setValidating(true);
104     SAXParser parser = factory.newSAXParser();
105     parser.parse("file:///" + sourceFileName, this);
106 
107     String output = destination.substring(0, destination.length() - 1);
108     output = output.replace('\u0000', ' ');
109     saveString(destinationFileName, output);
110   }
111 
112   /***
113    * @param	xml
114    * @exception	IOException
115    * @exception	SAXException
116    * @exception	ParserConfigurationException
117    */
118   public String parseXml(String xml)throws ParserConfigurationException,SAXException,IOException {
119     destination = new StringBuffer();
120 
121     SAXParserFactory factory = SAXParserFactory.newInstance();
122     factory.setValidating(true);
123     SAXParser parser = factory.newSAXParser();
124     parser.parse(new InputSource(new StringReader(xml)), this);
125 
126     String output;
127     if (destination.length() > 0) {
128       output = destination.substring(0, destination.length() - 1);
129       output = output.replace('\u0000', ' ');
130     } else
131       output = "";
132     return output;
133   }
134 
135   // DocumentHandler methods
136 
137   /****/
138   public void startDocument() {
139     fieldcounter = 0;
140     offset_size = 0;
141   }
142 
143   /***
144    * @param	uri
145    * @param	local
146    * @param	qname
147    * @param	attrs
148    */
149   public void startElement(String uri, String local, String qname, Attributes attrs) {
150     elementcontent = new StringBuffer();
151     inleaf = true;
152   }
153 
154   /***
155    * @param	ch
156    * @param	start
157    * @param	length
158    */
159   public void characters(char ch[], int start, int length) {
160     elementcontent.append(ch, start, length);
161   }
162 
163 
164   /***
165    * @param	uri
166    * @param	local
167    * @param	qname
168    */
169   public void endElement(String uri, String local, String qname) {
170     if (inleaf) {
171       if(qname.equalsIgnoreCase("ROW")) {
172         nbRow ++;
173       }
174       if (toCSV) {
175         if (++fieldcounter % nfields == 0) {
176           destination.append(elementcontent);
177           destination.append(this.lineBreakMarker);
178         }
179         else{
180           destination.append(elementcontent);
181           destination.append(delimiter);
182         }
183       }
184       else {
185         fieldcounter++;
186         Integer sColumns = (Integer) startColumns.get(fieldcounter - 1);
187         String insertedElement = elementcontent.toString();
188         if (suppressquotes) {
189           insertedElement = (elementcontent.toString()).replace('\'','\u0000');
190         }
191         if (fieldcounter % nfields == 0) {
192           destination.append(this.lineBreakMarker);
193           destination.insert(offset_size + sColumns.intValue()-1, insertedElement);
194           offset_size = destination.length();
195         }
196         else {
197           Integer next_eColumns = (Integer) endColumns.get(fieldcounter);
198           destination.setLength(offset_size + next_eColumns.intValue());
199           destination.insert(offset_size + sColumns.intValue()-1, insertedElement);
200         }
201         fieldcounter = fieldcounter % nfields;
202       }
203       inleaf = false;
204     }
205   }
206 
207 
208   /****/
209   public void endDocument() {
210   }
211 
212   /***
213    * @param	filename
214    * @param	string
215    * @exception	IOException
216    */
217   public void saveString(String filename, String string)throws IOException {
218     FileHelper.stringToFile(string,filename);
219   }
220 
221   public int getNbRow() {
222     return nbRow;
223   }
224 }