1 | package org.greenstone.gsdl3.util;
|
---|
2 |
|
---|
3 |
|
---|
4 | import org.xml.sax.InputSource;
|
---|
5 | import org.xml.sax.EntityResolver;
|
---|
6 | import java.io.File;
|
---|
7 | import java.net.URL;
|
---|
8 |
|
---|
9 | import org.apache.log4j.*;
|
---|
10 |
|
---|
11 | // uses a class loader to find entities
|
---|
12 | // 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.
|
---|
13 |
|
---|
14 | public class GSEntityResolver implements EntityResolver {
|
---|
15 |
|
---|
16 | ClassLoader class_loader = null;
|
---|
17 |
|
---|
18 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GSEntityResolver.class.getName());
|
---|
19 |
|
---|
20 | public void setClassLoader(ClassLoader loader) {
|
---|
21 | this.class_loader = loader;
|
---|
22 | }
|
---|
23 |
|
---|
24 | public InputSource resolveEntity (String public_id, String system_id) {
|
---|
25 |
|
---|
26 | logger.debug("entity resolver for "+system_id);
|
---|
27 | String temp_id = system_id;
|
---|
28 | if (temp_id.startsWith("file://")) {
|
---|
29 | File f = new File(system_id);
|
---|
30 | if (f.exists()) {
|
---|
31 | return new InputSource(system_id);
|
---|
32 | } else {
|
---|
33 | temp_id = temp_id.substring(temp_id.lastIndexOf("/")+1);
|
---|
34 | }
|
---|
35 | } else {
|
---|
36 | if (temp_id.indexOf("/")!= -1) {
|
---|
37 | temp_id = temp_id.substring(temp_id.lastIndexOf("/")+1);
|
---|
38 | }
|
---|
39 | }
|
---|
40 | // try using a class loader
|
---|
41 | if (this.class_loader==null) {
|
---|
42 | this.class_loader = this.getClass().getClassLoader();
|
---|
43 | }
|
---|
44 | URL url = class_loader.getResource(temp_id);
|
---|
45 | if (url == null) {
|
---|
46 | return null;
|
---|
47 | }
|
---|
48 | return new InputSource("file://"+url.getFile());
|
---|
49 | }
|
---|
50 | }
|
---|