001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.util;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.ObjectInputStream;
022: import java.io.Serializable;
023:
024: import org.compass.core.config.ConfigurationException;
025: import org.xml.sax.EntityResolver;
026: import org.xml.sax.InputSource;
027:
028: public class DTDEntityResolver implements EntityResolver, Serializable {
029:
030: private static final long serialVersionUID = 3256440291954406962L;
031:
032: private static final String URL = "http://www.opensymphony.com/compass/dtd/";
033:
034: private transient ClassLoader resourceLoader;
035:
036: /**
037: * Default constructor using DTDEntityResolver classloader for resource
038: * loading.
039: */
040: public DTDEntityResolver() {
041: }
042:
043: /**
044: * Set the class loader used to load resouces
045: *
046: * @param resourceLoader class loader to use
047: */
048: public DTDEntityResolver(ClassLoader resourceLoader) {
049: this .resourceLoader = resourceLoader;
050: }
051:
052: public InputSource resolveEntity(String publicId, String systemId) {
053: if (systemId != null
054: && systemId
055: .startsWith("http://compass.sourceforge.net")) {
056: throw new IllegalArgumentException(
057: "Using old format for DTD, please use the url ["
058: + URL + "]");
059: }
060: if (systemId != null
061: && systemId
062: .startsWith("http://static.compassframework")) {
063: throw new IllegalArgumentException(
064: "Using old format for DTD, please use the url ["
065: + URL + "]");
066: }
067: if (systemId != null && systemId.startsWith(URL)) {
068: // Search for DTD
069: String location = "/org/compass/core/"
070: + systemId.substring(URL.length());
071: InputStream dtdStream = resourceLoader == null ? getClass()
072: .getResourceAsStream(location) : resourceLoader
073: .getResourceAsStream(location);
074: if (dtdStream == null) {
075: throw new ConfigurationException(
076: "DTD system id ["
077: + systemId
078: + "] not found at ["
079: + location
080: + "], "
081: + "please check it has the correct location. Have you included compass in your class path?");
082: } else {
083: InputSource source = new InputSource(dtdStream);
084: source.setPublicId(publicId);
085: source.setSystemId(systemId);
086: return source;
087: }
088: } else {
089: throw new ConfigurationException("DTD system id ["
090: + systemId
091: + "] not found, please check it has the "
092: + "correct location");
093: }
094: }
095:
096: private void readObject(ObjectInputStream ois) throws IOException,
097: ClassNotFoundException {
098: /** to allow serialization of configuration */
099: ois.defaultReadObject();
100: this.resourceLoader = this.getClass().getClassLoader();
101: }
102: }
|