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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 System.setProperty("com.bluecast.xml.ValidatingSAXParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl");
50 SAXParserFactory factory = SAXParserFactory.newInstance();
51 factory.setNamespaceAware(false);
52
53 XMLReader xmlReader = factory.newSAXParser().getXMLReader();
54 xmlReader.setContentHandler(this);
55 org.opensync.tools.EntityResolver entityResolver = new org.opensync.tools.EntityResolver();
56
57
58
59
60
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
72
73
74
75
76 }
77
78
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
115 connections.addElement(connection);
116 }
117
118 }
119
120 public void endElement( String namespaceURI, String localName, String qName ) throws SAXException {
121
122 }
123
124 public void characters( char[] ch, int start, int length ) throws SAXException {
125 contents.write( ch, start, length );
126 }
127
128 }
129