1 /**
   2  * Copyright (c) 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package simp;
  25 
  26 import javax.imageio.metadata.IIOMetadata;
  27 import javax.imageio.metadata.IIOMetadataNode;
  28 import javax.imageio.metadata.IIOMetadataFormatImpl;
  29 import org.w3c.dom.Node;
  30 
  31 public class SIMPMetadata extends IIOMetadata {
  32 
  33    static final String nativeMetadataFormatName = "simp_metadata_1.0";
  34 
  35    private int width, height;
  36 
  37    public SIMPMetadata() {
  38        super(true,
  39              nativeMetadataFormatName, "simp.SIMPMetadataFormat", null, null);
  40    }
  41 
  42    public SIMPMetadata(int width, int height) {
  43        this();
  44        this.width = width;
  45        this.height = height;
  46    }
  47 
  48    public boolean isReadOnly() {
  49         return true;
  50    }
  51 
  52    public void setFromTree(String formatName, Node root) {
  53     }
  54 
  55     public void mergeTree(String formatName, Node root) {
  56         throw new IllegalStateException("read only metadata");
  57     }
  58 
  59     public void reset() {
  60         throw new IllegalStateException("read only metadata");
  61     }
  62 
  63     private IIOMetadataNode addChildNode(IIOMetadataNode root,
  64                                          String name,
  65                                          Object object) {
  66         IIOMetadataNode child = new IIOMetadataNode(name);
  67         if (object != null) {
  68             child.setUserObject(object);
  69             child.setNodeValue(object.toString());
  70         }
  71         root.appendChild(child);
  72         return child;
  73     }
  74 
  75     private Node getNativeTree() {
  76         IIOMetadataNode root =
  77             new IIOMetadataNode(nativeMetadataFormatName);
  78         addChildNode(root, "width", width);
  79         addChildNode(root, "weight", height);
  80         return root;
  81     }
  82 
  83     public Node getAsTree(String formatName) {
  84         if (formatName.equals(nativeMetadataFormatName)) {
  85             return getNativeTree();
  86         } else if (formatName.equals
  87                    (IIOMetadataFormatImpl.standardMetadataFormatName)) {
  88             return getStandardTree();
  89         } else {
  90             throw new IllegalArgumentException("unsupported format");
  91         }
  92     }
  93 }
  94