KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > config > MainResourceHandler


1 /*
2  * $Id: MainResourceHandler.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.base.config;
26
27 import java.io.InputStream JavaDoc;
28 import java.net.URL JavaDoc;
29
30 import org.ofbiz.base.util.UtilXml;
31 import org.ofbiz.base.util.Debug;
32
33 import org.w3c.dom.Document JavaDoc;
34 import org.w3c.dom.Element JavaDoc;
35
36 /**
37  * Contains resource information and provides for loading data
38  *
39  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
40  * @version $Rev: 5462 $
41  * @since 2.0
42  */

43 public class MainResourceHandler implements ResourceHandler {
44
45     public static final String JavaDoc module = MainResourceHandler.class.getName();
46     protected String JavaDoc xmlFilename;
47     protected String JavaDoc loaderName;
48     protected String JavaDoc location;
49
50     public MainResourceHandler(String JavaDoc xmlFilename, Element JavaDoc element) {
51         this.xmlFilename = xmlFilename;
52         this.loaderName = element.getAttribute("loader");
53         this.location = element.getAttribute("location");
54         if (Debug.verboseOn()) Debug.logVerbose("Created " + this.toString(), module);
55     }
56
57     public MainResourceHandler(String JavaDoc xmlFilename, String JavaDoc loaderName, String JavaDoc location) {
58         this.xmlFilename = xmlFilename;
59         this.loaderName = loaderName;
60         this.location = location;
61     }
62
63     public String JavaDoc getLoaderName() {
64         return this.loaderName;
65     }
66
67     public String JavaDoc getLocation() {
68         return this.location;
69     }
70
71     public Document JavaDoc getDocument() throws GenericConfigException {
72         try {
73             return UtilXml.readXmlDocument(this.getStream(), this.xmlFilename);
74         } catch (org.xml.sax.SAXException JavaDoc e) {
75             throw new GenericConfigException("Error reading " + this.toString(), e);
76         } catch (javax.xml.parsers.ParserConfigurationException JavaDoc e) {
77             throw new GenericConfigException("Error reading " + this.toString(), e);
78         } catch (java.io.IOException JavaDoc e) {
79             throw new GenericConfigException("Error reading " + this.toString(), e);
80         }
81     }
82
83     public InputStream JavaDoc getStream() throws GenericConfigException {
84         return ResourceLoader.loadResource(this.xmlFilename, this.location, this.loaderName);
85     }
86
87     public URL JavaDoc getURL() throws GenericConfigException {
88         return ResourceLoader.getURL(this.xmlFilename, this.location, this.loaderName);
89     }
90
91     public boolean isFileResource() throws GenericConfigException {
92         ResourceLoader loader = ResourceLoader.getLoader(this.xmlFilename, this.loaderName);
93
94         if (loader instanceof FileLoader) {
95             return true;
96         } else {
97             return false;
98         }
99     }
100
101     public String JavaDoc getFullLocation() throws GenericConfigException {
102         ResourceLoader loader = ResourceLoader.getLoader(this.xmlFilename, this.loaderName);
103
104         return loader.fullLocation(location);
105     }
106
107     public boolean equals(Object JavaDoc obj) {
108         if (obj instanceof MainResourceHandler) {
109             MainResourceHandler other = (MainResourceHandler) obj;
110
111             if (this.loaderName.equals(other.loaderName) &&
112                 this.xmlFilename.equals(other.xmlFilename) &&
113                 this.location.equals(other.location)) {
114                 return true;
115             }
116         }
117         return false;
118     }
119
120     public int hashCode() {
121         // the hashCode will weight by a combination xmlFilename and the combination of loaderName and location
122
return (this.xmlFilename.hashCode() + ((this.loaderName.hashCode() + this.location.hashCode()) >> 1)) >> 1;
123     }
124
125     public String JavaDoc toString() {
126         return "ResourceHandler from XML file [" + this.xmlFilename + "] with loaderName [" + loaderName + "] and location [" + location + "]";
127     }
128 }
129
Popular Tags