001/*
002 * $RCSfile: UUIDListBox.java,v $
003 *
004 * 
005 * Copyright (c) 2005 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/10/10 23:48:57 $
043 * $State: Exp $
044 */
045package com.sun.media.imageioimpl.plugins.jpeg2000;
046
047import javax.imageio.metadata.IIOInvalidTreeException;
048import javax.imageio.metadata.IIOMetadataNode;
049import org.w3c.dom.Node;
050import org.w3c.dom.NodeList;
051
052import com.sun.media.imageioimpl.common.ImageUtil;
053
054/** This class is defined to represent a UUID list Box of JPEG JP2
055 *  file format.  This type of box has a length, a type of "ulst".  Its
056 *  contents include the number of UUID entry and a list of 16-byte UUIDs.
057 */
058public class UUIDListBox extends Box {
059    /** The data elements of this box. */
060    private short num;
061    private byte[][] uuids;
062
063    /** Constructs a <code>UUIDListBox</code> from the provided uuid number
064     *  and uuids.  The provided uuids should have a size of 16; otherwise,
065     *  <code>Exception</code> may thrown in later the process.  The provided
066     *  number should consistent with the size of the uuid array.
067     */
068    public UUIDListBox(short num, byte[][] uuids) {
069        super(10 + (uuids.length << 4), 0x756c7374, null);
070        this.num = num;
071        this.uuids = uuids;
072    }
073
074    /** Constructs a <code>UUIDListBox</code> from the provided content
075     *  data array.
076     */
077    public UUIDListBox(byte[] data) {
078        super(8 + data.length, 0x756c7374, data);
079    }
080
081    /** Constructs a <code>UUIDListBox</code> based on the provided
082     *  <code>org.w3c.dom.Node</code>.
083     */
084    public UUIDListBox(Node node) throws IIOInvalidTreeException {
085        super(node);
086        NodeList children = node.getChildNodes();
087        int index = 0;
088
089        for (int i = 0; i < children.getLength(); i++) {
090            Node child = children.item(i);
091
092            if ("NumberUUID".equals(child.getNodeName())) {
093                num = (short)Box.getShortElementValue(child);
094                uuids = new byte[num][];
095            }
096
097        }
098
099        for (int i = 0; i < children.getLength(); i++) {
100            Node child = children.item(i);
101
102            if ("UUID".equals(child.getNodeName()) && index < num) {
103                uuids[index++] = Box.getByteArrayElementValue(child);
104            }
105        }
106    }
107
108    /** Parses the data elements from the provided content data array. */
109    protected void parse(byte[] data) {
110        num = (short)(((data[0] & 0xFF) << 8) | (data[1] & 0xFF));
111
112        uuids = new byte[num][];
113        int pos = 2;
114        for (int i = 0; i < num; i++) {
115            uuids[i] = new byte[16];
116            System.arraycopy(data, pos, uuids[i], 0, 16);
117            pos += 16;
118        }
119    }
120
121    /** Creates an <code>IIOMetadataNode</code> from this UUID list
122     *  box.  The format of this node is defined in the XML dtd and xsd
123     *  for the JP2 image file.
124     */
125    public IIOMetadataNode getNativeNode() {
126        IIOMetadataNode node = new IIOMetadataNode(Box.getName(getType()));
127        setDefaultAttributes(node);
128
129        IIOMetadataNode child = new IIOMetadataNode("NumberUUID");
130        child.setUserObject(new Short(num));
131        child.setNodeValue("" + num);
132        node.appendChild(child);
133
134        for (int i = 0; i < num; i++) {
135            child = new IIOMetadataNode("UUID");
136            child.setUserObject(uuids[i]);
137            child.setNodeValue(ImageUtil.convertObjectToString(uuids[i]));
138            node.appendChild(child);
139        }
140
141        return node;
142    }
143
144    protected void compose() {
145        if (data != null)
146            return;
147        data = new byte[2 + num * 16];
148
149        data[0] = (byte)(num >> 8);
150        data[1] = (byte)(num & 0xFF);
151
152        for (int i = 0, pos = 2; i < num; i++) {
153            System.arraycopy(uuids[i], 0, data, pos, 16);
154            pos += 16;
155        }
156    }
157}