KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > imageio > plugins > jpeg > JPEGMetadataFormat


1 /*
2  * @(#)JPEGMetadataFormat.java 1.10 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.imageio.plugins.jpeg;
9
10 import javax.imageio.metadata.IIOMetadataFormat JavaDoc;
11 import javax.imageio.metadata.IIOMetadataFormatImpl JavaDoc;
12 import javax.imageio.ImageTypeSpecifier JavaDoc;
13 import javax.imageio.plugins.jpeg.JPEGQTable JavaDoc;
14 import javax.imageio.plugins.jpeg.JPEGHuffmanTable JavaDoc;
15
16 import java.util.List JavaDoc;
17 import java.util.ArrayList JavaDoc;
18
19 abstract class JPEGMetadataFormat extends IIOMetadataFormatImpl JavaDoc {
20     // 2-byte length reduces max to 65533
21
private static final int MAX_JPEG_DATA_SIZE = 65533;
22
23     String JavaDoc resourceBaseName = this.getClass().getName() + "Resources";
24
25     JPEGMetadataFormat(String JavaDoc formatName, int childPolicy) {
26         super(formatName, childPolicy);
27         setResourceBaseName(resourceBaseName);
28     }
29
30     // Format shared between image and stream formats
31
void addStreamElements(String JavaDoc parentName) {
32         addElement("dqt", parentName, 1, 4);
33
34         addElement("dqtable", "dqt", CHILD_POLICY_EMPTY);
35
36         addAttribute("dqtable",
37                      "elementPrecision",
38                      DATATYPE_INTEGER,
39                      false,
40                      "0");
41         List JavaDoc tabids = new ArrayList JavaDoc();
42         tabids.add("0");
43         tabids.add("1");
44         tabids.add("2");
45         tabids.add("3");
46         addAttribute("dqtable",
47                      "qtableId",
48                      DATATYPE_INTEGER,
49                      true,
50                      null,
51                      tabids);
52         addObjectValue("dqtable",
53                        JPEGQTable JavaDoc.class,
54                        true,
55                        null);
56
57         addElement("dht", parentName, 1, 4);
58         addElement("dhtable", "dht", CHILD_POLICY_EMPTY);
59         List JavaDoc classes = new ArrayList JavaDoc();
60         classes.add("0");
61         classes.add("1");
62         addAttribute("dhtable",
63                      "class",
64                      DATATYPE_INTEGER,
65                      true,
66                      null,
67                      classes);
68         addAttribute("dhtable",
69                      "htableId",
70                      DATATYPE_INTEGER,
71                      true,
72                      null,
73                      tabids);
74         addObjectValue("dhtable",
75                        JPEGHuffmanTable JavaDoc.class,
76                        true,
77                        null);
78
79         
80         addElement("dri", parentName, CHILD_POLICY_EMPTY);
81         addAttribute("dri",
82                      "interval",
83                      DATATYPE_INTEGER,
84                      true,
85                      null,
86                      "0", "65535",
87                      true, true);
88
89         addElement("com", parentName, CHILD_POLICY_EMPTY);
90         addAttribute("com",
91                      "comment",
92                      DATATYPE_STRING,
93                      false,
94                      null);
95         addObjectValue("com", byte[].class, 1, MAX_JPEG_DATA_SIZE);
96
97         addElement("unknown", parentName, CHILD_POLICY_EMPTY);
98         addAttribute("unknown",
99                      "MarkerTag",
100                      DATATYPE_INTEGER,
101                      true,
102                      null,
103                      "0", "255",
104                      true, true);
105         addObjectValue("unknown", byte[].class, 1, MAX_JPEG_DATA_SIZE);
106     }
107
108     public boolean canNodeAppear(String JavaDoc elementName,
109                                  ImageTypeSpecifier JavaDoc imageType) {
110         // Just check if it appears in the format
111
if (isInSubtree(elementName, getRootName())){
112             return true;
113         }
114         return false;
115     }
116
117     /**
118      * Returns <code>true</code> if the named element occurs in the
119      * subtree of the format starting with the node named by
120      * <code>subtreeName</code>, including the node
121      * itself. <code>subtreeName</code> may be any node in
122      * the format. If it is not, an
123      * <code>IllegalArgumentException</code> is thrown.
124      */

125     protected boolean isInSubtree(String JavaDoc elementName,
126                                   String JavaDoc subtreeName) {
127         if (elementName.equals(subtreeName)) {
128             return true;
129         }
130         String JavaDoc [] children = getChildNames(elementName);
131         for (int i=0; i < children.length; i++) {
132             if (isInSubtree(elementName, children[i])) {
133                 return true;
134             }
135         }
136         return false;
137     }
138
139 }
140
Popular Tags