001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: XmlEntityResolver.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.xml;
009:
010: import com.uwyn.rife.resources.ResourceFinder;
011: import com.uwyn.rife.xml.exceptions.CantFindEntityException;
012: import java.net.MalformedURLException;
013: import java.net.URL;
014: import java.util.HashMap;
015: import org.xml.sax.EntityResolver;
016: import org.xml.sax.InputSource;
017:
018: public class XmlEntityResolver implements EntityResolver {
019: private ResourceFinder mResourceFinder = null;
020: private HashMap<String, String> mCatalog = null;
021: private boolean mRestrictToCatalog = false;
022:
023: public XmlEntityResolver(ResourceFinder resourcefinder) {
024: mResourceFinder = resourcefinder;
025: }
026:
027: public XmlEntityResolver addToCatalog(String original, String alias) {
028: if (null == mCatalog) {
029: mCatalog = new HashMap<String, String>();
030: }
031:
032: mCatalog.put(original, alias);
033:
034: return this ;
035: }
036:
037: public XmlEntityResolver restrictToCatalog(boolean restrict) {
038: mRestrictToCatalog = restrict;
039:
040: return this ;
041: }
042:
043: public InputSource resolveEntity(String publicId, String systemId) {
044: assert systemId != null;
045: assert systemId.length() > 0;
046:
047: if (mCatalog != null) {
048: String alias = mCatalog.get(systemId);
049: if (alias != null) {
050: systemId = alias;
051: } else if (mRestrictToCatalog) {
052: throw new CantFindEntityException(systemId, null);
053: }
054: } else if (mRestrictToCatalog) {
055: throw new CantFindEntityException(systemId, null);
056: }
057:
058: URL resource = null;
059:
060: if (systemId.startsWith("http://")) {
061: try {
062: resource = new URL(systemId);
063: return new XmlInputSource(resource);
064: } catch (MalformedURLException e) {
065: resource = null;
066: }
067: }
068:
069: // fix around Resin's incompatible classloader resource urls
070: resource = mResourceFinder.getResource(systemId);
071:
072: if (resource != null) {
073: return new XmlInputSource(resource);
074: }
075:
076: // support orion's classloader resource url
077: if (systemId.startsWith("classloader:/")) {
078: systemId = systemId.substring("classloader:/".length());
079: }
080: // support weblogic's classloader resource url
081: if (systemId.startsWith("zip:/")) {
082: systemId = systemId.substring("zip:/".length());
083: }
084: if (systemId.startsWith("jar:/")) {
085: systemId = systemId.substring("jar:/".length());
086: }
087: if (systemId.startsWith("tx:/")) {
088: systemId = systemId.substring("tx:/".length());
089: }
090: if (systemId.startsWith("file:/")) {
091: systemId = systemId.substring("file:/".length());
092: }
093: if (systemId.startsWith("//")) {
094: systemId = systemId.substring("//".length());
095: }
096: int jar_entry_index = systemId.lastIndexOf("!/");
097: if (jar_entry_index != -1) {
098: systemId = systemId.substring(jar_entry_index
099: + "!/".length());
100: }
101:
102: resource = mResourceFinder.getResource(systemId);
103:
104: if (null == resource) {
105: throw new CantFindEntityException(systemId, null);
106: }
107:
108: return new XmlInputSource(resource);
109: }
110: }
|