KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > schema2beans > AttrProp


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
20 package org.netbeans.modules.schema2beans;
21
22
23 import java.util.*;
24
25 /**
26  * This class hold the information about a property attribute.
27  *
28  * This class is used by the TreeBuilder to build a representation of the
29  * attributes, by the BeanClass to convert these information into
30  * generated attribute code, and by the BeanProp class to hold at runtime
31  * the information about the attributes of a property.
32  *
33  */

34 public class AttrProp implements BaseAttribute {
35
36     static public final int MASK_KIND = 0x00FF;
37     static public final int CDATA = 0x0001;
38     static public final int ENUM = 0x0002;
39     static public final int NMTOKEN = 0x0003;
40     static public final int ID = 0x0004;
41     static public final int IDREF = 0x0005;
42     static public final int IDREFS = 0x0006;
43     static public final int ENTITY = 0x0007;
44     static public final int ENTITIES = 0x0008;
45     static public final int NOTATION = 0x0009;
46     
47     static final String JavaDoc[] kinds =
48     new String JavaDoc[] {"CDATA", "ENUM", "NMTOKEN", "ID", "IDREF", // NOI18N
49
"IDREFS", "ENTITY", "ENTITIES", "NOTATION"}; // NOI18N
50

51     static final int[] kindValues =
52     new int[] {CDATA, ENUM, NMTOKEN, ID, IDREF,
53            IDREFS, ENTITY, ENTITIES, NOTATION};
54     
55     static public final int MASK_OPTION = 0x0F00;
56     static public final int REQUIRED = 0x0100;
57     static public final int IMPLIED = 0x0200;
58     static public final int FIXED = 0x0300;
59     
60     static public final int TRANSIENT = 0x1000;
61     
62     static final String JavaDoc[] options =
63         new String JavaDoc[] {"#REQUIRED", "#IMPLIED", "#FIXED"}; // NOI18N
64

65     static final int[] optionValues = new int[] {REQUIRED, IMPLIED, FIXED};
66     
67     // Property this attribure belongs to
68
String JavaDoc propertyName;
69     
70     // Name of the attribute
71
String JavaDoc name;
72     
73     // Name of the attribute
74
String JavaDoc dtdName;
75
76     String JavaDoc namespace;
77     
78     // Its type (CDATA, ID, ...)
79
int type;
80
81     // Proposed java class for it.
82
String JavaDoc javaType;
83     
84     // Enum values if any (null if none)
85
ArrayList values;
86     
87     // The default value of the attribute
88
String JavaDoc defaultValue;
89
90     //
91
// The attribute content is populated only calling addValue()
92
// assuming it is built from a left to right parsing. This state
93
// is used to know which value is being added.
94
//
95
private int state;
96     
97     private int enumMode;
98
99     private List extraData;
100     //private GraphNode sourceGraphNode;
101

102     // The state values when the attribute is populated
103
private static final int NEED_NAME = 0;
104     private static final int NEED_TYPE = 1;
105     private static final int NEED_ENUM = 2;
106     private static final int NEED_OPTION = 3;
107     private static final int NEED_DEFVAL = 4;
108     private static final int NEED_VALUE = 5;
109     private static final int DONE = 6;
110     
111     public AttrProp() {
112         this.values = null;
113         this.state = NEED_NAME;
114         this.type = 0;
115         this.enumMode = 0;
116     }
117     
118     public AttrProp(String JavaDoc propName) {
119         this();
120         this.propertyName = propName;
121     }
122     
123     public AttrProp(String JavaDoc propName, String JavaDoc dtdName, String JavaDoc name, int type,
124                     String JavaDoc[] values, String JavaDoc defValue) {
125             
126         this.dtdName = dtdName;
127         this.name = name;
128         this.propertyName = propName;
129     
130         if (values != null && values.length > 0) {
131             this.values = new ArrayList();
132             for (int i=0; i<values.length; i++)
133                 this.values.add(values[i]);
134         }
135     
136         this.defaultValue = defValue;
137         this.state = DONE;
138         this.type = type;
139     }
140
141     /**
142      * @return Common.TYPE_1, Common.TYPE_0_1, or Common.TYPE_0 (maybe)
143      */

144     public int getInstance() {
145         if (defaultValue != null)
146             return Common.TYPE_1;
147         switch (type & MASK_OPTION) {
148         case FIXED:
149         case REQUIRED:
150             return Common.TYPE_1;
151         }
152         // optional
153
return Common.TYPE_0_1;
154     }
155     
156     public void setEnum(boolean enume) {
157         enumMode += (enume?1:-1);
158         if (enumMode == 1) {
159             if (this.values == null)
160                 this.values = new ArrayList();
161             this.type = ENUM;
162             this.state = NEED_ENUM;
163         }
164         else
165             if (enumMode == 0) {
166                 this.state = NEED_OPTION;
167             }
168             else
169                 this.failed(Common.getMessage("WrongEnumDecl_msg"));
170     }
171     
172     public void addValue(String JavaDoc value) {
173         addValue(value, null);
174     }
175     
176     public void addValue(String JavaDoc value, String JavaDoc namespace) {
177         //
178
// Get rid of both heading and trailing " character
179
// (we assume that they live in pair)
180
//
181
int valueLen = value.length();
182         if (value.charAt(0) == '"') { // NOI18N
183
if (valueLen == 1)
184                 failed(Common.getMessage("TooLittleDeclaration_msg", value));
185             value = value.substring(1, value.length()-1);
186         } else if (value.charAt(0) == '\'') { // NOI18N
187
if (valueLen == 1)
188                 failed(Common.getMessage("TooLittleDeclaration_msg", value));
189             value = value.substring(1, value.length()-1);
190         }
191     
192     // Name Type_OR_Enums DefValue_OR_#Opt [Val for #FIXED]
193
switch(this.state) {
194         case NEED_NAME:
195             // name of the attribute
196
this.dtdName = value;
197             this.namespace = namespace;
198             this.name = Common.convertName(value);
199             this.state = NEED_TYPE;
200             break;
201         case NEED_TYPE:
202             // Should be the type
203
this.type = this.stringToInt(value, kinds, kindValues);
204             this.state = NEED_OPTION;
205             if (this.type == -1)
206                 this.failed(Common.getMessage("UnknownType_msg", value));
207             break;
208         case NEED_ENUM:
209             this.values.add(value);
210             break;
211         case NEED_OPTION:
212             int opt = this.stringToInt(value, options, optionValues);
213             if (opt != -1) {
214                 this.type |= opt;
215                 if (opt == FIXED)
216                     this.state = NEED_VALUE;
217                 else
218                     this.state = DONE;
219                 break;
220             }
221             // do not break - no option this is a default value;
222
case NEED_VALUE:
223             this.defaultValue = value;
224             this.state = DONE;
225             break;
226         case DONE:
227             this.failed(Common.getMessage("TooMuchDeclaration_msg"));
228         }
229     }
230     
231     //
232
// The attribute is transient if it has been added on the fly
233
// when reading the XML document (was not defined in the DTD)
234
//
235
public boolean isTransient() {
236         return ((this.type & TRANSIENT) == TRANSIENT);
237     }
238     
239     // Return true if this instance of attrProp is properly and completly
240
// built from the DTD declaration.
241
public boolean isComplete() {
242         return (this.state == DONE);
243     }
244     
245     // true if the name is either the mangled name or the dtd name
246
public boolean hasName(String JavaDoc name) {
247         if (name.equals(this.name) || name.equals(this.dtdName))
248             return true;
249         else
250             return false;
251     }
252     
253     //
254
// Return the list of possible values (enum)
255
// If there is no value defined, the array is empty
256
//
257
public String JavaDoc[] getValues() {
258         int size = 0;
259     
260         if (this.values != null)
261             size = this.values.size();
262     
263         String JavaDoc[] ret = new String JavaDoc[size];
264     
265         if (size > 0)
266             return (String JavaDoc[])this.values.toArray(ret);
267         else
268             return ret;
269     }
270
271     public void setDefaultValue(String JavaDoc d) {
272         defaultValue = d;
273     }
274     
275     public String JavaDoc getDefaultValue() {
276         return this.defaultValue;
277     }
278     
279     public String JavaDoc getPropertyName() {
280     return this.propertyName;
281     }
282     
283     public String JavaDoc getName() {
284         return this.name;
285     }
286
287     public void setName(String JavaDoc n) {
288         name = n;
289     }
290     /*
291     public void setSourceGraphNode(GraphNode node) {
292         sourceGraphNode = node;
293     }
294
295     public GraphNode getSourceGraphNode() {
296         return sourceGraphNode;
297     }
298     */

299     
300     public String JavaDoc getDtdName() {
301         return this.dtdName;
302     }
303
304     public String JavaDoc getNamespace() {
305         return this.namespace;
306     }
307     
308     public String JavaDoc typeAsString() {
309         String JavaDoc str = "AttrProp." + // NOI18N
310
intToString(this.type & MASK_KIND, kinds, kindValues);
311     
312         int opt = this.type & MASK_OPTION;
313         if (opt != 0) {
314             str += " | " + "AttrProp." + // NOI18N
315
intToString(opt, options, optionValues).substring(1);
316         }
317     
318         return str;
319     }
320     
321     String JavaDoc enumsToString() {
322         String JavaDoc[] e = this.getValues();
323         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
324         for (int i=0; i<e.length; i++) {
325             ret.append(e[i]);
326             ret.append(" "); // NOI18N
327
}
328         return ret.toString();
329     }
330     
331     public void validate() {
332         // Called at the end of the element parsing
333
if (this.state != DONE)
334             this.failed(Common.getMessage("BadAttributeDecl_msg"));
335     }
336     
337     public void checkEnum() {
338         // Called when a | is found
339
if (this.enumMode == 0)
340             this.failed(Common.getMessage("UseCharORWithEnum_msg"));
341     }
342     
343     public boolean isEnum() {
344         return ((this.type & MASK_KIND) == ENUM);
345     }
346     
347     public boolean isFixed() {
348         return ((this.type & MASK_OPTION) == FIXED);
349     }
350     
351     public int getType() {
352         return (this.type & MASK_KIND);
353     }
354     
355     public int getOption() {
356         return (this.type & MASK_OPTION);
357     }
358     
359     public String JavaDoc getJavaType() {
360         return javaType;
361     }
362
363     public void setJavaType(String JavaDoc jt) {
364         javaType = jt;
365     }
366
367     int stringToInt(String JavaDoc str, String JavaDoc[] map, int[] val) {
368         for(int i=0; i<map.length; i++) {
369             if (str.equals(map[i]))
370                 return val[i];
371         }
372         return -1;
373     }
374     
375     String JavaDoc intToString(int id, String JavaDoc[] map, int[] val) {
376         for(int i=0; i<val.length; i++) {
377             if (id == val[i])
378                 return map[i];
379         }
380         return "?"; // NOI18N
381
}
382     
383     private void failed(String JavaDoc err) {
384         throw new RuntimeException JavaDoc(Common.getMessage("ATTLISTParseError_msg", this.name, err));
385     }
386
387     public void addExtraData(Object JavaDoc data) {
388         if (extraData == null)
389             extraData = new ArrayList();
390         extraData.add(data);
391     }
392
393     public List getExtraData() {
394         if (extraData == null)
395             return Collections.EMPTY_LIST;
396         return extraData;
397     }
398     
399     public String JavaDoc toString() {
400         String JavaDoc str = this.dtdName + " " + // NOI18N
401
intToString(this.type & MASK_KIND, kinds, kindValues) + " "; // NOI18N
402

403         int opt = this.type & MASK_OPTION;
404         if (opt != 0)
405             str += intToString(opt, options, optionValues) + " "; // NOI18N
406

407         if (this.values != null) {
408             int size = this.values.size();
409             str += "( "; // NOI18N
410
for(int i=0; i<size; i++) {
411                 str += this.values.get(i) + " "; // NOI18N
412
}
413             str += ") "; // NOI18N
414
}
415         if (this.defaultValue != null)
416             str += this.defaultValue;
417     
418         if (this.isTransient())
419             str += " (transient)"; // NOI18N
420
if (javaType != null)
421             str += " : " + javaType;
422         return str;
423     }
424 }
425
426
Popular Tags