KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > bridge > URIResolver


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.bridge;
19
20 import java.io.IOException JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22
23 import org.apache.batik.dom.svg.SVGOMDocument;
24 import org.apache.batik.dom.svg.XMLBaseSupport;
25 import org.apache.batik.util.ParsedURL;
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.Element JavaDoc;
28 import org.w3c.dom.Node JavaDoc;
29 import org.w3c.dom.svg.SVGDocument;
30
31 /**
32  * This class is used to resolve the URI that can be found in a SVG document.
33  *
34  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
35  * @version $Id: URIResolver.java,v 1.28 2005/02/22 09:12:57 cam Exp $
36  */

37 public class URIResolver {
38     /**
39      * The reference document.
40      */

41     protected SVGOMDocument document;
42
43     /**
44      * The document URI.
45      */

46     protected String JavaDoc documentURI;
47
48     /**
49      * The document loader.
50      */

51     protected DocumentLoader documentLoader;
52
53     /**
54      * Creates a new URI resolver object.
55      * @param doc The reference document.
56      * @param dl The document loader.
57      */

58     public URIResolver(SVGDocument doc, DocumentLoader dl) {
59         document = (SVGOMDocument)doc;
60         documentLoader = dl;
61     }
62
63     /**
64      * Imports the Element referenced by the given URI on Element
65      * <tt>ref</tt>.
66      * @param uri The element URI.
67      * @param ref The Element in the DOM tree to evaluate <tt>uri</tt>
68      * from.
69      * @return The referenced element or null if element can't be found.
70      */

71     public Element JavaDoc getElement(String JavaDoc uri, Element JavaDoc ref)
72         throws MalformedURLException JavaDoc, IOException JavaDoc {
73
74         Node JavaDoc n = getNode(uri, ref);
75         if (n == null) {
76             return null;
77         } else if (n.getNodeType() == Node.DOCUMENT_NODE) {
78             throw new IllegalArgumentException JavaDoc();
79         } else {
80             return (Element JavaDoc)n;
81         }
82     }
83
84     /**
85      * Imports the Node referenced by the given URI on Element
86      * <tt>ref</tt>.
87      * @param uri The element URI.
88      * @param ref The Element in the DOM tree to evaluate <tt>uri</tt>
89      * from.
90      * @return The referenced Node/Document or null if element can't be found.
91      */

92     public Node JavaDoc getNode(String JavaDoc uri, Element JavaDoc ref)
93         throws MalformedURLException JavaDoc, IOException JavaDoc, SecurityException JavaDoc {
94
95         String JavaDoc baseURI = XMLBaseSupport.getCascadedXMLBase(ref);
96         // System.err.println("baseURI: " + baseURI);
97
// System.err.println("URI: " + uri);
98
if ((baseURI == null) &&
99             (uri.startsWith("#")))
100             return document.getElementById(uri.substring(1));
101
102         ParsedURL purl = new ParsedURL(baseURI, uri);
103         // System.err.println("PURL: " + purl);
104

105         if (documentURI == null)
106             documentURI = document.getURL();
107
108         String JavaDoc frag = purl.getRef();
109         if ((frag != null) && (documentURI != null)) {
110             ParsedURL pDocURL = new ParsedURL(documentURI);
111             // System.out.println("doc: " + pDocURL);
112
// System.out.println("Purl: " + purl);
113
if (pDocURL.sameFile(purl)) {
114                 // System.out.println("match");
115
return document.getElementById(frag);
116             }
117         }
118
119         // uri is not a reference into this document, so load the
120
// document it does reference after doing a security
121
// check with the UserAgent
122
ParsedURL pDocURL = null;
123         if (documentURI != null) {
124             pDocURL = new ParsedURL(documentURI);
125         }
126
127         UserAgent userAgent = documentLoader.getUserAgent();
128         userAgent.checkLoadExternalResource(purl, pDocURL);
129
130         String JavaDoc purlStr = purl.toString();
131         if (frag != null) {
132             purlStr = purlStr.substring(0, purlStr.length()-(frag.length()+1));
133         }
134
135         Document doc = documentLoader.loadDocument(purlStr);
136         if (frag != null)
137             return doc.getElementById(frag);
138         return doc;
139     }
140 }
141
Popular Tags