KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > runtime > ExtractorConfiguration


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.extractor.runtime;
24
25 import java.io.IOException JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.Properties JavaDoc;
29
30 import javax.xml.parsers.ParserConfigurationException JavaDoc;
31 import javax.xml.parsers.SAXParserFactory JavaDoc;
32
33 import org.xml.sax.*;
34 import org.xml.sax.helpers.DefaultHandler JavaDoc;
35 import org.xquark.extractor.common.Constants;
36 import org.xquark.extractor.common.MessageLibrary;
37 import org.xquark.schema.SchemaManager;
38 import org.xquark.schema.validation.SchemaValidationContext;
39 import org.xquark.schema.validation.ValidatingSchemaFilter;
40 import org.xquark.xml.xdbc.XMLDBCException;
41
42 /**
43  * SAX2 content handler implementation that performs configuration file loading.
44  */

45 public class ExtractorConfiguration extends DefaultHandler JavaDoc implements Constants {
46     
47     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
48     private static final String JavaDoc RCSName = "$Name: $";
49
50     /* private "status" constants */
51     private static final byte IN_CATALOG = 1;
52     private static final byte IN_SCHEMA = 2;
53     private static final byte IN_TABLE = 1;
54     private static final byte IN_COLUMN = 1;
55
56     private static SAXParserFactory JavaDoc spf = SAXParserFactory.newInstance();
57     static {
58         spf.setNamespaceAware(true);
59     }
60
61     public static final String JavaDoc DATASOURCE_XSD = "/org/xquark/extractor/resources/Datasource.xsd";
62     private static SchemaManager loadingManager = null;
63     static {
64         try {
65             SchemaValidationContext context = new SchemaValidationContext();
66             loadingManager = context.getSchemaManager();
67
68             URL JavaDoc url = ExtractorConfiguration.class.getResource(DATASOURCE_XSD);
69             InputSource source = new InputSource(url.toString());
70             loadingManager.loadSchema(context, source);
71         } catch (SAXException saxe) {
72         }
73     }
74
75     /* copied for defaultAccessorhandler */
76     protected Properties JavaDoc xmlproperties;
77
78     private Selection current_site;
79     private Selection current_catalog;
80     private Selection current_schema;
81     private Selection current_table;
82     private Selection current_column;
83     private String JavaDoc _currentView = null;
84     private StringBuffer JavaDoc _currentChars = new StringBuffer JavaDoc();
85     private String JavaDoc _dataSourceName = null;
86     private Properties JavaDoc _substitutions = null;
87     private Integer JavaDoc _nameCase = new Integer JavaDoc(CASE_MIXED);
88     private byte includeType = Selection.NONE;
89
90
91     public ExtractorConfiguration() {
92         xmlproperties = new Properties JavaDoc();
93     }
94
95     public static Properties JavaDoc loadConfiguration(String JavaDoc configURI) throws MalformedURLException JavaDoc, XMLDBCException {
96         URL JavaDoc url = new URL JavaDoc(configURI);
97         try {
98             XMLReader reader = spf.newSAXParser().getXMLReader();
99             ExtractorConfiguration handler = new ExtractorConfiguration();
100             ValidatingSchemaFilter filter = new ValidatingSchemaFilter(reader, new SchemaValidationContext(loadingManager));
101             filter.setContentHandler(handler);
102             filter.setErrorHandler(handler);
103             filter.parse(url.toString());
104             return handler.getProperties();
105         } catch (SAXParseException ex) {
106             String JavaDoc msg = "Error in parsing file " + ex.getSystemId() + " at line " + Integer.toString(ex.getLineNumber()) +
107                          " : " + ex.getMessage();
108             throw new XMLDBCException(msg, ex);
109         } catch (SAXException ex) {
110             throw new XMLDBCException("Could not parse configuration file", ex);
111         } catch (IOException JavaDoc ex) {
112             throw new XMLDBCException("Could not read configuration file", ex);
113         } catch (ParserConfigurationException JavaDoc ex) {
114             throw new XMLDBCException("Could not allocate XML parser", ex);
115         }
116         
117     }
118     
119     public static Properties JavaDoc getDefaultProperties(String JavaDoc url, String JavaDoc user) {
120         Properties JavaDoc properties = new Properties JavaDoc();
121         properties.put(LABEL_SPEC_SUBST_NAMECASE, new Integer JavaDoc(CASE_MIXED));
122         properties.put(LABEL_SPEC_URL, url);
123         properties.put(LABEL_SPEC_USER, user);
124         Properties JavaDoc substitutions = new Properties JavaDoc();
125         substitutions.setProperty("$", "_");
126         substitutions.setProperty("#", "_");
127         properties.put(LABEL_SPEC_SUBST, substitutions);
128         return properties;
129     }
130
131     public Properties JavaDoc getProperties() {
132         return xmlproperties;
133     }
134
135     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qualifiedName, Attributes atts) throws SAXException {
136         _currentChars.setLength(0);
137         if (localName.equalsIgnoreCase(LABEL_DATASOURCE)) {
138             String JavaDoc name = atts.getValue(LABEL_SPEC_NAME);
139             _dataSourceName = name;
140             setProperty(LABEL_SPEC_NAME, name);
141             current_site = new Selection(name, null, Selection.NONE, null);
142             xmlproperties.put("selections", current_site);
143             includeType = Selection.INCLUDES;
144         } else if (localName.equalsIgnoreCase(LABEL_SPEC_SUBST)) {
145             _substitutions = new Properties JavaDoc();
146             xmlproperties.put(LABEL_SPEC_SUBST, _substitutions);
147         } else if (localName.equalsIgnoreCase(LABEL_SPEC_SUBST_CHAR)) {
148             _substitutions.setProperty(atts.getValue(LABEL_SPEC_SUBST_VALUE), atts.getValue(LABEL_SPEC_SUBST_SUBST));
149         } else if (localName.equalsIgnoreCase(LABEL_SPEC_CATALOG)) {
150             String JavaDoc name = atts.getValue(LABEL_SPEC_NAME);
151             current_catalog = new Selection(name, null, includeType, null);
152             current_site.addToMap(name, current_catalog);
153             includeType = Selection.INCLUDES;
154         } else if (localName.equalsIgnoreCase(LABEL_SPEC_SCHEMA)) {
155             String JavaDoc name = atts.getValue(LABEL_SPEC_NAME);
156             String JavaDoc targetNamespace = atts.getValue(LABEL_SPEC_TARGETNAMESPACE);
157             current_schema = new Selection(name, targetNamespace, includeType, null);
158             String JavaDoc efd = atts.getValue(LABEL_SPEC_ELEMENTFORMDEFAULT);
159             int elementFormDefault;
160             if ("qualified".equalsIgnoreCase(efd)) {
161                 elementFormDefault = Selection.QUALIFIED;
162             } else {
163                 elementFormDefault = Selection.UNQUALIFIED;
164             }
165             current_schema.setElementFormDefault(elementFormDefault);
166
167             current_catalog.addToMap(name, current_schema);
168         } else if (localName.equalsIgnoreCase(LABEL_SPEC_TABLE)) {
169             String JavaDoc name = atts.getValue(LABEL_SPEC_NAME);
170             if (null != name) {
171                 current_table = new Selection(name, null, includeType, null);
172                 String JavaDoc alias = atts.getValue(LABEL_SPEC_ALIAS);
173                 if (null != alias && !Selection.verifyName(alias)) {
174                     throw new SAXException(MessageLibrary.getMessage("C_INV_NCNAME", alias));
175                 }
176                 current_table.setAlias(alias);
177                 current_schema.addToMap(name, current_table);
178             } else {
179                 name = atts.getValue(LABEL_SPEC_REGEX);
180                 if (null != name) {
181                     current_table = new Selection(name, true, null, includeType, null);
182                     current_schema.addToMap(name, current_table);
183                 } else {
184                     // throw new Exception("invalid table element in configuration file.");
185
}
186             }
187         } else if (localName.equalsIgnoreCase(LABEL_SPEC_COLUMN)) {
188             String JavaDoc name = atts.getValue(LABEL_SPEC_NAME);
189
190             if (null != name) {
191                 current_column = new Selection(name, null, includeType, null);
192                 String JavaDoc alias = atts.getValue(LABEL_SPEC_ALIAS);
193                 if (null != alias && !Selection.verifyName(alias)) {
194                     throw new SAXException(MessageLibrary.getMessage("C_INV_NCNAME", alias));
195                 }
196                 current_column.setAlias(alias);
197                 current_table.addToMap(name, current_column);
198             } else {
199                 name = atts.getValue(LABEL_SPEC_REGEX);
200                 if (null != name) {
201                     current_column = new Selection(name, true, null, includeType, null);
202                     current_table.addToMap(name, current_table);
203                 } else {
204                     // throw new Exception("invalid table element in configuration file.");
205
}
206             }
207
208             String JavaDoc isPK = atts.getValue(LABEL_SPEC_PK_COLUMN);
209
210             if (null != isPK && isPK.equalsIgnoreCase("true")) {
211                 current_column.setPKColumn(true);
212             }
213
214         } else if (localName.equalsIgnoreCase(LABEL_SPEC_INCLUDES)) {
215             includeType = Selection.INCLUDES;
216         } else if (localName.equalsIgnoreCase(LABEL_SPEC_EXCLUDES)) {
217             includeType = Selection.EXCLUDES;
218         }
219
220     }
221
222     public void characters(char ch[], int start, int length) throws SAXException {
223         _currentChars.append(ch, start, length);
224     }
225
226     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qualifiedName) throws SAXException {
227         if (localName.equalsIgnoreCase(LABEL_SPEC_DESCRIPTION)) {
228             setProperty(LABEL_SPEC_DESCRIPTION, _currentChars.toString().trim());
229         } else if (localName.equalsIgnoreCase(LABEL_SPEC_CASE)) {
230             setProperty(LABEL_SPEC_CASE, _currentChars.toString().trim());
231         } else if (localName.equalsIgnoreCase(LABEL_SPEC_SUBST_NAMECASE)) {
232             String JavaDoc cur_car = _currentChars.toString().trim();
233             if (cur_car.equalsIgnoreCase(CASE_LOWER_TOKEN)) {
234                 _nameCase = new Integer JavaDoc(CASE_LOWER);
235             } else if (cur_car.equalsIgnoreCase(CASE_UPPER_TOKEN)) {
236                 _nameCase = new Integer JavaDoc(CASE_UPPER);
237             } else if (cur_car.equalsIgnoreCase(CASE_MIXED_TOKEN)) {
238                 _nameCase = new Integer JavaDoc(CASE_MIXED);
239             }
240             xmlproperties.put(LABEL_SPEC_SUBST_NAMECASE, _nameCase);
241         } else if (localName.equalsIgnoreCase(LABEL_SPEC_DRIVER)) {
242             setProperty(LABEL_SPEC_DRIVER, _currentChars.toString().trim());
243         } else if (localName.equalsIgnoreCase(LABEL_SPEC_URL)) {
244             setProperty(LABEL_SPEC_URL, _currentChars.toString().trim());
245         } else if (localName.equalsIgnoreCase(LABEL_SPEC_USER)) {
246             setProperty(LABEL_SPEC_USER, _currentChars.toString().trim());
247         } else if (localName.equalsIgnoreCase(LABEL_SPEC_PASSWORD)) {
248             setProperty(LABEL_SPEC_PASSWORD, _currentChars.toString().trim());
249         } else if (localName.equalsIgnoreCase(LABEL_SPEC_JNDI)) {
250             setProperty(LABEL_SPEC_JNDI, _currentChars.toString().trim());
251         }
252         _currentChars.setLength(0);
253     }
254
255     /**
256      * Set a property of the accessor.
257      *
258      * @param a property of the accessor.
259      */

260     protected void setProperty(String JavaDoc key, String JavaDoc value) {
261         if (key == null) {
262             System.err.println("Key " + key + " is null.");
263             return;
264         }
265         if (value == null) {
266             System.err.println("Value " + value + " of " + key + " is null.");
267             return;
268         }
269         xmlproperties.setProperty(key, value);
270     }
271
272     /**
273      * Return a property of the accessor.
274      *
275      * @return a property of the accessor.
276      */

277     protected String JavaDoc getProperty(String JavaDoc key) {
278         return (String JavaDoc) xmlproperties.getProperty(key);
279     }
280
281     public void error(SAXParseException spe) throws SAXException {
282         throw spe;
283     }
284 }
285
Popular Tags