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.BufferedInputStream; |
23 |
|
import java.io.File; |
24 |
|
import java.io.FileInputStream; |
25 |
|
import java.io.FileNotFoundException; |
26 |
|
import java.util.Stack; |
27 |
|
import java.util.Vector; |
28 |
|
|
29 |
|
import javax.xml.parsers.SAXParser; |
30 |
|
import javax.xml.parsers.SAXParserFactory; |
31 |
|
|
32 |
|
import org.apache.commons.logging.Log; |
33 |
|
import org.apache.commons.logging.LogFactory; |
34 |
|
import org.apache.torque.engine.EngineException; |
35 |
|
import org.apache.torque.engine.database.model.Column; |
36 |
|
import org.apache.torque.engine.database.model.Database; |
37 |
|
import org.apache.torque.engine.database.model.Domain; |
38 |
|
import org.apache.torque.engine.database.model.ForeignKey; |
39 |
|
import org.apache.torque.engine.database.model.Index; |
40 |
|
import org.apache.torque.engine.database.model.Table; |
41 |
|
import org.apache.torque.engine.database.model.Unique; |
42 |
|
import org.kuali.core.db.torque.DatabaseParser; |
43 |
|
import org.xml.sax.Attributes; |
44 |
|
import org.xml.sax.InputSource; |
45 |
|
import org.xml.sax.SAXException; |
46 |
|
import org.xml.sax.SAXParseException; |
47 |
|
import org.xml.sax.helpers.DefaultHandler; |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
@author |
54 |
|
@author |
55 |
|
@author |
56 |
|
@author |
57 |
|
@author |
58 |
|
@author |
59 |
|
@version |
60 |
|
|
|
|
| 0% |
Uncovered Elements: 191 (191) |
Complexity: 50 |
Complexity Density: 0.44 |
|
61 |
|
public class XmlToAppData extends DefaultHandler implements DatabaseParser |
62 |
|
{ |
63 |
|
|
64 |
|
private static Log log = LogFactory.getLog(XmlToAppData.class); |
65 |
|
|
66 |
|
private Database database; |
67 |
|
private Table currTable; |
68 |
|
private Column currColumn; |
69 |
|
private ForeignKey currFK; |
70 |
|
private Index currIndex; |
71 |
|
private Unique currUnique; |
72 |
|
|
73 |
|
private boolean firstPass; |
74 |
|
private boolean isExternalSchema; |
75 |
|
private String currentPackage; |
76 |
|
private String currentXmlFile; |
77 |
|
private String defaultPackage; |
78 |
|
|
79 |
|
private static SAXParserFactory saxFactory; |
80 |
|
|
81 |
|
|
82 |
|
private Vector alreadyReadFiles; |
83 |
|
|
84 |
|
|
85 |
|
private Stack parsingStack = new Stack(); |
86 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
87 |
0
|
static... |
88 |
|
{ |
89 |
0
|
saxFactory = SAXParserFactory.newInstance(); |
90 |
0
|
saxFactory.setValidating(true); |
91 |
|
} |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
@param |
97 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
98 |
0
|
public XmlToAppData(String databaseType)... |
99 |
|
{ |
100 |
0
|
database = new Database(databaseType); |
101 |
0
|
firstPass = true; |
102 |
|
} |
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
@param |
108 |
|
@param |
109 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
110 |
0
|
public XmlToAppData(String databaseType, String defaultPackage)... |
111 |
|
{ |
112 |
0
|
database = new Database(databaseType); |
113 |
0
|
this.defaultPackage = defaultPackage; |
114 |
0
|
firstPass = true; |
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
@param |
122 |
|
@return |
123 |
|
|
|
|
| 0% |
Uncovered Elements: 35 (35) |
Complexity: 9 |
Complexity Density: 0.33 |
|
124 |
0
|
public Database parseResource(String xmlFile)... |
125 |
|
throws EngineException |
126 |
|
{ |
127 |
0
|
try |
128 |
|
{ |
129 |
|
|
130 |
0
|
if (!firstPass) |
131 |
|
{ |
132 |
0
|
throw new Error("No more double pass"); |
133 |
|
} |
134 |
|
|
135 |
0
|
if ((alreadyReadFiles != null) |
136 |
|
&& alreadyReadFiles.contains(xmlFile)) |
137 |
|
{ |
138 |
0
|
return database; |
139 |
|
} |
140 |
0
|
else if (alreadyReadFiles == null) |
141 |
|
{ |
142 |
0
|
alreadyReadFiles = new Vector(3, 1); |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
0
|
alreadyReadFiles.add(xmlFile); |
147 |
|
|
148 |
0
|
currentXmlFile = xmlFile; |
149 |
|
|
150 |
0
|
SAXParser parser = saxFactory.newSAXParser(); |
151 |
|
|
152 |
0
|
FileInputStream fileInputStream = null; |
153 |
0
|
try |
154 |
|
{ |
155 |
0
|
fileInputStream = new FileInputStream(xmlFile); |
156 |
|
} |
157 |
|
catch (FileNotFoundException fnfe) |
158 |
|
{ |
159 |
0
|
throw new FileNotFoundException |
160 |
|
(new File(xmlFile).getAbsolutePath()); |
161 |
|
} |
162 |
0
|
BufferedInputStream bufferedInputStream |
163 |
|
= new BufferedInputStream(fileInputStream); |
164 |
0
|
try |
165 |
|
{ |
166 |
0
|
log.info("Parsing file: '" |
167 |
|
+ (new File(xmlFile)).getName() + "'"); |
168 |
0
|
InputSource is = new InputSource(bufferedInputStream); |
169 |
0
|
is.setSystemId( new File( xmlFile ).getAbsolutePath() ); |
170 |
0
|
parser.parse(is, this); |
171 |
|
} |
172 |
|
finally |
173 |
|
{ |
174 |
0
|
bufferedInputStream.close(); |
175 |
|
} |
176 |
|
} |
177 |
|
catch (SAXParseException e) |
178 |
|
{ |
179 |
0
|
throw new EngineException("Sax error on line " |
180 |
|
+ e.getLineNumber() |
181 |
|
+ " column " |
182 |
|
+ e.getColumnNumber() |
183 |
|
+ " : " |
184 |
|
+ e.getMessage(), |
185 |
|
e); |
186 |
|
} |
187 |
|
catch (Exception e) |
188 |
|
{ |
189 |
0
|
throw new EngineException(e); |
190 |
|
} |
191 |
0
|
if (!isExternalSchema) |
192 |
|
{ |
193 |
0
|
firstPass = false; |
194 |
|
} |
195 |
0
|
database.doFinalInitialization(); |
196 |
0
|
return database; |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
@param |
203 |
|
@param |
204 |
|
@return |
205 |
|
@see |
206 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
207 |
0
|
public InputSource resolveEntity(String publicId, String systemId)... |
208 |
|
throws SAXException |
209 |
|
{ |
210 |
0
|
try |
211 |
|
{ |
212 |
0
|
return new DTDResolver().resolveEntity(publicId, systemId); |
213 |
|
} |
214 |
|
catch (Exception e) |
215 |
|
{ |
216 |
0
|
throw new SAXException(e); |
217 |
|
} |
218 |
|
} |
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
@param |
224 |
|
@param |
225 |
|
|
226 |
|
@param |
227 |
|
|
228 |
|
@param |
229 |
|
|
|
|
| 0% |
Uncovered Elements: 86 (86) |
Complexity: 21 |
Complexity Density: 0.44 |
|
230 |
0
|
public void startElement(String uri, String localName, String rawName,... |
231 |
|
Attributes attributes) |
232 |
|
throws SAXException |
233 |
|
{ |
234 |
0
|
try |
235 |
|
{ |
236 |
0
|
if (rawName.equals("database")) |
237 |
|
{ |
238 |
0
|
if (isExternalSchema) |
239 |
|
{ |
240 |
0
|
currentPackage = attributes.getValue("package"); |
241 |
0
|
if (currentPackage == null) |
242 |
|
{ |
243 |
0
|
currentPackage = defaultPackage; |
244 |
|
} |
245 |
|
} |
246 |
|
else |
247 |
|
{ |
248 |
0
|
database.loadFromXML(attributes); |
249 |
0
|
if (database.getPackage() == null) |
250 |
|
{ |
251 |
0
|
database.setPackage(defaultPackage); |
252 |
|
} |
253 |
|
} |
254 |
|
} |
255 |
0
|
else if (rawName.equals("external-schema")) |
256 |
|
{ |
257 |
0
|
String xmlFile = attributes.getValue("filename"); |
258 |
0
|
if (xmlFile.charAt(0) != '/') |
259 |
|
{ |
260 |
0
|
File f = new File(currentXmlFile); |
261 |
0
|
xmlFile = new File(f.getParent(), xmlFile).getPath(); |
262 |
|
} |
263 |
|
|
264 |
|
|
265 |
0
|
ParseStackElement.pushState(this); |
266 |
|
|
267 |
0
|
isExternalSchema = true; |
268 |
|
|
269 |
0
|
parseResource(xmlFile); |
270 |
|
|
271 |
0
|
ParseStackElement.popState(this); |
272 |
|
} |
273 |
0
|
else if (rawName.equals("domain")) |
274 |
|
{ |
275 |
0
|
Domain domain = new Domain(); |
276 |
0
|
domain.loadFromXML(attributes, database.getPlatform()); |
277 |
0
|
database.addDomain(domain); |
278 |
|
} |
279 |
0
|
else if (rawName.equals("table")) |
280 |
|
{ |
281 |
0
|
currTable = database.addTable(attributes); |
282 |
0
|
if (isExternalSchema) |
283 |
|
{ |
284 |
0
|
currTable.setForReferenceOnly(true); |
285 |
0
|
currTable.setPackage(currentPackage); |
286 |
|
} |
287 |
|
} |
288 |
0
|
else if (rawName.equals("column")) |
289 |
|
{ |
290 |
0
|
currColumn = currTable.addColumn(attributes); |
291 |
|
} |
292 |
0
|
else if (rawName.equals("inheritance")) |
293 |
|
{ |
294 |
0
|
currColumn.addInheritance(attributes); |
295 |
|
} |
296 |
0
|
else if (rawName.equals("foreign-key")) |
297 |
|
{ |
298 |
0
|
currFK = currTable.addForeignKey(attributes); |
299 |
|
} |
300 |
0
|
else if (rawName.equals("reference")) |
301 |
|
{ |
302 |
0
|
currFK.addReference(attributes); |
303 |
|
} |
304 |
0
|
else if (rawName.equals("index")) |
305 |
|
{ |
306 |
0
|
currIndex = currTable.addIndex(attributes); |
307 |
|
} |
308 |
0
|
else if (rawName.equals("index-column")) |
309 |
|
{ |
310 |
0
|
currIndex.addColumn(attributes); |
311 |
|
} |
312 |
0
|
else if (rawName.equals("unique")) |
313 |
|
{ |
314 |
0
|
currUnique = currTable.addUnique(attributes); |
315 |
|
} |
316 |
0
|
else if (rawName.equals("unique-column")) |
317 |
|
{ |
318 |
0
|
currUnique.addColumn(attributes); |
319 |
|
} |
320 |
0
|
else if (rawName.equals("id-method-parameter")) |
321 |
|
{ |
322 |
0
|
currTable.addIdMethodParameter(attributes); |
323 |
|
} |
324 |
0
|
else if (rawName.equals("option")) |
325 |
|
{ |
326 |
0
|
setOption(attributes); |
327 |
|
} |
328 |
|
} |
329 |
|
catch (Exception e) |
330 |
|
{ |
331 |
0
|
throw new SAXException(e); |
332 |
|
} |
333 |
|
} |
334 |
|
|
335 |
|
|
336 |
|
|
337 |
|
|
338 |
|
@param |
339 |
|
@param |
340 |
|
|
341 |
|
@param |
342 |
|
|
343 |
|
|
|
|
| 0% |
Uncovered Elements: 26 (26) |
Complexity: 8 |
Complexity Density: 0.57 |
|
344 |
0
|
public void endElement(String uri, String localName, String rawName)... |
345 |
|
throws SAXException |
346 |
|
{ |
347 |
0
|
if (log.isDebugEnabled()) |
348 |
|
{ |
349 |
0
|
log.debug("endElement(" + uri + ", " + localName + ", " |
350 |
|
+ rawName + ") called"); |
351 |
|
} |
352 |
0
|
try |
353 |
|
{ |
354 |
|
|
355 |
|
|
356 |
0
|
if (rawName.equals("table")) |
357 |
|
{ |
358 |
0
|
currTable = null; |
359 |
|
} |
360 |
0
|
else if (rawName.equals("column")) |
361 |
|
{ |
362 |
0
|
currColumn = null; |
363 |
|
} |
364 |
0
|
else if (rawName.equals("foreign-key")) |
365 |
|
{ |
366 |
0
|
currFK = null; |
367 |
|
} |
368 |
0
|
else if (rawName.equals("index")) |
369 |
|
{ |
370 |
0
|
currIndex = null; |
371 |
|
} |
372 |
0
|
else if (rawName.equals("unique")) |
373 |
|
{ |
374 |
0
|
currUnique = null; |
375 |
|
} |
376 |
|
} |
377 |
|
catch (Exception e) |
378 |
|
{ |
379 |
0
|
throw new SAXException(e); |
380 |
|
} |
381 |
|
} |
382 |
|
|
|
|
| 0% |
Uncovered Elements: 23 (23) |
Complexity: 6 |
Complexity Density: 0.46 |
|
383 |
0
|
public void setOption(Attributes attributes)... |
384 |
|
{ |
385 |
|
|
386 |
|
|
387 |
|
|
388 |
0
|
String key = attributes.getValue("key"); |
389 |
0
|
String value = attributes.getValue("value"); |
390 |
0
|
if (currUnique != null) |
391 |
|
{ |
392 |
0
|
currUnique.addOption(key, value); |
393 |
|
} |
394 |
0
|
else if (currIndex != null) |
395 |
|
{ |
396 |
0
|
currIndex.addOption(key, value); |
397 |
|
} |
398 |
0
|
else if (currFK != null) |
399 |
|
{ |
400 |
0
|
currFK.addOption(key, value); |
401 |
|
} |
402 |
0
|
else if (currColumn != null) |
403 |
|
{ |
404 |
0
|
currColumn.addOption(key, value); |
405 |
|
} |
406 |
0
|
else if (currTable != null) |
407 |
|
{ |
408 |
0
|
currTable.addOption(key, value); |
409 |
|
} |
410 |
|
else |
411 |
|
{ |
412 |
0
|
database.addOption(key, value); |
413 |
|
} |
414 |
|
} |
415 |
|
|
416 |
|
|
417 |
|
|
418 |
|
@param |
419 |
|
@throws |
420 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
421 |
0
|
public void error(SAXParseException e) throws SAXException... |
422 |
|
{ |
423 |
0
|
log.error("Sax parser threw an Exception", e); |
424 |
0
|
throw new SAXException( |
425 |
|
"Error while parsing " |
426 |
|
+ currentXmlFile |
427 |
|
+ " at line " |
428 |
|
+ e.getLineNumber() |
429 |
|
+ " column " |
430 |
|
+ e.getColumnNumber() |
431 |
|
+ " : " |
432 |
|
+ e.getMessage()); |
433 |
|
} |
434 |
|
|
435 |
|
|
436 |
|
|
437 |
|
|
438 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 4 |
Complexity Density: 0.33 |
|
439 |
|
private static class ParseStackElement |
440 |
|
{ |
441 |
|
private boolean isExternalSchema; |
442 |
|
private String currentPackage; |
443 |
|
private String currentXmlFile; |
444 |
|
private boolean firstPass; |
445 |
|
|
446 |
|
|
447 |
|
|
448 |
|
@param |
449 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
450 |
0
|
public ParseStackElement(XmlToAppData parser)... |
451 |
|
{ |
452 |
|
|
453 |
0
|
isExternalSchema = parser.isExternalSchema; |
454 |
0
|
currentPackage = parser.currentPackage; |
455 |
0
|
currentXmlFile = parser.currentXmlFile; |
456 |
0
|
firstPass = parser.firstPass; |
457 |
|
|
458 |
|
|
459 |
0
|
parser.parsingStack.push(this); |
460 |
|
} |
461 |
|
|
462 |
|
|
463 |
|
|
464 |
|
|
465 |
|
@param |
466 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
467 |
0
|
public static void popState(XmlToAppData parser)... |
468 |
|
{ |
469 |
0
|
if (!parser.parsingStack.isEmpty()) |
470 |
|
{ |
471 |
0
|
ParseStackElement elem = (ParseStackElement) |
472 |
|
parser.parsingStack.pop(); |
473 |
|
|
474 |
|
|
475 |
0
|
parser.isExternalSchema = elem.isExternalSchema; |
476 |
0
|
parser.currentPackage = elem.currentPackage; |
477 |
0
|
parser.currentXmlFile = elem.currentXmlFile; |
478 |
0
|
parser.firstPass = elem.firstPass; |
479 |
|
} |
480 |
|
} |
481 |
|
|
482 |
|
|
483 |
|
|
484 |
|
|
485 |
|
@param |
486 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
487 |
0
|
public static void pushState(XmlToAppData parser)... |
488 |
|
{ |
489 |
0
|
new ParseStackElement(parser); |
490 |
|
} |
491 |
|
} |
492 |
|
} |