KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > util > CompiledXMLInputStream


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6
7  Copyright (C) 2000 The Apache Software Foundation. All rights reserved.
8
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24
25  4. The names "Cocoon" and "Apache Software Foundation" must not be used to
26     endorse or promote products derived from this software without prior
27     written permission. For written permission, please contact
28     apache@apache.org.
29
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation and was originally created by
47  Stefano Mazzocchi <stefano@apache.org>. For more information on the Apache
48  Software Foundation, please see <http://www.apache.org/>.
49
50  */

51
52 package org.ozoneDB.xml.util;
53
54 import java.io.InputStream JavaDoc;
55 import java.io.IOException JavaDoc;
56 import java.io.EOFException JavaDoc;
57 import java.io.UTFDataFormatException JavaDoc;
58 import java.io.Serializable JavaDoc;
59
60 import java.util.ArrayList JavaDoc;
61
62 /**
63  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>, modifications
64  * for Ozone by <a HREF="mailto:conny@smb-tec.com">Conny Krappatsch</a>
65  */

66
67
68 final class CompiledXMLInputStream extends InputStream JavaDoc implements Serializable JavaDoc {
69     
70     private final InputStream JavaDoc in;
71     private final ArrayList JavaDoc list = new ArrayList JavaDoc();
72     
73     
74     public CompiledXMLInputStream( InputStream JavaDoc input ) throws IOException JavaDoc{
75         this.in = input;
76     }
77     
78     
79     public final int readEvent() throws IOException JavaDoc {
80         return this.in.read();
81     }
82     
83     
84     public final int readAttributes() throws IOException JavaDoc {
85         InputStream JavaDoc in = this.in;
86         int ch1 = in.read();
87         int ch2 = in.read();
88         return (ch1 << 8) + (ch2 << 0);
89     }
90     
91     
92     public final String JavaDoc readString() throws IOException JavaDoc {
93         int length = this.readLength();
94         int index = length & 0x00007FFF;
95         if (length >= 0x00008000) {
96             return (String JavaDoc)list.get( index );
97         } else {
98             String JavaDoc str = new String JavaDoc( this.readChars( index ) );
99             list.add( str );
100            // System.out.println( "index: " + list.size() + ", str=" + str);
101
return str;
102         }
103     }
104     
105     
106     public final char[] readChars() throws IOException JavaDoc {
107         return readChars( this.readLength() );
108     }
109     
110     
111     public final int read() throws IOException JavaDoc {
112         return this.in.read();
113     }
114     
115     
116     private char[] readChars( int len ) throws IOException JavaDoc {
117         char[] str = new char[len];
118         byte[] bytearr = new byte[len];
119         int c;
120         int char2;
121         int char3;
122         int count = 0;
123         int i = 0;
124         
125         this.readBytes( bytearr );
126         
127         while (count < len) {
128             c = (int)bytearr[count] & 0xff;
129             switch (c >> 4) {
130             case 0:
131             case 1:
132             case 2:
133             case 3:
134             case 4:
135             case 5:
136             case 6:
137             case 7:
138                 /* 0xxxxxxx*/
139                 count++;
140                 str[i++] = (char)c;
141                 break;
142             case 12:
143             case 13:
144                 /* 110x xxxx 10xx xxxx*/
145                 count += 2;
146                 char2 = (int)bytearr[count - 1];
147                 str[i++] = (char)((c & 0x1F) << 6 | char2 & 0x3F);
148                 break;
149             case 14:
150                 /* 1110 xxxx 10xx xxxx 10xx xxxx */
151                 count += 3;
152                 char2 = (int)bytearr[count - 2];
153                 char3 = (int)bytearr[count - 1];
154                 str[i++] = (char)((c & 0x0F) << 12 | (char2 & 0x3F) << 6 | (char3 & 0x3F) << 0);
155                 break;
156             default:
157                 /* 10xx xxxx, 1111 xxxx */
158                 throw new UTFDataFormatException JavaDoc();
159             }
160         }
161         
162         if (i < len) {
163             char[] newstr = new char[i];
164             System.arraycopy(str, 0, newstr, 0, i );
165             return newstr;
166         }
167         
168         return str;
169     }
170     
171     
172     private void readBytes( byte[] b ) throws IOException JavaDoc {
173         InputStream JavaDoc in = this.in;
174         int n = 0;
175         int len = b.length;
176         while (n < len) {
177             int count = in.read( b, n, len - n );
178             if (count < 0) {
179                 throw new EOFException JavaDoc();
180             }
181             n += count;
182         }
183     }
184     
185     
186     private int readLength() throws IOException JavaDoc {
187         InputStream JavaDoc in = this.in;
188         int ch1 = in.read();
189         int ch2 = in.read();
190         return (ch1 << 8) + (ch2 << 0);
191     }
192 }
193
Popular Tags