001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.tax;
042:
043: import org.netbeans.tax.spec.DocumentFragment;
044: import org.netbeans.tax.spec.Element;
045: import org.netbeans.tax.spec.GeneralEntityReference;
046: import org.netbeans.tax.spec.Attribute;
047:
048: /**
049: *
050: * @author Libor Kramolis
051: * @version 0.1
052: */
053: public class TreeCharacterReference extends TreeChild implements
054: TreeReference, TreeCharacterData, DocumentFragment.Child,
055: Element.Child, GeneralEntityReference.Child, Attribute.Value {
056: /** */
057: public static final String PROP_NAME = "name"; // NOI18N
058:
059: /** */
060: private String name; //literal occuring in document "#99" // NOI18N
061:
062: //
063: // init
064: //
065:
066: /** Creates new TreeCharacterReference.
067: * @throws InvalidArgumentException
068: */
069: public TreeCharacterReference(String name)
070: throws InvalidArgumentException {
071: super ();
072:
073: checkName(name);
074: this .name = name;
075: }
076:
077: /** Creates new TreeCharacterReference -- copy constructor. */
078: protected TreeCharacterReference(
079: TreeCharacterReference characterReference) {
080: super (characterReference);
081:
082: this .name = characterReference.name;
083: }
084:
085: //
086: // from TreeObject
087: //
088:
089: /**
090: */
091: public Object clone() {
092: return new TreeCharacterReference(this );
093: }
094:
095: /**
096: */
097: public boolean equals(Object object, boolean deep) {
098: if (!!!super .equals(object, deep))
099: return false;
100:
101: TreeCharacterReference peer = (TreeCharacterReference) object;
102: if (!!!Util.equals(this .getName(), peer.getName()))
103: return false;
104:
105: return true;
106: }
107:
108: /*
109: * Merge name property.
110: */
111: public void merge(TreeObject treeObject)
112: throws CannotMergeException {
113: super .merge(treeObject);
114:
115: TreeCharacterReference peer = (TreeCharacterReference) treeObject;
116: setNameImpl(peer.getName());
117: }
118:
119: //
120: // itself
121: //
122:
123: public final String getName() {
124: return name;
125: }
126:
127: /**
128: */
129: private final void setNameImpl(String newName) {
130: String oldName = this .name;
131:
132: this .name = newName;
133:
134: firePropertyChange(PROP_NAME, oldName, newName);
135: }
136:
137: /**
138: * @throws ReadOnlyException
139: * @throws InvalidArgumentException
140: */
141: public final void setName(String newName) throws ReadOnlyException,
142: InvalidArgumentException {
143: //
144: // check new value
145: //
146: if (Util.equals(this .name, newName))
147: return;
148: checkReadOnly();
149: checkName(newName);
150:
151: //
152: // set new value
153: //
154: setNameImpl(newName);
155: }
156:
157: /**
158: */
159: protected final void checkName(String name)
160: throws InvalidArgumentException {
161: TreeUtilities.checkCharacterReferenceName(name);
162: }
163:
164: /**
165: * @return string representing value (may be a surrogate)
166: */
167: public final String getData() {
168:
169: //!!! does not work for surrogates
170:
171: short val;
172:
173: if (name.startsWith("#x")) { // NOI18N
174: val = Short.parseShort(name.substring(2), 16);
175: } else {
176: val = Short.parseShort(name.substring(1));
177: }
178: return new String(new char[] { (char) val });
179: }
180:
181: }
|