/******************************************************************************* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. *******************************************************************************/ package org.apache.ofbiz.base.config; import java.io.InputStream; import java.net.URL; import org.apache.ofbiz.base.util.UtilXml; import org.apache.ofbiz.base.util.Debug; import org.w3c.dom.Document; import org.w3c.dom.Element; /** * Contains resource information and provides for loading data * */ @SuppressWarnings("serial") public final class MainResourceHandler implements ResourceHandler { public static final String module = MainResourceHandler.class.getName(); protected final String xmlFilename; protected final String loaderName; protected final String location; public MainResourceHandler(String xmlFilename, Element element) { this.xmlFilename = xmlFilename; this.loaderName = element.getAttribute("loader"); this.location = element.getAttribute("location"); if (Debug.verboseOn()) Debug.logVerbose("Created " + this.toString(), module); } public MainResourceHandler(String xmlFilename, String loaderName, String location) { this.xmlFilename = xmlFilename; this.loaderName = loaderName; this.location = location; } public String getLoaderName() { return this.loaderName; } public String getLocation() { return this.location; } public Document getDocument() throws GenericConfigException { try { return UtilXml.readXmlDocument(this.getStream(), this.xmlFilename, true); } catch (org.xml.sax.SAXException e) { throw new GenericConfigException("Error reading " + this.toString(), e); } catch (javax.xml.parsers.ParserConfigurationException e) { throw new GenericConfigException("Error reading " + this.toString(), e); } catch (java.io.IOException e) { throw new GenericConfigException("Error reading " + this.toString(), e); } } public InputStream getStream() throws GenericConfigException { return ResourceLoader.loadResource(this.xmlFilename, this.location, this.loaderName); } public URL getURL() throws GenericConfigException { return ResourceLoader.getURL(this.xmlFilename, this.location, this.loaderName); } public boolean isFileResource() throws GenericConfigException { ResourceLoader loader = ResourceLoader.getLoader(this.xmlFilename, this.loaderName); if (loader instanceof FileLoader) { return true; } else { return false; } } public String getFullLocation() throws GenericConfigException { ResourceLoader loader = ResourceLoader.getLoader(this.xmlFilename, this.loaderName); return loader.fullLocation(location); } @Override public boolean equals(Object obj) { if (obj instanceof MainResourceHandler) { MainResourceHandler other = (MainResourceHandler) obj; if (this.loaderName.equals(other.loaderName) && this.xmlFilename.equals(other.xmlFilename) && this.location.equals(other.location)) { return true; } } return false; } @Override public int hashCode() { // the hashCode will weight by a combination xmlFilename and the combination of loaderName and location return (this.xmlFilename.hashCode() + ((this.loaderName.hashCode() + this.location.hashCode()) >> 1)) >> 1; } @Override public String toString() { return "ResourceHandler from XML file [" + this.xmlFilename + "] with loaderName [" + loaderName + "] and location [" + location + "]"; } }