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<String> 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 <T> void addObjectValue (String elementName, Class<T> classType,
572: boolean required, T 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 <T> void addObjectValue (String elementName, Class<T> classType,
582: boolean required, T defaultValue,
583: List<? extends T> 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 <T extends Object & Comparable<? super T>>
594: void addObjectValue (String elementName, Class<T> classType,
595: T defaultValue,
596: Comparable<? super T> minValue,
597: Comparable<? super T> maxValue,
598: boolean minInclusive,
599: boolean maxInclusive)
600: {
601: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
602: addNodeObject (node, new NodeObjectBounded (node,
603: classType,
604: defaultValue,
605: minValue,
606: maxValue,
607: minInclusive,
608: maxInclusive));
609: }
610:
611: protected void addObjectValue (String elementName, Class<?> classType,
612: int arrayMinLength, int arrayMaxLength)
613: {
614: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
615: addNodeObject (node, new NodeObjectArray (node,
616: classType,
617: arrayMinLength,
618: arrayMaxLength));
619: }
620:
621: public String getRootName ()
622: {
623: return rootName;
624: }
625:
626: protected String getResourceBaseName ()
627: {
628: return resourceBaseName;
629: }
630:
631: public static IIOMetadataFormat getStandardFormatInstance ()
632: {
633:
634: return new IIOMetadataFormatImpl (standardMetadataFormatName,
635: IIOMetadataFormat.CHILD_POLICY_ALL)
636: {
637: public boolean canNodeAppear (String elementName,
638: ImageTypeSpecifier specifier)
639: {
640: return true;
641: }
642: };
643: }
644:
645: public abstract boolean canNodeAppear (String elementName,
646: ImageTypeSpecifier specifier);
647:
648: protected void removeAttribute (String elementName,
649: String attrName)
650: {
651: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
652: node.removeAttribute (attrName);
653: }
654:
655: protected void removeElement (String elementName)
656: {
657: nodes.remove (elementName);
658: }
659:
660: protected void removeObjectValue (String elementName)
661: {
662: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
663: removeNodeObject (node);
664: }
665:
666: protected void setResourceBaseName (String resourceBaseName)
667: {
668: this.resourceBaseName = resourceBaseName;
669: }
670:
671: public int getAttributeDataType (String elementName, String attrName)
672: {
673: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
674: IIOMetadataNodeAttr attr = (IIOMetadataNodeAttr) node.getAttributeNode (attrName);
675: return attr.getDataType ();
676: }
677:
678: public String getAttributeDefaultValue (String elementName, String attrName)
679: {
680: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
681: IIOMetadataNodeAttr attr = (IIOMetadataNodeAttr) node.getAttributeNode (attrName);
682: return attr.getValue();
683: }
684:
685: public String getAttributeDescription (String elementName, String attrName, Locale locale)
686: {
687: return getDescription (elementName + "/" + attrName, locale);
688: }
689:
690: public String[] getAttributeEnumerations (String elementName, String attrName)
691: {
692: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
693: IIOMetadataNodeAttrEnumerated attr =
694: (IIOMetadataNodeAttrEnumerated) node.getAttributeNode (attrName);
695:
696: Object[] attrEnums = attr.getEnumerations();
697:
698: String[] attrNames = new String[attrEnums.length];
699:
700: for (int i = 0; i < attrEnums.length; i++)
701: {
702: attrNames[i] = (String) attrEnums[i];
703: }
704:
705: return attrNames;
706: }
707:
708: public int getAttributeListMaxLength (String elementName, String attrName)
709: {
710: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
711: IIOMetadataNodeAttrList attr =
712: (IIOMetadataNodeAttrList) node.getAttributeNode (attrName);
713: return attr.getListMaxLength();
714: }
715:
716: public int getAttributeListMinLength (String elementName, String attrName)
717: {
718: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
719: IIOMetadataNodeAttrList attr =
720: (IIOMetadataNodeAttrList) node.getAttributeNode (attrName);
721: return attr.getListMinLength();
722: }
723:
724: public String getAttributeMaxValue (String elementName, String attrName)
725: {
726: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
727: IIOMetadataNodeAttrBounded attr =
728: (IIOMetadataNodeAttrBounded) node.getAttributeNode (attrName);
729: return attr.getMaxValue();
730: }
731:
732: public String getAttributeMinValue (String elementName, String attrName)
733: {
734: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
735: IIOMetadataNodeAttrBounded attr =
736: (IIOMetadataNodeAttrBounded) node.getAttributeNode (attrName);
737: return attr.getMinValue();
738: }
739:
740: public String[] getAttributeNames (String elementName)
741: {
742: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
743:
744: NamedNodeMap attrNodes = node.getAttributes();
745:
746: String[] attrNames = new String[attrNodes.getLength()];
747:
748: for (int i = 0; i < attrNodes.getLength(); i++)
749: {
750: attrNames[i] = attrNodes.item (i).getLocalName();
751: }
752:
753: return attrNames;
754: }
755:
756: public int getAttributeValueType (String elementName, String attrName)
757: {
758: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
759: IIOMetadataNodeAttr attr = (IIOMetadataNodeAttr) node.getAttributeNode (attrName);
760: return attr.getDataType();
761: }
762:
763: public String[] getChildNames (String elementName)
764: {
765: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
766:
767: NodeList childNodes = node.getChildNodes();
768:
769: String[] childNames = new String[childNodes.getLength()];
770:
771: for (int i = 0; i < childNodes.getLength(); i++)
772: {
773: childNames[i] = childNodes.item (i).getLocalName();
774: }
775:
776: return childNames;
777: }
778:
779: public int getChildPolicy (String elementName)
780: {
781: return ((Integer) childPolicies.get (elementName)).intValue();
782: }
783:
784: private String getDescription (String resourceName, Locale locale)
785: {
786: if (resourceBaseName == null)
787: return null;
788:
789: Locale l = locale;
790:
791: if (l == null)
792: l = Locale.getDefault();
793:
794: ResourceBundle bundle = ResourceBundle.getBundle (resourceBaseName, locale);
795:
796: String desc = null;
797:
798: if (bundle == null)
799: {
800: try
801: {
802: desc = bundle.getString (resourceName);
803: }
804: catch (MissingResourceException e)
805: {
806: desc = null;
807: }
808: }
809:
810: return desc;
811: }
812:
813: public String getElementDescription (String elementName, Locale locale)
814: {
815: return getDescription (elementName, locale);
816: }
817:
818: public int getElementMaxChildren (String elementName)
819: {
820: return ((int[]) childRanges.get (elementName))[1];
821: }
822:
823: public int getElementMinChildren (String elementName)
824: {
825: return ((int[]) childRanges.get (elementName))[0];
826: }
827:
828: public int getObjectArrayMaxLength (String elementName)
829: {
830: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
831: return ((Integer) ((NodeObjectArray) getNodeObject (node)).getArrayMaxLength ()).intValue();
832: }
833:
834: public int getObjectArrayMinLength (String elementName)
835: {
836: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
837: return ((Integer) ((NodeObjectArray) getNodeObject (node)).getArrayMinLength ()).intValue();
838: }
839:
840: public Class<?> getObjectClass (String elementName)
841: {
842: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
843: return getNodeObject (node).getClassType ();
844: }
845:
846: public Object getObjectDefaultValue (String elementName)
847: {
848: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
849: return getNodeObject (node).getDefaultValue ();
850: }
851:
852: public Object[] getObjectEnumerations (String elementName)
853: {
854: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
855: return ((NodeObjectEnumerated) getNodeObject (node)).getEnumerations ();
856: }
857:
858: public Comparable<?> getObjectMaxValue (String elementName)
859: {
860: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
861: return ((NodeObjectBounded) getNodeObject (node)).getMaxValue ();
862: }
863:
864: public Comparable<?> getObjectMinValue (String elementName)
865: {
866: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
867: return ((NodeObjectBounded) getNodeObject (node)).getMinValue ();
868: }
869:
870: public int getObjectValueType (String elementName)
871: {
872: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
873: NodeObject n = getNodeObject (node);
874:
875: if (n == null)
876: return IIOMetadataFormat.VALUE_NONE;
877: else
878: return n.getValueType ();
879: }
880:
881: public boolean isAttributeRequired (String elementName, String attrName)
882: {
883: IIOMetadataNode node = (IIOMetadataNode) nodes.get (elementName);
884: return ((IIOMetadataNodeAttr) node.getAttributeNode (attrName)).isRequired();
885: }
886: }