KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > StandardURIResolver


1 package com.icl.saxon;
2 import org.xml.sax.*;
3 import java.util.*;
4 import java.net.*;
5 import javax.xml.parsers.SAXParserFactory JavaDoc;
6 import javax.xml.transform.*;
7 import javax.xml.transform.sax.SAXSource JavaDoc;
8
9
10 /**
11 * This class provides the service of converting a URI into an InputSource.
12 * It is used to get stylesheet modules referenced by xsl:import and xsl:include,
13 * and source documents referenced by the document() function. The standard version
14 * handles anything that the java URL class will handle.
15 * You can write a subclass to handle other kinds of URI, e.g. references to things in
16 * a database.
17 * @author Michael H. Kay
18 */

19
20 public class StandardURIResolver implements URIResolver {
21     
22     private TransformerFactoryImpl factory = null;
23
24     protected StandardURIResolver() {
25         this(null);
26     }
27     
28     public StandardURIResolver(TransformerFactoryImpl factory) {
29         this.factory = factory;
30     }
31
32     /**
33     * Resolve a URI
34     * @param baseURI The base URI that should be used. May be null if uri is absolute.
35     * @params uri The relative or absolute URI. May be an empty string. May contain
36     * a fragment identifier starting with "#", which must be the value of an ID attribute
37     * in the referenced XML document.
38     * @return a Source object representing an XML document
39     */

40
41     public Source JavaDoc resolve(String JavaDoc href, String JavaDoc base)
42     throws TransformerException {
43             
44         String JavaDoc relativeURI = href;
45         String JavaDoc id = null;
46         int hash = href.indexOf('#');
47         if (hash>=0) {
48             relativeURI = href.substring(0, hash);
49             id = href.substring(hash+1);
50             // System.err.println("StandaredURIResolver, HREF=" + href + ", id=" + id);
51
}
52
53         URL url;
54         try {
55             if (base==null) {
56                 url = new URL(relativeURI);
57                 // System.err.println("Resolved " + relativeURI + " as " + url.toString());
58
} else {
59                 // System.err.println("Resolving " + relativeURI + " against " + base);
60
URL baseURL = new URL(base);
61                 url = (relativeURI.length()==0 ?
62                                  baseURL :
63                                  new URL(baseURL, relativeURI)
64                              );
65             }
66         } catch (java.net.MalformedURLException JavaDoc err) {
67             // System.err.println("Recovering from " + err);
68
// last resort: if the base URI is null, or is itself a relative URI, we
69
// try to expand it relative to the current working directory
70
String JavaDoc expandedBase = tryToExpand(base);
71             if (!expandedBase.equals(base)) { // prevent infinite recursion
72
return resolve(href, expandedBase);
73             }
74             //err.printStackTrace();
75
throw new TransformerException("Malformed URL [" + relativeURI + "] - base [" + base + "]", err);
76         }
77
78         
79         SAXSource JavaDoc source = new SAXSource JavaDoc();
80         source.setInputSource(new InputSource(url.toString()));
81             
82         if (id!=null) {
83             IDFilter filter = new IDFilter(id);
84             XMLReader parser;
85             if (factory==null) {
86                 try {
87                     parser = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
88                 } catch (Exception JavaDoc err) {
89                     throw new TransformerException(err);
90                 }
91             } else {
92                 parser = factory.getSourceParser();
93             }
94             filter.setParent(parser);
95             source.setXMLReader(filter);
96         }
97         return source;
98     }
99
100
101     /**
102     * If a base URI is unknown, we'll try to expand the relative
103     * URI using the current directory as the base URI.
104     * (Code is identical to that in com.icl.saxon.aelfred.SAXDriver)
105     */

106     
107     private String JavaDoc tryToExpand(String JavaDoc systemId) {
108         if (systemId==null) {
109             systemId = "";
110         }
111         try {
112             URL u = new URL(systemId);
113             return systemId; // all is well
114
} catch (MalformedURLException err) {
115             String JavaDoc dir = System.getProperty("user.dir");
116             if (dir.startsWith("/")) {
117                 dir = "file://" + dir;
118             } else {
119                 dir = "file:///" + dir;
120             }
121             if (!(dir.endsWith("/") || systemId.startsWith("/"))) {
122                 dir = dir + "/";
123             }
124             String JavaDoc file = dir + systemId;
125             try {
126                 URL u2 = new URL(file);
127                 // System.err.println("URI Resolver: expanded " + systemId + " to " + file);
128
return file; // it seems to be OK
129
} catch (MalformedURLException err2) {
130                 // go with the original one
131
return systemId;
132             }
133         }
134     }
135
136 }
137
138
139
140
141
142 //
143
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
144
// you may not use this file except in compliance with the License. You may obtain a copy of the
145
// License at http://www.mozilla.org/MPL/
146
//
147
// Software distributed under the License is distributed on an "AS IS" basis,
148
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
149
// See the License for the specific language governing rights and limitations under the License.
150
//
151
// The Original Code is: all this file.
152
//
153
// The Initial Developer of the Original Code is
154
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
155
//
156
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
157
//
158
// Contributor(s): none.
159
//
160
Popular Tags