source: greenstone3/trunk/src/java/org/greenstone/gsdl3/util/GSEntityResolver.java@ 16869

Last change on this file since 16869 was 16869, checked in by kjdon, 16 years ago

added license message

  • Property svn:keywords set to Author Date Id Revision
File size: 2.5 KB
Line 
1/*
2 * GSEntityResolver.java
3 * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.util;
20
21
22import org.xml.sax.InputSource;
23import org.xml.sax.EntityResolver;
24import java.io.File;
25import java.net.URL;
26
27import org.apache.log4j.*;
28
29// uses a class loader to find entities
30// The class loader to use can be set by setClassLoader(), otherwise it will use the class loader that loaded itself. For the Tomcat webapp, this will be the webapp class loader, not the system class loader. the webapp classloader knows about the classes in the WEB-INF/classes dir, the system classloader knows about the ones on the classpath. The system class loader is a parent to web app classloader, so this will be used as well.
31
32public class GSEntityResolver implements EntityResolver {
33
34 ClassLoader class_loader = null;
35
36 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GSEntityResolver.class.getName());
37
38 public void setClassLoader(ClassLoader loader) {
39 this.class_loader = loader;
40 }
41
42 public InputSource resolveEntity (String public_id, String system_id) {
43
44 logger.debug("entity resolver for "+system_id);
45 String temp_id = system_id;
46 if (temp_id.startsWith("file://")) {
47 File f = new File(system_id);
48 if (f.exists()) {
49 return new InputSource(system_id);
50 } else {
51 temp_id = temp_id.substring(temp_id.lastIndexOf("/")+1);
52 }
53 } else {
54 if (temp_id.indexOf("/")!= -1) {
55 temp_id = temp_id.substring(temp_id.lastIndexOf("/")+1);
56 }
57 }
58 // try using a class loader
59 if (this.class_loader==null) {
60 this.class_loader = this.getClass().getClassLoader();
61 }
62 URL url = class_loader.getResource(temp_id);
63 if (url == null) {
64 return null;
65 }
66 return new InputSource("file://"+url.getFile());
67 }
68}
Note: See TracBrowser for help on using the repository browser.