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.Properties;
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.InputSource;
15  import org.xml.sax.SAXException;
16  import org.xml.sax.XMLReader;
17  import org.xml.sax.helpers.DefaultHandler;
18  
19  public class ConnectionProperties extends DefaultHandler {
20    private CharArrayWriter contents;
21    static Vector connections;
22  
23    /***
24     * Constructor
25     */
26    public ConnectionProperties(String filename) throws IOException,FileNotFoundException,SAXException,ParserConfigurationException {
27  
28        contents = new CharArrayWriter();
29  
30       /*
31        XMLReader xr = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
32        xr.setEntityResolver(new org.opensync.tools.EntityResolver());
33        xr.setContentHandler( this );
34        //xr.parse( new InputSource(new FileInputStream(filename)));
35        //PV 05-03-2002 because of weblogic 6.1 war deployement...
36        File aFile = new File(filename);
37        if (aFile.exists()) {
38          InputSource inputSource = new InputSource(filename);
39          xr.parse(inputSource);
40        } else {
41          System.out.println("The URL "+filename+" has been specified");
42          xr.parse( new InputSource(new URL(java.net.URLEncoder.encode(filename)).toExternalForm()));
43        }
44        */
45  
46        /* JAXP */
47        // If a validating parsing is needed, then Piccolo will use the following property to create
48        // a validating parser: here, we specify Xerces.
49        System.setProperty("com.bluecast.xml.ValidatingSAXParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl");
50        SAXParserFactory factory = SAXParserFactory.newInstance();
51        factory.setNamespaceAware(false);
52        //factory.setValidating(true);
53        XMLReader xmlReader = factory.newSAXParser().getXMLReader();
54        xmlReader.setContentHandler(this);
55        org.opensync.tools.EntityResolver entityResolver = new org.opensync.tools.EntityResolver();
56  //JT
57  //      entityResolver.setURI(new File(filename).getParentFile().toURL().toExternalForm());
58  //      System.out.println(filename);
59  //      System.out.println(new URL(filename).toString());
60  //      System.out.println(new URL(new URL(filename),"songs.dtd").toString());
61      if (new File(filename).exists()){
62        entityResolver.setURI(new File(filename).getParentFile().toURL().toExternalForm());
63        xmlReader.setEntityResolver(entityResolver);
64        xmlReader.parse(new InputSource(new File(filename).toURL().toExternalForm()));
65      }else{
66        entityResolver.setURI(filename.substring(0,filename.lastIndexOf("/")+1));
67        xmlReader.setEntityResolver(entityResolver);
68        xmlReader.parse(new InputSource(filename));
69      }
70  
71  //JT
72        //xmlReader.setErrorHandler(this);
73  //      System.out.println(filename);
74  //      System.out.println(new File(filename).toURL().toExternalForm());
75  //    xmlReader.parse(new InputSource(new File(filename).toURL().toExternalForm()));
76    }
77  
78     // Methode get
79    public Properties getConnection(int i) {
80      return((Properties)connections.elementAt(i));
81      }
82  
83    public Vector getConnections() {
84      return connections;
85      }
86  
87    public int getNumberOfConnections() {
88      return(connections.size());
89      }
90  
91      public void startElement( String namespaceURI, String localName, String qName, Attributes attr ) throws SAXException {
92      String ch1,ch2;
93      contents.reset();
94  
95        if (qName.equals("CONNECTIONS")) {
96            connections = new Vector();
97          }
98  
99        if (qName.equals("CONNECTION")) {
100             Properties connection = new Properties();
101             connection.put("name",attr.getValue("name"));
102             connection.put("drivers",attr.getValue("drivers"));
103             connection.put("logfile",attr.getValue("logfile"));
104             connection.put("url",attr.getValue("url"));
105             connection.put("user",attr.getValue("user"));
106             connection.put("password",attr.getValue("password"));
107             connection.put("initconns",attr.getValue("initconns"));
108             connection.put("maxconns",attr.getValue("maxconns"));
109             connection.put("logintimeout",attr.getValue("logintimeout"));
110             connection.put("loglevel",attr.getValue("loglevel"));
111             connection.put("operator_concat",attr.getValue("operator_concat"));
112             connection.put("conversion_function",attr.getValue("conversion_function"));
113             if (Utils.debug) System.out.println("ConnectionProperties::startElement - connection="+connection);
114             // Add into Theme(vector v) the attributs of element
115             connections.addElement(connection);
116         }
117 
118     }
119 
120    public void endElement( String namespaceURI, String localName, String qName ) throws SAXException {
121             //if (qName.equals("CONNECTIONS")) throw new SAXException( "Stop" );
122     }
123 
124    public void characters( char[] ch, int start, int length ) throws SAXException {
125     contents.write( ch, start, length );
126    }
127 
128 }
129