01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.lib.plugin;
28:
29: import java.net.URL;
30:
31: import org.cougaar.util.ConfigFinder;
32: import org.cougaar.util.log.Logger;
33: import org.xml.sax.EntityResolver;
34: import org.xml.sax.InputSource;
35:
36: /**
37: * <pre>
38: * EntityResolver for use with all UTIL xml parsers.
39: *
40: * Wraps config file finder and uses it to resolve (find) xml entities.
41: *
42: * Need to provide an entity resolver to the parser.
43: * The entity resolver is just a wrapper of the configFinder.
44: * The resolver is expected to resolve a systemID, where:
45: *
46: * </pre>
47: */
48: public class UTILEntityResolver implements EntityResolver {
49:
50: protected Logger logger;
51:
52: public UTILEntityResolver(Logger logger) {
53: this .logger = logger;
54: }
55:
56: /**
57: * Set org.cougaar.lib.plugin.UTILEntityResolver.debug to true to see debug info. <p>
58: * Will say what file is getting read in...
59: **/
60: public InputSource resolveEntity(String publicId, String systemId) {
61: URL url = null;
62:
63: try {
64: url = new URL(systemId);
65: } catch (Exception e) {
66: }
67:
68: String filename = url.getFile();
69:
70: filename = filename.replace('\\', '/');
71: filename = filename.substring(filename.lastIndexOf("/") + 1,
72: filename.length());
73:
74: // return a special input source
75: try {
76: InputSource is = new InputSource(ConfigFinder.getInstance()
77: .open(filename));
78: if (logger.isDebugEnabled())
79: logger
80: .debug("UTILEntityResolver.resolveEntity - publicID "
81: + publicId
82: + " system ID "
83: + systemId
84: + " filename from systemID "
85: + filename
86: + " input source " + is);
87:
88: return is;
89: } catch (Exception e) {
90: logger
91: .error(
92: "UTILLdmXMLPlugin.getParsedDocument - Could not find on config path",
93: e);
94: return null;
95: }
96: }
97: }
|