01: /*
02:
03: Licensed to the Apache Software Foundation (ASF) under one or more
04: contributor license agreements. See the NOTICE file distributed with
05: this work for additional information regarding copyright ownership.
06: The ASF licenses this file to You under the Apache License, Version 2.0
07: (the "License"); you may not use this file except in compliance with
08: the License. You may obtain a copy of the License at
09:
10: http://www.apache.org/licenses/LICENSE-2.0
11:
12: Unless required by applicable law or agreed to in writing, software
13: distributed under the License is distributed on an "AS IS" BASIS,
14: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: See the License for the specific language governing permissions and
16: limitations under the License.
17:
18: */
19: package org.apache.batik.bridge.svg12;
20:
21: import org.apache.batik.bridge.DocumentLoader;
22: import org.apache.batik.bridge.URIResolver;
23: import org.apache.batik.dom.AbstractNode;
24: import org.apache.batik.dom.xbl.NodeXBL;
25: import org.apache.batik.dom.xbl.XBLShadowTreeElement;
26:
27: import org.w3c.dom.Element;
28: import org.w3c.dom.Node;
29: import org.w3c.dom.NodeList;
30: import org.w3c.dom.svg.SVGDocument;
31:
32: /**
33: * A URIResolver for SVG 1.2 documents. This is to allow resolution of
34: * fragment IDs within shadow trees to work properly.
35: *
36: * @author <a href="mailto:cam%40mcc%2eid%2eau">Cameron McCormack</a>
37: * @version $Id: SVG12URIResolver.java 475477 2006-11-15 22:44:28Z cam $
38: */
39: public class SVG12URIResolver extends URIResolver {
40:
41: /**
42: * Creates a new SVG12URIResolver object.
43: */
44: public SVG12URIResolver(SVGDocument doc, DocumentLoader dl) {
45: super (doc, dl);
46: }
47:
48: /**
49: * Returns the base URI of the referer element.
50: */
51: protected String getRefererBaseURI(Element ref) {
52: AbstractNode aref = (AbstractNode) ref;
53: if (aref.getXblBoundElement() != null) {
54: return null;
55: }
56: return aref.getBaseURI();
57: }
58:
59: /**
60: * Returns the node referenced by the given fragment identifier.
61: * This is called when the whole URI just contains a fragment identifier
62: * and there is no XML Base URI in effect.
63: * @param frag the URI fragment
64: * @param ref the context element from which to resolve the URI fragment
65: */
66: protected Node getNodeByFragment(String frag, Element ref) {
67: NodeXBL refx = (NodeXBL) ref;
68: NodeXBL boundElt = (NodeXBL) refx.getXblBoundElement();
69: if (boundElt != null) {
70: XBLShadowTreeElement shadow = (XBLShadowTreeElement) boundElt
71: .getXblShadowTree();
72: Node n = shadow.getElementById(frag);
73: if (n != null) {
74: return n;
75: }
76: NodeList nl = refx.getXblDefinitions();
77: for (int i = 0; i < nl.getLength(); i++) {
78: n = nl.item(i).getOwnerDocument().getElementById(frag);
79: if (n != null) {
80: return n;
81: }
82: }
83: }
84: return super.getNodeByFragment(frag, ref);
85: }
86: }
|