01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: XmlInputSource.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.xml;
09:
10: import com.uwyn.rife.resources.ResourceFinder;
11: import com.uwyn.rife.xml.exceptions.CantFindResourceException;
12: import com.uwyn.rife.xml.exceptions.XmlErrorException;
13: import java.io.IOException;
14: import java.net.URL;
15: import java.net.URLConnection;
16: import org.xml.sax.InputSource;
17:
18: public class XmlInputSource extends InputSource {
19: private URL mResource = null;
20:
21: public XmlInputSource(String xmlPath, ResourceFinder resourceFinder)
22: throws XmlErrorException {
23: super ();
24:
25: if (null == xmlPath)
26: throw new IllegalArgumentException("xmlPath can't be null.");
27: if (xmlPath.length() == 0)
28: throw new IllegalArgumentException(
29: "xmlPath can't be empty.");
30: if (null == resourceFinder)
31: throw new IllegalArgumentException(
32: "resourceFinder can't be null.");
33:
34: URL resource = resourceFinder.getResource(xmlPath);
35:
36: if (null == resource) {
37: throw new CantFindResourceException(xmlPath, null);
38: }
39:
40: setResource(resource);
41: }
42:
43: public XmlInputSource(URL resource) throws XmlErrorException {
44: super ();
45:
46: if (null == resource)
47: throw new IllegalArgumentException(
48: "resource can't be null.");
49:
50: setResource(resource);
51: }
52:
53: public void setResource(URL resource) {
54: mResource = resource;
55:
56: setSystemId(mResource.toExternalForm());
57:
58: // catch orion's classloader resource protocol
59: try {
60: if (resource.getProtocol().equals("classloader")) {
61: // force byte stream that is used so that Orion doesn't set a wrong
62: // one
63: URLConnection connection = resource.openConnection();
64: connection.setUseCaches(false);
65: setByteStream(connection.getInputStream());
66: } else {
67: String sax_driver = System
68: .getProperty("org.xml.sax.driver");
69:
70: if (sax_driver != null
71: && sax_driver.equals("com.caucho.xml.Xml")) {
72: // force byte stream that is used so that Resin doesn't set a wrong
73: // one
74: URLConnection connection = resource
75: .openConnection();
76: connection.setUseCaches(false);
77: setByteStream(connection.getInputStream());
78: }
79: }
80: }
81: // if an exception occurs, clear to byte stream and let the sax driver
82: // try to find it
83: catch (IOException e) {
84: setByteStream(null);
85: }
86: }
87:
88: public String toString() {
89: return mResource.toExternalForm();
90: }
91: }
|