1 package org.opensync.tools;
2
3 import java.io.CharArrayWriter;
4 import java.io.File;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.util.Hashtable;
8 import java.util.Vector;
9
10 import javax.xml.parsers.ParserConfigurationException;
11 import javax.xml.parsers.SAXParserFactory;
12
13 import org.xml.sax.Attributes;
14 import org.xml.sax.ErrorHandler;
15 import org.xml.sax.InputSource;
16 import org.xml.sax.SAXException;
17 import org.xml.sax.SAXParseException;
18 import org.xml.sax.XMLReader;
19 import org.xml.sax.helpers.DefaultHandler;
20
21 public class DbFile extends DefaultHandler implements ErrorHandler {
22
23 private CharArrayWriter contents;
24 private Hashtable parents;
25 private Parent parent;
26 private Vector currentChildren;
27
28 /***
29 * Constructor
30 */
31 public DbFile(String _name) throws IOException,FileNotFoundException,SAXException,ParserConfigurationException {
32 contents = new CharArrayWriter();
33 parents = new Hashtable();
34 currentChildren = new Vector();
35
36
37
38
39
40 System.setProperty("com.bluecast.xml.ValidatingSAXParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl");
41 SAXParserFactory factory = SAXParserFactory.newInstance();
42 factory.setNamespaceAware(false);
43 factory.setValidating(true);
44 XMLReader xmlReader = factory.newSAXParser().getXMLReader();
45 xmlReader.setContentHandler(this);
46 org.opensync.tools.EntityResolver entityResolver = new org.opensync.tools.EntityResolver();
47 xmlReader.setErrorHandler(this);
48
49 if (new File(_name).exists()){
50 entityResolver.setURI(new File(_name).getParentFile().toURL().toExternalForm());
51 xmlReader.setEntityResolver(entityResolver);
52 xmlReader.parse(new InputSource(new File(_name).toURL().toExternalForm()));
53 }else{
54 entityResolver.setURI(_name.substring(0,_name.lastIndexOf("/")+1));
55 xmlReader.setEntityResolver(entityResolver);
56 xmlReader.parse(new InputSource(_name));
57 }
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 }
78
79 public void error(SAXParseException exception) throws SAXException {
80 throw new SAXException("\r\nError at line"+exception.getLineNumber()+" in the file "+exception.getSystemId()+": "+exception.getLocalizedMessage());
81 }
82
83 public void fatalError(SAXParseException exception) throws SAXException {
84 throw new SAXException("\r\nFatal error at line"+exception.getLineNumber()+" in the file "+exception.getSystemId()+": "+exception.getLocalizedMessage());
85 }
86
87 public void warning(SAXParseException exception) throws SAXException {
88 throw new SAXException("\r\nWarning at line"+exception.getLineNumber()+" in the file "+exception.getSystemId()+": "+exception.getLocalizedMessage());
89 }
90
91 public void destroy() {
92 parents.clear();
93 currentChildren.clear();
94 }
95
96 public void startElement( String namespaceURI, String localName, String qName, Attributes attr ) throws SAXException {
97 contents.reset();
98
99 if (qName.equals("PARENT")) {
100 parent = new Parent();
101 parent.setIdParentFile(attr.getValue("IdParentFile"));
102 parent.setLabel(attr.getValue("Label"));
103
104 }
105
106 if (qName.equals("CHILD")) {
107
108 Child child = new Child();
109
110 child.setIdChildFile(attr.getValue("IdChildFile"));
111 child.setKeyChildFile(attr.getValue("KeyChildFile"));
112 child.setKeyParentFile(attr.getValue("KeyParentFile"));
113
114
115 currentChildren.addElement(child);
116 }
117 }
118
119 public void endElement( String namespaceURI, String localName, String qName ) throws SAXException {
120
121 if (qName.equals("PARENT")) {
122 if (currentChildren!=null && !currentChildren.isEmpty()) parent.setChildren(currentChildren);
123 parents.put(parent.getIdParentFile(),parent);
124 }
125 if (qName.equals("DB")) {
126
127
128 }
129 }
130
131 public void characters( char[] ch, int start, int length ) throws SAXException {
132 contents.write( ch, start, length );
133 }
134
135 public Hashtable getParents() {
136 return (parents);
137 }
138
139 public Parent getParent(String k) {
140 return (Parent)parents.get(k);
141 }
142
143 public void displayDbFile (String s) {
144 int i,j,iMax;
145 String idChildFile,keyChildFile,keyParentFile;
146
147 Parent it = (Parent)parents.get(s);
148
149 iMax = it.getChildSize();
150
151 for (i=0;i<iMax;i++) {
152 idChildFile = it.getIdChildFile(i);
153 keyChildFile = it.getKeyChildFile(i);
154 keyParentFile = it.getKeyParentFile(i);
155
156 if (Utils.debug) {
157 System.out.print("idChildFile=\t"+idChildFile+"\t");
158 System.out.print("keyChildFile=\t"+keyChildFile+"\t");
159 System.out.print("keyParentFile=\t"+keyParentFile+"\t");
160 System.out.println();
161 }
162 }
163 }
164 }
165
166
167
168