View Javadoc

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      /* JAXP */
38      // If a validating parsing is needed, then Piccolo will use the following property to create
39      // a validating parser: here, we specify Xerces.
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      /* Xerces */
59      /*
60      XMLReader xr = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
61      xr.setContentHandler( this );
62      xr.setFeature("http://xml.org/sax/features/validation", true);
63      // installing error handler
64      org.xml.sax.ErrorHandler errorHandler = this;
65      xr.setErrorHandler(errorHandler);
66      //xr.parse( new InputSource(new FileInputStream(_filename)));
67      //PV 05-03-2002 because of weblogic 6.1 war deployement...
68      xr.parse( new InputSource(_name);
69      */
70      // Is it a file ?
71      /*if (new File(_name).exists()) {
72        xr.parse( new InputSource(new File(_name).toURL().toExternalForm()));
73      } else {
74        // The file doesn't exist, then try URL !
75        xr.parse( new InputSource(new URL(_name).toExternalForm()));
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         //System.out.println("PARENT : "+attr.getValue("IdParentFile"));
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         // Add into Parent (vector v) the attributs of Child
114         //parent.addChild(child);
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           // themes.put(theme.getCodeNumber(),theme);
127           // displayDbFile("aTheme");
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