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:
027: package com.sun.perseus.model;
028:
029: import org.w3c.dom.DOMException;
030:
031: /**
032: * @version $Id: StringTraitAnim.java,v 1.2 2006/04/21 06:38:55 st125089 Exp $
033: */
034: final class StringTraitAnim extends TraitAnim {
035: /**
036: * The trait's base value.
037: */
038: String[] baseValue = new String[1];
039:
040: /**
041: * Constructs a new StringTraitAnim for a given ElementNode trait
042: * in the given namespace.
043: *
044: * @param targetElement the ElementNode whose trait is animated.
045: * @param targetNamespace the target trait's namespace. Should not
046: * be null. The per-element partition namespace should be
047: * represented by the ElementNode.NULL_NS value.
048: * @param targetTrait the name of the animated trait.
049: */
050: public StringTraitAnim(final ElementNode targetElement,
051: final String traitNamespace, final String traitName) {
052: super (targetElement, traitNamespace, traitName,
053: ElementNode.TRAIT_TYPE_STRING);
054: }
055:
056: /**
057: * @return the trait's value, as a String.
058: */
059: protected String getTraitImpl() {
060: // This returns the computed trait value, using the specified
061: // trait value.
062: return targetElement
063: .validateTraitNS(traitNamespace, traitName,
064: getSpecifiedTraitNS(), targetElement
065: .getNamespaceURI(), targetElement
066: .getLocalName(), traitNamespace,
067: traitName);
068: }
069:
070: /**
071: * Sets the trait's base value, as a String.
072: *
073: * @param value the new trait base value.
074: *
075: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
076: * value is an invalid value for the given trait or null.
077: */
078: void setTraitImpl(String value) throws DOMException {
079: // Validate on set so that we do not get an error later.
080: // However, we keep the specified value around, for properties
081: // that may have relative or inherited values.
082: targetElement.validateTraitNS(traitNamespace, traitName, value,
083: targetElement.getNamespaceURI(), targetElement
084: .getLocalName(), traitNamespace, traitName);
085: specifiedTraitValue = value;
086: }
087:
088: /**
089: * Returns the BaseValue as an array of objects.
090: *
091: * @return the base value as an object array. The dimensions of the
092: * returned array depend on the trait.
093: * @see com.sun.perseus.model.BaseValue
094: */
095: public Object[] getBaseValue() {
096: baseValue[0] = getTraitImpl();
097: return baseValue;
098: }
099:
100: /**
101: * Applies the animation effect. The implementation makes sure it
102: * implements the sandwich model by 'pulling' values from the
103: * root animation (i.e., the animation with the highest priority).
104: */
105: void apply() {
106: String value = (String) rootAnim.compute()[0];
107:
108: if (traitNamespace == ElementNode.NULL_NS) {
109: targetElement.setTraitImpl(traitName, value);
110: } else {
111: targetElement.setTraitNSImpl(traitNamespace, traitName,
112: value);
113: }
114: }
115:
116: /**
117: * Converts the input values set to a RefValues object.
118: *
119: * @param anim the <code>Animation</code> for which the values should be
120: * converted.
121: * @param values a semi-colon seperated list of values which need to be
122: * validated.
123: * @param reqTraitNamespace the namespace of the trait which has the values
124: * value on the requesting element.
125: * @param reqTraitName the name of the trait which has the values value on
126: * the requesting element.
127: * @throws DOMException with error code INVALID_ACCESS_ERR if the input
128: * value is incompatible with the given trait.
129: * @throws NullPointerException if values is null.
130: */
131: RefValues toRefValues(final Animation anim, String[] values,
132: final String reqTraitNamespace, final String reqTraitName)
133: throws DOMException {
134: StringRefValues refValues = new StringRefValues();
135:
136: if (values.length < 1) {
137: throw new IllegalArgumentException();
138: }
139:
140: if (values.length == 1) {
141: String[] tmpValues = new String[2];
142: tmpValues[0] = values[0];
143: tmpValues[1] = values[0];
144: values = tmpValues;
145: }
146:
147: int nSegments = values.length - 1;
148: refValues.segments = new StringSegment[nSegments];
149:
150: // Build the first segment.
151: refValues.segments[0] = new StringSegment();
152: refValues.segments[0].start = new String[] { values[0] };
153: refValues.segments[0].end = new String[] { values[1] };
154:
155: targetElement.validateTraitNS(anim.traitNamespace,
156: anim.traitName, refValues.segments[0].start[0], anim
157: .getNamespaceURI(), anim.getLocalName(),
158: reqTraitNamespace, reqTraitName);
159:
160: targetElement.validateTraitNS(anim.traitNamespace,
161: anim.traitName, refValues.segments[0].end[0], anim
162: .getNamespaceURI(), anim.getLocalName(),
163: reqTraitNamespace, reqTraitName);
164:
165: StringSegment prevSegment = refValues.segments[0];
166:
167: for (int i = 1; i < nSegments; i++) {
168: refValues.segments[i] = new StringSegment();
169: refValues.segments[i].start = prevSegment.end;
170: refValues.segments[i].end = new String[] { values[i + 1] };
171: targetElement.validateTraitNS(anim.traitNamespace,
172: anim.traitName, refValues.segments[i].end[0], anim
173: .getNamespaceURI(), anim.getLocalName(),
174: reqTraitNamespace, reqTraitName);
175: prevSegment = refValues.segments[i];
176: }
177:
178: return refValues;
179: }
180:
181: /**
182: * Used to sum two animated trait values. Because String traits cannot
183: * be summed, this always returns the second parameter value.
184: *
185: * @param valueA the base value. May be null.
186: * @param valueB the value to add to the base value. If the baseValue
187: */
188: public Object[] sum(Object[] valueA, Object[] valueB) {
189: // String animations do not support additive behavior.
190: throw new Error();
191: }
192:
193: /**
194: * Used to multiply an animated trait value by a number of iterations.
195: *
196: * @param value the animated trait value to multiply.
197: * @param iter the number of iteration to account for.
198: * @return the multiply result.
199: */
200: public Object[] multiply(Object[] value, int iter) {
201: // String animations do not support additive behavior.
202: throw new Error();
203: }
204:
205: }
|