001: /*
002: * $RCSfile: TIFFFieldNode.java,v $
003: *
004: *
005: * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * - Redistribution of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * - Redistribution in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * Neither the name of Sun Microsystems, Inc. or the names of
020: * contributors may be used to endorse or promote products derived
021: * from this software without specific prior written permission.
022: *
023: * This software is provided "AS IS," without a warranty of any
024: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
025: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
026: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
027: * EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
028: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
029: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
030: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
031: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
032: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
033: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
034: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
035: * POSSIBILITY OF SUCH DAMAGES.
036: *
037: * You acknowledge that this software is not designed or intended for
038: * use in the design, construction, operation or maintenance of any
039: * nuclear facility.
040: *
041: * $Revision: 1.2 $
042: * $Date: 2006/04/18 20:47:02 $
043: * $State: Exp $
044: */
045: package com.sun.media.imageioimpl.plugins.tiff;
046:
047: import java.io.IOException;
048: import java.io.Serializable;
049: import java.util.Arrays;
050: import java.util.List;
051: import javax.imageio.metadata.IIOMetadata;
052: import javax.imageio.metadata.IIOMetadataNode;
053: import javax.imageio.stream.ImageOutputStream;
054: import org.w3c.dom.Node;
055: import org.w3c.dom.NodeList;
056: import com.sun.media.imageio.plugins.tiff.TIFFDirectory;
057: import com.sun.media.imageio.plugins.tiff.TIFFField;
058: import com.sun.media.imageio.plugins.tiff.TIFFTag;
059: import com.sun.media.imageio.plugins.tiff.TIFFTagSet;
060: import com.sun.media.imageioimpl.plugins.tiff.TIFFIFD;
061: import com.sun.media.imageioimpl.plugins.tiff.TIFFImageMetadata;
062:
063: /**
064: * The <code>Node</code> representation of a <code>TIFFField</code>
065: * wherein the child node is procedural rather than buffered.
066: *
067: * @since 1.1-beta
068: */
069: public class TIFFFieldNode extends IIOMetadataNode {
070: private static String getNodeName(TIFFField f) {
071: return f.getData() instanceof TIFFDirectory ? "TIFFIFD"
072: : "TIFFField";
073: }
074:
075: private boolean isIFD;
076:
077: /** Initialization flag. */
078: private Boolean isInitialized = Boolean.FALSE;
079:
080: private TIFFField field;
081:
082: // XXX Set the user object to "field"?
083: public TIFFFieldNode(TIFFField field) {
084: super (getNodeName(field));
085:
086: isIFD = field.getData() instanceof TIFFDirectory;
087:
088: this .field = field;
089:
090: TIFFTag tag = field.getTag();
091: int tagNumber = tag.getNumber();
092: String tagName = tag.getName();
093:
094: if (isIFD) {
095: if (tagNumber != 0) {
096: setAttribute("parentTagNumber", Integer
097: .toString(tagNumber));
098: }
099: if (tagName != null) {
100: setAttribute("parentTagName", tagName);
101: }
102:
103: TIFFDirectory dir = (TIFFDirectory) field.getData();
104: TIFFTagSet[] tagSets = dir.getTagSets();
105: if (tagSets != null) {
106: String tagSetNames = "";
107: for (int i = 0; i < tagSets.length; i++) {
108: tagSetNames += tagSets[i].getClass().getName();
109: if (i != tagSets.length - 1) {
110: tagSetNames += ",";
111: }
112: }
113: setAttribute("tagSets", tagSetNames);
114: }
115: } else {
116: setAttribute("number", Integer.toString(tagNumber));
117: setAttribute("name", tagName);
118: }
119: }
120:
121: private synchronized void initialize() {
122: if (isInitialized == Boolean.TRUE)
123: return;
124:
125: if (isIFD) {
126: TIFFDirectory dir = (TIFFDirectory) field.getData();
127: TIFFField[] fields = dir.getTIFFFields();
128: if (fields != null) {
129: TIFFTagSet[] tagSets = dir.getTagSets();
130: List tagSetList = Arrays.asList(tagSets);
131: int numFields = fields.length;
132: for (int i = 0; i < numFields; i++) {
133: TIFFField f = fields[i];
134: int tagNumber = f.getTagNumber();
135: TIFFTag tag = TIFFIFD.getTag(tagNumber, tagSetList);
136:
137: Node node = f.getAsNativeNode();
138:
139: if (node != null) {
140: appendChild(node);
141: }
142: }
143: }
144: } else {
145: IIOMetadataNode child;
146: int count = field.getCount();
147: if (field.getType() == TIFFTag.TIFF_UNDEFINED) {
148: child = new IIOMetadataNode("TIFFUndefined");
149:
150: byte[] data = field.getAsBytes();
151: StringBuffer sb = new StringBuffer();
152: for (int i = 0; i < count; i++) {
153: sb.append(Integer.toString(data[i] & 0xff));
154: if (i < count - 1) {
155: sb.append(",");
156: }
157: }
158: child.setAttribute("value", sb.toString());
159: } else {
160: child = new IIOMetadataNode("TIFF"
161: + field.getTypeName(field.getType()) + "s");
162:
163: TIFFTag tag = field.getTag();
164:
165: for (int i = 0; i < count; i++) {
166: IIOMetadataNode cchild = new IIOMetadataNode("TIFF"
167: + field.getTypeName(field.getType()));
168:
169: cchild.setAttribute("value", field
170: .getValueAsString(i));
171: if (tag.hasValueNames() && field.isIntegral()) {
172: int value = field.getAsInt(i);
173: String name = tag.getValueName(value);
174: if (name != null) {
175: cchild.setAttribute("description", name);
176: }
177: }
178:
179: child.appendChild(cchild);
180: }
181: }
182: appendChild(child);
183: }
184:
185: isInitialized = Boolean.TRUE;
186: }
187:
188: // Need to override this method to avoid a stack overflow exception
189: // which will occur if super.appendChild is called from initialize().
190: public Node appendChild(Node newChild) {
191: if (newChild == null) {
192: throw new IllegalArgumentException("newChild == null!");
193: }
194:
195: return super .insertBefore(newChild, null);
196: }
197:
198: // Override all methods which refer to child nodes.
199:
200: public boolean hasChildNodes() {
201: initialize();
202: return super .hasChildNodes();
203: }
204:
205: public int getLength() {
206: initialize();
207: return super .getLength();
208: }
209:
210: public Node getFirstChild() {
211: initialize();
212: return super .getFirstChild();
213: }
214:
215: public Node getLastChild() {
216: initialize();
217: return super .getLastChild();
218: }
219:
220: public Node getPreviousSibling() {
221: initialize();
222: return super .getPreviousSibling();
223: }
224:
225: public Node getNextSibling() {
226: initialize();
227: return super .getNextSibling();
228: }
229:
230: public Node insertBefore(Node newChild, Node refChild) {
231: initialize();
232: return super .insertBefore(newChild, refChild);
233: }
234:
235: public Node replaceChild(Node newChild, Node oldChild) {
236: initialize();
237: return super .replaceChild(newChild, oldChild);
238: }
239:
240: public Node removeChild(Node oldChild) {
241: initialize();
242: return super .removeChild(oldChild);
243: }
244:
245: public Node cloneNode(boolean deep) {
246: initialize();
247: return super.cloneNode(deep);
248: }
249: }
|