KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > ejb > cmp3 > xml > parser > PersistenceContentHandler


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.ejb.cmp3.xml.parser;
23
24 import org.xml.sax.Locator JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26 import org.xml.sax.Attributes JavaDoc;
27 import org.xml.sax.ContentHandler JavaDoc;
28
29 import java.net.URL JavaDoc;
30 import java.net.MalformedURLException JavaDoc;
31 import java.util.Vector JavaDoc;
32 import javax.persistence.spi.PersistenceUnitTransactionType;
33
34 import oracle.toplink.essentials.ejb.cmp3.persistence.SEPersistenceUnitInfo;
35 import oracle.toplink.essentials.internal.ejb.cmp3.jdbc.base.DataSourceImpl;
36 import oracle.toplink.essentials.logging.AbstractSessionLog;
37
38 public class PersistenceContentHandler implements ContentHandler JavaDoc {
39     private static final String JavaDoc NAMESPACE_URI = "http://java.sun.com/xml/ns/persistence";
40     private static final String JavaDoc ELEMENT_PERSISTENCE_UNIT = "persistence-unit";
41     private static final String JavaDoc ELEMENT_PROVIDER = "provider";
42     private static final String JavaDoc ELEMENT_JTA_DATA_SOURCE = "jta-data-source";
43     private static final String JavaDoc ELEMENT_NON_JTA_DATA_SOURCE = "non-jta-data-source";
44     private static final String JavaDoc ELEMENT_MAPPING_FILE = "mapping-file";
45     private static final String JavaDoc ELEMENT_JAR_FILE = "jar-file";
46     private static final String JavaDoc ELEMENT_CLASS = "class";
47     private static final String JavaDoc ELEMENT_EXCLUDE_UNLISTED_CLASSES = "exclude-unlisted-classes";
48     private static final String JavaDoc ELEMENT_PROPERTY = "property";
49     private static final String JavaDoc ATTRIBUTE_NAME = "name";
50     private static final String JavaDoc ATTRIBUTE_VALUE = "value";
51     private static final String JavaDoc ATTRIBUTE_TRANSACTION_TYPE = "transaction-type";
52
53     private SEPersistenceUnitInfo persistenceUnitInfo;
54     private Vector JavaDoc<SEPersistenceUnitInfo> persistenceUnits;
55     private StringBuffer JavaDoc stringBuffer;
56     private boolean readCharacters = false;
57
58     public PersistenceContentHandler() {
59         super();
60         stringBuffer = new StringBuffer JavaDoc();
61         persistenceUnits = new Vector JavaDoc();
62     }
63
64    public Vector JavaDoc<SEPersistenceUnitInfo> getPersistenceUnits() {
65         return persistenceUnits;
66     }
67
68     public void setDocumentLocator(Locator JavaDoc locator) {
69     }
70
71     public void startDocument() throws SAXException JavaDoc {
72     }
73
74     public void endDocument() throws SAXException JavaDoc {
75     }
76
77     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
78     }
79
80     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
81     }
82
83     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
84         if (NAMESPACE_URI.equals(namespaceURI)) {
85             if (ELEMENT_PERSISTENCE_UNIT.equals(localName)) {
86                 persistenceUnitInfo = new SEPersistenceUnitInfo();
87                 persistenceUnitInfo.setPersistenceUnitName(atts.getValue(ATTRIBUTE_NAME));
88                 String JavaDoc transactionType = atts.getValue(ATTRIBUTE_TRANSACTION_TYPE);
89                 if(transactionType != null) {
90                     persistenceUnitInfo.setTransactionType(PersistenceUnitTransactionType.valueOf(transactionType));
91                 }
92                 return;
93             } else if (ELEMENT_PROPERTY.equals(localName)) {
94                 String JavaDoc name = atts.getValue(ATTRIBUTE_NAME);
95                 String JavaDoc value = atts.getValue(ATTRIBUTE_VALUE);
96                 persistenceUnitInfo.getProperties().setProperty(name, value);
97             } else if (ELEMENT_PROVIDER.equals(localName)) {
98                 readCharacters = true;
99                 return;
100             } else if (ELEMENT_JTA_DATA_SOURCE.equals(localName)) {
101                 readCharacters = true;
102                 return;
103             } else if (ELEMENT_NON_JTA_DATA_SOURCE.equals(localName)) {
104                 readCharacters = true;
105                 return;
106             } else if (ELEMENT_MAPPING_FILE.equals(localName)) {
107                 readCharacters = true;
108                 return;
109             } else if (ELEMENT_JAR_FILE.equals(localName)) {
110                 readCharacters = true;
111                 return;
112             } else if (ELEMENT_EXCLUDE_UNLISTED_CLASSES.equals(localName)) {
113                 readCharacters = true;
114                 return;
115             } else if (ELEMENT_CLASS.equals(localName)) {
116                 readCharacters = true;
117                 return;
118             }
119         }
120     }
121
122     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
123         String JavaDoc string = stringBuffer.toString().trim();
124         stringBuffer.delete(0, stringBuffer.length());
125         readCharacters = false;
126
127         if (NAMESPACE_URI.equals(namespaceURI)) {
128             if (ELEMENT_PROVIDER.equals(localName)) {
129                 persistenceUnitInfo.setPersistenceProviderClassName(string);
130                 return;
131             } else if (ELEMENT_JTA_DATA_SOURCE.equals(localName)) {
132                 persistenceUnitInfo.setJtaDataSource(
133                     // Create a dummy DataSource that will
134
// throw an exception on access
135
new DataSourceImpl(string, null, null, null));
136                 return;
137             } else if (ELEMENT_NON_JTA_DATA_SOURCE.equals(localName)) {
138                 persistenceUnitInfo.setNonJtaDataSource(
139                     // Create a dummy DataSource that will
140
// throw an exception on access
141
new DataSourceImpl(string, null, null, null));
142                 return;
143             } else if (ELEMENT_MAPPING_FILE.equals(localName)) {
144                 persistenceUnitInfo.getMappingFileNames().add(string);
145                 return;
146             } else if (ELEMENT_JAR_FILE.equals(localName)) {
147                 URL JavaDoc url = null;
148                 try{
149                     url = new URL JavaDoc(string);
150                 } catch (MalformedURLException JavaDoc exc){
151                     try{
152                         url = new URL JavaDoc("file:///" + string);
153                     } catch (MalformedURLException JavaDoc exception){
154                         AbstractSessionLog.getLog().log(AbstractSessionLog.INFO, "jar_file_url_exception", exception);
155                     }
156                 }
157                 persistenceUnitInfo.getJarFileUrls().add(url);
158                 return;
159             } else if (ELEMENT_CLASS.equals(localName)) {
160                 persistenceUnitInfo.getManagedClassNames().add(string);
161                 return;
162             } else if (ELEMENT_EXCLUDE_UNLISTED_CLASSES.equals(localName)) {
163                 if (string.equals("true") || string.equals("1")){
164                     persistenceUnitInfo.setExcludeUnlistedClasses(true);
165                 } else {
166                     persistenceUnitInfo.setExcludeUnlistedClasses(false);
167                 }
168                 return;
169             } else if (ELEMENT_PERSISTENCE_UNIT.equals(localName)) {
170                 if (persistenceUnitInfo != null){
171                     persistenceUnits.add(persistenceUnitInfo);
172                     persistenceUnitInfo = null;
173                 }
174             }
175         }
176     }
177
178     public void characters(char[] ch, int start, int length) throws SAXException JavaDoc {
179         if (readCharacters) {
180             stringBuffer.append(ch, start, length);
181         }
182     }
183
184     public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException JavaDoc {
185     }
186
187     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
188     }
189
190     public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
191     }
192 }
193
Popular Tags