01: /**
02: *
03: * Licensed to the Apache Software Foundation (ASF) under one or more
04: * contributor license agreements. See the NOTICE file distributed with
05: * this work for additional information regarding copyright ownership.
06: * The ASF licenses this file to You under the Apache License, Version 2.0
07: * (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */package org.apache.openejb.jee.jpa.unit;
18:
19: import org.xml.sax.InputSource;
20: import org.xml.sax.SAXException;
21: import org.xml.sax.XMLReader;
22: import org.xml.sax.helpers.XMLFilterImpl;
23:
24: import javax.xml.bind.JAXBContext;
25: import javax.xml.bind.Unmarshaller;
26: import javax.xml.bind.UnmarshallerHandler;
27: import javax.xml.parsers.SAXParser;
28: import javax.xml.parsers.SAXParserFactory;
29: import javax.xml.transform.sax.SAXSource;
30: import java.io.InputStream;
31: import java.io.ByteArrayInputStream;
32: import java.io.IOException;
33: import java.net.URL;
34:
35: /**
36: * @version $Revision: 585036 $ $Date: 2007-10-15 21:38:53 -0700 $
37: */
38: public class JaxbPersistenceFactory {
39: public static final String PERSISTENCE_SCHEMA = "http://java.sun.com/xml/ns/persistence";
40:
41: public static Persistence getPersistence(URL url) throws Exception {
42: InputStream persistenceDescriptor = null;
43:
44: try {
45:
46: persistenceDescriptor = url.openStream();
47:
48: JAXBContext jc = JAXBContext.newInstance(Persistence.class);
49: Unmarshaller u = jc.createUnmarshaller();
50: UnmarshallerHandler uh = u.getUnmarshallerHandler();
51:
52: // create a new XML parser
53: SAXParserFactory factory = SAXParserFactory.newInstance();
54: factory.setNamespaceAware(true);
55: factory.setValidating(true);
56: SAXParser parser = factory.newSAXParser();
57:
58: XMLReader xmlReader = parser.getXMLReader();
59:
60: // Create a filter to intercept events
61: PersistenceFilter xmlFilter = new PersistenceFilter(
62: xmlReader);
63:
64: // Be sure the filter has the JAXB content handler set (or it wont
65: // work)
66: xmlFilter.setContentHandler(uh);
67: SAXSource source = new SAXSource(xmlFilter,
68: new InputSource(persistenceDescriptor));
69:
70: return (Persistence) u.unmarshal(source);
71:
72: } finally {
73: if (persistenceDescriptor != null)
74: persistenceDescriptor.close();
75: }
76: }
77:
78: // Inject the proper namespace
79: public static class PersistenceFilter extends XMLFilterImpl {
80: private static final InputSource EMPTY_INPUT_SOURCE = new InputSource(
81: new ByteArrayInputStream(new byte[0]));
82:
83: public PersistenceFilter(XMLReader xmlReader) {
84: super (xmlReader);
85: }
86:
87: public InputSource resolveEntity(String publicId,
88: String systemId) throws SAXException, IOException {
89: return EMPTY_INPUT_SOURCE;
90: }
91: }
92: }
|