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.util.SVGConstants;
029:
030: import org.w3c.dom.DOMException;
031:
032: /**
033: * A <code>GenericElementNode</code> is used to model elements which are either
034: * unknown to Perseus or have no special behavior or attributes.
035: *
036: * Note that <code>GenericElementNode</code> is needed because we want to avoid
037: * storing a namespaceURI and localName references for nodes which always return
038: * constants for these values. Another option was to have namespaceURI and
039: * localName be attributes of <code>ElementNode</code>, but that would have
040: * forced us to carry two extra references on all nodes, which is superfluous.
041: *
042: * @version $Id: GenericElementNode.java,v 1.3 2006/04/21 06:37:21 st125089 Exp $
043: */
044: public class GenericElementNode extends ElementNode {
045: /**
046: * The element's namespace URI.
047: */
048: protected String namespaceURI;
049:
050: /**
051: * The element's local name.
052: */
053: protected String localName;
054:
055: /**
056: * The text content
057: */
058: protected String content;
059:
060: /**
061: * Constructor, providing the element's namespace and local name
062: *
063: * @param namespaceURI the element's namespace. May be null.
064: * @param localName the element's localName. Should not be null.
065: * @param doc the <code>DocumentNode</code> this node belongs to. Should
066: * not be null.
067: * @throws IllegalArgumentException if one of the arguments is null.
068: */
069: public GenericElementNode(final String namespaceURI,
070: final String localName, final DocumentNode doc) {
071: super (doc);
072:
073: if (localName == null) {
074: throw new IllegalArgumentException();
075: }
076:
077: this .namespaceURI = namespaceURI;
078: this .localName = localName;
079: }
080:
081: /**
082: * @return the namespaceURI provided in the constructor.
083: */
084: public String getNamespaceURI() {
085: return namespaceURI;
086: }
087:
088: /**
089: * @return the localName provided in the constructor.
090: */
091:
092: public String getLocalName() {
093: return localName;
094: }
095:
096: /**
097: * Used by <code>DocumentNode</code> to create a new instance from
098: * a prototype <code>GenericElementNode</code>.
099: *
100: * @param doc the <code>DocumentNode</code> for which a new node is
101: * should be created.
102: * @return a new <code>GenericElementNode</code> for the requested document.
103: */
104: public ElementNode newInstance(final DocumentNode doc) {
105: return new GenericElementNode(namespaceURI, localName, doc);
106: }
107:
108: /**
109: * @param text the text to append to this node's content.
110: * If text is null or empty, this does nothing.
111: */
112: public void appendTextChild(final String text) {
113: if (text == null || text.length() == 0) {
114: return;
115: }
116:
117: if (content == null) {
118: setContent(text);
119: } else {
120: setContent(content + text);
121: }
122: }
123:
124: /**
125: * @param newContent this node's new content string
126: */
127: public void setContent(final String newContent) {
128: if (equal(newContent, content)) {
129: return;
130: }
131: modifyingNode();
132: this .content = newContent;
133: modifiedNode();
134: }
135:
136: /**
137: * @return this node's text content as a string
138: */
139: public String getContent() {
140: return content;
141: }
142:
143: /**
144: * Handles #text traits.
145: *
146: * @param name the requested trait's name (e.g., "#text")
147: * @return the requested trait's string value (e.g., "Hello SVG Text")
148: *
149: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
150: * trait is not supported on this element or null.
151: * @throws DOMException with error code TYPE_MISMATCH_ERR if requested
152: * trait's computed value cannot be converted to a String (SVG Tiny only).
153: */
154: public String getTraitImpl(final String name) throws DOMException {
155: if (SVGConstants.SVG_TEXT_PSEUDO_ATTRIBUTE == name) {
156: if (content == null) {
157: return "";
158: }
159: return getContent();
160: } else {
161: return super .getTraitImpl(name);
162: }
163: }
164:
165: /**
166: * @param traitName the trait name.
167: */
168: TraitAnim createTraitAnimImpl(final String traitName) {
169: if (SVGConstants.SVG_TEXT_PSEUDO_ATTRIBUTE == traitName) {
170: return new StringTraitAnim(this , NULL_NS, traitName);
171: } else {
172: return super .createTraitAnimImpl(traitName);
173: }
174: }
175:
176: /**
177: * Handles the #text traits.
178: *
179: * @param name the trait's name (e.g., "#text")
180: * @param value the trait's value (e.g, "Hello SVG Text")
181: *
182: * @throws DOMException with error code NOT_SUPPORTED_ERROR if the requested
183: * trait is not supported on this element or null.
184: * @throws DOMException with error code TYPE_MISMATCH_ERR if the requested
185: * trait's value cannot be specified as a String
186: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
187: * value is an invalid value for the given trait or null.
188: * @throws DOMException with error code NO_MODIFICATION_ALLOWED_ERR: if
189: * attempt is made to change readonly trait.
190: */
191: public void setTraitImpl(final String name, final String value)
192: throws DOMException {
193: if (SVGConstants.SVG_TEXT_PSEUDO_ATTRIBUTE == name) {
194: if (value == null) {
195: throw illegalTraitValue(name, value);
196: }
197: setContent(value);
198: } else {
199: super.setTraitImpl(name, value);
200: }
201: }
202:
203: }
|