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 addObjectValue(String elementName, 829 Class <T> classType, 830 T defaultValue, 831 Comparable <? super T> minValue, 832 Comparable <? super T> maxValue, 833 boolean minInclusive, 834 boolean maxInclusive) 835 { 836 Element element = getElement(elementName); 837 ObjectValue obj = new ObjectValue(); 838 obj.valueType = VALUE_RANGE; 839 if (minInclusive) { 840 obj.valueType |= VALUE_RANGE_MIN_INCLUSIVE_MASK; 841 } 842 if (maxInclusive) { 843 obj.valueType |= VALUE_RANGE_MAX_INCLUSIVE_MASK; 844 } 845 obj.classType = classType; 846 obj.defaultValue = defaultValue; 847 obj.minValue = minValue; 848 obj.maxValue = maxValue; 849 850 element.objectValue = obj; 851 } 852 853 873 protected void addObjectValue(String elementName, 874 Class <?> classType, 875 int arrayMinLength, 876 int arrayMaxLength) { 877 Element element = getElement(elementName); 878 ObjectValue obj = new ObjectValue(); 879 obj.valueType = VALUE_LIST; 880 obj.classType = classType; 881 obj.arrayMinLength = arrayMinLength; 882 obj.arrayMaxLength = arrayMaxLength; 883 884 element.objectValue = obj; 885 } 886 887 896 protected void removeObjectValue(String elementName) { 897 Element element = getElement(elementName); 898 element.objectValue = null; 899 } 900 901 903 905 907 public String getRootName() { 908 return rootName; 909 } 910 911 913 public abstract boolean canNodeAppear(String elementName, 914 ImageTypeSpecifier imageType); 915 916 public int getElementMinChildren(String elementName) { 917 Element element = getElement(elementName); 918 if (element.childPolicy != CHILD_POLICY_REPEAT) { 919 throw new IllegalArgumentException ("Child policy not CHILD_POLICY_REPEAT!"); 920 } 921 return element.minChildren; 922 } 923 924 public int getElementMaxChildren(String elementName) { 925 Element element = getElement(elementName); 926 if (element.childPolicy != CHILD_POLICY_REPEAT) { 927 throw new IllegalArgumentException ("Child policy not CHILD_POLICY_REPEAT!"); 928 } 929 return element.maxChildren; 930 } 931 932 private String getResource(String key, Locale locale) { 933 if (locale == null) { 934 locale = Locale.getDefault(); 935 } 936 937 945 ClassLoader loader = (ClassLoader ) 946 java.security.AccessController.doPrivileged( 947 new java.security.PrivilegedAction () { 948 public Object run() { 949 return Thread.currentThread().getContextClassLoader(); 950 } 951 }); 952 953 ResourceBundle bundle = null; 954 try { 955 bundle = ResourceBundle.getBundle(resourceBaseName, 956 locale, loader); 957 } catch (MissingResourceException mre) { 958 try { 959 bundle = ResourceBundle.getBundle(resourceBaseName, locale); 960 } catch (MissingResourceException mre1) { 961 return null; 962 } 963 } 964 965 try { 966 return bundle.getString(key); 967 } catch (MissingResourceException e) { 968 return null; 969 } 970 } 971 972 1002 public String getElementDescription(String elementName, 1003 Locale locale) { 1004 Element element = getElement(elementName); 1005 return getResource(elementName, locale); 1006 } 1007 1008 1010 public int getChildPolicy(String elementName) { 1011 Element element = getElement(elementName); 1012 return element.childPolicy; 1013 } 1014 1015 public String [] getChildNames(String elementName) { 1016 Element element = getElement(elementName); 1017 if (element.childPolicy == CHILD_POLICY_EMPTY) { 1018 return null; 1019 } 1020 return (String [])element.childList.toArray(new String [0]); 1021 } 1022 1023 1025 public String [] getAttributeNames(String elementName) { 1026 Element element = getElement(elementName); 1027 List names = element.attrList; 1028 1029 String [] result = new String [names.size()]; 1030 return (String [])names.toArray(result); 1031 } 1032 1033 public int getAttributeValueType(String elementName, String attrName) { 1034 Attribute attr = getAttribute(elementName, attrName); 1035 return attr.valueType; 1036 } 1037 1038 public int getAttributeDataType(String elementName, String attrName) { 1039 Attribute attr = getAttribute(elementName, attrName); 1040 return attr.dataType; 1041 } 1042 1043 public boolean isAttributeRequired(String elementName, String attrName) { 1044 Attribute attr = getAttribute(elementName, attrName); 1045 return attr.required; 1046 } 1047 1048 public String getAttributeDefaultValue(String elementName, 1049 String attrName) { 1050 Attribute attr = getAttribute(elementName, attrName); 1051 return attr.defaultValue; 1052 } 1053 1054 public String [] getAttributeEnumerations(String elementName, 1055 String attrName) { 1056 Attribute attr = getAttribute(elementName, attrName); 1057 if (attr.valueType != VALUE_ENUMERATION) { 1058 throw new IllegalArgumentException 1059 ("Attribute not an enumeration!"); 1060 } 1061 1062 List values = attr.enumeratedValues; 1063 Iterator iter = values.iterator(); 1064 String [] result = new String [values.size()]; 1065 return (String [])values.toArray(result); 1066 } 1067 1068 public String getAttributeMinValue(String elementName, String attrName) { 1069 Attribute attr = getAttribute(elementName, attrName); 1070 if (attr.valueType != VALUE_RANGE && 1071 attr.valueType != VALUE_RANGE_MIN_INCLUSIVE && 1072 attr.valueType != VALUE_RANGE_MAX_INCLUSIVE && 1073 attr.valueType != VALUE_RANGE_MIN_MAX_INCLUSIVE) { 1074 throw new IllegalArgumentException ("Attribute not a range!"); 1075 } 1076 1077 return attr.minValue; 1078 } 1079 1080 public String getAttributeMaxValue(String elementName, String attrName) { 1081 Attribute attr = getAttribute(elementName, attrName); 1082 if (attr.valueType != VALUE_RANGE && 1083 attr.valueType != VALUE_RANGE_MIN_INCLUSIVE && 1084 attr.valueType != VALUE_RANGE_MAX_INCLUSIVE && 1085 attr.valueType != VALUE_RANGE_MIN_MAX_INCLUSIVE) { 1086 throw new IllegalArgumentException ("Attribute not a range!"); 1087 } 1088 1089 return attr.maxValue; 1090 } 1091 1092 public int getAttributeListMinLength(String elementName, String attrName) { 1093 Attribute attr = getAttribute(elementName, attrName); 1094 if (attr.valueType != VALUE_LIST) { 1095 throw new IllegalArgumentException ("Attribute not a list!"); 1096 } 1097 1098 return attr.listMinLength; 1099 } 1100 1101 public int getAttributeListMaxLength(String elementName, String attrName) { 1102 Attribute attr = getAttribute(elementName, attrName); 1103 if (attr.valueType != VALUE_LIST) { 1104 throw new IllegalArgumentException ("Attribute not a list!"); 1105 } 1106 1107 return attr.listMaxLength; 1108 } 1109 1110 1146 public String getAttributeDescription(String elementName, 1147 String attrName, 1148 Locale locale) { 1149 Element element = getElement(elementName); 1150 if (attrName == null) { 1151 throw new IllegalArgumentException ("attrName == null!"); 1152 } 1153 Attribute attr = (Attribute)element.attrMap.get(attrName); 1154 if (attr == null) { 1155 throw new IllegalArgumentException ("No such attribute!"); 1156 } 1157 1158 String key = elementName + "/" + attrName; 1159 return getResource(key, locale); 1160 } 1161 1162 private ObjectValue getObjectValue(String elementName) { 1163 Element element = getElement(elementName); 1164 ObjectValue objv = (ObjectValue)element.objectValue; 1165 if (objv == null) { 1166 throw new IllegalArgumentException ("No object within element " + 1167 elementName + "!"); 1168 } 1169 return objv; 1170 } 1171 1172 public int getObjectValueType(String elementName) { 1173 Element element = getElement(elementName); 1174 ObjectValue objv = (ObjectValue)element.objectValue; 1175 if (objv == null) { 1176 return VALUE_NONE; 1177 } 1178 return objv.valueType; 1179 } 1180 1181 public Class <?> getObjectClass(String elementName) { 1182 ObjectValue objv = getObjectValue(elementName); 1183 return objv.classType; 1184 } 1185 1186 public Object getObjectDefaultValue(String elementName) { 1187 ObjectValue objv = getObjectValue(elementName); 1188 return objv.defaultValue; 1189 } 1190 1191 public Object [] getObjectEnumerations(String elementName) { 1192 ObjectValue objv = getObjectValue(elementName); 1193 if (objv.valueType != VALUE_ENUMERATION) { 1194 throw new IllegalArgumentException ("Not an enumeration!"); 1195 } 1196 List vlist = objv.enumeratedValues; 1197 Object [] values = new Object [vlist.size()]; 1198 return vlist.toArray(values); 1199 } 1200 1201 public Comparable <?> getObjectMinValue(String elementName) { 1202 ObjectValue objv = getObjectValue(elementName); 1203 if ((objv.valueType & VALUE_RANGE) != VALUE_RANGE) { 1204 throw new IllegalArgumentException ("Not a range!"); 1205 } 1206 return objv.minValue; 1207 } 1208 1209 public Comparable <?> getObjectMaxValue(String elementName) { 1210 ObjectValue objv = getObjectValue(elementName); 1211 if ((objv.valueType & VALUE_RANGE) != VALUE_RANGE) { 1212 throw new IllegalArgumentException ("Not a range!"); 1213 } 1214 return objv.maxValue; 1215 } 1216 1217 public int getObjectArrayMinLength(String elementName) { 1218 ObjectValue objv = getObjectValue(elementName); 1219 if (objv.valueType != VALUE_LIST) { 1220 throw new IllegalArgumentException ("Not a list!"); 1221 } 1222 return objv.arrayMinLength; 1223 } 1224 1225 public int getObjectArrayMaxLength(String elementName) { 1226 ObjectValue objv = getObjectValue(elementName); 1227 if (objv.valueType != VALUE_LIST) { 1228 throw new IllegalArgumentException ("Not a list!"); 1229 } 1230 return objv.arrayMaxLength; 1231 } 1232 1233 1235 private synchronized static void createStandardFormat() { 1236 if (standardFormat == null) { 1237 standardFormat = new StandardMetadataFormat(); 1238 } 1239 } 1240 1241 1249 public static IIOMetadataFormat getStandardFormatInstance() { 1250 createStandardFormat(); 1251 return standardFormat; 1252 } 1253} 1254 | Popular Tags |