1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59:
60: public abstract class IIOMetadataFormatImpl implements IIOMetadataFormat
61: {
62:
66: public static final String standardMetadataFormatName = "javax_imageio_1.0";
67:
68: private String rootName;
69:
70:
71:
72: private Map nodes = new HashMap();
73:
74:
75: private Map childPolicies = new HashMap();
76:
77:
78:
79:
80:
81: private Map childRanges = new HashMap();
82:
83: private String resourceBaseName;
84:
85:
86: static class IIOMetadataNodeAttr extends IIOMetadataNode
87: implements Attr
88: {
89: protected Element owner;
90: protected String name;
91: protected int dataType;
92: protected boolean required;
93: protected String defaultValue;
94:
95: public IIOMetadataNodeAttr (Element owner,
96: String name,
97: String defaultValue)
98: {
99: this (owner, name, IIOMetadataFormat.DATATYPE_STRING,
100: true, defaultValue);
101: }
102:
103: public IIOMetadataNodeAttr (Element owner,
104: String name,
105: int dataType,
106: boolean required,
107: String defaultValue)
108: {
109: this.owner = owner;
110: this.name = name;
111: this.dataType = dataType;
112: this.required = required;
113: this.defaultValue = defaultValue;
114: }
115:
116: public String getName ()
117: {
118: return name;
119: }
120:
121: public Element getOwnerElement ()
122: {
123: return owner;
124: }
125:
126: public int getDataType ()
127: {
128: return dataType;
129: }
130:
131: public TypeInfo getSchemaTypeInfo ()
132: {
133: return null;
134: }
135:
136: public boolean getSpecified ()
137: {
138: return false;
139: }
140:
141: public String getValue ()
142: {
143: return defaultValue;
144: }
145:
146: public boolean isId()
147: {
148: return false;
149: }
150:
151: public void setValue (String value)
152: {
153: }
154:
155:
156:
157: public boolean isRequired ()
158: {
159: return required;
160: }
161: }
162:
163: private class IIOMetadataNodeAttrEnumerated extends IIOMetadataNodeAttr
164: {
165: protected List enumeratedValues;
166:
167: public IIOMetadataNodeAttrEnumerated (Element owner,
168: String name,
169: int dataType,
170: boolean required,
171: String defaultValue,
172: List enumeratedValues)
173: {
174: super (owner, name, dataType, required, defaultValue);
175: this.enumeratedValues = new ArrayList (enumeratedValues);
176: }
177:
178: public Object[] getEnumerations ()
179: {
180: return enumeratedValues.toArray ();
181: }
182: }
183:
184: private class IIOMetadataNodeAttrBounded extends IIOMetadataNodeAttr
185: {
186: protected String minValue;
187: protected String maxValue;
188: protected boolean minInclusive;
189: protected boolean maxInclusive;
190:
191: public IIOMetadataNodeAttrBounded (Element owner,
192: String name,
193: int dataType,
194: boolean required,
195: String defaultValue,
196: String minValue,
197: String maxValue,
198: boolean minInclusive,
199: boolean maxInclusive)
200: {
201: super (owner, name, dataType, required, defaultValue);
202: this.minValue = minValue;
203: this.maxValue = maxValue;
204: this.minInclusive = minInclusive;
205: this.maxInclusive = maxInclusive;
206: }
207:
208: public String getMinValue ()
209: {
210: return minValue;
211: }
212:
213: public String getMaxValue ()
214: {
215: return maxValue;
216: }
217: }
218:
219: private class IIOMetadataNodeAttrList extends IIOMetadataNodeAttr
220: {
221: protected int listMinLength;
222: protected int listMaxLength;
223:
224: public IIOMetadataNodeAttrList (Element owner,
225: String name,
226: int dataType,
227: boolean required,
228: int listMinLength,
229: int listMaxLength)
230: {
231: super (owner, name, dataType, required, null);
232: this.listMinLength = listMinLength;
233: this.listMaxLength = listMaxLength;
234: }
235:
236: public int getListMinLength ()
237: {
238: return listMinLength;
239: }
240:
241: public int getListMaxLength ()
242: {
243: return listMaxLength;
244: }
245: }
246:
247: private class NodeObject
248: {
249: protected Element owner;
250: protected Class classType;
251: protected boolean required;
252: protected Object defaultValue;
253: protected int valueType;
254:
255: public NodeObject (Element owner,
256: Class classType,
257: boolean required,
258: Object defaultValue)
259: {
260: this.owner = owner;
261: this.classType = classType;
262: this.required = required;
263: this.defaultValue = defaultValue;
264: valueType = IIOMetadataFormat.VALUE_ARBITRARY;
265: }
266:
267: public int getValueType ()
268: {
269: return valueType;
270: }
271:
272: public Class getClassType ()
273: {
274: return classType;
275: }
276:
277: public Element getOwnerElement ()
278: {
279: return owner;
280: }
281:
282: public Object getDefaultValue ()
283: {
284: return defaultValue;
285: }
286:
287: public boolean isRequired ()
288: {
289: return required;
290: }
291: }
292:
293: private class NodeObjectEnumerated extends NodeObject
294: {
295: protected List enumeratedValues;
296:
297: public NodeObjectEnumerated (Element owner,
298: Class classType,
299: boolean required,
300: Object defaultValue,
301: List enumeratedValues)
302: {
303: super (owner, classType, false, defaultValue);
304: this.enumeratedValues = enumeratedValues;
305: valueType = IIOMetadataFormat.VALUE_ENUMERATION;
306: }
307:
308: public Object[] getEnumerations ()
309: {
310: return enumeratedValues.toArray();
311: }
312: }
313:
314: private class NodeObjectBounded extends NodeObject
315: {
316: protected Comparable minValue;
317: protected Comparable maxValue;
318: protected boolean minInclusive;
319: protected boolean maxInclusive;
320:
321: public NodeObjectBounded (Element owner,
322: Class classType,
323: Object defaultValue,
324: Comparable minValue,
325: Comparable maxValue,
326: boolean minInclusive,
327: boolean maxInclusive)
328: {
329: super (owner, classType, false, defaultValue);
330: this.minValue = minValue;
331: this.maxValue = maxValue;
332: this.minInclusive = minInclusive;
333: this.maxInclusive = maxInclusive;
334: if (minInclusive)
335: {
336: if (maxInclusive)
337: valueType = IIOMetadataFormat.VALUE_RANGE_MIN_MAX_INCLUSIVE;
338: else
339: valueType = IIOMetadataFormat.VALUE_RANGE_MIN_INCLUSIVE;
340: }
341: else
342: {
343: if (maxInclusive)
344: valueType = IIOMetadataFormat.VALUE_RANGE_MAX_INCLUSIVE;
345: else
346: valueType = IIOMetadataFormat.VALUE_RANGE;
347: }
348: }
349:
350: public Comparable getMinValue ()
351: {
352: return minValue;
353: }
354:
355: public Comparable getMaxValue ()
356: {
357: return maxValue;
358: }
359: }
360:
361: private class NodeObjectArray extends NodeObject
362: {
363: protected Integer arrayMinLength;
364: protected Integer arrayMaxLength;
365:
366: public NodeObjectArray (Element owner,
367: Class classType,
368: int arrayMinLength,
369: int arrayMaxLength)
370: {
371: super (owner, classType, false, null);
372: this.arrayMinLength = new Integer (arrayMinLength);
373: this.arrayMaxLength = new Integer (arrayMaxLength);
374: valueType = IIOMetadataFormat.VALUE_LIST;
375: }
376:
377: public Comparable getArrayMinLength ()
378: {
379: return arrayMinLength;
380: }
381:
382: public Comparable getArrayMaxLength ()
383: {
384: return arrayMaxLength;
385: }
386: }
387:
388:
400: public IIOMetadataFormatImpl (String rootName, int childPolicy)
401: {
402: if (rootName == null)
403: throw new IllegalArgumentException ("null argument");
404:
405: if (childPolicy < IIOMetadataFormat.CHILD_POLICY_ALL
406: || childPolicy > IIOMetadataFormat.CHILD_POLICY_SOME
407: || childPolicy == IIOMetadataFormat.CHILD_POLICY_REPEAT)
408: throw new IllegalArgumentException ("wrong child policy");
409:
410: nodes.put (rootName, new IIOMetadataNode (rootName));
411: childPolicies.put (rootName, new Integer (childPolicy));
412: this.rootName = rootName;
413: }
414:
415:
430: public IIOMetadataFormatImpl (String rootName,
431: int minChildren,
432: int maxChildren)
433: {
434: if (rootName == null)
435: throw new IllegalArgumentException ("null argument");
436:
437: if (minChildren < 0 || maxChildren < minChildren)
438: throw new IllegalArgumentException ("invalid min or max children argument");
439:
440: nodes.put (rootName, new IIOMetadataNode (rootName));
441: childPolicies.put (rootName, new Integer (IIOMetadataFormat.CHILD_POLICY_REPEAT));
442: childRanges.put (rootName, new int [] { minChildren, maxChildren });
443: this.rootName = rootName;
444: }
445:
446: protected void addAttribute (String elementName,
447: String attrName,
448: int dataType,
449: boolean required,
450: String defaultValue)
451: {
452: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
453: node.setAttributeNode (new IIOMetadataNodeAttr (node,
454: attrName,
455: dataType,
456: required,
457: defaultValue));
458: }
459:
460: protected void addAttribute (String elementName,
461: String attrName,
462: int dataType,
463: boolean required,
464: String defaultValue,
465: List enumeratedValues)
466: {
467: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
468: node.setAttributeNode (new IIOMetadataNodeAttrEnumerated (node,
469: attrName,
470: dataType,
471: required,
472: defaultValue,
473: enumeratedValues));
474: }
475:
476: protected void addAttribute (String elementName,
477: String attrName,
478: int dataType,
479: boolean required,
480: String defaultValue,
481: String minValue,
482: String maxValue,
483: boolean minInclusive,
484: boolean maxInclusive)
485: {
486: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
487: node.setAttributeNode (new IIOMetadataNodeAttrBounded (node,
488: attrName,
489: dataType,
490: required,
491: defaultValue,
492: minValue,
493: maxValue,
494: minInclusive,
495: maxInclusive));
496: }
497:
498: protected void addAttribute (String elementName,
499: String attrName,
500: int dataType,
501: boolean required,
502: int listMinLength,
503: int listMaxLength)
504: {
505: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
506: node.setAttributeNode (new IIOMetadataNodeAttrList (node,
507: attrName,
508: dataType,
509: required,
510: listMinLength,
511: listMaxLength));
512: }
513:
514: protected void addBooleanAttribute (String elementName,
515: String attrName,
516: boolean hasDefaultValue,
517: boolean defaultValue)
518: {
519: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
520:
521: List enumeratedValues = new ArrayList();
522: enumeratedValues.add ("TRUE");
523: enumeratedValues.add ("FALSE");
524:
525: node.setAttributeNode (new IIOMetadataNodeAttrEnumerated (node,
526: attrName,
527: IIOMetadataFormat.DATATYPE_BOOLEAN,
528: hasDefaultValue,
529: defaultValue ? "TRUE" : "FALSE",
530: enumeratedValues));
531: }
532:
533: protected void addChildElement (String elementName, String parentName)
534: {
535: IIOMetadataNode node = (IIOMetadataNode) nodes.get (parentName);
536:
537: node.appendChild (new IIOMetadataNode (elementName));
538: childPolicies.put (elementName, new Integer (IIOMetadataFormat.CHILD_POLICY_REPEAT));
539: }
540:
541: protected void addElement (String elementName, String parentName, int childPolicy)
542: {
543: IIOMetadataNode node = (IIOMetadataNode) nodes.get (parentName);
544:
545: node.appendChild (new IIOMetadataNode (elementName));
546: childPolicies.put (elementName, new Integer (childPolicy));
547: }
548:
549: protected void addElement (String elementName, String parentName,
550: int minChildren, int maxChildren)
551: {
552: addChildElement (elementName, parentName);
553: childRanges.put (elementName, new int [] { minChildren, maxChildren });
554: }
555:
556: private void addNodeObject (IIOMetadataNode node, NodeObject o)
557: {
558: node.setUserObject (o);
559: }
560:
561: private NodeObject getNodeObject (IIOMetadataNode node)
562: {
563: return (NodeObject) node.getUserObject ();
564: }
565:
566: private void removeNodeObject (IIOMetadataNode node)
567: {
568: node.setUserObject (null);
569: }
570:
571: protected void addObjectValue (String elementName, Class classType,
572: boolean required, Object defaultValue)
573: {
574: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
575: addNodeObject (node, new NodeObject (node,
576: classType,
577: required,
578: defaultValue));
579: }
580:
581: protected void addObjectValue (String elementName, Class classType,
582: boolean required, Object defaultValue,
583: List enumeratedValues)
584: {
585: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
586: addNodeObject (node, new NodeObjectEnumerated (node,
587: classType,
588: required,
589: defaultValue,
590: enumeratedValues));
591: }
592:
593: protected void addObjectValue (String elementName, Class classType,
594: Object defaultValue,
595: Comparable minValue,
596: Comparable maxValue,
597: boolean minInclusive,
598: boolean maxInclusive)
599: {
600: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
601: addNodeObject (node, new NodeObjectBounded (node,
602: classType,
603: defaultValue,
604: minValue,
605: maxValue,
606: minInclusive,
607: maxInclusive));
608: }
609:
610: protected void addObjectValue (String elementName, Class classType,
611: int arrayMinLength, int arrayMaxLength)
612: {
613: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
614: addNodeObject (node, new NodeObjectArray (node,
615: classType,
616: arrayMinLength,
617: arrayMaxLength));
618: }
619:
620: public String getRootName ()
621: {
622: return rootName;
623: }
624:
625: protected String getResourceBaseName ()
626: {
627: return resourceBaseName;
628: }
629:
630: public static IIOMetadataFormat getStandardFormatInstance ()
631: {
632:
633: return new IIOMetadataFormatImpl (standardMetadataFormatName,
634: IIOMetadataFormat.CHILD_POLICY_ALL)
635: {
636: public boolean canNodeAppear (String elementName,
637: ImageTypeSpecifier specifier)
638: {
639: return true;
640: }
641: };
642: }
643:
644: public abstract boolean canNodeAppear (String elementName,
645: ImageTypeSpecifier specifier);
646:
647: protected void removeAttribute (String elementName,
648: String attrName)
649: {
650: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
651: node.removeAttribute (attrName);
652: }
653:
654: protected void removeElement (String elementName)
655: {
656: nodes.remove (elementName);
657: }
658:
659: protected void removeObjectValue (String elementName)
660: {
661: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
662: removeNodeObject (node);
663: }
664:
665: protected void setResourceBaseName (String resourceBaseName)
666: {
667: this.resourceBaseName = resourceBaseName;
668: }
669:
670: public int getAttributeDataType (String elementName, String attrName)
671: {
672: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
673: IIOMetadataNodeAttr attr = (IIOMetadataNodeAttr) node.getAttributeNode (attrName);
674: return attr.getDataType ();
675: }
676:
677: public String getAttributeDefaultValue (String elementName, String attrName)
678: {
679: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
680: IIOMetadataNodeAttr attr = (IIOMetadataNodeAttr) node.getAttributeNode (attrName);
681: return attr.getValue();
682: }
683:
684: public String getAttributeDescription (String elementName, String attrName, Locale locale)
685: {
686: return getDescription (elementName + "/" + attrName, locale);
687: }
688:
689: public String[] getAttributeEnumerations (String elementName, String attrName)
690: {
691: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
692: IIOMetadataNodeAttrEnumerated attr =
693: (IIOMetadataNodeAttrEnumerated) node.getAttributeNode (attrName);
694:
695: Object[] attrEnums = attr.getEnumerations();
696:
697: String[] attrNames = new String[attrEnums.length];
698:
699: for (int i = 0; i < attrEnums.length; i++)
700: {
701: attrNames[i] = (String) attrEnums[i];
702: }
703:
704: return attrNames;
705: }
706:
707: public int getAttributeListMaxLength (String elementName, String attrName)
708: {
709: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
710: IIOMetadataNodeAttrList attr =
711: (IIOMetadataNodeAttrList) node.getAttributeNode (attrName);
712: return attr.getListMaxLength();
713: }
714:
715: public int getAttributeListMinLength (String elementName, String attrName)
716: {
717: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
718: IIOMetadataNodeAttrList attr =
719: (IIOMetadataNodeAttrList) node.getAttributeNode (attrName);
720: return attr.getListMinLength();
721: }
722:
723: public String getAttributeMaxValue (String elementName, String attrName)
724: {
725: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
726: IIOMetadataNodeAttrBounded attr =
727: (IIOMetadataNodeAttrBounded) node.getAttributeNode (attrName);
728: return attr.getMaxValue();
729: }
730:
731: public String getAttributeMinValue (String elementName, String attrName)
732: {
733: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
734: IIOMetadataNodeAttrBounded attr =
735: (IIOMetadataNodeAttrBounded) node.getAttributeNode (attrName);
736: return attr.getMinValue();
737: }
738:
739: public String[] getAttributeNames (String elementName)
740: {
741: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
742:
743: NamedNodeMap attrNodes = node.getAttributes();
744:
745: String[] attrNames = new String[attrNodes.getLength()];
746:
747: for (int i = 0; i < attrNodes.getLength(); i++)
748: {
749: attrNames[i] = attrNodes.item (i).getLocalName();
750: }
751:
752: return attrNames;
753: }
754:
755: public int getAttributeValueType (String elementName, String attrName)
756: {
757: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
758: IIOMetadataNodeAttr attr = (IIOMetadataNodeAttr) node.getAttributeNode (attrName);
759: return attr.getDataType();
760: }
761:
762: public String[] getChildNames (String elementName)
763: {
764: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
765:
766: NodeList childNodes = node.getChildNodes();
767:
768: String[] childNames = new String[childNodes.getLength()];
769:
770: for (int i = 0; i < childNodes.getLength(); i++)
771: {
772: childNames[i] = childNodes.item (i).getLocalName();
773: }
774:
775: return childNames;
776: }
777:
778: public int getChildPolicy (String elementName)
779: {
780: return ((Integer) childPolicies.get (elementName)).intValue();
781: }
782:
783: private String getDescription (String resourceName, Locale locale)
784: {
785: if (resourceBaseName == null)
786: return null;
787:
788: Locale l = locale;
789:
790: if (l == null)
791: l = Locale.getDefault();
792:
793: ResourceBundle bundle = ResourceBundle.getBundle (resourceBaseName, locale);
794:
795: String desc = null;
796:
797: if (bundle == null)
798: {
799: try
800: {
801: desc = bundle.getString (resourceName);
802: }
803: catch (MissingResourceException e)
804: {
805: desc = null;
806: }
807: }
808:
809: return desc;
810: }
811:
812: public String getElementDescription (String elementName, Locale locale)
813: {
814: return getDescription (elementName, locale);
815: }
816:
817: public int getElementMaxChildren (String elementName)
818: {
819: return ((int[]) childRanges.get (elementName))[1];
820: }
821:
822: public int getElementMinChildren (String elementName)
823: {
824: return ((int[]) childRanges.get (elementName))[0];
825: }
826:
827: public int getObjectArrayMaxLength (String elementName)
828: {
829: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
830: return ((Integer) ((NodeObjectArray) getNodeObject (node)).getArrayMaxLength ()).intValue();
831: }
832:
833: public int getObjectArrayMinLength (String elementName)
834: {
835: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
836: return ((Integer) ((NodeObjectArray) getNodeObject (node)).getArrayMinLength ()).intValue();
837: }
838:
839: public Class getObjectClass (String elementName)
840: {
841: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
842: return getNodeObject (node).getClassType ();
843: }
844:
845: public Object getObjectDefaultValue (String elementName)
846: {
847: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
848: return getNodeObject (node).getDefaultValue ();
849: }
850:
851: public Object[] getObjectEnumerations (String elementName)
852: {
853: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
854: return ((NodeObjectEnumerated) getNodeObject (node)).getEnumerations ();
855: }
856:
857: public Comparable getObjectMaxValue (String elementName)
858: {
859: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
860: return ((NodeObjectBounded) getNodeObject (node)).getMaxValue ();
861: }
862:
863: public Comparable getObjectMinValue (String elementName)
864: {
865: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
866: return ((NodeObjectBounded) getNodeObject (node)).getMinValue ();
867: }
868:
869: public int getObjectValueType (String elementName)
870: {
871: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
872: NodeObject n = getNodeObject (node);
873:
874: if (n == null)
875: return IIOMetadataFormat.VALUE_NONE;
876: else
877: return n.getValueType ();
878: }
879:
880: public boolean isAttributeRequired (String elementName, String attrName)
881: {
882: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
883: return ((IIOMetadataNodeAttr) node.getAttributeNode (attrName)).isRequired();
884: }
885: }