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 */ 25package com.sun.imageio.plugins.tiff; 26 27import java.nio.ByteOrder; 28import javax.imageio.metadata.IIOMetadata; 29import javax.imageio.metadata.IIOMetadataNode; 30import javax.imageio.metadata.IIOInvalidTreeException; 31import org.w3c.dom.NamedNodeMap; 32import org.w3c.dom.Node; 33 34public class TIFFStreamMetadata extends IIOMetadata { 35 36 // package scope 37 static final String NATIVE_METADATA_FORMAT_NAME = 38 "javax_imageio_tiff_stream_1.0"; 39 40 static final String NATIVE_METADATA_FORMAT_CLASS_NAME = 41 "javax.imageio.plugins.tiff.TIFFStreamMetadataFormat"; 42 43 private static final String bigEndianString = 44 ByteOrder.BIG_ENDIAN.toString(); 45 private static final String littleEndianString = 46 ByteOrder.LITTLE_ENDIAN.toString(); 47 48 public ByteOrder byteOrder = ByteOrder.BIG_ENDIAN; 49 50 public TIFFStreamMetadata() { 51 super(false, 52 NATIVE_METADATA_FORMAT_NAME, 53 NATIVE_METADATA_FORMAT_CLASS_NAME, 54 null, null); 55 } 56 57 public boolean isReadOnly() { 58 return false; 59 } 60 61 // Shorthand for throwing an IIOInvalidTreeException 62 private static void fatal(Node node, String reason) 63 throws IIOInvalidTreeException { 64 throw new IIOInvalidTreeException(reason, node); 65 } 66 67 public Node getAsTree(String formatName) { 68 IIOMetadataNode root = new IIOMetadataNode(nativeMetadataFormatName); 69 70 IIOMetadataNode byteOrderNode = new IIOMetadataNode("ByteOrder"); 71 byteOrderNode.setAttribute("value", byteOrder.toString()); 72 73 root.appendChild(byteOrderNode); 74 return root; 75 } 76 77// public void setFromTree(String formatName, Node root) { 78// } 79 80 private void mergeNativeTree(Node root) throws IIOInvalidTreeException { 81 Node node = root; 82 if (!node.getNodeName().equals(nativeMetadataFormatName)) { 83 fatal(node, "Root must be " + nativeMetadataFormatName); 84 } 85 86 node = node.getFirstChild(); 87 if (node == null || !node.getNodeName().equals("ByteOrder")) { 88 fatal(node, "Root must have \"ByteOrder\" child"); 89 } 90 91 NamedNodeMap attrs = node.getAttributes(); 92 String order = attrs.getNamedItem("value").getNodeValue(); 93 94 if (order == null) { 95 fatal(node, "ByteOrder node must have a \"value\" attribute"); 96 } 97 if (order.equals(bigEndianString)) { 98 this.byteOrder = ByteOrder.BIG_ENDIAN; 99 } else if (order.equals(littleEndianString)) { 100 this.byteOrder = ByteOrder.LITTLE_ENDIAN; 101 } else { 102 fatal(node, "Incorrect value for ByteOrder \"value\" attribute"); 103 } 104 } 105 106 public void mergeTree(String formatName, Node root) 107 throws IIOInvalidTreeException { 108 if (formatName.equals(nativeMetadataFormatName)) { 109 if (root == null) { 110 throw new NullPointerException("root == null!"); 111 } 112 mergeNativeTree(root); 113 } else { 114 throw new IllegalArgumentException("Not a recognized format!"); 115 } 116 } 117 118 public void reset() { 119 this.byteOrder = ByteOrder.BIG_ENDIAN; 120 } 121} 122