KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.InputStreamReader JavaDoc;
24 import java.io.Reader JavaDoc;
25 import java.net.URL JavaDoc;
26
27 import javax.xml.XMLConstants JavaDoc;
28 import javax.xml.parsers.SAXParserFactory JavaDoc;
29 import javax.xml.transform.stream.StreamSource JavaDoc;
30 import javax.xml.validation.Schema JavaDoc;
31 import javax.xml.validation.SchemaFactory JavaDoc;
32
33 import org.apache.cayenne.jpa.map.JpaEntityMap;
34 import org.apache.cayenne.xml.XMLDecoder;
35 import org.xml.sax.SAXException JavaDoc;
36
37 /**
38  * {@link org.apache.cayenne.jpa.map.JpaEntityMap} loader that reads mapping information
39  * from the XML sources compatible with the JPA ORM schema.
40  *
41  * @author Andrus Adamchik
42  */

43 public class EntityMapXMLLoader {
44
45     static final String JavaDoc XML_CODER_MAPPING = "META-INF/cayenne/orm-coder.xml";
46     static final String JavaDoc ORM_SCHEMA = "META-INF/schemas/orm_1_0.xsd";
47
48     Schema JavaDoc schema;
49
50     protected ClassLoader JavaDoc classLoader;
51
52     public EntityMapXMLLoader() {
53         this(Thread.currentThread().getContextClassLoader(), false);
54     }
55
56     public EntityMapXMLLoader(ClassLoader JavaDoc classLoader, boolean validateDescriptors) {
57
58         this.classLoader = classLoader;
59
60         // TODO: andrus, 04/18/2006 - merge validation capabilities to the XMLDecoder...
61
if (validateDescriptors) {
62             SAXParserFactory JavaDoc parserFactory = SAXParserFactory.newInstance();
63             parserFactory.setNamespaceAware(true);
64
65             // note that validation requires that orm.xml declares a schema like this:
66
// <orm xmlns="http://java.sun.com/xml/ns/persistence/orm"
67
// xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
68
// xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
69
// http://java.sun.com/xml/ns/persistence/orm/orm_1_0.xsd">
70

71             URL JavaDoc schemaURL = classLoader.getResource(ORM_SCHEMA);
72
73             SchemaFactory JavaDoc factory = SchemaFactory
74                     .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
75             StreamSource JavaDoc ss = new StreamSource JavaDoc(schemaURL.toExternalForm());
76
77             try {
78                 this.schema = factory.newSchema(ss);
79             }
80             catch (Exception JavaDoc e) {
81                 throw new RuntimeException JavaDoc("Error loading ORM schema", e);
82             }
83         }
84     }
85
86     /**
87      * Loads {@link JpaEntityMap} using provided class loader to locate the XML. Returns
88      * null if no mapping is found.
89      */

90
91     public JpaEntityMap getEntityMap(URL JavaDoc resource) {
92
93         // TODO: andrus, 04/18/2006 XMLDecoder should support classpath locations for
94
// mapping descriptors
95
URL JavaDoc mappingURL = classLoader.getResource(XML_CODER_MAPPING);
96         if (mappingURL == null) {
97             throw new RuntimeException JavaDoc("No code mapping found: " + XML_CODER_MAPPING);
98         }
99
100         validate(resource);
101
102         try {
103
104             Reader JavaDoc in = new InputStreamReader JavaDoc(resource.openStream(), "UTF-8");
105
106             // TODO: andrus, 04/18/2006 - an inefficiency in XMLDecoder - it
107
// doesn't cache the mapping
108

109             XMLDecoder decoder = new XMLDecoder();
110             JpaEntityMap entityMap = (JpaEntityMap) decoder.decode(in, mappingURL
111                     .toExternalForm());
112
113             return entityMap;
114         }
115         catch (Exception JavaDoc e) {
116             throw new RuntimeException JavaDoc("Error processing ORM mapping " + resource, e);
117         }
118     }
119
120     // TODO: andrus, 04/18/2006 - move schema validation to the XMLDecoder
121
void validate(URL JavaDoc resource) {
122         if (schema != null) {
123             try {
124                 schema.newValidator().validate(new StreamSource JavaDoc(resource.openStream()));
125             }
126             catch (SAXException JavaDoc e) {
127                 throw new RuntimeException JavaDoc("Error validating ORM mapping " + resource, e);
128             }
129             catch (IOException JavaDoc e) {
130                 throw new RuntimeException JavaDoc("Error processing ORM mapping " + resource, e);
131             }
132         }
133     }
134 }
135
Popular Tags