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