package org.dbxml.xml;

/*
 * dbXML License, Version 1.0
 *
 *
 * Copyright (c) 1999-2001 The dbXML Group, L.L.C.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by
 *        The dbXML Group (http://www.dbxml.com/)."
 *    Alternately, this acknowledgment may appear in the software
 *    itself, if and wherever such third-party acknowledgments normally
 *    appear.
 *
 * 4. The names "dbXML" and "The dbXML Group" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    info@dbxml.com.
 *
 * 5. Products derived from this software may not be called "dbXML",
 *    nor may "dbXML" appear in their name, without prior written
 *    permission of The dbXML Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE DBXML GROUP OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id: XMLCompressedInput.java,v 1.6 2001/08/05 07:23:09 tom Exp $
 */

import java.io.*;
import org.w3c.dom.*;

/**
 * XMLCompressedInput is an InputStream extension that provides functions for
 * reading type from a dbXML Compressed XML Stream.
 */

public class XMLCompressedInput extends DataInputStream {
   private byte signature = 0;
   private SymbolTable st = null;

   public XMLCompressedInput(InputStream is, SymbolTable st) {
      super(is);
      this.st = st;
   }

   /**
    * readSignature reads a Node signature byte from the input stream.
    *
    * @return The signature
    * @throws IOException if an IOException occurs
    */
   public final byte readSignature() throws IOException {
      signature = readByte();
      return signature;
   }

   /**
    * getSignature returns the most recently read signature byte from the
    * input stream.
    *
    * @return The signature
    */
   public final byte getSignature() {
      return signature;
   }

   /**
    * getNodeType resolves and returns the DOM Node type from the given
    * dbXML compressed signature.
    *
    * @return The DOM Node type
    */
   public final short getNodeType() {
      byte type = (byte)((signature & 0xC0) >> 0x6);
      switch ( type ) {

         case Signatures.Char:
            type = (byte)((signature & 0x20) >> 0x5);
            if ( type == Signatures.CharText )
               return Node.TEXT_NODE;
            else
               return Node.ENTITY_REFERENCE_NODE;

         case Signatures.Elem:
            return Node.ELEMENT_NODE;

         case Signatures.Proc:
            return Node.PROCESSING_INSTRUCTION_NODE;

         case Signatures.Decl:
            type = (byte)((signature & 0x3C) >> 0x2);
            switch ( type ) {

               case Signatures.DeclCData:
                  return Node.CDATA_SECTION_NODE;

               case Signatures.DeclEntity:
                  return Node.ENTITY_NODE;

               case Signatures.DeclComment:
                  return Node.COMMENT_NODE;

               case Signatures.DeclDocType:
                  return Node.DOCUMENT_TYPE_NODE;

               case Signatures.DeclNotation:
                  return Node.NOTATION_NODE;

               default:
                  return Signatures.Unknown;

            }

      }
      return Signatures.Unknown;
   }

   /**
    * readContentSize reads the size of the upcoming content from the Stream,
    * based on the object signature that is provided.
    *
    * @return The Content Size
    * @throws IOException if an IOException occurs
    */
   public final int readContentSize() throws IOException {
      int sizeType = 0;
      short nodeType = getNodeType();
      switch ( nodeType ) {

         case Node.TEXT_NODE:
         case Node.ELEMENT_NODE:
            sizeType = (byte)((signature & 0x0C) >> 0x2);
            break;

         case Node.PROCESSING_INSTRUCTION_NODE:
         case Node.COMMENT_NODE:
         case Node.CDATA_SECTION_NODE:
            sizeType = Signatures.IntContent;
            break;

         case Node.NOTATION_NODE:
            sizeType = (byte)(signature & 0x03);
            break;
      }

      switch ( sizeType ) {

         case Signatures.ByteContent:
            return readByte();

         case Signatures.ShortContent:
            return readShort();

         case Signatures.IntContent:
            return readInt();

         case Signatures.NoContent:
            // Do Nothing

      }
      return 0;
   }

   /**
    * readAttributeCount reads the count of the upcoming Attributes from the
    * Stream based on the object signature that is provided.
    *
    * @return The Content Size
    * @throws IOException if an IOException occurs
    */
   public final int readAttributeCount() throws IOException {
      int countType = 0;
      if ( getNodeType() == Node.ELEMENT_NODE )
         countType = (byte)(signature & 0x03);

      switch ( countType ) {

         case Signatures.ByteContent:
            return readByte();

         case Signatures.ShortContent:
            return readShort();

         case Signatures.IntContent:
            return readInt();

         case Signatures.NoContent:
            // Do Nothing

      }
      return 0;
   }
}
