jdk/src/java.desktop/share/classes/com/sun/imageio/plugins/tiff/TIFFFieldNode.java
changeset 34416 68c0d866db5d
child 36443 d9d59282fc1a
equal deleted inserted replaced
34415:098d54b4051d 34416:68c0d866db5d
       
     1 /*
       
     2  * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package com.sun.imageio.plugins.tiff;
       
    26 
       
    27 import java.util.Arrays;
       
    28 import java.util.List;
       
    29 import javax.imageio.metadata.IIOMetadataNode;
       
    30 import org.w3c.dom.Node;
       
    31 import javax.imageio.plugins.tiff.TIFFDirectory;
       
    32 import javax.imageio.plugins.tiff.TIFFField;
       
    33 import javax.imageio.plugins.tiff.TIFFTag;
       
    34 import javax.imageio.plugins.tiff.TIFFTagSet;
       
    35 
       
    36 /**
       
    37  * The <code>Node</code> representation of a <code>TIFFField</code>
       
    38  * wherein the child node is procedural rather than buffered.
       
    39  */
       
    40 public class TIFFFieldNode extends IIOMetadataNode {
       
    41     private static String getNodeName(TIFFField f) {
       
    42         return f.getData() instanceof TIFFDirectory ?
       
    43             "TIFFIFD" : "TIFFField";
       
    44     }
       
    45 
       
    46     private boolean isIFD;
       
    47 
       
    48     private Boolean isInitialized = Boolean.FALSE;
       
    49 
       
    50     private TIFFField field;
       
    51 
       
    52     public TIFFFieldNode(TIFFField field) {
       
    53         super(getNodeName(field));
       
    54 
       
    55         isIFD = field.getData() instanceof TIFFDirectory;
       
    56 
       
    57         this.field = field;
       
    58 
       
    59         TIFFTag tag = field.getTag();
       
    60         int tagNumber = tag.getNumber();
       
    61         String tagName = tag.getName();
       
    62 
       
    63         if(isIFD) {
       
    64             if(tagNumber != 0) {
       
    65                 setAttribute("parentTagNumber", Integer.toString(tagNumber));
       
    66             }
       
    67             if(tagName != null) {
       
    68                 setAttribute("parentTagName", tagName);
       
    69             }
       
    70 
       
    71             TIFFDirectory dir = (TIFFDirectory)field.getData();
       
    72             TIFFTagSet[] tagSets = dir.getTagSets();
       
    73             if(tagSets != null) {
       
    74                 StringBuilder tagSetNames = new StringBuilder();
       
    75                 for(int i = 0; i < tagSets.length; i++) {
       
    76                     tagSetNames.append(tagSets[i].getClass().getName());
       
    77                     if(i != tagSets.length - 1) {
       
    78                         tagSetNames.append(",");
       
    79                     }
       
    80                 }
       
    81                 setAttribute("tagSets", tagSetNames.toString());
       
    82             }
       
    83         } else {
       
    84             setAttribute("number", Integer.toString(tagNumber));
       
    85             setAttribute("name", tagName);
       
    86         }
       
    87     }
       
    88 
       
    89     private synchronized void initialize() {
       
    90         if(isInitialized) return;
       
    91 
       
    92         if(isIFD) {
       
    93             TIFFDirectory dir = (TIFFDirectory)field.getData();
       
    94             TIFFField[] fields = dir.getTIFFFields();
       
    95             if(fields != null) {
       
    96                 TIFFTagSet[] tagSets = dir.getTagSets();
       
    97                 List<TIFFTagSet> tagSetList = Arrays.asList(tagSets);
       
    98                 int numFields = fields.length;
       
    99                 for(int i = 0; i < numFields; i++) {
       
   100                     TIFFField f = fields[i];
       
   101                     int tagNumber = f.getTagNumber();
       
   102                     TIFFTag tag = TIFFIFD.getTag(tagNumber, tagSetList);
       
   103 
       
   104                     Node node = f.getAsNativeNode();
       
   105 
       
   106                     if (node != null) {
       
   107                         appendChild(node);
       
   108                     }
       
   109                 }
       
   110             }
       
   111         } else {
       
   112             IIOMetadataNode child;
       
   113             int count = field.getCount();
       
   114             if (field.getType() == TIFFTag.TIFF_UNDEFINED) {
       
   115                 child = new IIOMetadataNode("TIFFUndefined");
       
   116 
       
   117                 byte[] data = field.getAsBytes();
       
   118                 StringBuffer sb = new StringBuffer();
       
   119                 for (int i = 0; i < count; i++) {
       
   120                     sb.append(Integer.toString(data[i] & 0xff));
       
   121                     if (i < count - 1) {
       
   122                         sb.append(",");
       
   123                     }
       
   124                 }
       
   125                 child.setAttribute("value", sb.toString());
       
   126             } else {
       
   127                 child = new IIOMetadataNode("TIFF" +
       
   128                                             TIFFField.getTypeName(field.getType()) +
       
   129                                             "s");
       
   130 
       
   131                 TIFFTag tag = field.getTag();
       
   132 
       
   133                 for (int i = 0; i < count; i++) {
       
   134                     IIOMetadataNode cchild =
       
   135                         new IIOMetadataNode("TIFF" +
       
   136                                             TIFFField.getTypeName(field.getType()));
       
   137 
       
   138                     cchild.setAttribute("value", field.getValueAsString(i));
       
   139                     if (tag.hasValueNames() && field.isIntegral()) {
       
   140                         int value = field.getAsInt(i);
       
   141                         String name = tag.getValueName(value);
       
   142                         if (name != null) {
       
   143                             cchild.setAttribute("description", name);
       
   144                         }
       
   145                     }
       
   146 
       
   147                     child.appendChild(cchild);
       
   148                 }
       
   149             }
       
   150             appendChild(child);
       
   151         }
       
   152 
       
   153         isInitialized = Boolean.TRUE;
       
   154     }
       
   155 
       
   156     // Need to override this method to avoid a stack overflow exception
       
   157     // which will occur if super.appendChild is called from initialize().
       
   158     public Node appendChild(Node newChild) {
       
   159         if (newChild == null) {
       
   160             throw new NullPointerException("newChild == null!");
       
   161         }
       
   162 
       
   163         return super.insertBefore(newChild, null);
       
   164     }
       
   165 
       
   166     // Override all methods which refer to child nodes.
       
   167 
       
   168     public boolean hasChildNodes() {
       
   169         initialize();
       
   170         return super.hasChildNodes();
       
   171     }
       
   172 
       
   173     public int getLength() {
       
   174         initialize();
       
   175         return super.getLength();
       
   176     }
       
   177 
       
   178     public Node getFirstChild() {
       
   179         initialize();
       
   180         return super.getFirstChild();
       
   181     }
       
   182 
       
   183     public Node getLastChild() {
       
   184         initialize();
       
   185         return super.getLastChild();
       
   186     }
       
   187 
       
   188     public Node getPreviousSibling() {
       
   189         initialize();
       
   190         return super.getPreviousSibling();
       
   191     }
       
   192 
       
   193     public Node getNextSibling() {
       
   194         initialize();
       
   195         return super.getNextSibling();
       
   196     }
       
   197 
       
   198     public Node insertBefore(Node newChild,
       
   199                              Node refChild) {
       
   200         initialize();
       
   201         return super.insertBefore(newChild, refChild);
       
   202     }
       
   203 
       
   204     public Node replaceChild(Node newChild,
       
   205                              Node oldChild) {
       
   206         initialize();
       
   207         return super.replaceChild(newChild, oldChild);
       
   208     }
       
   209 
       
   210     public Node removeChild(Node oldChild) {
       
   211         initialize();
       
   212         return super.removeChild(oldChild);
       
   213     }
       
   214 
       
   215     public Node cloneNode(boolean deep) {
       
   216         initialize();
       
   217         return super.cloneNode(deep);
       
   218     }
       
   219 }