KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > jpa > conf > UnitDescriptorParser


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.jpa.conf;
21
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 import javax.xml.XMLConstants JavaDoc;
29 import javax.xml.parsers.ParserConfigurationException JavaDoc;
30 import javax.xml.parsers.SAXParser JavaDoc;
31 import javax.xml.parsers.SAXParserFactory JavaDoc;
32 import javax.xml.transform.stream.StreamSource JavaDoc;
33 import javax.xml.validation.Schema JavaDoc;
34 import javax.xml.validation.SchemaFactory JavaDoc;
35
36 import org.apache.cayenne.jpa.JpaUnit;
37 import org.apache.cayenne.jpa.Provider;
38 import org.apache.cayenne.jpa.instrument.InstrumentingUnit;
39 import org.xml.sax.Attributes JavaDoc;
40 import org.xml.sax.InputSource JavaDoc;
41 import org.xml.sax.SAXException JavaDoc;
42 import org.xml.sax.SAXParseException JavaDoc;
43 import org.xml.sax.helpers.DefaultHandler JavaDoc;
44
45 /**
46  * A parser of persistence descriptor files. Can be used in serial processing of multiple
47  * documents.
48  *
49  * @author Andrus Adamchik
50  */

51 public class UnitDescriptorParser {
52
53     static final String JavaDoc PERSISTENCE_SCHEMA = "META-INF/schemas/persistence_1_0.xsd";
54
55     static final String JavaDoc PERSISTENCE = "persistence";
56     static final String JavaDoc PERSISTENCE_UNIT = "persistence-unit";
57     static final String JavaDoc DESCRIPTION = "description";
58     static final String JavaDoc NAME = "name";
59     static final String JavaDoc PROVIDER = "provider";
60     static final String JavaDoc TRANSACTION_TYPE = "transaction-type";
61     static final String JavaDoc JTA_DATASOURCE = "jta-data-source";
62     static final String JavaDoc NON_JTA_DATASOURCE = "non-jta-data-source";
63     static final String JavaDoc MAPPING_FILE = "mapping-file";
64     static final String JavaDoc JAR_FILE = "jar-file";
65     static final String JavaDoc CLASS = "class";
66     static final String JavaDoc EXCLUDE_UNLISTED_CLASSES = "exclude-unlisted-classes";
67     static final String JavaDoc PROPERTIES = "properties";
68     static final String JavaDoc PROPERTY = "property";
69     static final String JavaDoc VALUE = "value";
70
71     protected SAXParserFactory JavaDoc parserFactory;
72
73     public UnitDescriptorParser() throws SAXException JavaDoc, ParserConfigurationException JavaDoc {
74         this(false);
75     }
76
77     public UnitDescriptorParser(boolean validatesAgainstSchema) throws SAXException JavaDoc {
78
79         parserFactory = SAXParserFactory.newInstance();
80         parserFactory.setNamespaceAware(true);
81
82         // note that validation requires that persistence.xml declares a schema like this:
83
// <persistence xmlns="http://java.sun.com/xml/ns/persistence"
84
// xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
85
// xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
86
// http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
87

88         if (validatesAgainstSchema) {
89             URL JavaDoc schemaURL = Thread.currentThread().getContextClassLoader().getResource(
90                     PERSISTENCE_SCHEMA);
91
92             SchemaFactory JavaDoc factory = SchemaFactory
93                     .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
94             StreamSource JavaDoc ss = new StreamSource JavaDoc(schemaURL.toExternalForm());
95             Schema JavaDoc schema = factory.newSchema(ss);
96             parserFactory.setSchema(schema);
97         }
98     }
99
100     /**
101      * Loads and returns a Collection of PersistenceUnitInfos from the XML descriptor.
102      */

103     public Collection JavaDoc<JpaUnit> getPersistenceUnits(
104             InputSource JavaDoc in,
105             final URL JavaDoc persistenceUnitRootUrl) throws SAXException JavaDoc, IOException JavaDoc,
106             ParserConfigurationException JavaDoc {
107
108         final Collection JavaDoc<JpaUnit> unitInfos = new ArrayList JavaDoc<JpaUnit>(2);
109
110         // note that parser is not reused - some parser implementations blow on
111
// parser.reset() call
112
SAXParser JavaDoc parser = parserFactory.newSAXParser();
113         parser.parse(in, new DefaultHandler JavaDoc() {
114
115             JpaUnit unit;
116             Properties JavaDoc properties;
117             StringBuilder JavaDoc charBuffer;
118
119             @Override JavaDoc
120             public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
121                 throw e;
122             }
123
124             @Override JavaDoc
125             public void startElement(
126                     String JavaDoc uri,
127                     String JavaDoc localName,
128                     String JavaDoc qName,
129                     Attributes JavaDoc attributes) throws SAXException JavaDoc {
130
131                 if (PERSISTENCE_UNIT.equals(qName)) {
132                     String JavaDoc name = attributes.getValue("", NAME);
133                     String JavaDoc transactionType = attributes.getValue("", TRANSACTION_TYPE);
134
135                     unit = new InstrumentingUnit();
136                     unit.setPersistenceUnitName(name);
137                     unit.setPersistenceUnitRootUrl(persistenceUnitRootUrl);
138
139                     if (transactionType != null) {
140                         unit.putProperty(
141                                 Provider.TRANSACTION_TYPE_PROPERTY,
142                                 transactionType);
143                     }
144                 }
145                 else if (PROPERTIES.equals(qName)) {
146                     properties = new Properties JavaDoc();
147                 }
148                 else if (PROPERTY.equals(qName)) {
149                     String JavaDoc name = attributes.getValue("", NAME);
150                     String JavaDoc value = attributes.getValue("", VALUE);
151                     properties.put(name, value);
152                 }
153                 else if (EXCLUDE_UNLISTED_CLASSES.equals(qName)) {
154                     unit.setExcludeUnlistedClasses(true);
155                 }
156             }
157
158             @Override JavaDoc
159             public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
160                     throws SAXException JavaDoc {
161                 if (PERSISTENCE_UNIT.equals(qName)) {
162                     unitInfos.add(unit);
163                 }
164                 else if (PROPERTIES.equals(qName)) {
165                     unit.addProperties(properties);
166                 }
167                 else {
168                     // process string values
169
String JavaDoc string = resetCharBuffer();
170
171                     if (string != null) {
172                         if (CLASS.equals(qName)) {
173                             unit.addManagedClassName(string);
174                         }
175                         else if (PROVIDER.equals(qName)) {
176                             unit.putProperty(Provider.PROVIDER_PROPERTY, string);
177                         }
178                         else if (JAR_FILE.equals(qName)) {
179                             unit.addJarFileUrl(string);
180                         }
181                         else if (MAPPING_FILE.equals(qName)) {
182                             unit.addMappingFileName(string);
183                         }
184                         else if (JTA_DATASOURCE.equals(qName)) {
185                             unit.putProperty(Provider.JTA_DATA_SOURCE_PROPERTY, string);
186                         }
187                         else if (NON_JTA_DATASOURCE.equals(qName)) {
188                             unit.putProperty(
189                                     Provider.NON_JTA_DATA_SOURCE_PROPERTY,
190                                     string);
191                         }
192                         else if (DESCRIPTION.equals(qName)) {
193                             unit.setDescription(string);
194                         }
195                     }
196                 }
197             }
198
199             @Override JavaDoc
200             public void characters(char[] ch, int start, int length) throws SAXException JavaDoc {
201                 if (charBuffer == null) {
202                     charBuffer = new StringBuilder JavaDoc();
203                 }
204
205                 charBuffer.append(ch, start, length);
206             }
207
208             String JavaDoc resetCharBuffer() {
209                 if (charBuffer == null) {
210                     return null;
211                 }
212
213                 String JavaDoc string = charBuffer.toString().trim();
214                 if (string.length() == 0) {
215                     string = null;
216                 }
217                 charBuffer = null;
218
219                 return string;
220             }
221
222         });
223         return unitInfos;
224     }
225
226 }
227
Popular Tags