001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.perseus.model;
027:
028: import com.sun.perseus.platform.URLResolver;
029:
030: import com.sun.perseus.util.SVGConstants;
031:
032: import org.w3c.dom.DOMException;
033:
034: /**
035: * Represents an SVG Tiny <code><a></code> element.
036: * An anchor is a simple <code>Group</code> extension which
037: * simply has a href and a target attribute.
038: *
039: * @version $Id: Anchor.java,v 1.4 2006/04/21 06:36:17 st125089 Exp $
040: */
041: public class Anchor extends Group {
042: /**
043: * The anchor's hyperlink reference
044: */
045: protected String href = "";
046:
047: /**
048: * The anchor's target
049: */
050: protected String target = "";
051:
052: /**
053: * Constructor.
054: *
055: * @param ownerDocument this element's owner <code>DocumentNode</code>
056: */
057: public Anchor(final DocumentNode ownerDocument) {
058: super (ownerDocument);
059: }
060:
061: /**
062: * @return the SVGConstants.SVG_A_TAG value
063: */
064: public String getLocalName() {
065: return SVGConstants.SVG_A_TAG;
066: }
067:
068: /**
069: * Used by <code>DocumentNode</code> to create a new instance from
070: * a prototype <code>AnchorNode</code>.
071: *
072: * @param doc the <code>DocumentNode</code> for which a new node is
073: * should be created.
074: * @return a new <code>Anchor</code> for the requested document.
075: */
076: public ElementNode newInstance(final DocumentNode doc) {
077: return new Anchor(doc);
078: }
079:
080: /**
081: * This returns the <b>absolute</b> URI, even though
082: * the href may have been a relative URI
083: *
084: * @return this anchor's href, as an absolute URL or
085: * null if the href set was null or if the
086: * absolute URL could not be computed.
087: */
088: public String getHref() {
089: String uriBase = getURIBase();
090: String docBase = ownerDocument.getURIBase();
091: if (uriBase != null && uriBase.equals(docBase) && href != null
092: && href.length() > 0 && href.charAt(0) == '#') {
093: // IMPORTANT: this prevents prepending the document URI
094: // to a relative URI if the reference is a local one.
095: // See:
096: // http://www.ietf.org/rfc/rfc2396.txt
097: // Paragraph 4.2 Same-Document References.
098: uriBase = null;
099: }
100: try {
101: if (uriBase != null) {
102: return URLResolver.resolve(uriBase, href);
103: } else {
104: return href;
105: }
106: } catch (IllegalArgumentException iae) {
107: return null;
108: }
109: }
110:
111: /**
112: * @param href the new anchor's href
113: */
114: public void setHref(final String href) {
115: if (href == null) {
116: throw new IllegalArgumentException();
117: }
118:
119: if (href.equals(this .href)) {
120: return;
121: }
122:
123: modifyingNode();
124: this .href = href;
125: modifiedNode();
126: }
127:
128: /**
129: * @return the anchor's target
130: */
131: public String getTarget() {
132: return target;
133: }
134:
135: /**
136: * @param target the new anchor target. Should not be null.
137: */
138: public void setTarget(final String target) {
139: if (target == null) {
140: throw new IllegalArgumentException();
141: }
142:
143: if (target.equals(this .target)) {
144: return;
145: }
146:
147: modifyingNode();
148: this .target = target;
149: modifiedNode();
150: }
151:
152: /**
153: * Anchor handles the target trait.
154: *
155: * @param traitName the name of the trait which the element may support.
156: * @return true if this element supports the given trait in one of the
157: * trait accessor methods.
158: */
159: boolean supportsTrait(final String traitName) {
160: if (SVGConstants.SVG_TARGET_ATTRIBUTE == traitName) {
161: return true;
162: } else {
163: return super .supportsTrait(traitName);
164: }
165: }
166:
167: /**
168: * Supported traits: xlink:href
169: *
170: * @param namespaceURI the trait's namespace.
171: * @param traitName the name of the trait which the element may support.
172: * @return true if this element supports the given trait in one of the
173: * trait accessor methods.
174: */
175: boolean supportsTraitNS(final String namespaceURI,
176: final String traitName) {
177: if (SVGConstants.XLINK_NAMESPACE_URI == namespaceURI
178: && SVGConstants.SVG_HREF_ATTRIBUTE == traitName) {
179: return true;
180: } else {
181: return super .supportsTraitNS(namespaceURI, traitName);
182: }
183: }
184:
185: /**
186: * Anchor handles the target trait.
187: *
188: * @param name the requested trait name
189: * @return the requested trait's value.
190: *
191: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
192: * trait is not supported on this element or null.
193: * @throws DOMException with error code TYPE_MISMATCH_ERR if requested
194: * trait's computed value cannot be converted to a String (SVG Tiny only).
195: */
196: public String getTraitImpl(final String name) throws DOMException {
197: if (SVGConstants.SVG_TARGET_ATTRIBUTE == name) {
198: return getTarget();
199: } else {
200: return super .getTraitImpl(name);
201: }
202: }
203:
204: /**
205: * Anchor handles the xlink:href attribute
206: *
207: * @param namespaceURI the URI for the requested trait.
208: * @param name the requested trait's local name (i.e., un-prefixed).
209: *
210: * @return the requested trait's value, as a string.
211: *
212: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
213: * trait is not supported on this element or null.
214: * @throws DOMException with error code TYPE_MISMATCH_ERR if requested
215: * trait's computed value cannot be converted to a String (SVG Tiny only).
216: * @throws SecurityException if the application does not have the necessary
217: * privilege rights to access this (SVG) content.
218: */
219: String getTraitNSImpl(final String namespaceURI, final String name)
220: throws DOMException {
221: if (SVGConstants.XLINK_NAMESPACE_URI == namespaceURI
222: && SVGConstants.SVG_HREF_ATTRIBUTE == name) {
223: String res = getHref();
224: if (res == null) {
225: res = href;
226: }
227:
228: return res;
229: } else {
230: return super .getTraitNSImpl(namespaceURI, name);
231: }
232: }
233:
234: /**
235: * Anchor handles the target trait.
236: *
237: * @param name the name of the trait to set.
238: * @param value the value of the trait to set.
239: *
240: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
241: * trait is not supported on this element or null.
242: * @throws DOMException with error code TYPE_MISMATCH_ERR if the requested
243: * trait's value cannot be specified as a String
244: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
245: * value is an invalid value for the given trait or null.
246: * @throws DOMException with error code NO_MODIFICATION_ALLOWED_ERR: if
247: * attempt is made to change readonly trait.
248: */
249: public void setTraitImpl(final String name, final String value)
250: throws DOMException {
251: if (SVGConstants.SVG_TARGET_ATTRIBUTE == name) {
252: if (value == null) {
253: throw illegalTraitValue(name, value);
254: }
255: setTarget(value);
256: } else {
257: super .setTraitImpl(name, value);
258: }
259: }
260:
261: /**
262: * Anchor supports the xlink:href trait.
263: *
264: * @param namespaceURI the URI for the trait's namespace.
265: * @param name the trait's local name (i.e., un-prefixed).
266: * @param value the trait's value.
267: *
268: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
269: * trait is not supported on this element or null.
270: * @throws DOMException with error code TYPE_MISMATCH_ERR if the requested
271: * trait's value cannot be specified as a String
272: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
273: * value is an invalid value for the given trait or null.
274: * @throws DOMException with error code NO_MODIFICATION_ALLOWED_ERR: if
275: * attempt is made to change readonly trait.
276: * @throws SecurityException if the application does not have the necessary
277: * privilege rights to access this (SVG) content.
278: */
279:
280: public void setTraitNSImpl(final String namespaceURI,
281: final String name, final String value) throws DOMException {
282: try {
283: if (SVGConstants.XLINK_NAMESPACE_URI == namespaceURI
284: && SVGConstants.SVG_HREF_ATTRIBUTE == name) {
285: setHref(value);
286: } else {
287: super .setTraitNSImpl(namespaceURI, name, value);
288: }
289: } catch (IllegalArgumentException iae) {
290: throw new DOMException(DOMException.INVALID_ACCESS_ERR, iae
291: .getMessage());
292: }
293: }
294:
295: /**
296: * @param traitName the trait name.
297: */
298: TraitAnim createTraitAnimImpl(final String traitName) {
299: if (SVGConstants.SVG_TARGET_ATTRIBUTE == traitName) {
300: return new StringTraitAnim(this , NULL_NS, traitName);
301: } else {
302: return super .createTraitAnimImpl(traitName);
303: }
304: }
305:
306: /**
307: * @param traitName the trait name.
308: * @param traitNamespace the trait's namespace. Should not be null.
309: */
310: TraitAnim createTraitAnimNSImpl(final String traitNamespace,
311: final String traitName) {
312: if (traitNamespace == SVGConstants.XLINK_NAMESPACE_URI
313: && traitName == SVGConstants.SVG_HREF_ATTRIBUTE) {
314: return new StringTraitAnim(this , traitNamespace, traitName);
315: }
316:
317: return super .createTraitAnimNSImpl(traitNamespace, traitName);
318: }
319:
320: public String toString() {
321: return "Anchor[href=(" + href + ") absolute href=(" + getHref()
322: + ")]";
323: }
324: }
|