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

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

Related to commits 29687, 29729, 29730. The changes to incorporating the new servlets.xml in web.xml when GLI launches requires additional processing for windows too, similar to the change for launching the GS3 server, which was part of the previous commit 29752.

  • Property svn:keywords set to Author Date Id Revision
File size: 3.4 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
21import org.greenstone.util.Misc;
22
23import org.xml.sax.InputSource;
24import org.xml.sax.EntityResolver;
25import java.io.File;
26import java.net.URL;
27
28import org.apache.log4j.*;
29
30// uses a class loader to find entities
31// 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.
32
33public class GSEntityResolver implements EntityResolver {
34
35 ClassLoader class_loader = null;
36 File baseFilepath = null;
37
38 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GSEntityResolver.class.getName());
39
40 public GSEntityResolver() {}
41
42 public GSEntityResolver(File baseFilepath) {
43 this.baseFilepath = baseFilepath;
44 }
45
46 /* Methods with the ClassLoader parameter are unused at present */
47 public GSEntityResolver(ClassLoader loader) {
48 this.class_loader = loader;
49 }
50
51 public void setClassLoader(ClassLoader loader) {
52 this.class_loader = loader;
53 }
54
55 public InputSource resolveEntity (String public_id, String system_id) {
56
57 logger.debug("entity resolver for "+system_id);
58 String temp_id = system_id;
59 if (temp_id.startsWith("file://")) {
60 File f = new File(system_id);
61 if (f.exists()) {
62 return new InputSource(system_id);
63 } else {
64 temp_id = f.getName(); //temp_id = temp_id.substring(temp_id.lastIndexOf("/")+1);
65 }
66 } else { // not a file
67 if (temp_id.indexOf("/")!= -1) {
68 temp_id = temp_id.substring(temp_id.lastIndexOf("/")+1);
69 }
70 }
71
72 // use the baseFilepath, if one was provided
73 if(this.baseFilepath != null) {
74
75 // The file protocol for windows starts with file:/// and has URL slashes
76 // http://en.wikipedia.org/wiki/File_URI_scheme#Windows
77 String newpath = this.baseFilepath + File.separator + temp_id;
78 if(Misc.isWindows()) {
79 newpath = "file:///" + newpath.replace("\\", "/");
80 } else { // linux version, file protocol starts with file:// and slashes are already URL-style
81 newpath = "file://" + newpath;
82 }
83 return new InputSource(newpath);
84 }
85
86 // try using a class loader
87 if (this.class_loader==null) {
88 this.class_loader = this.getClass().getClassLoader();
89 }
90 URL url = class_loader.getResource(temp_id);
91 if (url == null) {
92 return null;
93 }
94 return new InputSource("file://"+url.getFile());
95 }
96}
Note: See TracBrowser for help on using the repository browser.