Package org.ozoneDB.xml.util

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

/*

============================================================================
                   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.OutputStream;
import java.io.IOException;
import java.io.UTFDataFormatException;
import java.io.Serializable;

import java.util.HashMap;

/**
@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 CompiledXMLOutputStream extends OutputStream implements Serializable {
   
    private static final boolean COMPRESS = true;
   
    private final OutputStream out;
    private final HashMap map = new HashMap();
    private int indexCount = 0;
   
   
    public CompiledXMLOutputStream( OutputStream out ) throws IOException{
        this.out = out;
    }
   
   
    public final void writeEvent( int event ) throws IOException {
        this.out.write( event );
    }
   
   
    public final void writeAttributes( int attributes ) throws IOException {
        OutputStream out = this.out;
        out.write( attributes >>> 8 & 0xFF );
        out.write( attributes & 0xFF );
//        out.write( attributes >>> 0 & 0xFF );
    }
   
   
    public final void writeString( String str ) throws IOException {
        Integer index;
       
        if (CompiledXMLOutputStream.COMPRESS) {
            index = (Integer)map.get( str );
        } else {
            index = null;
        }
       
        if (index == null) {
            int length = str.length();
            if (CompiledXMLOutputStream.COMPRESS) {
                map.put(str, new Integer(indexCount++));
               // System.out.println( "index=" + indexCount + ", str=" + str);
            }
            if (length > 2 << 15) {
                throw new IOException( "String cannot be bigger than 32K" );
            }
            this.writeChars( str.toCharArray(), 0, length );
        } else {
            int i = index.intValue();
            this.out.write( i >>> 8 & 0xFF | 0x80 );
            this.out.write( i & 0xFF );
        }
    }
   
   
    public final void writeChars( char[] ch, int start, int length ) throws IOException {
        int utflen = 0;
        int c;
        int count = 0;
        int end = start + length;
       
        /* looks strange but is fast */
        for (int i = start; i < end; i++) {
            c = ch[i];
            utflen += (c >= 0x0001 && c <= 0x007F) ? 1 : (c > 0x07FF) ? 3 : 2;
        }
       
        if (utflen > 65535) {
            throw new UTFDataFormatException();
        }
       
        byte[] bytearr = new byte[utflen + 2];
        bytearr[count++] = (byte)(utflen >>> 8 & 0xFF);
        bytearr[count++] = (byte)(utflen >>> 0 & 0xFF);
        for (int i = start; i < end; i++) {
            c = ch[i];
            if (c >= 0x0001 && c <= 0x007F) {
                bytearr[count++] = (byte)c;
            } else if (c > 0x07FF) {
                bytearr[count++] = (byte)(0xE0 | c >> 12 & 0x0F);
                bytearr[count++] = (byte)(0x80 | c >> 6 & 0x3F);
                bytearr[count++] = (byte)(0x80 | c >> 0 & 0x3F);
            } else {
                bytearr[count++] = (byte)(0xC0 | c >> 6 & 0x1F);
                bytearr[count++] = (byte)(0x80 | c >> 0 & 0x3F);
            }
        }
       
        this.out.write(bytearr, 0, count);
    }
   
   
    public final void write(int c) throws IOException {
        this.out.write(c);
    }
}
TOP

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

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.