KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > services > DataSourcesParser


1 package com.quadcap.services;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.Reader JavaDoc;
42 import java.io.IOException JavaDoc;
43
44 import java.util.Hashtable JavaDoc;
45
46 import org.xml.sax.AttributeList JavaDoc;
47 import org.xml.sax.DocumentHandler JavaDoc;
48 import org.xml.sax.ErrorHandler JavaDoc;
49 import org.xml.sax.InputSource JavaDoc;
50 import org.xml.sax.Parser JavaDoc;
51 import org.xml.sax.Locator JavaDoc;
52 import org.xml.sax.SAXException JavaDoc;
53 import org.xml.sax.SAXParseException JavaDoc;
54
55 import org.xml.sax.helpers.ParserFactory JavaDoc;
56
57 import com.quadcap.text.sax.Handler;
58
59 /**
60  * Parser for Quadcap DataSource Deployment Descriptor.
61  *
62  * @author Stan Bailes
63  */

64 /*{com.quadcap.services.DataSources.xml}
65  *
66  * <el><name>data-sources</name>
67  * <p>This document is used to define and initialize JDBC DataSources
68  * and bind them to a JNDI Context.</p>
69  *
70  * <el><name>data-source</name>
71  * <p>Each DataSource definition has the following
72  * allowed elements:</p>
73  *
74  * <el><name>jndi-name</name>
75  * <p>The JNDI name to bind this data source to.</p></el>
76  *
77  * <el><name>jdbc-url</name>
78  * <p>The JDBC URL of the connection.</p></el>
79  *
80  * <el><name>connection-param</name>
81  * <p>A connection property, as a name/value pair. The properties
82  * so specified are stored in a
83  * <code>java.util.Properties</code>
84  * object which is passed to the JDBC Driver's
85  * <code>getConnection()</code> method.</p>
86  *
87  * <el><name>param-name</name>
88  * <p>The name of a connection property</p></el>
89  * <el><name>param-value</name>
90  * <p>The value of a connection property</p></el>
91  * </el>
92  * </el>
93  * </el>
94  */

95 public class DataSourcesParser extends Handler JavaDoc {
96     DataSources dss;
97     DataSource ds;
98     int state = INIT;
99     
100     final static int INIT = 0;
101     final static int DATA_SOURCE = 1;
102
103     /**
104      * Parser constructor
105      */

106     public DataSourcesParser() throws Exception JavaDoc {
107         super();
108     }
109     
110     /**
111      * Parse the specified deployment descriptor in the context of the
112      * specified DataSources object
113      *
114      * @param dd the reader containing the deployment descriptor
115      * @param ds the DataSources object
116      */

117     public void parse(Reader JavaDoc dd, DataSources dss)
118         throws SAXException JavaDoc
119     {
120         this.dss = dss;
121         this.state = INIT;
122         super.parse(dd, dss);
123     }
124
125
126     /**
127      * SAX parser callback function called for the end of an element.
128      *
129      * @param name the name of this element
130      * @exception SAXException may be thrown
131      */

132     public void endElement(String JavaDoc name) throws SAXException JavaDoc {
133         try {
134             switch (state) {
135             case DATA_SOURCE:
136                 if (name.equals("data-source")) {
137                     dss.addDataSource(ds);
138                     env.clear();
139                     state = INIT;
140                 } else if (name.equals("jndi-name")) {
141                     ds.setName(consumeData());
142                 } else if (name.equals("driver-class")) {
143                     ds.setDriverClass(consumeData());
144                 } else if (name.equals("jdbc-url")) {
145                     ds.setUrl(consumeData());
146                 } else if (name.equals("connection-param")) {
147                     ds.addConnectionProperty(consume("param-name"),
148                                              consume("param-value"));
149                 } else {
150                     env.put(name, consumeData());
151                 }
152                 break;
153             case INIT:
154                 break;
155             }
156         } catch (SAXException JavaDoc e) {
157             throw e;
158         } catch (Exception JavaDoc e) {
159             throw new SAXException JavaDoc(e);
160         }
161     }
162
163     /**
164      * SAX parser callback for the start of an element.
165      *
166      * @param name the element name
167      * @param attrs the element's attributes
168      *
169      * @exception SAXException may be thrown
170      */

171     public void startElement(String JavaDoc name, AttributeList JavaDoc attrs)
172     throws SAXException JavaDoc
173     {
174     data.setLength(0);
175         if (name.equals("data-source")) {
176             ds = new DataSource();
177             state = DATA_SOURCE;
178         }
179     }
180
181 }
182
183
Popular Tags