| 1 7 8 package javax.imageio.metadata; 9 10 import java.util.ArrayList ; 11 import java.util.Collection ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.Locale ; 16 import java.util.Map ; 17 import java.util.MissingResourceException ; 18 import java.util.ResourceBundle ; 19 import javax.imageio.ImageTypeSpecifier ; 20 import com.sun.imageio.plugins.common.StandardMetadataFormat; 21 22 60 public abstract class IIOMetadataFormatImpl implements IIOMetadataFormat { 61 62 66 public static final String standardMetadataFormatName = 67 "javax_imageio_1.0"; 68 69 private static IIOMetadataFormat standardFormat = null; 70 71 private String resourceBaseName = this.getClass().getName() + "Resources"; 72 73 private String rootName; 74 75 private HashMap elementMap = new HashMap (); 77 78 class Element { 79 String elementName; 80 81 int childPolicy; 82 int minChildren = 0; 83 int maxChildren = 0; 84 85 List childList = new ArrayList (); 87 88 List parentList = new ArrayList (); 90 91 List attrList = new ArrayList (); 93 Map attrMap = new HashMap (); 95 96 ObjectValue objectValue; 97 } 98 99 class Attribute { 100 String attrName; 101 102 int valueType = VALUE_ARBITRARY; 103 int dataType; 104 boolean required; 105 String defaultValue = null; 106 107 List enumeratedValues; 109 110 String minValue; 112 String maxValue; 113 114 int listMinLength; 116 int listMaxLength; 117 } 118 119 class ObjectValue { 120 int valueType = VALUE_NONE; 121 Class classType = null; 122 Object defaultValue = null; 123 124 List enumeratedValues = null; 126 127 Comparable minValue = null; 129 Comparable maxValue = null; 130 131 int arrayMinLength = 0; 133 int arrayMaxLength = 0; 134 } 135 136 152 public IIOMetadataFormatImpl(String rootName, 153 int childPolicy) { 154 if (rootName == null) { 155 throw new IllegalArgumentException ("rootName == null!"); 156 } 157 if (childPolicy < CHILD_POLICY_EMPTY || 158 childPolicy > CHILD_POLICY_MAX || 159 childPolicy == CHILD_POLICY_REPEAT) { 160 throw new IllegalArgumentException ("Invalid value for childPolicy!"); 161 } 162 163 this.rootName = rootName; 164 165 Element root = new Element(); 166 root.elementName = rootName; 167 root.childPolicy = childPolicy; 168 169 elementMap.put(rootName, root); 170 } 171 172 188 public IIOMetadataFormatImpl(String rootName, 189 int minChildren, 190 int maxChildren) { 191 if (rootName == null) { 192 throw new IllegalArgumentException ("rootName == null!"); 193 } 194 if (minChildren < 0) { 195 throw new IllegalArgumentException ("minChildren < 0!"); 196 } 197 if (minChildren > maxChildren) { 198 throw new IllegalArgumentException ("minChildren > maxChildren!"); 199 } 200 201 Element root = new Element(); 202 root.elementName = rootName; 203 root.childPolicy = CHILD_POLICY_REPEAT; 204 root.minChildren = minChildren; 205 root.maxChildren = maxChildren; 206 207 this.rootName = rootName; 208 elementMap.put(rootName, root); 209 } 210 211 228 protected void setResourceBaseName(String resourceBaseName) { 229 if (resourceBaseName == null) { 230 throw new IllegalArgumentException ("resourceBaseName == null!"); 231 } 232 this.resourceBaseName = resourceBaseName; 233 } 234 235 243 protected String getResourceBaseName() { 244 return resourceBaseName; 245 } 246 247 254 private Element getElement(String elementName, boolean mustAppear) { 255 if (mustAppear && (elementName == null)) { 256 throw new IllegalArgumentException ("element name is null!"); 257 } 258 Element element = (Element)elementMap.get(elementName); 259 if (mustAppear && (element == null)) { 260 throw new IllegalArgumentException ("No such element: " + 261 elementName); 262 } 263 return element; 264 } 265 266 private Element getElement(String elementName) { 267 return getElement(elementName, true); 268 } 269 270 private Attribute getAttribute(String elementName, String attrName) { 272 Element element = getElement(elementName); 273 Attribute attr = (Attribute)element.attrMap.get(attrName); 274 if (attr == null) { 275 throw new IllegalArgumentException ("No such attribute \"" + 276 attrName + "\"!"); 277 } 278 return attr; 279 } 280 281 283 300 protected void addElement(String elementName, 301 String parentName, 302 int childPolicy) { 303 Element parent = getElement(parentName); 304 if (childPolicy < CHILD_POLICY_EMPTY || 305 childPolicy > CHILD_POLICY_MAX || 306 childPolicy == CHILD_POLICY_REPEAT) { 307 throw new IllegalArgumentException  308 ("Invalid value for childPolicy!"); 309 } 310 311 Element element = new Element(); 312 element.elementName = elementName; 313 element.childPolicy = childPolicy; 314 315 parent.childList.add(elementName); 316 element.parentList.add(parentName); 317 318 elementMap.put(elementName, element); 319 } 320 321 337 protected void addElement(String elementName, 338 String parentName, 339 int minChildren, 340 int maxChildren) { 341 Element parent = getElement(parentName); 342 if (minChildren < 0) { 343 throw new IllegalArgumentException ("minChildren < 0!"); 344 } 345 if (minChildren > maxChildren) { 346 throw new IllegalArgumentException ("minChildren > maxChildren!"); 347 } 348 349 Element element = new Element(); 350 element.elementName = elementName; 351 element.childPolicy = CHILD_POLICY_REPEAT; 352 element.minChildren = minChildren; 353 element.maxChildren = maxChildren; 354 355 parent.childList.add(elementName); 356 element.parentList.add(parentName); 357 358 elementMap.put(elementName, element); 359 } 360 361 377 protected void addChildElement(String elementName, String parentName) { 378 Element parent = getElement(parentName); 379 Element element = getElement(elementName); 380 parent.childList.add(elementName); 381 element.parentList.add(parentName); 382 } 383 384 391 protected void removeElement(String elementName) { 392 Element element = getElement(elementName, false); 393 if (element != null) { 394 Iterator iter = element.parentList.iterator(); 395 while (iter.hasNext()) { 396 String parentName = (String )iter.next(); 397 Element parent = getElement(parentName, false); 398 if (parent != null) { 399 parent.childList.remove(elementName); 400 } 401 } 402 elementMap.remove(elementName); 403 } 404 } 405 406 426 protected void addAttribute(String elementName, 427 String attrName, 428 int dataType, 429 boolean required, 430 String defaultValue) { 431 Element element = getElement(elementName); 432 if (attrName == null) { 433 throw new IllegalArgumentException ("attrName == null!"); 434 } 435 if (dataType < DATATYPE_STRING || dataType > DATATYPE_DOUBLE) { 436 throw new IllegalArgumentException ("Invalid value for dataType!"); 437 } 438 439 Attribute attr = new Attribute(); 440 attr.attrName = attrName; 441 attr.valueType = VALUE_ARBITRARY; 442 attr.dataType = dataType; 443 attr.required = required; 444 attr.defaultValue = defaultValue; 445 446 element.attrList.add(attrName); 447 element.attrMap.put(attrName, attr); 448 } 449 450 481 protected void addAttribute(String elementName, 482 String attrName, 483 int dataType, 484 boolean required, 485 String defaultValue, 486 List <String > enumeratedValues) { 487 Element element = getElement(elementName); 488 if (attrName == null) { 489 throw new IllegalArgumentException ("attrName == null!"); 490 } 491 if (dataType < DATATYPE_STRING || dataType > DATATYPE_DOUBLE) { 492 throw new IllegalArgumentException ("Invalid value for dataType!"); 493 } 494 if (enumeratedValues == null) { 495 throw new IllegalArgumentException ("enumeratedValues == null!"); 496 } 497 if (enumeratedValues.size() == 0) { 498 throw new IllegalArgumentException ("enumeratedValues is empty!"); 499 } 500 Iterator iter = enumeratedValues.iterator(); 501 while (iter.hasNext()) { 502 Object o = iter.next(); 503 if (o == null) { 504 throw new IllegalArgumentException  505 ("enumeratedValues contains a null!"); 506 } 507 if (!(o instanceof String )) { 508 throw new IllegalArgumentException  509 ("enumeratedValues contains a non-String value!"); 510 } 511 } 512 513 Attribute attr = new Attribute(); 514 attr.attrName = attrName; 515 attr.valueType = VALUE_ENUMERATION; 516 attr.dataType = dataType; 517 attr.required = required; 518 attr.defaultValue = defaultValue; 519 attr.enumeratedValues = enumeratedValues; 520 521 element.attrList.add(attrName); 522 element.attrMap.put(attrName, attr); 523 } 524 525 555 protected void addAttribute(String elementName, 556 String attrName, 557 int dataType, 558 boolean required, 559 String defaultValue, 560 String minValue, 561 String maxValue, 562 boolean minInclusive, 563 boolean maxInclusive) { 564 Element element = getElement(elementName); 565 if (attrName == null) { 566 throw new IllegalArgumentException ("attrName == null!"); 567 } 568 if (dataType < DATATYPE_STRING || dataType > DATATYPE_DOUBLE) { 569 throw new IllegalArgumentException ("Invalid value for dataType!"); 570 } 571 572 Attribute attr = new Attribute(); 573 attr.attrName = attrName; 574 attr.valueType = VALUE_RANGE; 575 if (minInclusive) { 576 attr.valueType |= VALUE_RANGE_MIN_INCLUSIVE_MASK; 577 } 578 if (maxInclusive) { 579 attr.valueType |= VALUE_RANGE_MAX_INCLUSIVE_MASK; 580 } 581 attr.dataType = dataType; 582 attr.required = required; 583 attr.defaultValue = defaultValue; 584 attr.minValue = minValue; 585 attr.maxValue = maxValue; 586 587 element.attrList.add(attrName); 588 element.attrMap.put(attrName, attr); 589 } 590 591 614 protected void addAttribute(String elementName, 615 String attrName, 616 int dataType, 617 boolean required, 618 int listMinLength, 619 int listMaxLength) { 620 Element element = getElement(elementName); 621 if (attrName == null) { 622 throw new IllegalArgumentException ("attrName == null!"); 623 } 624 if (dataType < DATATYPE_STRING || dataType > DATATYPE_DOUBLE) { 625 throw new IllegalArgumentException ("Invalid value for dataType!"); 626 } 627 if (listMinLength < 0 || listMinLength > listMaxLength) { 628 throw new IllegalArgumentException ("Invalid list bounds!"); 629 } 630 631 Attribute attr = new Attribute(); 632 attr.attrName = attrName; 633 attr.valueType = VALUE_LIST; 634 attr.dataType = dataType; 635 attr.required = required; 636 attr.listMinLength = listMinLength; 637 attr.listMaxLength = listMaxLength; 638 639 element.attrList.add(attrName); 640 element.attrMap.put(attrName, attr); 641 } 642 643 663 protected void addBooleanAttribute(String elementName, 664 String attrName, 665 boolean hasDefaultValue, 666 boolean defaultValue) { 667 List values = new ArrayList (); 668 values.add("TRUE"); 669 values.add("FALSE"); 670 671 String dval = null; 672 if (hasDefaultValue) { 673 dval = defaultValue ? "TRUE" : "FALSE"; 674 } 675 addAttribute(elementName, 676 attrName, 677 DATATYPE_BOOLEAN, 678 true, 679 dval, 680 values); 681 } 682 683 694 protected void removeAttribute(String elementName, String attrName) { 695 Element element = getElement(elementName); 696 element.attrList.remove(attrName); 697 element.attrMap.remove(attrName); 698 } 699 700 719 protected <T> void addObjectValue(String elementName, 720 Class <T> classType, 721 boolean required, 722 T defaultValue) 723 { 724 Element element = getElement(elementName); 725 ObjectValue obj = new ObjectValue(); 726 obj.valueType = VALUE_ARBITRARY; 727 obj.classType = classType; 728 obj.defaultValue = defaultValue; 729 730 element.objectValue = obj; 731 } 732 733 764 protected <T> void addObjectValue(String elementName, 765 Class <T> classType, 766 boolean required, 767 T defaultValue, 768 List <? extends T> enumeratedValues) 769 { 770 Element element = getElement(elementName); 771 if (enumeratedValues == null) { 772 throw new IllegalArgumentException ("enumeratedValues == null!"); 773 } 774 if (enumeratedValues.size() == 0) { 775 throw new IllegalArgumentException ("enumeratedValues is empty!"); 776 } 777 Iterator iter = enumeratedValues.iterator(); 778 while (iter.hasNext()) { 779 Object o = iter.next(); 780 if (o == null) { 781 throw new IllegalArgumentException ("enumeratedValues contains a null!"); 782 } 783 if (!classType.isInstance(o)) { 784 throw new IllegalArgumentException ("enumeratedValues contains a value not of class classType!"); 785 } 786 } 787 788 ObjectValue obj = new ObjectValue(); 789 obj.valueType = VALUE_ENUMERATION; 790 obj.classType = classType; 791 obj.defaultValue = defaultValue; 792 obj.enumeratedValues = enumeratedValues; 793 794 element.objectValue = obj; 795 } 796 797 827 protected <T extends Object & Comparable <? super T>> void 828 addObj
|