1 package org.opensync.engine.server.adapter;
2
3 import org.xml.sax.*;
4 import org.xml.sax.helpers.*;
5
6 import java.io.*;
7 import java.util.*;
8
9 /***
10 * Titre : OpenSync
11 * Description : This class is the base class of Xml2Txt and Txt2Xml classes.
12 * These classes translates XML files to text files and text
13 * files to XML files respectively.
14 * The text files may be tag separated values files or fixe size
15 * columns files.
16 * An XML file called descriptor is read before translation to
17 * initialize the fields of the translator.
18 */
19
20 public class Translator extends DefaultHandler {
21
22 private DescriptorParser configParser;
23
24 protected String type = "";
25
26 protected boolean append = false;
27 protected boolean remove_file = false;
28
29 public boolean getAppend() { return append; };
30 public boolean getRemove_file() { return remove_file; };
31
32 protected boolean suppressquotes = false;
33 protected String delimiter = "";
34 protected int skipfirstlines = 0;
35 protected int skiplastlines = 0;
36 protected int nfields = 0;
37
38
39 protected String row_element="ROW";
40 protected String document_element="DATA";
41 protected String trim_columns="no";
42 protected String linebreak="crlf";
43 protected boolean pretty_print = true;
44 protected String starting_offset="0";
45
46 private static final String CR = "\015";
47 private static final String LF = "\012";
48 private static final String CRLF = "\015\012";
49 private static final String BASE_DOCUMENT_ELEMENT = "DATA";
50 private static final String BASE_ROW_ELEMENT = "ROW";
51 private static final String NULL_STRING = "";
52 private static final String PARSER_ERROR = "PARSER.ERROR";
53 protected static final String FATAL = "fatal";
54 protected static final String NONFATAL = "nonfatal";
55 protected static final String SEVERITY = "severity";
56 protected static final String DESCRIPTION = "description";
57 protected static final String UNTERMINATED_ROW = "Unterminated row";
58 protected static final String OFF_WIDTH_ROW ="Off-width row";
59 protected static final String MISSING_LAST_FIELD = "Missing at least the last field";
60 protected static final String LINE_NUMBER = "line.number";
61 private static final String CDATA = "CDATA";
62 private static final Attributes EMPTY_ATT_LIST = new AttributesImpl();
63
64 protected boolean offWidthOK = false;
65 protected boolean trimFields = true;
66 protected String lineBreakMarker = CR;
67 protected String documentElementTag = BASE_DOCUMENT_ELEMENT;
68 protected String rowElementTag = BASE_ROW_ELEMENT;
69
70 protected int startingOffset = -1;
71 protected boolean prettyPrint = false;
72
73 protected boolean toCSV = false;
74
75 protected List colNames = null;
76
77 public List fields = null;
78
79 protected ArrayList startColumns;
80
81 protected ArrayList endColumns;
82
83 public Translator() {
84 }
85
86 public static String checkSpecialCharXml(String s) {
87 s = checkSpecialChar(s, '&', "&");
88 s = checkSpecialChar(s, '"', """);
89 s = checkSpecialChar(s, '<', "<");
90 s = checkSpecialChar(s, '>', ">");
91 s = checkSpecialChar(s,'\'', "'");
92
93 return s;
94 }
95
96 public static String checkSpecialChar(String in, char c, String replace ) {
97
98 if ( in==null || in.equals("") ) return "";
99
100 StringBuffer out;
101 String s1;
102 int i,j;
103 boolean found;
104
105 i = -1;
106 j = 0;
107 found = false;
108 out = new StringBuffer();
109 while ((i=in.indexOf(c,i+1)) != -1) {
110 found = true;
111 s1 = in.substring(j,i);
112 out.append(s1);
113 out.append(replace);
114 j = i+1;
115 }
116 if (found==true) {
117 s1 = in.substring(j);
118 out.append(s1);
119 in = out.toString();
120 }
121
122 return in;
123 }
124
125 protected long linesCounter(Reader input) throws IOException {
126 long cnt = 0;
127 BufferedReader br = new BufferedReader(input);
128 while (br.readLine() != null) {
129 cnt++;
130 }
131 return cnt;
132 }
133 }