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