KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ejb > packaging > PersistenceXmlLoader


1 package org.hibernate.ejb.packaging;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.net.URL JavaDoc;
6 import javax.xml.parsers.DocumentBuilder JavaDoc;
7 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.w3c.dom.Document JavaDoc;
12 import org.w3c.dom.Element JavaDoc;
13 import org.w3c.dom.Node JavaDoc;
14 import org.w3c.dom.NodeList JavaDoc;
15 import org.xml.sax.InputSource JavaDoc;
16
17 /**
18  * Comment
19  *
20  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
21  * @version $Revision: 1.3 $
22  */

23 public final class PersistenceXmlLoader {
24     private static Log log = LogFactory.getLog( PersistenceXmlLoader.class );
25
26     private PersistenceXmlLoader() {
27     }
28
29     private static Document JavaDoc loadURL(URL JavaDoc configURL) throws Exception JavaDoc {
30         InputStream JavaDoc is = configURL != null ? configURL.openStream() : null;
31         if ( is == null ) {
32             throw new IOException JavaDoc( "Failed to obtain InputStream from url: " + configURL );
33         }
34
35         DocumentBuilderFactory JavaDoc docBuilderFactory = null;
36         docBuilderFactory = DocumentBuilderFactory.newInstance();
37         docBuilderFactory.setValidating( false );
38         InputSource JavaDoc source = new InputSource JavaDoc( is );
39         //URL url = AspectXmlLoader.class.getResource("/jboss-aop_1_0.dtd");
40
//source.setSystemId( url.toString() );
41
DocumentBuilder JavaDoc docBuilder = docBuilderFactory.newDocumentBuilder();
42         //docBuilder.setEntityResolver( new AspectXmlLoader.Resolver() );
43
//docBuilder.setErrorHandler( new LocalErrorHandler() );
44
Document JavaDoc doc = docBuilder.parse( source );
45         return doc;
46     }
47
48     public static PersistenceMetadata deploy(URL JavaDoc url) throws Exception JavaDoc {
49         PersistenceMetadata metadata = new PersistenceMetadata();
50         Document JavaDoc doc = loadURL( url );
51         Element JavaDoc top = doc.getDocumentElement();
52         NodeList JavaDoc children = top.getChildNodes();
53         for ( int i = 0 ; i < children.getLength() ; i++ ) {
54             if ( children.item( i ).getNodeType() == Node.ELEMENT_NODE ) {
55                 Element JavaDoc element = (Element JavaDoc) children.item( i );
56                 String JavaDoc tag = element.getTagName();
57                 if ( tag.equals( "name" ) ) {
58                     metadata.setName( XmlHelper.getElementContent( element ) );
59                 }
60                 else if ( tag.equals( "non-jta-data-source" ) ) {
61                     metadata.setDatasource( XmlHelper.getElementContent( element ) );
62                 }
63                 else if ( tag.equals( "jta-data-source" ) ) {
64                     metadata.setJtaDatasource( XmlHelper.getElementContent( element ) );
65                 }
66                 else if ( tag.equals( "provider" ) ) {
67                     metadata.setProvider( XmlHelper.getElementContent( element ) );
68                 }
69                 else if ( tag.equals( "class" ) ) {
70                     metadata.getClasses().add( XmlHelper.getElementContent( element ) );
71                 }
72                 else if ( tag.equals( "mapping-file" ) ) {
73                     metadata.getMappingFiles().add( XmlHelper.getElementContent( element ) );
74                 }
75                 else if ( tag.equals( "jar-file" ) ) {
76                     metadata.getJarFiles().add( XmlHelper.getElementContent( element ) );
77                 }
78                 else if ( tag.equals( "properties" ) ) {
79                     NodeList JavaDoc props = element.getChildNodes();
80                     for ( int j = 0 ; j < props.getLength() ; j++ ) {
81                         if ( props.item( j ).getNodeType() == Node.ELEMENT_NODE ) {
82                             Element JavaDoc propElement = (Element JavaDoc) props.item( j );
83                             if ( !"property".equals( propElement.getTagName() ) ) continue;
84                             String JavaDoc propName = propElement.getAttribute( "name" ).trim();
85                             String JavaDoc propValue = propElement.getAttribute( "value" ).trim();
86                             metadata.getProps().put( propName, propValue );
87                         }
88                     }
89
90                 }
91             }
92         }
93         return metadata;
94     }
95
96 }
97
Popular Tags