KickJava   Java API By Example, From Geeks To Geeks.

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


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.Arrays JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 /**
27  * @author Libor Kramolis
28  * @version 0.1
29  */

30 public class TreeAttlistDeclAttributeDef extends TreeNodeDecl.Content implements TreeNamedObjectMap.NamedObject {
31
32     /** */
33     public static final String JavaDoc PROP_NAME = "name"; // NOI18N
34
/** */
35     public static final String JavaDoc PROP_TYPE = "type"; // NOI18N
36
/** */
37     public static final String JavaDoc PROP_ENUMERATED_TYPE = "enumeratedType"; // NOI18N
38
/** */
39     public static final String JavaDoc PROP_DEFAULT_TYPE = "defaultType"; // NOI18N
40
/** */
41     public static final String JavaDoc PROP_DEFAULT_VALUE = "defaultValue"; // NOI18N
42

43     /** */
44     public static final short TYPE_CDATA = 0;
45     /** */
46     public static final short TYPE_ID = 1;
47     /** */
48     public static final short TYPE_IDREF = 2;
49     /** */
50     public static final short TYPE_IDREFS = 3;
51     /** */
52     public static final short TYPE_ENTITY = 4;
53     /** */
54     public static final short TYPE_ENTITIES = 5;
55     /** */
56     public static final short TYPE_NMTOKEN = 6;
57     /** */
58     public static final short TYPE_NMTOKENS = 7;
59     /** */
60     public static final short TYPE_ENUMERATED = 8;
61     /** */
62     public static final short TYPE_NOTATION = 9;
63     
64     /** */
65     public static final short DEFAULT_TYPE_NULL = 0;
66     /** */
67     public static final short DEFAULT_TYPE_REQUIRED = 1;
68     /** */
69     public static final short DEFAULT_TYPE_IMPLIED = 2;
70     /** */
71     public static final short DEFAULT_TYPE_FIXED = 3;
72     
73     /** */
74     public static final String JavaDoc[] NAMED_TYPE_LIST = new String JavaDoc [] {
75         "CDATA", // TYPE_CDATA // NOI18N
76
"ID", // TYPE_ID // NOI18N
77
"IDREF", // TYPE_IDREF // NOI18N
78
"IDREFS", // TYPE_IDREFS // NOI18N
79
"ENTITY", // TYPE_ENTITY // NOI18N
80
"ENTITIES", // TYPE_ENTITIES // NOI18N
81
"NMTOKEN", // TYPE_NMTOKEN // NOI18N
82
"NMTOKENS", // TYPE_NMTOKENS // NOI18N
83
null, // TYPE_ENUMERATED
84
"NOTATION" // TYPE_NOTATION // NOI18N
85
};
86     
87     /** */
88     public static final String JavaDoc[] NAMED_DEFAULT_TYPE_LIST = new String JavaDoc [] {
89         null, // DEFAULT_TYPE_NULL
90
"#REQUIRED", // DEFAULT_TYPE_REQUIRED // NOI18N
91
"#IMPLIED", // DEFAULT_TYPE_IMPLIED // NOI18N
92
"#FIXED" // DEFAULT_TYPE_FIXED // NOI18N
93
};
94     
95     
96     /** */
97     private String JavaDoc name;
98     
99     /** */
100     private short type;
101     
102     /** */
103     private String JavaDoc[] enumeratedType;
104     
105     /** */
106     private short defaultType;
107     
108     /** */
109     private String JavaDoc defaultValue;
110     
111     /** */
112     private TreeNamedObjectMap.KeyListener mapKeyListener;
113     
114     
115     //
116
// init
117
//
118

119     /** Creates new TreeAttlistDeclAttributeDef. */
120     public TreeAttlistDeclAttributeDef (String JavaDoc name, short type, String JavaDoc[] enumeratedType, short defaultType, String JavaDoc defaultValue) throws InvalidArgumentException {
121         super ();
122         
123         checkName (name);
124         checkType (type, enumeratedType);
125         checkDefaultType (defaultType, defaultValue);
126         this.name = name;
127         this.type = type;
128         this.enumeratedType = enumeratedType;
129         this.defaultType = defaultType;
130         this.defaultValue = defaultValue;
131     }
132     
133     /** Creates new TreeAttlistDeclAttributeDef -- copy constructor. */
134     protected TreeAttlistDeclAttributeDef (TreeAttlistDeclAttributeDef attributeDef) {
135         super (attributeDef);
136         
137         this.name = attributeDef.name;
138         this.type = attributeDef.type;
139         this.enumeratedType = arraycopy (attributeDef.enumeratedType);
140         this.defaultType = attributeDef.defaultType;
141         this.defaultValue = attributeDef.defaultValue;
142     }
143     
144     
145     //
146
// itself
147
//
148

149     /**
150      */

151     private String JavaDoc[] arraycopy (String JavaDoc[] array) {
152         if ( array == null )
153             return null;
154         
155         int length = array.length;
156         String JavaDoc[] arrayCopy = new String JavaDoc [length];
157         System.arraycopy (array, 0, arrayCopy, 0, length);
158         
159         return arrayCopy;
160     }
161     
162     
163     //
164
// from TreeObject
165
//
166

167     /**
168      */

169     public Object JavaDoc clone () {
170         return new TreeAttlistDeclAttributeDef (this);
171     }
172     
173     /**
174      */

175     public boolean equals (Object JavaDoc object, boolean deep) {
176         if (!!! super.equals (object, deep))
177             return false;
178         
179         TreeAttlistDeclAttributeDef peer = (TreeAttlistDeclAttributeDef) object;
180         if (!!! Util.equals (this.name, peer.name))
181             return false;
182         if (this.type != peer.type)
183             return false;
184         if (!!! Util.equals (this.enumeratedType, peer.enumeratedType))
185             return false;
186         if (this.defaultType != peer.defaultType)
187             return false;
188         if (!!! Util.equals (this.defaultValue, peer.defaultValue))
189             return false;
190         
191         return true;
192     }
193     
194     /*
195      * Merge name, type, enumeration, default type and default value properties.
196      */

197     public void merge (TreeObject treeObject) throws CannotMergeException {
198         super.merge (treeObject);
199         
200         TreeAttlistDeclAttributeDef peer = (TreeAttlistDeclAttributeDef) treeObject;
201         setNameImpl (peer.getName ());
202         setDefaultTypeImpl (peer.getDefaultType (), peer.getDefaultValue ());
203         setTypeImpl (peer.getType (), peer.getEnumeratedType ());
204     }
205     
206     
207     //
208
// context
209
//
210

211     /**
212      */

213     public final void removeFromContext () throws ReadOnlyException {
214         if ( isInContext () ) {
215             getOwnerAttlistDecl ().removeAttributeDef (this.getName ());
216         }
217     }
218     
219     
220     //
221
// itself
222
//
223

224     /**
225      */

226     public final TreeAttlistDecl getOwnerAttlistDecl () {
227         return (TreeAttlistDecl)getNodeDecl ();
228     }
229     
230     
231     //
232
// name
233
//
234

235     /**
236      */

237     public final String JavaDoc getElementName () {
238         if ( getNodeDecl () == null )
239             return null;
240         return getOwnerAttlistDecl ().getElementName ();
241     }
242     
243     /**
244      */

245     public final String JavaDoc getName () {
246         return name;
247     }
248     
249     /**
250      */

251     private final void setNameImpl (String JavaDoc newName) {
252         String JavaDoc oldName = this.name;
253         
254         this.name = newName;
255         
256         fireMapKeyChanged (oldName);
257 // firePropertyChange (PROP_NAME, oldName, newName);
258
// getNodeDecl().firePropertyChange (TreeAttlistDecl.PROP_ATTRIBUTE_DEF_MAP_CONTENT, this, this); //!!!
259
}
260     
261     /**
262      * @throws ReadOnlyException
263      * @throws InvalidArgumentException
264      */

265     public final void setName (String JavaDoc newName) throws ReadOnlyException, InvalidArgumentException {
266         //
267
// check new value
268
//
269
if ( Util.equals (this.name, newName) )
270             return;
271         checkReadOnly ();
272         checkName (newName);
273         
274         //
275
// set new value
276
//
277
setNameImpl (newName);
278     }
279     
280     /**
281      */

282     protected final void checkName (String JavaDoc name) throws InvalidArgumentException {
283         TreeUtilities.checkAttlistDeclAttributeName (name);
284     }
285     
286     
287     //
288
// type
289
//
290

291     /**
292      */

293     public final short getType () {
294         return type;
295     }
296     
297     /**
298      */

299     public final String JavaDoc getTypeName () {
300         try {
301             return NAMED_TYPE_LIST [type];
302         } catch (ArrayIndexOutOfBoundsException JavaDoc exc) {
303             return null;
304         }
305     }
306     
307     /**
308      */

309     public final String JavaDoc[] getEnumeratedType () {
310         return enumeratedType;
311     }
312     
313     /**
314      */

315     public final String JavaDoc getEnumeratedTypeString () {
316         if ( enumeratedType == null ) {
317             return null;
318         }
319         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
320         sb.append ("( ").append (enumeratedType[0]); // NOI18N
321
for ( int i = 1; i < enumeratedType.length; i++ ) {
322             sb.append (" | ").append (enumeratedType [i]); // NOI18N
323
}
324         sb.append (" )"); // NOI18N
325
return sb.toString ();
326     }
327     
328     public static final String JavaDoc[] createEnumeratedType (String JavaDoc enumeratedType) {
329         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("TreeAttlistDeclAttributeDef.createEnumeratedType: enumeratedType = " + enumeratedType);
330
331         if ( enumeratedType == null ) {
332             return null;
333         }
334         int begin = enumeratedType.indexOf ("("); // NOI18N
335
int end = enumeratedType.indexOf (")"); // NOI18N
336

337         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" begin = " + begin);
338         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" end = " + end);
339
340         if ( ( begin == -1 ) ||
341              ( end == -1 ) ) {
342             return null;
343         }
344         String JavaDoc noParenthesis = enumeratedType.substring (begin + 1, end);
345         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc (noParenthesis, "|"); // NOI18N
346
List JavaDoc tokens = new LinkedList JavaDoc ();
347         while (st.hasMoreTokens ()) {
348             tokens.add (st.nextToken ().trim ());
349         }
350
351         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" tokens = " + tokens);
352
353         if ( tokens.isEmpty () )
354             return null;
355
356         String JavaDoc[] arrayType = (String JavaDoc[])tokens.toArray (new String JavaDoc[0]);
357
358         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" RETURN arrayType = " + arrayType);
359
360         return arrayType;
361     }
362     
363     /**
364      */

365     private final void setTypeImpl (short newType, String JavaDoc[] newEnumeratedType) {
366 // short oldType = this.type;
367
// String[] oldEnumeratedType = this.enumeratedType;
368

369         this.type = newType;
370         this.enumeratedType = newEnumeratedType;
371         
372 // firePropertyChange (PROP_???, old???, new???);
373
// getNodeDecl().firePropertyChange (TreeAttlistDecl.PROP_ATTRIBUTE_DEF_MAP_CONTENT, this, this); //!!!
374
}
375     
376     /**
377      * @throws ReadOnlyException
378      * @throws InvalidArgumentException
379      */

380     public final void setType (short newType, String JavaDoc[] newEnumeratedType) throws ReadOnlyException, InvalidArgumentException {
381         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("TreeAttlistDeclAttributeDef.setType");
382         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" newType = " + newType);
383         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" newEnumeratedType = " + ( newEnumeratedType == null ? null : Arrays.asList (newEnumeratedType)));
384
385         //
386
// check new value
387
//
388
boolean setType = this.type != newType;
389         boolean setEnumeratedType = !!! Arrays.equals (this.enumeratedType, newEnumeratedType);
390
391         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" setEnumeratedType = " + setEnumeratedType);
392
393         if ( !!! setType &&
394              !!! setEnumeratedType ) {
395             return;
396         }
397         checkReadOnly ();
398         checkType (newType, newEnumeratedType);
399         
400         //
401
// set new value
402
//
403
setTypeImpl (newType, newEnumeratedType);
404     }
405     
406     
407     /**
408      */

409     protected final void checkType (short type, String JavaDoc[] enumeratedType) throws InvalidArgumentException {
410         TreeUtilities.checkAttlistDeclAttributeType (type);
411         TreeUtilities.checkAttlistDeclAttributeEnumeratedType (enumeratedType);
412     }
413     
414     /**
415      */

416     public static final short findType (String JavaDoc type) {
417         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("TreeAttlistDeclAttributeDef::findType: type = " + type); // NOI18N
418

419         for ( short i = 0; i < NAMED_TYPE_LIST.length; i++ ) {
420             if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" test_type = " + NAMED_TYPE_LIST[i]); // NOI18N
421

422             if ( Util.equals (NAMED_TYPE_LIST[i], type) ) {
423                 return i;
424             }
425         }
426         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" type[" + type + "] not found"); // NOI18N
427

428         return -1;
429     }
430     
431     //
432
// default decl
433
//
434

435     /**
436      */

437     public final short getDefaultType () {
438         return defaultType;
439     }
440     
441     /**
442      */

443     public final String JavaDoc getDefaultTypeName () {
444         try {
445             return NAMED_DEFAULT_TYPE_LIST [defaultType];
446         } catch (ArrayIndexOutOfBoundsException JavaDoc exc) {
447             return null;
448         }
449     }
450     
451     /**
452      */

453     public final String JavaDoc getDefaultValue () {
454         return defaultValue;
455     }
456     
457     /**
458      */

459     private final void setDefaultTypeImpl (short newDefaultType, String JavaDoc newDefaultValue) {
460         // short oldDefaultType = this.defaultType;
461
// String oldDefaultValue = this.defaultValue;
462

463         this.defaultType = newDefaultType;
464         this.defaultValue = newDefaultValue;
465         
466 // firePropertyChange (PROP_???, old???, new???);
467
// getNodeDecl().firePropertyChange (TreeAttlistDecl.PROP_ATTRIBUTE_DEF_MAP_CONTENT, this, this); //!!!
468
}
469     
470     /**
471      * @throws ReadOnlyException
472      * @throws InvalidArgumentException
473      */

474     public final void setDefaultType (short newDefaultType, String JavaDoc newDefaultValue) throws ReadOnlyException, InvalidArgumentException {
475         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("TreeAttlistDeclAttributeDef.setDefaultType");
476         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" newDefaultType = " + NAMED_DEFAULT_TYPE_LIST [newDefaultType]);
477         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" newDefaultValue = " + newDefaultValue);
478
479         //
480
// check new value
481
//
482
boolean setDefaultType = this.defaultType != newDefaultType;
483         boolean setDefaultValue = !!! Util.equals (this.defaultValue, newDefaultValue);
484         if ( !!! setDefaultType &&
485              !!! setDefaultValue ) {
486             return;
487         }
488         checkReadOnly ();
489         checkDefaultType (newDefaultType, newDefaultValue);
490         
491         //
492
// set new value
493
//
494
setDefaultTypeImpl (newDefaultType, newDefaultValue);
495     }
496     
497     /**
498      */

499     protected final void checkDefaultType (short defaultType, String JavaDoc defaultValue) throws InvalidArgumentException {
500         TreeUtilities.checkAttlistDeclAttributeDefaultType (defaultType);
501         TreeUtilities.checkAttlistDeclAttributeDefaultValue (defaultValue);
502     }
503     
504     /**
505      */

506     public static final short findDefaultType (String JavaDoc defaultType) {
507         for ( short i = 0; i < NAMED_DEFAULT_TYPE_LIST.length; i++ ) {
508             if ( Util.equals (NAMED_DEFAULT_TYPE_LIST[i], defaultType) )
509                 return i;
510         }
511         return -1;
512     }
513     
514     
515     //
516
// TreeNamedObjectMap.NamedObject
517
//
518

519     /**
520      */

521     public Object JavaDoc mapKey () {
522         return getName ();
523     }
524     
525     /**
526      */

527     // public String mapKeyPropertyName () {
528
// return PROP_NAME;
529
// }
530

531     /** Attach NamedObject to NamedObject Map. */
532     public void setKeyListener (TreeNamedObjectMap.KeyListener keyListener) {
533         mapKeyListener = keyListener;
534     }
535     
536     private void fireMapKeyChanged (Object JavaDoc oldKey) {
537         if ( mapKeyListener == null ) {
538             return;
539         }
540         mapKeyListener.mapKeyChanged (oldKey);
541     }
542     
543 }
544
Popular Tags