001: package com.quadcap.services;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.Reader;
042: import java.io.IOException;
043:
044: import java.util.Hashtable;
045:
046: import org.xml.sax.AttributeList;
047: import org.xml.sax.DocumentHandler;
048: import org.xml.sax.ErrorHandler;
049: import org.xml.sax.InputSource;
050: import org.xml.sax.Parser;
051: import org.xml.sax.Locator;
052: import org.xml.sax.SAXException;
053: import org.xml.sax.SAXParseException;
054:
055: import org.xml.sax.helpers.ParserFactory;
056:
057: import com.quadcap.text.sax.Handler;
058:
059: /**
060: * Parser for Quadcap DataSource Deployment Descriptor.
061: *
062: * @author Stan Bailes
063: */
064: /*{com.quadcap.services.DataSources.xml}
065: *
066: * <el><name>data-sources</name>
067: * <p>This document is used to define and initialize JDBC DataSources
068: * and bind them to a JNDI Context.</p>
069: *
070: * <el><name>data-source</name>
071: * <p>Each DataSource definition has the following
072: * allowed elements:</p>
073: *
074: * <el><name>jndi-name</name>
075: * <p>The JNDI name to bind this data source to.</p></el>
076: *
077: * <el><name>jdbc-url</name>
078: * <p>The JDBC URL of the connection.</p></el>
079: *
080: * <el><name>connection-param</name>
081: * <p>A connection property, as a name/value pair. The properties
082: * so specified are stored in a
083: * <code>java.util.Properties</code>
084: * object which is passed to the JDBC Driver's
085: * <code>getConnection()</code> method.</p>
086: *
087: * <el><name>param-name</name>
088: * <p>The name of a connection property</p></el>
089: * <el><name>param-value</name>
090: * <p>The value of a connection property</p></el>
091: * </el>
092: * </el>
093: * </el>
094: */
095: public class DataSourcesParser extends Handler {
096: DataSources dss;
097: DataSource ds;
098: int state = INIT;
099:
100: final static int INIT = 0;
101: final static int DATA_SOURCE = 1;
102:
103: /**
104: * Parser constructor
105: */
106: public DataSourcesParser() throws Exception {
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 dd, DataSources dss) throws SAXException {
118: this .dss = dss;
119: this .state = INIT;
120: super .parse(dd, dss);
121: }
122:
123: /**
124: * SAX parser callback function called for the end of an element.
125: *
126: * @param name the name of this element
127: * @exception SAXException may be thrown
128: */
129: public void endElement(String name) throws SAXException {
130: try {
131: switch (state) {
132: case DATA_SOURCE:
133: if (name.equals("data-source")) {
134: dss.addDataSource(ds);
135: env.clear();
136: state = INIT;
137: } else if (name.equals("jndi-name")) {
138: ds.setName(consumeData());
139: } else if (name.equals("driver-class")) {
140: ds.setDriverClass(consumeData());
141: } else if (name.equals("jdbc-url")) {
142: ds.setUrl(consumeData());
143: } else if (name.equals("connection-param")) {
144: ds.addConnectionProperty(consume("param-name"),
145: consume("param-value"));
146: } else {
147: env.put(name, consumeData());
148: }
149: break;
150: case INIT:
151: break;
152: }
153: } catch (SAXException e) {
154: throw e;
155: } catch (Exception e) {
156: throw new SAXException(e);
157: }
158: }
159:
160: /**
161: * SAX parser callback for the start of an element.
162: *
163: * @param name the element name
164: * @param attrs the element's attributes
165: *
166: * @exception SAXException may be thrown
167: */
168: public void startElement(String name, AttributeList attrs)
169: throws SAXException {
170: data.setLength(0);
171: if (name.equals("data-source")) {
172: ds = new DataSource();
173: state = DATA_SOURCE;
174: }
175: }
176:
177: }
|