1 19 20 package org.netbeans.modules.schema2beans; 21 22 23 import java.util.*; 24 25 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 [] kinds = 48 new String [] {"CDATA", "ENUM", "NMTOKEN", "ID", "IDREF", "IDREFS", "ENTITY", "ENTITIES", "NOTATION"}; 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 [] options = 63 new String [] {"#REQUIRED", "#IMPLIED", "#FIXED"}; 65 static final int[] optionValues = new int[] {REQUIRED, IMPLIED, FIXED}; 66 67 String propertyName; 69 70 String name; 72 73 String dtdName; 75 76 String namespace; 77 78 int type; 80 81 String javaType; 83 84 ArrayList values; 86 87 String defaultValue; 89 90 private int state; 96 97 private int enumMode; 98 99 private List extraData; 100 102 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 propName) { 119 this(); 120 this.propertyName = propName; 121 } 122 123 public AttrProp(String propName, String dtdName, String name, int type, 124 String [] values, String 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 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 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 value) { 173 addValue(value, null); 174 } 175 176 public void addValue(String value, String namespace) { 177 int valueLen = value.length(); 182 if (value.charAt(0) == '"') { if (valueLen == 1) 184 failed(Common.getMessage("TooLittleDeclaration_msg", value)); 185 value = value.substring(1, value.length()-1); 186 } else if (value.charAt(0) == '\'') { if (valueLen == 1) 188 failed(Common.getMessage("TooLittleDeclaration_msg", value)); 189 value = value.substring(1, value.length()-1); 190 } 191 192 switch(this.state) { 194 case NEED_NAME: 195 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 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 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 public boolean isTransient() { 236 return ((this.type & TRANSIENT) == TRANSIENT); 237 } 238 239 public boolean isComplete() { 242 return (this.state == DONE); 243 } 244 245 public boolean hasName(String name) { 247 if (name.equals(this.name) || name.equals(this.dtdName)) 248 return true; 249 else 250 return false; 251 } 252 253 public String [] getValues() { 258 int size = 0; 259 260 if (this.values != null) 261 size = this.values.size(); 262 263 String [] ret = new String [size]; 264 265 if (size > 0) 266 return (String [])this.values.toArray(ret); 267 else 268 return ret; 269 } 270 271 public void setDefaultValue(String d) { 272 defaultValue = d; 273 } 274 275 public String getDefaultValue() { 276 return this.defaultValue; 277 } 278 279 public String getPropertyName() { 280 return this.propertyName; 281 } 282 283 public String getName() { 284 return this.name; 285 } 286 287 public void setName(String n) { 288 name = n; 289 } 290 299 300 public String getDtdName() { 301 return this.dtdName; 302 } 303 304 public String getNamespace() { 305 return this.namespace; 306 } 307 308 public String typeAsString() { 309 String str = "AttrProp." + intToString(this.type & MASK_KIND, kinds, kindValues); 311 312 int opt = this.type & MASK_OPTION; 313 if (opt != 0) { 314 str += " | " + "AttrProp." + intToString(opt, options, optionValues).substring(1); 316 } 317 318 return str; 319 } 320 321 String enumsToString() { 322 String [] e = this.getValues(); 323 StringBuffer ret = new StringBuffer (); 324 for (int i=0; i<e.length; i++) { 325 ret.append(e[i]); 326 ret.append(" "); } 328 return ret.toString(); 329 } 330 331 public void validate() { 332 if (this.state != DONE) 334 this.failed(Common.getMessage("BadAttributeDecl_msg")); 335 } 336 337 public void checkEnum() { 338 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 getJavaType() { 360 return javaType; 361 } 362 363 public void setJavaType(String jt) { 364 javaType = jt; 365 } 366 367 int stringToInt(String str, String [] 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 intToString(int id, String [] map, int[] val) { 376 for(int i=0; i<val.length; i++) { 377 if (id == val[i]) 378 return map[i]; 379 } 380 return "?"; } 382 383 private void failed(String err) { 384 throw new RuntimeException (Common.getMessage("ATTLISTParseError_msg", this.name, err)); 385 } 386 387 public void addExtraData(Object 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 toString() { 400 String str = this.dtdName + " " + intToString(this.type & MASK_KIND, kinds, kindValues) + " "; 403 int opt = this.type & MASK_OPTION; 404 if (opt != 0) 405 str += intToString(opt, options, optionValues) + " "; 407 if (this.values != null) { 408 int size = this.values.size(); 409 str += "( "; for(int i=0; i<size; i++) { 411 str += this.values.get(i) + " "; } 413 str += ") "; } 415 if (this.defaultValue != null) 416 str += this.defaultValue; 417 418 if (this.isTransient()) 419 str += " (transient)"; if (javaType != null) 421 str += " : " + javaType; 422 return str; 423 } 424 } 425 426 | Popular Tags |