001: /*
002:
003: ============================================================================
004: The Apache Software License, Version 1.1
005: ============================================================================
006:
007: Copyright (C) 2000 The Apache Software Foundation. All rights reserved.
008:
009: Redistribution and use in source and binary forms, with or without modifica-
010: tion, are permitted provided that the following conditions are met:
011:
012: 1. Redistributions of source code must retain the above copyright notice,
013: this list of conditions and the following disclaimer.
014:
015: 2. Redistributions in binary form must reproduce the above copyright notice,
016: this list of conditions and the following disclaimer in the documentation
017: and/or other materials provided with the distribution.
018:
019: 3. The end-user documentation included with the redistribution, if any, must
020: include the following acknowledgment: "This product includes software
021: developed by the Apache Software Foundation (http://www.apache.org/)."
022: Alternately, this acknowledgment may appear in the software itself, if
023: and wherever such third-party acknowledgments normally appear.
024:
025: 4. The names "Cocoon" and "Apache Software Foundation" must not be used to
026: endorse or promote products derived from this software without prior
027: written permission. For written permission, please contact
028: apache@apache.org.
029:
030: 5. Products derived from this software may not be called "Apache", nor may
031: "Apache" appear in their name, without prior written permission of the
032: Apache Software Foundation.
033:
034: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
035: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
036: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
037: APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
038: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
039: DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
040: OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
041: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
042: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
043: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: This software consists of voluntary contributions made by many individuals
046: on behalf of the Apache Software Foundation and was originally created by
047: Stefano Mazzocchi <stefano@apache.org>. For more information on the Apache
048: Software Foundation, please see <http://www.apache.org/>.
049:
050: */
051:
052: package org.ozoneDB.xml.util;
053:
054: import java.io.InputStream;
055: import java.io.IOException;
056: import java.io.EOFException;
057: import java.io.UTFDataFormatException;
058: import java.io.Serializable;
059:
060: import java.util.ArrayList;
061:
062: /**
063: * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>, modifications
064: * for Ozone by <a href="mailto:conny@smb-tec.com">Conny Krappatsch</a>
065: */
066:
067: final class CompiledXMLInputStream extends InputStream implements
068: Serializable {
069:
070: private final InputStream in;
071: private final ArrayList list = new ArrayList();
072:
073: public CompiledXMLInputStream(InputStream input) throws IOException {
074: this .in = input;
075: }
076:
077: public final int readEvent() throws IOException {
078: return this .in.read();
079: }
080:
081: public final int readAttributes() throws IOException {
082: InputStream in = this .in;
083: int ch1 = in.read();
084: int ch2 = in.read();
085: return (ch1 << 8) + (ch2 << 0);
086: }
087:
088: public final String readString() throws IOException {
089: int length = this .readLength();
090: int index = length & 0x00007FFF;
091: if (length >= 0x00008000) {
092: return (String) list.get(index);
093: } else {
094: String str = new String(this .readChars(index));
095: list.add(str);
096: // System.out.println( "index: " + list.size() + ", str=" + str);
097: return str;
098: }
099: }
100:
101: public final char[] readChars() throws IOException {
102: return readChars(this .readLength());
103: }
104:
105: public final int read() throws IOException {
106: return this .in.read();
107: }
108:
109: private char[] readChars(int len) throws IOException {
110: char[] str = new char[len];
111: byte[] bytearr = new byte[len];
112: int c;
113: int char2;
114: int char3;
115: int count = 0;
116: int i = 0;
117:
118: this .readBytes(bytearr);
119:
120: while (count < len) {
121: c = (int) bytearr[count] & 0xff;
122: switch (c >> 4) {
123: case 0:
124: case 1:
125: case 2:
126: case 3:
127: case 4:
128: case 5:
129: case 6:
130: case 7:
131: /* 0xxxxxxx*/
132: count++;
133: str[i++] = (char) c;
134: break;
135: case 12:
136: case 13:
137: /* 110x xxxx 10xx xxxx*/
138: count += 2;
139: char2 = (int) bytearr[count - 1];
140: str[i++] = (char) ((c & 0x1F) << 6 | char2 & 0x3F);
141: break;
142: case 14:
143: /* 1110 xxxx 10xx xxxx 10xx xxxx */
144: count += 3;
145: char2 = (int) bytearr[count - 2];
146: char3 = (int) bytearr[count - 1];
147: str[i++] = (char) ((c & 0x0F) << 12
148: | (char2 & 0x3F) << 6 | (char3 & 0x3F) << 0);
149: break;
150: default:
151: /* 10xx xxxx, 1111 xxxx */
152: throw new UTFDataFormatException();
153: }
154: }
155:
156: if (i < len) {
157: char[] newstr = new char[i];
158: System.arraycopy(str, 0, newstr, 0, i);
159: return newstr;
160: }
161:
162: return str;
163: }
164:
165: private void readBytes(byte[] b) throws IOException {
166: InputStream in = this .in;
167: int n = 0;
168: int len = b.length;
169: while (n < len) {
170: int count = in.read(b, n, len - n);
171: if (count < 0) {
172: throw new EOFException();
173: }
174: n += count;
175: }
176: }
177:
178: private int readLength() throws IOException {
179: InputStream in = this .in;
180: int ch1 = in.read();
181: int ch2 = in.read();
182: return (ch1 << 8) + (ch2 << 0);
183: }
184: }
|