1 |
|
package org.apache.torque.engine.database.transform; |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
import java.io.BufferedReader; |
23 |
|
import java.io.File; |
24 |
|
import java.io.FileReader; |
25 |
|
import java.io.IOException; |
26 |
|
import java.net.MalformedURLException; |
27 |
|
import java.net.URL; |
28 |
|
import java.util.ArrayList; |
29 |
|
import java.util.List; |
30 |
|
|
31 |
|
import javax.xml.parsers.SAXParser; |
32 |
|
import javax.xml.parsers.SAXParserFactory; |
33 |
|
|
34 |
|
import org.apache.commons.lang.StringUtils; |
35 |
|
import org.apache.commons.logging.Log; |
36 |
|
import org.apache.commons.logging.LogFactory; |
37 |
|
import org.apache.torque.engine.database.model.Column; |
38 |
|
import org.apache.torque.engine.database.model.Database; |
39 |
|
import org.apache.torque.engine.database.model.Table; |
40 |
|
import org.xml.sax.Attributes; |
41 |
|
import org.xml.sax.EntityResolver; |
42 |
|
import org.xml.sax.InputSource; |
43 |
|
import org.xml.sax.SAXException; |
44 |
|
import org.xml.sax.helpers.DefaultHandler; |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
public class XmlToData extends DefaultHandler implements EntityResolver |
57 |
|
{ |
58 |
|
|
59 |
|
private static Log log = LogFactory.getLog(XmlToData.class); |
60 |
|
private Database database; |
61 |
|
private List data; |
62 |
|
private String dtdFileName; |
63 |
|
private File dtdFile; |
64 |
|
private InputSource dataDTD; |
65 |
|
|
66 |
|
private static SAXParserFactory saxFactory; |
67 |
|
|
68 |
|
static |
69 |
|
{ |
70 |
|
saxFactory = SAXParserFactory.newInstance(); |
71 |
|
saxFactory.setValidating(true); |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
public XmlToData(Database database, String dtdFilePath) |
78 |
|
throws MalformedURLException, IOException |
79 |
|
{ |
80 |
|
this.database = database; |
81 |
|
dtdFile = new File(dtdFilePath); |
82 |
|
this.dtdFileName = "file://" + dtdFile.getName(); |
83 |
|
dataDTD = new InputSource(dtdFile.toURL().openStream()); |
84 |
|
} |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
public List parseFile(String xmlFile) |
90 |
|
throws Exception |
91 |
|
{ |
92 |
|
data = new ArrayList(); |
93 |
|
|
94 |
|
SAXParser parser = saxFactory.newSAXParser(); |
95 |
|
|
96 |
|
FileReader fr = new FileReader (xmlFile); |
97 |
|
BufferedReader br = new BufferedReader (fr); |
98 |
|
try |
99 |
|
{ |
100 |
|
InputSource is = new InputSource (br); |
101 |
|
parser.parse(is, this); |
102 |
|
} |
103 |
|
finally |
104 |
|
{ |
105 |
|
br.close(); |
106 |
|
} |
107 |
|
return data; |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
public void startElement(String uri, String localName, String rawName, |
114 |
|
Attributes attributes) |
115 |
|
throws SAXException |
116 |
|
{ |
117 |
|
try |
118 |
|
{ |
119 |
|
if (rawName.equals("dataset")) |
120 |
|
{ |
121 |
|
|
122 |
|
} |
123 |
|
else |
124 |
|
{ |
125 |
|
Table table = database.getTableByJavaName(rawName); |
126 |
|
|
127 |
|
if (table == null) |
128 |
|
{ |
129 |
|
throw new SAXException("Table '" + rawName + "' unknown"); |
130 |
|
} |
131 |
|
List columnValues = new ArrayList(); |
132 |
|
for (int i = 0; i < attributes.getLength(); i++) |
133 |
|
{ |
134 |
|
Column col = table |
135 |
|
.getColumnByJavaName(attributes.getQName(i)); |
136 |
|
|
137 |
|
if (col == null) |
138 |
|
{ |
139 |
|
throw new SAXException("Column " |
140 |
|
+ attributes.getQName(i) + " in table " |
141 |
|
+ rawName + " unknown."); |
142 |
|
} |
143 |
|
|
144 |
|
String value = attributes.getValue(i); |
145 |
|
columnValues.add(new ColumnValue(col, value)); |
146 |
|
} |
147 |
|
data.add(new DataRow(table, columnValues)); |
148 |
|
} |
149 |
|
} |
150 |
|
catch (Exception e) |
151 |
|
{ |
152 |
|
throw new SAXException(e); |
153 |
|
} |
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
public InputSource resolveEntity(String class="keyword">publicId, String systemId) |
162 |
|
throws SAXException |
163 |
|
{ |
164 |
|
try |
165 |
|
{ |
166 |
|
if (dataDTD != null && dtdFileName.equals(systemId)) |
167 |
|
{ |
168 |
|
log.info("Resolver: used " + dtdFile.getPath()); |
169 |
|
return dataDTD; |
170 |
|
} |
171 |
|
else |
172 |
|
{ |
173 |
|
log.info("Resolver: used " + systemId); |
174 |
|
return getInputSource(systemId); |
175 |
|
} |
176 |
|
} |
177 |
|
catch (IOException e) |
178 |
|
{ |
179 |
|
throw new SAXException(e); |
180 |
|
} |
181 |
|
} |
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
public InputSource getInputSource(String urlString) |
190 |
|
throws IOException |
191 |
|
{ |
192 |
|
URL url = new URL(urlString); |
193 |
|
InputSource src = new InputSource(url.openStream()); |
194 |
|
return src; |
195 |
|
} |
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
public class DataRow |
201 |
|
{ |
202 |
|
private Table table; |
203 |
|
private List columnValues; |
204 |
|
|
205 |
|
public DataRow(Table table, List columnValues) |
206 |
0 |
{ |
207 |
0 |
this.table = table; |
208 |
0 |
this.columnValues = columnValues; |
209 |
0 |
} |
210 |
|
|
211 |
|
public Table getTable() |
212 |
|
{ |
213 |
0 |
return table; |
214 |
|
} |
215 |
|
|
216 |
|
public List getColumnValues() |
217 |
|
{ |
218 |
0 |
return columnValues; |
219 |
|
} |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
public class ColumnValue |
226 |
|
{ |
227 |
|
private Column col; |
228 |
|
private String val; |
229 |
|
|
230 |
|
public ColumnValue(Column col, String val) |
231 |
|
{ |
232 |
|
this.col = col; |
233 |
|
this.val = val; |
234 |
|
} |
235 |
|
|
236 |
|
public Column getColumn() |
237 |
|
{ |
238 |
|
return col; |
239 |
|
} |
240 |
|
|
241 |
|
public String getValue() |
242 |
|
{ |
243 |
|
return val; |
244 |
|
} |
245 |
|
|
246 |
|
public String getEscapedValue() |
247 |
|
{ |
248 |
|
StringBuffer sb = new StringBuffer(); |
249 |
|
sb.append("'"); |
250 |
|
sb.append(StringUtils.replace(val, "'", "''")); |
251 |
|
sb.append("'"); |
252 |
|
return sb.toString(); |
253 |
|
} |
254 |
|
} |
255 |
|
} |