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

Last change on this file since 29728 was 29728, checked in by ak19, 9 years ago

Adjusting gs3-server code to work with changes made to 29686 where web.xml was split into web.xml and servlets.xml, with the first including the second.

  • Property svn:keywords set to Author Date Id Revision
File size: 3.0 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 File baseFilepath = null;
36
37 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GSEntityResolver.class.getName());
38
39 public GSEntityResolver() {}
40
41 public GSEntityResolver(File baseFilepath) {
42 this.baseFilepath = baseFilepath;
43 }
44
45 /* Methods with the ClassLoader parameter are unused at present */
46 public GSEntityResolver(ClassLoader loader) {
47 this.class_loader = loader;
48 }
49
50 public void setClassLoader(ClassLoader loader) {
51 this.class_loader = loader;
52 }
53
54 public InputSource resolveEntity (String public_id, String system_id) {
55
56 logger.debug("entity resolver for "+system_id);
57 String temp_id = system_id;
58 if (temp_id.startsWith("file://")) {
59 File f = new File(system_id);
60 if (f.exists()) {
61 return new InputSource(system_id);
62 } else {
63 temp_id = temp_id.substring(temp_id.lastIndexOf("/")+1);
64 }
65 } else {
66 if (temp_id.indexOf("/")!= -1) {
67 temp_id = temp_id.substring(temp_id.lastIndexOf("/")+1);
68 }
69 }
70
71 // use the baseFilepath, if one was provided
72 if(this.baseFilepath != null) {
73 return new InputSource("file://" + this.baseFilepath + File.separator + temp_id);
74 }
75
76 // try using a class loader
77 if (this.class_loader==null) {
78 this.class_loader = this.getClass().getClassLoader();
79 }
80 URL url = class_loader.getResource(temp_id);
81 if (url == null) {
82 return null;
83 }
84 return new InputSource("file://"+url.getFile());
85 }
86}
Note: See TracBrowser for help on using the repository browser.