001: /*
002: * $Id: MainResourceHandler.java,v 1.2 2003/08/20 23:02:13 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.base.config;
026:
027: import java.io.InputStream;
028: import java.net.URL;
029:
030: import org.ofbiz.base.util.UtilXml;
031: import org.w3c.dom.Document;
032: import org.w3c.dom.Element;
033:
034: /**
035: * Contains resource information and provides for loading data
036: *
037: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
038: * @version $Revision: 1.2 $
039: * @since 2.0
040: */
041: public class MainResourceHandler implements ResourceHandler {
042:
043: protected String xmlFilename;
044: protected String loaderName;
045: protected String location;
046:
047: public MainResourceHandler(String xmlFilename, Element element) {
048: this .xmlFilename = xmlFilename;
049: this .loaderName = element.getAttribute("loader");
050: this .location = element.getAttribute("location");
051: }
052:
053: public MainResourceHandler(String xmlFilename, String loaderName,
054: String location) {
055: this .xmlFilename = xmlFilename;
056: this .loaderName = loaderName;
057: this .location = location;
058: }
059:
060: public String getLoaderName() {
061: return this .loaderName;
062: }
063:
064: public String getLocation() {
065: return this .location;
066: }
067:
068: public Document getDocument() throws GenericConfigException {
069: try {
070: return UtilXml.readXmlDocument(this .getStream());
071: } catch (org.xml.sax.SAXException e) {
072: throw new GenericConfigException("Error reading "
073: + this .toString(), e);
074: } catch (javax.xml.parsers.ParserConfigurationException e) {
075: throw new GenericConfigException("Error reading "
076: + this .toString(), e);
077: } catch (java.io.IOException e) {
078: throw new GenericConfigException("Error reading "
079: + this .toString(), e);
080: }
081: }
082:
083: public InputStream getStream() throws GenericConfigException {
084: return ResourceLoader.loadResource(this .xmlFilename,
085: this .location, this .loaderName);
086: }
087:
088: public URL getURL() throws GenericConfigException {
089: return ResourceLoader.getURL(this .xmlFilename, this .location,
090: this .loaderName);
091: }
092:
093: public boolean isFileResource() throws GenericConfigException {
094: ResourceLoader loader = ResourceLoader.getLoader(
095: this .xmlFilename, this .loaderName);
096:
097: if (loader instanceof FileLoader) {
098: return true;
099: } else {
100: return false;
101: }
102: }
103:
104: public String getFullLocation() throws GenericConfigException {
105: ResourceLoader loader = ResourceLoader.getLoader(
106: this .xmlFilename, this .loaderName);
107:
108: return loader.fullLocation(location);
109: }
110:
111: public boolean equals(Object obj) {
112: if (obj instanceof MainResourceHandler) {
113: MainResourceHandler other = (MainResourceHandler) obj;
114:
115: if (this .loaderName.equals(other.loaderName)
116: && this .xmlFilename.equals(other.xmlFilename)
117: && this .location.equals(other.location)) {
118: return true;
119: }
120: }
121: return false;
122: }
123:
124: public int hashCode() {
125: // the hashCode will weight by a combination xmlFilename and the combination of loaderName and location
126: return (this .xmlFilename.hashCode() + ((this .loaderName
127: .hashCode() + this .location.hashCode()) >> 1)) >> 1;
128: }
129:
130: public String toString() {
131: return "ResourceHandler from XML file [" + this .xmlFilename
132: + "] with loaderName [" + loaderName
133: + "] and location [" + location + "]";
134: }
135: }
|