01: /*
02: * Copyright 2005 jWic group (http://www.jwic.de)
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: *
16: * de.jwic.util.DTDEntityResolver
17: * Created on 24.07.2006
18: * $Id: DTDEntityResolver.java,v 1.3 2006/07/28 14:21:52 lordsam Exp $
19: */
20: package de.jwic.util;
21:
22: import java.io.FileNotFoundException;
23: import java.io.IOException;
24: import java.io.InputStream;
25:
26: import org.xml.sax.EntityResolver;
27: import org.xml.sax.InputSource;
28: import org.xml.sax.SAXException;
29:
30: /**
31: * Loads a specified DTD file from the classpath.
32: *
33: * @author Florian Lippisch
34: * @version $Revision: 1.3 $
35: */
36: public class DTDEntityResolver implements EntityResolver {
37:
38: private String pubId;
39: private String sysId;
40: private String resourcePath;
41:
42: public DTDEntityResolver(String publicId, String systemId,
43: String resourcePath) {
44: this .pubId = publicId;
45: this .sysId = systemId;
46: this .resourcePath = resourcePath;
47: }
48:
49: /* (non-Javadoc)
50: * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)
51: */
52: public InputSource resolveEntity(String publicId, String systemId)
53: throws SAXException, IOException {
54:
55: if (pubId.equals(publicId) || sysId.equals(systemId)) {
56: InputStream in = getClass().getResourceAsStream(
57: resourcePath);
58: if (in == null) {
59: throw new FileNotFoundException("Resource '"
60: + resourcePath + "' not found in classpath");
61: }
62: return new InputSource(in);
63: }
64: return null;
65: }
66:
67: }
|