KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > TreeAttribute


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.tax;
20
21 import java.util.Iterator JavaDoc;
22
23 import org.netbeans.tax.spec.Element;
24 import org.netbeans.tax.spec.Attribute;
25
26 /**
27  * TreeAtribute represents attribute name value pair.
28  * Value of attribute can be: Text or EntityReference.
29  *
30  * @author Libor Kramolis
31  * @version 0.1
32  */

33 public class TreeAttribute extends TreeNode implements Element.Attribute, TreeNamedObjectMap.NamedObject {
34
35     /** */
36     public static final String JavaDoc PROP_NAME = "name"; // NOI18N
37
/** */
38     public static final String JavaDoc PROP_VALUE = "value"; // NOI18N
39
/** */
40     public static final String JavaDoc PROP_OWNER_ELEMENT = "ownerElement"; // NOI18N
41
/** */
42     public static final String JavaDoc PROP_SPECIFIED = "specified"; // NOI18N
43

44     /** -- can be null. */
45     private TreeElement ownerElement; //my "parent" -- element in which it is attribute // NOI18N
46

47     /** */
48     private TreeName name; //attribute qName
49

50     /** */
51     private TreeObjectList valueList;
52     
53     /** */
54     private boolean specified; //is the attribute specified in document? (or default)
55

56     /** */
57     private TreeNamedObjectMap.KeyListener mapKeyListener;
58     
59     
60     //
61
// init
62
//
63

64     /**
65      * Creates new TreeAttribute.
66      * @param qName XML qualified name e.g. "myns:root" or "root".
67      * @param value unnormalized attribute value (general refs allowed ???)
68      * @param specified true means that the attribute must be represented literaly in document
69      * @throws InvalidArgumentException if qName or value contains unacceptable values
70      */

71     public TreeAttribute (String JavaDoc qName, String JavaDoc value, boolean specified) throws InvalidArgumentException {
72         super ();
73         
74         TreeName treeName = new TreeName (qName);
75         checkName (treeName);
76         checkValue (value);
77         
78         this.name = treeName;
79         this.specified = specified;
80         this.valueList = new TreeObjectList (createValueListContentManager ());
81         setValueImpl (value);
82         
83         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("TreeAttribute::INIT : name = " + qName + " : specified = " + specified); // NOI18N
84
}
85     
86     /**
87      * Creates new specified TreeAttribute.
88      * @param qName XML qualified name e.g. "myns:root" or "root".
89      * @param value unnormalized attribute value (no general refs allowed)
90      * @throws InvalidArgumentException if qName or value contains unacceptable values
91      */

92     public TreeAttribute (String JavaDoc qName, String JavaDoc value) throws InvalidArgumentException {
93         this (qName, value, true);
94     }
95     
96     
97     /**
98      * Creates new TreeAttribute -- copy constructor.
99      */

100     protected TreeAttribute (TreeAttribute attribute) {
101         super (attribute);
102         
103         this.name = attribute.name;
104         this.specified = true; //??? -- copy will be specified
105
this.valueList = new TreeObjectList (createValueListContentManager ());
106         this.valueList.addAll ((TreeObjectList)attribute.valueList.clone ());
107     }
108     
109     
110     //
111
// from TreeObject
112
//
113

114     /**
115      */

116     public Object JavaDoc clone () {
117         return new TreeAttribute (this);
118     }
119     
120     /**
121      */

122     public boolean equals (Object JavaDoc object, boolean deep) {
123         if (!!! super.equals (object, deep))
124             return false;
125         
126         TreeAttribute peer = (TreeAttribute) object;
127         if (!!! Util.equals (this.getTreeName (), peer.getTreeName ()))
128             return false;
129         if (this.specified != peer.isSpecified ())
130             return false;
131         if (!!! Util.equals (this.valueList, peer.valueList))
132             return false;
133         
134         return true;
135     }
136     
137     /*
138      * Merge name and specified (sticky) properties and delegate value list merging.
139      */

140     public void merge (TreeObject treeObject) throws CannotMergeException {
141         super.merge (treeObject);
142         
143         TreeAttribute peer = (TreeAttribute) treeObject;
144         
145         try {
146             setTreeNameImpl (peer.getTreeName ());
147             setSpecifiedImpl (peer.isSpecified ());
148             valueList.merge (peer.valueList);
149         } catch (Exception JavaDoc exc) {
150             throw new CannotMergeException (treeObject, exc);
151         }
152     }
153     
154     
155     //
156
// read only
157
//
158

159     
160     /**
161      */

162     protected void setReadOnly (boolean newReadOnly) {
163         super.setReadOnly (newReadOnly);
164         
165         valueList.setReadOnly (newReadOnly);
166     }
167     
168     
169     //
170
// context
171
//
172

173     /**
174      */

175     public final boolean isInContext () {
176         return ( getOwnerElement () != null );
177     }
178     
179     /**
180      */

181     public final void removeFromContext () throws ReadOnlyException {
182         if ( isInContext () ) {
183             getOwnerElement ().removeAttribute (this);
184         }
185     }
186     
187     
188     //
189
// itself
190
//
191

192     /**
193      */

194     public final String JavaDoc getQName () {
195         return name.getQualifiedName ();
196     }
197     
198     /**
199      * @throws ReadOnlyException
200      * @throws InvalidArgumentException if given name is not acceptable by constains
201      */

202     public final void setQName (String JavaDoc name) throws ReadOnlyException, InvalidArgumentException {
203         setTreeName (new TreeName (name));
204     }
205     
206     /**
207      */

208     public final TreeName getTreeName () {
209         return name;
210     }
211     
212     /**
213      */

214     private final void setTreeNameImpl (TreeName newName) {
215         TreeName oldName = this.name;
216         
217         this.name = newName;
218         
219         fireMapKeyChanged (oldName);
220         firePropertyChange (PROP_NAME, oldName, newName);
221     }
222     
223     /**
224      * @throws ReadOnlyException
225      * @throws InvalidArgumentException if passed argument does not pass checks
226      */

227     public final void setTreeName (TreeName newName) throws ReadOnlyException, InvalidArgumentException {
228         //
229
// check new value
230
//
231
if ( Util.equals (this.name, newName) )
232             return;
233         checkReadOnly ();
234         checkName (newName);
235         
236         //
237
// set new value
238
//
239
setTreeNameImpl (newName);
240     }
241     
242     /**
243      */

244     protected final void checkName (TreeName name) throws InvalidArgumentException {
245         TreeUtilities.checkAttributeName (name);
246     }
247     
248     
249     
250     public boolean isSpecified () {
251         return specified;
252     }
253     
254     /**
255      * Set the value and fire a property change event.
256      * It may change just during merge operation.
257      */

258     private void setSpecifiedImpl (boolean newValue) {
259         if (this.specified == newValue)
260             return;
261         
262         Boolean JavaDoc oldValue = this.specified ? Boolean.TRUE : Boolean.FALSE;
263         
264         this.specified = newValue;
265         
266         firePropertyChange (PROP_SPECIFIED, oldValue, newValue ? Boolean.TRUE : Boolean.FALSE);
267     }
268     
269     /**
270      * @return structured representation of attribute value.
271      */

272     public final TreeObjectList getValueList () {
273         return valueList;
274     }
275     
276     /**
277      * @return resolved attribute text value
278      */

279     public final String JavaDoc getValue () {
280         StringBuffer JavaDoc value = new StringBuffer JavaDoc (23);
281         Iterator JavaDoc it = valueList.iterator ();
282         
283         while (it.hasNext ()) {
284             Object JavaDoc next = it.next ();
285             if (next instanceof TreeData) {
286                 value.append (((TreeData)next).getData ());
287             } else if (next instanceof TreeGeneralEntityReference) {
288                 //!!! resolve it
289
value.append ("&" + ((TreeGeneralEntityReference)next).getName () + ";"); // NOI18N
290
} else if (next instanceof TreeCharacterReference) {
291                 value.append (((TreeCharacterReference)next).getData ());
292             }
293         }
294         return value.toString ();
295     }
296     
297     /**
298      * @return unresolved attribute value
299      */

300     public final String JavaDoc getNonNormalizedValue () {
301         StringBuffer JavaDoc value = new StringBuffer JavaDoc (23);
302         Iterator JavaDoc it = valueList.iterator ();
303         
304         while (it.hasNext ()) {
305             Object JavaDoc next = it.next ();
306             if (next instanceof TreeData) {
307                 value.append (((TreeData)next).getData ());
308             } else if (next instanceof TreeGeneralEntityReference) {
309                 value.append ("&" + ((TreeGeneralEntityReference)next).getName () + ";"); // NOI18N
310
} else if (next instanceof TreeCharacterReference) {
311                 value.append ("&" + ((TreeCharacterReference)next).getName () + ";"); // NOI18N
312
}
313         }
314         return value.toString ();
315     }
316     
317     /**
318      * Simplified attribute value setter.
319      *
320      */

321     private final void setValueImpl (String JavaDoc newValue) {
322         String JavaDoc oldValue = this.getValue ();
323         
324         this.valueList.clear ();
325         
326         if ( newValue.length () != 0) {
327             try {
328                 TreeText newText = new TreeText (newValue);
329                 this.valueList.add (newText);
330             } catch (TreeException exc) {
331                 // something is wrong -- OK
332
}
333         }
334         
335         firePropertyChange (PROP_VALUE, oldValue, newValue);
336     }
337     
338     /**
339      * Simplified attribute value setter.
340      *
341      * @throws ReadOnlyException
342      * @throws InvalidArgumentException
343      */

344     public final void setValue (String JavaDoc newValue) throws ReadOnlyException, InvalidArgumentException {
345         //
346
// check new value
347
//
348
if ( Util.equals (this.getValue (), newValue) )
349             return;
350         checkReadOnly ();
351         checkValue (newValue);
352         
353         //
354
// set new value
355
//
356
setValueImpl (newValue);
357     }
358     
359     /**
360      * Check value being set by setValue
361      */

362     protected final void checkValue (String JavaDoc value) throws InvalidArgumentException {
363         TreeUtilities.checkAttributeValue (value);
364     }
365     
366     
367     
368     //
369
// Namespaces
370
//
371

372     /**
373      * @return attribute namespace or TreeNamespace.NO_NAMESPACE.
374      */

375     public final TreeNamespace getNamespace () {
376         if (getOwnerElement () != null) {
377             TreeElement owner = getOwnerElement ();
378             TreeNamespaceContext ctx = owner.getNamespaceContext ();
379             String JavaDoc prefix = getNamespacePrefix ();
380             String JavaDoc uri = ctx.getURI (prefix);
381             if (uri == null) {
382                 return TreeNamespace.NO_NAMESPACE;
383             } else {
384                 return new TreeNamespace (prefix, uri);
385             }
386         }
387         return TreeNamespace.NO_NAMESPACE;
388     }
389     
390     /**
391      */

392     public final String JavaDoc getNamespacePrefix () {
393         return name.getPrefix ();
394     }
395     
396     
397     /**
398      */

399     public final String JavaDoc getNamespaceURI () {
400         return getNamespace ().getURI ();
401     }
402     
403     
404     /**
405      */

406     public final String JavaDoc getLocalName () {
407         return name.getName ();
408     }
409     
410     
411     //
412
// TreeNamedObjectMap.NamedObject
413
//
414

415     /**
416      */

417     public Object JavaDoc mapKey () {
418         return getTreeName ();
419     }
420     
421     /**
422      */

423     // public String mapKeyPropertyName () {
424
// return PROP_NAME;
425
// }
426

427     /** Attach NamedObject to NamedObject Map. */
428     public void setKeyListener (TreeNamedObjectMap.KeyListener keyListener) {
429         mapKeyListener = keyListener;
430     }
431     
432     private void fireMapKeyChanged (Object JavaDoc oldKey) {
433         if ( mapKeyListener == null ) {
434             return;
435         }
436         mapKeyListener.mapKeyChanged (oldKey);
437     }
438     
439     
440     //
441
// from TreeNode
442
//
443

444     /**
445      */

446     public final TreeDocumentRoot getOwnerDocument () {
447         if ( getOwnerElement () == null )
448             return null;
449         return getOwnerElement ().getOwnerDocument ();
450     }
451     
452     //
453
// ownerElement
454
//
455

456     /**
457      */

458     public final TreeElement getOwnerElement () {
459         return ownerElement;
460     }
461     
462     /**
463      */

464     protected final void setOwnerElement (TreeElement newOwnerElement) {
465         if (Util.equals (ownerElement, newOwnerElement))
466             return;
467         
468         TreeElement oldOwnerElement = this.ownerElement;
469         
470         this.ownerElement = newOwnerElement;
471         
472         firePropertyChange (getEventChangeSupport ().createEvent (PROP_OWNER_ELEMENT, oldOwnerElement, newOwnerElement));
473     }
474     
475     
476     
477     //
478
// TreeObjectList.ContentManager
479
//
480

481     /**
482      */

483     protected TreeObjectList.ContentManager createValueListContentManager () {
484         return new ValueListContentManager ();
485     }
486     
487     
488     /**
489      *
490      */

491     protected class ValueListContentManager extends TreeObjectList.ContentManager {
492         
493         /**
494          */

495         public TreeNode getOwnerNode () {
496             return TreeAttribute.this;
497         }
498         
499         /**
500          */

501         public void checkAssignableObject (Object JavaDoc obj) {
502             super.checkAssignableObject (obj);
503             checkAssignableClass (Attribute.Value.class, obj);
504         }
505         
506         /** */
507         public void objectInserted (TreeObject obj) {
508             TreeAttribute.this.firePropertyChange (PROP_VALUE, TreeAttribute.this.valueList, obj); //!!!
509
}
510         
511         /** */
512         public void objectRemoved (TreeObject obj) {
513             TreeAttribute.this.firePropertyChange (PROP_VALUE, TreeAttribute.this.valueList, obj); //!!!
514
}
515         
516         /** */
517         public void orderChanged (int[] permutation) {
518             TreeAttribute.this.firePropertyChange (PROP_VALUE, TreeAttribute.this.valueList, permutation); //!!!
519
}
520         
521     } // end: class ValueListContentManager
522

523 }
524
Popular Tags