KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
22
23 import org.netbeans.tax.spec.DTD;
24 import org.netbeans.tax.spec.ParameterEntityReference;
25 import org.netbeans.tax.spec.DocumentType;
26 import org.netbeans.tax.spec.ConditionalSection;
27
28 import org.netbeans.tax.decl.EMPTYType;
29 import org.netbeans.tax.decl.parser.ParserReader;
30 import org.netbeans.tax.decl.parser.ContentSpecParser;
31
32 /**
33  *
34  * @author Libor Kramolis
35  * @version 0.1
36  */

37 public class TreeElementDecl extends TreeNodeDecl implements DTD.Child, ParameterEntityReference.Child, DocumentType.Child, ConditionalSection.Child {
38     /** */
39     public static final String JavaDoc PROP_NAME = "name"; // NOI18N
40
/** */
41     public static final String JavaDoc PROP_CONTENT_TYPE = "contentType"; // NOI18N
42

43     
44     /** */
45     private String JavaDoc name;
46     
47     /** */
48     private ContentType contentType;
49     
50     // /** */
51
// private String contentTypeString;
52

53     
54     //
55
// init
56
//
57

58     /** Creates new TreeElementDecl.
59      * @throws InvalidArgumentException
60      */

61     public TreeElementDecl (String JavaDoc name, ContentType contentType) throws InvalidArgumentException {
62         super ();
63         
64         checkName (name);
65         checkContentType (contentType);
66         
67         this.name = name;
68         this.contentType = contentType;
69         this.contentType.setNodeDecl (this);
70     }
71     
72     
73     // /** Creates new TreeElementDecl.
74
// * @throws InvalidArgumentException
75
// */
76
// public TreeElementDecl (String name, String contentType) throws InvalidArgumentException {
77
// this (name, new ContentSpecParser().parseModel (new ParserReader (contentType)));
78
// }
79

80     /** Creates new TreeElementDecl -- copy constructor. */
81     protected TreeElementDecl (TreeElementDecl elementDecl) {
82         super (elementDecl);
83         
84         this.name = elementDecl.name;
85         this.contentType = (ContentType)elementDecl.contentType.clone ();
86     }
87     
88     
89     //
90
// from TreeObject
91
//
92

93     /**
94      */

95     public Object JavaDoc clone () {
96         return new TreeElementDecl (this);
97     }
98     
99     /**
100      */

101     public boolean equals (Object JavaDoc object, boolean deep) {
102         if (!!! super.equals (object, deep))
103             return false;
104         
105         TreeElementDecl peer = (TreeElementDecl) object;
106         if (!!! Util.equals (this.getName (), peer.getName ()))
107             return false;
108         if (!!! Util.equals (this.contentType, peer.contentType))
109             return false;
110         
111         return true;
112     }
113     
114     /*
115      * Merges name and content type properties.
116      */

117     public void merge (TreeObject treeObject) throws CannotMergeException {
118         super.merge (treeObject);
119         
120         TreeElementDecl peer = (TreeElementDecl) treeObject;
121         
122         setNameImpl (peer.getName ());
123         setContentTypeImpl (peer.getContentType ());
124         // contentType.merge (peer.contentType);
125
}
126     
127     
128     //
129
// itself
130
//
131

132     /**
133      */

134     public final String JavaDoc getName () {
135         return name;
136     }
137     
138     /**
139      */

140     private final void setNameImpl (String JavaDoc newName) {
141         String JavaDoc oldName = this.name;
142         
143         this.name = newName;
144         
145         firePropertyChange (PROP_NAME, oldName, newName);
146     }
147     
148     /**
149      * @throws ReadOnlyException
150      * @throws InvalidArgumentException
151      */

152     public final void setName (String JavaDoc newName) throws ReadOnlyException, InvalidArgumentException {
153         //
154
// check new value
155
//
156
if ( Util.equals (this.name, newName) )
157             return;
158         checkReadOnly ();
159         checkName (newName);
160         
161         //
162
// set new value
163
//
164
setNameImpl (newName);
165     }
166     
167     /**
168      */

169     protected final void checkName (String JavaDoc name) throws InvalidArgumentException {
170         TreeUtilities.checkElementDeclName (name);
171     }
172     
173     /**
174      */

175     public final ContentType getContentType () {
176         return contentType;
177     }
178     
179     /**
180      */

181     private final void setContentTypeImpl (ContentType newContentType) {
182         ContentType oldContentType = this.contentType;
183         
184         this.contentType = newContentType;
185         
186         firePropertyChange (PROP_CONTENT_TYPE, oldContentType, newContentType);
187     }
188     
189     /**
190      * @throws ReadOnlyException
191      * @throws InvalidArgumentException
192      */

193     public final void setContentType (ContentType newContentType) throws ReadOnlyException, InvalidArgumentException {
194         //
195
// check new value
196
//
197
if ( Util.equals (this.contentType, newContentType) )
198             return;
199         checkReadOnly ();
200         checkContentType (newContentType);
201         
202         //
203
// set new value
204
//
205
setContentTypeImpl (newContentType);
206     }
207     
208     /**
209      * @throws ReadOnlyException
210      * @throws InvalidArgumentException
211      */

212     public final void setContentType (String JavaDoc newContentType) throws ReadOnlyException, InvalidArgumentException {
213         setContentType (new ContentSpecParser ().parseModel (new ParserReader (newContentType)));
214     }
215     
216     /**
217      */

218     protected final void checkContentType (ContentType contentType) throws InvalidArgumentException {
219         TreeUtilities.checkElementDeclContentType (contentType);
220     }
221     
222     /** @return true for mixed model. */
223     public boolean isMixed () {
224         return getContentType ().isMixed ();
225     }
226     
227     /** */
228     public boolean isEmpty () {
229         return ! (allowText () || allowElements ());
230     }
231     
232     /** */
233     public boolean allowText () {
234         return getContentType ().allowText ();
235     }
236     
237     /** */
238     public boolean allowElements () {
239         return getContentType ().allowElements ();
240     }
241     
242     
243     /** */
244     public Collection JavaDoc getAttributeDefs () {
245         if ( getOwnerDTD () == null ) {
246             return null;
247         }
248         return getOwnerDTD ().getAttributeDeclarations (getName ());
249     }
250     
251     
252     //
253
// ContentType
254
//
255

256     /**
257      *
258      */

259     public abstract static class ContentType extends Content implements Comparable JavaDoc {
260         // public static final short TYPE_EMPTY = 0; // EMPTY
261
// public static final short TYPE_ANY = 1; // ANY
262
// public static final short TYPE_MIXED = 2; // (#PCDATA|xxx)*
263
// public static final short TYPE_CHILDREN = 3; // (xxx?,(yyy|zzz*))+
264

265         // public static final short SEPARATOR_CHOICE = 10; // xxx|yyy
266
// public static final short SEPARATOR_SEQUENCE = 11; // xxx,yyy
267

268         // public static final short OCCURS_ZERO_OR_ONE = 20; // xxx?
269
// public static final short OCCURS_EXACTLY_ONE = 21; // xxx
270
// public static final short OCCURS_ZERO_OR_MORE = 22; // xxx*
271
// public static final short OCCURS_ONE_OR_MORE = 23; // xxx+
272

273         /** */
274         private String JavaDoc multiplicity;
275         
276         private int index; // sample value of counter at creation time
277

278         private static int counter = 0; // to be able to create ordered collections
279

280         
281         //
282
// init
283
//
284

285         /** Creates new ContentType. */
286         protected ContentType (TreeElementDecl elementDecl) {
287             super (elementDecl);
288             
289             this.multiplicity = new String JavaDoc ();
290             
291             this.index = counter++;
292         }
293         
294         
295         /** Creates new ContentType. */
296         protected ContentType () {
297             this ((TreeElementDecl) null);
298         }
299         
300         /** Creates new ContentType -- copy constructor. */
301         protected ContentType (ContentType contentType) {
302             super (contentType);
303             
304             this.multiplicity = contentType.multiplicity;
305             
306             this.index = counter++;
307         }
308         
309         //
310
// from TreeObject
311
//
312

313         /**
314          */

315         public boolean equals (Object JavaDoc object, boolean deep) {
316             if (!!! super.equals (object, deep))
317                 return false;
318             
319             ContentType peer = (ContentType) object;
320             if (this.index != peer.index)
321                 return false;
322             if (!!! Util.equals (this.getMultiplicity (), peer.getMultiplicity ()))
323                 return false;
324             
325             return true;
326         }
327         
328         /*
329          * Merges changes from passed object to actual object.
330          * @param node merge peer (TreeAttributeDecl)
331          * @throws CannotMergeException if can not merge with given node (invalid class)
332          */

333         public void merge (TreeObject treeObject) throws CannotMergeException {
334             super.merge (treeObject);
335             
336             ContentType peer = (ContentType) treeObject;
337             
338             index = peer.index;
339             setMultiplicity (peer.getMultiplicity ());
340         }
341         
342         
343         //
344
// context
345
//
346

347         /**
348          */

349         public final void removeFromContext () throws ReadOnlyException {
350             if ( isInContext () ) {
351                 getOwnerElementDecl ().setContentTypeImpl (new EMPTYType ());
352             }
353         }
354         
355         
356         //
357
// itself
358
//
359

360         /**
361          */

362         public final TreeElementDecl getOwnerElementDecl () {
363             return (TreeElementDecl)getNodeDecl ();
364         }
365         
366         
367         /**
368          */

369         public void setMultiplicity (char s) {
370             multiplicity = new String JavaDoc (new char[] {s});
371         }
372         
373         /**
374          */

375         public void setMultiplicity (String JavaDoc s) {
376             multiplicity = s;
377         }
378         
379         /** Combines existing multiplicity with new one.
380          * <pre>
381          * | 1 | ? | + | *
382          * --+---+---+---+---
383          * 1 | 1 | ? | + | *
384          * --+---+---+---+---
385          * ? | ? | ? | * | *
386          * --+---+---+---+---
387          * + | + | * | + | *
388          * --+---+---+---+---
389          * * | * | * | * | *
390          * </pre>
391          */

392         public void addMultiplicity (String JavaDoc s) {
393             if (multiplicity.equals (s))
394                 return;
395             
396             if ("".equals (multiplicity)) { // NOI18N
397
multiplicity = s;
398             } else if ("".equals (s)) { // NOI18N
399
// stay intact
400
} else {
401                 multiplicity = "*"; // NOI18N
402
}
403         }
404         
405         
406         /** @return multiplicity of given type */
407         public String JavaDoc getMultiplicity () {
408             return multiplicity;
409         }
410         
411         /** @return true if element itself can contain mixed content. */
412         public boolean isMixed () {
413             return allowText () && allowElements ();
414         }
415         
416         /** @return true if sub elements are allowed. */
417         public abstract boolean allowElements ();
418         
419         /** @return true if text value is allowed. */
420         public abstract boolean allowText ();
421         
422         /** @return String representation of type. */
423         public abstract String JavaDoc toString ();
424         
425         /** Natural ordering by index. */
426         public int compareTo (final Object JavaDoc obj) {
427             if (this.equals (obj))
428                 return 0;
429             
430             ContentType type = (ContentType) obj;
431             
432             return index - type.index;
433         }
434         
435     } // end: class ContentType
436

437 }
438
Popular Tags