Package org.ozoneDB.xml.util

Source Code of org.ozoneDB.xml.util.CompiledXMLInputStream

/*

============================================================================
                   The Apache Software License, Version 1.1
============================================================================

Copyright (C) 2000 The Apache Software Foundation. All rights reserved.

Redistribution and use in source and binary forms, with or without modifica-
tion, 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  Apache Software Foundation  (http://www.apache.org/)."
    Alternately, this  acknowledgment may  appear in the software itself,  if
    and wherever such third-party acknowledgments normally appear.

4. The names "Cocoon" and  "Apache Software Foundation"  must not be used to
    endorse  or promote  products derived  from this  software without  prior
    written permission. For written permission, please contact
    apache@apache.org.

5. Products  derived from this software may not  be called "Apache", nor may
    "Apache" appear  in their name,  without prior written permission  of the
    Apache Software Foundation.

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
APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
DING, 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.

This software  consists of voluntary contributions made  by many individuals
on  behalf of the Apache Software  Foundation and was  originally created by
Stefano Mazzocchi  <stefano@apache.org>. For more  information on the Apache
Software Foundation, please see <http://www.apache.org/>.

*/

package org.ozoneDB.xml.util;

import java.io.InputStream;
import java.io.IOException;
import java.io.EOFException;
import java.io.UTFDataFormatException;
import java.io.Serializable;

import java.util.ArrayList;

/**
@author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>, modifications
*          for Ozone by <a href="mailto:conny@smb-tec.com">Conny Krappatsch</a>
*/


final class CompiledXMLInputStream extends InputStream implements Serializable {
   
    private final InputStream in;
    private final ArrayList list = new ArrayList();
   
   
    public CompiledXMLInputStream( InputStream input ) throws IOException{
        this.in = input;
    }
   
   
    public final int readEvent() throws IOException {
        return this.in.read();
    }
   
   
    public final int readAttributes() throws IOException {
        InputStream in = this.in;
        int ch1 = in.read();
        int ch2 = in.read();
        return (ch1 << 8) + (ch2 << 0);
    }
   
   
    public final String readString() throws IOException {
        int length = this.readLength();
        int index = length & 0x00007FFF;
        if (length >= 0x00008000) {
            return (String)list.get( index );
        } else {
            String str = new String( this.readChars( index ) );
            list.add( str );
           // System.out.println( "index: " + list.size() + ", str=" + str);
            return str;
        }
    }
   
   
    public final char[] readChars() throws IOException {
        return readChars( this.readLength() );
    }
   
   
    public final int read() throws IOException {
        return this.in.read();
    }
   
   
    private char[] readChars( int len ) throws IOException {
        char[] str = new char[len];
        byte[] bytearr = new byte[len];
        int c;
        int char2;
        int char3;
        int count = 0;
        int i = 0;
       
        this.readBytes( bytearr );
       
        while (count < len) {
            c = (int)bytearr[count] & 0xff;
            switch (c >> 4) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
                /* 0xxxxxxx*/
                count++;
                str[i++] = (char)c;
                break;
            case 12:
            case 13:
                /* 110x xxxx   10xx xxxx*/
                count += 2;
                char2 = (int)bytearr[count - 1];
                str[i++] = (char)((c & 0x1F) << 6 | char2 & 0x3F);
                break;
            case 14:
                /* 1110 xxxx  10xx xxxx  10xx xxxx */
                count += 3;
                char2 = (int)bytearr[count - 2];
                char3 = (int)bytearr[count - 1];
                str[i++] = (char)((c & 0x0F) << 12 | (char2 & 0x3F) << 6 | (char3 & 0x3F) << 0);
                break;
            default:
                /* 10xx xxxx,  1111 xxxx */
                throw new UTFDataFormatException();
            }
        }
       
        if (i < len) {
            char[] newstr = new char[i];
            System.arraycopy(str, 0, newstr, 0, i );
            return newstr;
        }
       
        return str;
    }
   
   
    private void readBytes( byte[] b ) throws IOException {
        InputStream in = this.in;
        int n = 0;
        int len = b.length;
        while (n < len) {
            int count = in.read( b, n, len - n );
            if (count < 0) {
                throw new EOFException();
            }
            n += count;
        }
    }
   
   
    private int readLength() throws IOException {
        InputStream in = this.in;
        int ch1 = in.read();
        int ch2 = in.read();
        return (ch1 << 8) + (ch2 << 0);
    }
}
TOP

Related Classes of org.ozoneDB.xml.util.CompiledXMLInputStream

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.