You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2001/04/12 14:13:27 UTC
cvs commit: xml-cocoon/src/org/apache/cocoon/components/sax XMLByteStreamCompiler.java XMLByteStreamInterpreter.java CompiledXMLInputStream.java CompiledXMLOutputStream.java FastInputStream.java
cziegeler 01/04/12 05:13:27
Modified: src/org/apache/cocoon/components/sax Tag: xml-cocoon2
XMLByteStreamCompiler.java
XMLByteStreamInterpreter.java
Removed: src/org/apache/cocoon/components/sax Tag: xml-cocoon2
CompiledXMLInputStream.java
CompiledXMLOutputStream.java FastInputStream.java
Log:
Removed obsolete stream handling and improved performance
Revision Changes Path
No revision
No revision
1.1.2.3 +134 -116 xml-cocoon/src/org/apache/cocoon/components/sax/Attic/XMLByteStreamCompiler.java
Index: XMLByteStreamCompiler.java
===================================================================
RCS file: /home/cvs/xml-cocoon/src/org/apache/cocoon/components/sax/Attic/XMLByteStreamCompiler.java,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -u -r1.1.2.2 -r1.1.2.3
--- XMLByteStreamCompiler.java 2001/04/11 15:32:29 1.1.2.2
+++ XMLByteStreamCompiler.java 2001/04/12 12:13:26 1.1.2.3
@@ -7,7 +7,7 @@
*****************************************************************************/
package org.apache.cocoon.components.sax;
-import java.io.*;
+import java.util.HashMap;
import org.apache.avalon.Component;
import org.apache.avalon.Recyclable;
@@ -24,25 +24,41 @@
*
* @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
* @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
- * @version CVS $Revision: 1.1.2.2 $ $Date: 2001/04/11 15:32:29 $
+ * @version CVS $Revision: 1.1.2.3 $ $Date: 2001/04/12 12:13:26 $
*/
public final class XMLByteStreamCompiler
implements XMLSerializer, Component, Recyclable {
- private CompiledXMLOutputStream out;
+ private HashMap map;
+ private int count;
- public XMLByteStreamCompiler()
- throws IOException {
- this.out = new CompiledXMLOutputStream();
+ /** The buffer for the compile xml byte stream. */
+ private byte buf[];
+
+ /** The number of valid bytes in the buffer. */
+ private int bufCount;
+
+ public XMLByteStreamCompiler() {
+ this.map = new HashMap();
+ this.initOutput();
+ }
+
+ private void initOutput() {
+ this.count = 0;
+ this.map.clear();
+ this.buf = new byte[2000];
+ this.buf[0] = (byte)'C';
+ this.buf[1] = (byte)'X';
+ this.buf[2] = (byte)'M';
+ this.buf[3] = (byte)'L';
+ this.buf[4] = (byte)1;
+ this.buf[5] = (byte)0;
+ this.bufCount = 6;
}
public void recycle() {
- try {
- this.out = new CompiledXMLOutputStream();
- } catch (IOException ioe) {
- throw new RuntimeException("IOException occured:" + ioe);
- }
+ this.initOutput();
}
private static final int START_DOCUMENT = 0;
@@ -58,108 +74,73 @@
public Object getSAXFragment() {
- return this.out.toByteArray();
+ byte newbuf[] = new byte[this.bufCount];
+ System.arraycopy(this.buf, 0, newbuf, 0, this.bufCount);
+ return newbuf;
}
public void startDocument() throws SAXException {
- try {
- out.writeEvent(START_DOCUMENT);
- } catch (Exception e) {
- throw new SAXException(e);
- }
+ this.writeEvent(START_DOCUMENT);
}
public void endDocument() throws SAXException {
- try {
- out.writeEvent(END_DOCUMENT);
- } catch (Exception e) {
- throw new SAXException(e);
- }
-
+ this.writeEvent(END_DOCUMENT);
}
public void startPrefixMapping(java.lang.String prefix, java.lang.String uri)
throws SAXException {
- try {
- out.writeEvent(START_PREFIX_MAPPING);
- out.writeString(prefix);
- out.writeString(uri);
- } catch (Exception e) {
- throw new SAXException(e);
- }
+ this.writeEvent(START_PREFIX_MAPPING);
+ this.writeString(prefix);
+ this.writeString(uri);
}
public void endPrefixMapping(String prefix) throws SAXException {
- try {
- out.writeEvent(END_PREFIX_MAPPING);
- out.writeString(prefix);
- } catch (Exception e) {
- throw new SAXException(e);
- }
+ this.writeEvent(END_PREFIX_MAPPING);
+ this.writeString(prefix);
}
public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
throws SAXException {
- try {
- int length = atts.getLength();
- out.writeEvent(START_ELEMENT);
- out.writeAttributes(length);
- for (int i = 0; i < length; i++) {
- out.writeString(atts.getURI(i));
- out.writeString(atts.getLocalName(i));
- out.writeString(atts.getQName(i));
- out.writeString(atts.getType(i));
- out.writeString(atts.getValue(i));
- }
- out.writeString((namespaceURI == null ? "" : namespaceURI));
- out.writeString(localName);
- out.writeString(qName);
- } catch (Exception e) {
- throw new SAXException(e);
+ int length = atts.getLength();
+ this.writeEvent(START_ELEMENT);
+ this.writeAttributes(length);
+ for (int i = 0; i < length; i++) {
+ this.writeString(atts.getURI(i));
+ this.writeString(atts.getLocalName(i));
+ this.writeString(atts.getQName(i));
+ this.writeString(atts.getType(i));
+ this.writeString(atts.getValue(i));
}
+ this.writeString((namespaceURI == null ? "" : namespaceURI));
+ this.writeString(localName);
+ this.writeString(qName);
}
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
- try {
- out.writeEvent(END_ELEMENT);
- out.writeString((namespaceURI == null ? "" : namespaceURI));
- out.writeString(localName);
- out.writeString(qName);
- } catch (Exception e) {
- throw new SAXException(e);
- }
+ this.writeEvent(END_ELEMENT);
+ this.writeString((namespaceURI == null ? "" : namespaceURI));
+ this.writeString(localName);
+ this.writeString(qName);
}
public void characters(char[] ch, int start, int length)
throws SAXException {
- try {
- out.writeEvent(CHARACTERS);
- out.writeChars(ch, start, length);
- } catch (Exception e) {
- throw new SAXException(e);
- }
+ this.writeEvent(CHARACTERS);
+ this.writeChars(ch, start, length);
}
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException {
- try {
- out.writeEvent(IGNORABLE_WHITESPACE);
- out.writeChars(ch, start, length);
- } catch (Exception e) {
- throw new SAXException(e);
- }
+ this.writeEvent(IGNORABLE_WHITESPACE);
+ this.writeChars(ch, start, length);
}
public void processingInstruction(String target, String data)
throws SAXException {
- try {
- out.writeEvent(PROCESSING_INSTRUCTION);
- out.writeString(target);
- out.writeString(data);
- } catch (Exception e) {
- throw new SAXException(e);
- }
+ this.writeEvent(PROCESSING_INSTRUCTION);
+ this.writeString(target);
+ this.writeString(data);
}
public void setDocumentLocator(Locator locator) {
@@ -219,59 +200,96 @@
public void comment(char ary[], int start, int length)
throws SAXException {
try {
- out.writeEvent(COMMENT);
- out.writeChars(ary, start, length);
+ this.writeEvent(COMMENT);
+ this.writeChars(ary, start, length);
} catch (Exception e) {
throw new SAXException(e);
}
}
-}
-
-
-
-class ErrorHandler implements org.xml.sax.ErrorHandler {
-
- /** Warning. */
- public void warning(SAXParseException ex) {
- System.err.println("[Warning] "+
- getLocationString(ex)+": "+
- ex.getMessage());
+ public final void writeEvent(int event) throws SAXException {
+ this.write(event);
}
- /** Error. */
- public void error(SAXParseException ex) {
- System.err.println("[Error] "+
- getLocationString(ex)+": "+
- ex.getMessage());
+ public final void writeAttributes(int attributes) throws SAXException {
+ this.write((attributes >>> 8) & 0xFF);
+ this.write((attributes >>> 0) & 0xFF);
+ }
+
+ public final void writeString(String str) throws SAXException {
+ Integer index = (Integer) map.get(str);
+ if (index == null) {
+ int length = str.length();
+ map.put(str, new Integer(count++));
+ if (length > (2 << 15)) throw new SAXException("String cannot be bigger than 32K");
+ this.writeChars(str.toCharArray(), 0, length);
+ } else {
+ int i = index.intValue();
+ this.write(((i >>> 8) & 0xFF) | 0x80);
+ this.write((i >>> 0) & 0xFF);
+ }
}
- /** Fatal error. */
+ public final void writeChars(char[] ch, int start, int length) throws SAXException {
+ int utflen = 0;
+ int c, count = 0;
+
+ for (int i = 0; i < length; i++) {
+ c = ch[i + start];
+ if ((c >= 0x0001) && (c <= 0x007F)) {
+ utflen++;
+ } else if (c > 0x07FF) {
+ utflen += 3;
+ } else {
+ utflen += 2;
+ }
+ }
- public void fatalError(SAXParseException ex) throws SAXException {
- System.err.println("[Fatal Error] "+
+ if (utflen > 65535) throw new SAXException("UTFDataFormatException");
- getLocationString(ex)+": "+
+ byte[] bytearr = new byte[utflen+2];
+ bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF);
+ bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF);
+ for (int i = 0; i < length; i++) {
+ c = ch[i + start];
+ 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));
+ }
+ }
- ex.getMessage());
+ this.write(bytearr);
}
- /** Returns a string of the location. */
- private String getLocationString(SAXParseException ex) {
- StringBuffer str = new StringBuffer();
- String systemId = ex.getSystemId();
- if (systemId != null) {
- int index = systemId.lastIndexOf('/');
- if (index != -1)
- systemId = systemId.substring(index + 1);
- str.append(systemId);
+ private void write(byte[] b) {
+ int len = b.length;
+ if (len == 0) return;
+ int newcount = this.bufCount + len;
+ if (newcount > this.buf.length) {
+ byte newbuf[] = new byte[Math.max(this.buf.length << 1, newcount)];
+ System.arraycopy(this.buf, 0, newbuf, 0, this.bufCount);
+ this.buf = newbuf;
}
- str.append(':');
- str.append(ex.getLineNumber());
- str.append(':');
- str.append(ex.getColumnNumber());
+ System.arraycopy(b, 0, this.buf, this.bufCount, len);
+ this.bufCount = newcount;
+ }
- return str.toString();
+ private void write(int b) {
+ int newcount = this.bufCount + 1;
+ if (newcount > this.buf.length) {
+ byte newbuf[] = new byte[Math.max(this.buf.length << 1, newcount)];
+ System.arraycopy(this.buf, 0, newbuf, 0, this.bufCount);
+ this.buf = newbuf;
+ }
+ this.buf[this.bufCount] = (byte)b;
+ this.bufCount = newcount;
}
+
}
1.1.2.2 +138 -35 xml-cocoon/src/org/apache/cocoon/components/sax/Attic/XMLByteStreamInterpreter.java
Index: XMLByteStreamInterpreter.java
===================================================================
RCS file: /home/cvs/xml-cocoon/src/org/apache/cocoon/components/sax/Attic/XMLByteStreamInterpreter.java,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -u -r1.1.2.1 -r1.1.2.2
--- XMLByteStreamInterpreter.java 2001/04/11 10:52:49 1.1.2.1
+++ XMLByteStreamInterpreter.java 2001/04/12 12:13:26 1.1.2.2
@@ -9,26 +9,14 @@
*/
package org.apache.cocoon.components.sax;
-import java.io.ByteArrayInputStream;
-import java.io.FileInputStream;
-import java.io.BufferedInputStream;
-import java.io.InputStream;
-import java.io.IOException;
+import java.util.ArrayList;
-import java.util.HashMap;
-
import org.apache.avalon.Component;
-import org.apache.avalon.Poolable;
+import org.apache.avalon.Recyclable;
-import org.xml.sax.XMLReader;
import org.xml.sax.ContentHandler;
-import org.xml.sax.ErrorHandler;
-import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
-import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
-import org.xml.sax.SAXNotRecognizedException;
-import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.helpers.AttributesImpl;
import org.apache.cocoon.xml.AbstractXMLProducer;
@@ -38,12 +26,12 @@
*
* @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
* @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
- * @version CVS $Revision: 1.1.2.1 $ $Date: 2001/04/11 10:52:49 $
+ * @version CVS $Revision: 1.1.2.2 $ $Date: 2001/04/12 12:13:26 $
*/
public final class XMLByteStreamInterpreter
extends AbstractXMLProducer
-implements XMLDeserializer, Component, Poolable {
+implements XMLDeserializer, Component, Recyclable {
private static final int START_DOCUMENT = 0;
private static final int END_DOCUMENT = 1;
@@ -56,20 +44,29 @@
private static final int PROCESSING_INSTRUCTION = 8;
private static final int COMMENT = 9;
+ private ArrayList list = new ArrayList();
+ private byte[] input;
+ private int currentPos;
+
+ public void recycle() {
+ super.recycle();
+ this.list.clear();
+ }
+
public void deserialize(Object saxFragment)
throws SAXException {
- try {
- this.parse(new ByteArrayInputStream((byte[]) saxFragment));
- } catch (IOException ioe) {
- throw new SAXException("IOException: " + ioe);
+ if (saxFragment instanceof byte[] == false) {
+ throw new SAXException("XMLDeserializer needs byte array for deserialization.");
}
+ this.input = (byte[])saxFragment;
+ this.currentPos = 0;
+ this.checkProlog();
+ this.parse();
}
-
- private void parse(InputStream stream) throws IOException, SAXException {
- CompiledXMLInputStream input = new CompiledXMLInputStream(stream);
+ private void parse() throws SAXException {
while (true) {
- switch (input.readEvent()) {
+ switch (this.readEvent()) {
case START_DOCUMENT:
contentHandler.startDocument();
break;
@@ -77,39 +74,39 @@
contentHandler.endDocument();
return;
case START_PREFIX_MAPPING:
- contentHandler.startPrefixMapping(input.readString(), input.readString());
+ contentHandler.startPrefixMapping(this.readString(), this.readString());
break;
case END_PREFIX_MAPPING:
- contentHandler.endPrefixMapping(input.readString());
+ contentHandler.endPrefixMapping(this.readString());
break;
case START_ELEMENT:
- int attributes = input.readAttributes();
+ int attributes = this.readAttributes();
AttributesImpl atts = new AttributesImpl();
for (int i = 0; i < attributes; i++) {
- atts.addAttribute(input.readString(), input.readString(), input.readString(), input.readString(), input.readString());
+ atts.addAttribute(this.readString(), this.readString(), this.readString(), this.readString(), this.readString());
}
- contentHandler.startElement(input.readString(), input.readString(), input.readString(), atts);
+ contentHandler.startElement(this.readString(), this.readString(), this.readString(), atts);
break;
case END_ELEMENT:
- contentHandler.endElement(input.readString(), input.readString(), input.readString());
+ contentHandler.endElement(this.readString(), this.readString(), this.readString());
break;
case CHARACTERS:
- char[] chars = input.readChars();
+ char[] chars = this.readChars();
int len = chars.length;
while (chars[len-1]==0) len--;
contentHandler.characters(chars, 0, len);
break;
case IGNORABLE_WHITESPACE:
- char[] spaces = input.readChars();
+ char[] spaces = this.readChars();
len = spaces.length;
while (spaces[len-1]==0) len--;
contentHandler.characters(spaces, 0, len);
break;
case PROCESSING_INSTRUCTION:
- contentHandler.processingInstruction(input.readString(), input.readString());
+ contentHandler.processingInstruction(this.readString(), this.readString());
break;
case COMMENT:
- chars = input.readChars();
+ chars = this.readChars();
if (this.lexicalHandler != null) {
len = chars.length;
while (chars[len-1]==0) len--;
@@ -117,8 +114,114 @@
}
break;
default:
- throw new IOException ("parsing error: event not supported.");
+ throw new SAXException ("parsing error: event not supported.");
}
}
}
+
+ private void checkProlog() throws SAXException {
+ int valid = 0;
+ if (this.read() == 'C') valid++;
+ if (this.read() == 'X') valid++;
+ if (this.read() == 'M') valid++;
+ if (this.read() == 'L') valid++;
+ if (this.read() == 1) valid++;
+ if (this.read() == 0) valid++;
+ if (valid != 6) throw new SAXException("Unrecognized file format.");
+ }
+
+ private int readEvent() throws SAXException {
+ return this.read();
+ }
+
+ private int readAttributes() throws SAXException {
+ int ch1 = this.read();
+ int ch2 = this.read();
+ return ((ch1 << 8) + (ch2 << 0));
+ }
+
+ private String readString() throws SAXException {
+ 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);
+ return str;
+ }
+ }
+
+ /**
+ * The returned char array might contain any number of zero bytes
+ * at the end
+ */
+ private char[] readChars() throws SAXException {
+ return this.readChars(this.readLength());
+ }
+
+ private int read() throws SAXException {
+ if (currentPos >= input.length)
+ throw new SAXException("Reached end of input.");
+ return input[currentPos++] & 0xff;
+ }
+
+ /**
+ * The returned char array might contain any number of zero bytes
+ * at the end
+ */
+ private char[] readChars(int len) throws SAXException {
+ char[] str = new char[len];
+ byte[] bytearr = new byte[len];
+ int c, char2, 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 SAXException("UTFDataFormatException");
+ }
+ }
+
+ return str;
+ }
+
+ private void readBytes(byte[] b)
+ throws SAXException {
+ if (this.currentPos + b.length >= this.input.length) {
+ throw new SAXException("End of input reached.");
+ }
+ System.arraycopy(this.input, this.currentPos, b, 0, b.length);
+ this.currentPos += b.length;
+ }
+
+ private int readLength() throws SAXException {
+ int ch1 = this.read();
+ int ch2 = this.read();
+ return ((ch1 << 8) + (ch2 << 0));
+ }
+
}
----------------------------------------------------------------------
In case of troubles, e-mail: webmaster@xml.apache.org
To unsubscribe, e-mail: cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org