001: /*
002: * $Id: ComponentResourceHandler.java,v 1.4 2003/08/20 23:01:41 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.component;
026:
027: import java.io.InputStream;
028: import java.net.URL;
029:
030: import org.ofbiz.base.config.GenericConfigException;
031: import org.ofbiz.base.config.ResourceHandler;
032: import org.ofbiz.base.util.UtilXml;
033: import org.w3c.dom.Document;
034: import org.w3c.dom.Element;
035:
036: /**
037: * Contains resource information and provides for loading data
038: *
039: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
040: * @version $Revision: 1.4 $
041: * @since 3.0
042: */
043: public class ComponentResourceHandler implements ResourceHandler {
044:
045: protected String componentName;
046: protected String loaderName;
047: protected String location;
048:
049: public ComponentResourceHandler(String componentName,
050: Element element) {
051: this .componentName = componentName;
052: this .loaderName = element.getAttribute("loader");
053: this .location = element.getAttribute("location");
054: }
055:
056: public ComponentResourceHandler(String componentName,
057: String loaderName, String location) {
058: this .componentName = componentName;
059: this .loaderName = loaderName;
060: this .location = location;
061: }
062:
063: public String getLoaderName() {
064: return this .loaderName;
065: }
066:
067: public String getLocation() {
068: return this .location;
069: }
070:
071: public Document getDocument() throws GenericConfigException {
072: try {
073: return UtilXml.readXmlDocument(this .getStream());
074: } catch (org.xml.sax.SAXException e) {
075: throw new GenericConfigException("Error reading "
076: + this .toString(), e);
077: } catch (javax.xml.parsers.ParserConfigurationException e) {
078: throw new GenericConfigException("Error reading "
079: + this .toString(), e);
080: } catch (java.io.IOException e) {
081: throw new GenericConfigException("Error reading "
082: + this .toString(), e);
083: }
084: }
085:
086: public InputStream getStream() throws GenericConfigException {
087: return ComponentConfig.getStream(componentName, loaderName,
088: location);
089: }
090:
091: public URL getURL() throws GenericConfigException {
092: return ComponentConfig.getURL(componentName, loaderName,
093: location);
094: }
095:
096: public boolean isFileResource() throws GenericConfigException {
097: return ComponentConfig.isFileResourceLoader(componentName,
098: loaderName);
099: }
100:
101: public String getFullLocation() throws GenericConfigException {
102: return ComponentConfig.getFullLocation(componentName,
103: loaderName, location);
104: }
105:
106: public boolean equals(Object obj) {
107: if (obj instanceof ComponentResourceHandler) {
108: ComponentResourceHandler other = (ComponentResourceHandler) obj;
109:
110: if (this .loaderName.equals(other.loaderName)
111: && this .componentName.equals(other.componentName)
112: && this .location.equals(other.location)) {
113: return true;
114: }
115: }
116: return false;
117: }
118:
119: public int hashCode() {
120: // the hashCode will weight by a combination componentName and the combination of loaderName and location
121: return (this .componentName.hashCode() + ((this .loaderName
122: .hashCode() + this .location.hashCode()) >> 1)) >> 1;
123: }
124:
125: public String toString() {
126: return "ComponentResourceHandler from XML file ["
127: + this .componentName + "] with loaderName ["
128: + loaderName + "] and location [" + location + "]";
129: }
130: }
|