cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From giac...@apache.org
Subject svn commit: r439271 - in /cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax: AbstractXMLByteStreamCompiler.java AbstractXMLByteStreamInterpreter.java XMLByteStreamCompiler.java XMLByteStreamInterpreter.java
Date Fri, 01 Sep 2006 11:25:41 GMT
Author: giacomo
Date: Fri Sep  1 04:25:40 2006
New Revision: 439271

URL: http://svn.apache.org/viewvc?rev=439271&view=rev
Log:
factor out common methods to give room for other implementations

Added:
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/AbstractXMLByteStreamCompiler.java   (with props)
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/AbstractXMLByteStreamInterpreter.java   (with props)
Modified:
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/XMLByteStreamCompiler.java
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/XMLByteStreamInterpreter.java

Added: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/AbstractXMLByteStreamCompiler.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/AbstractXMLByteStreamCompiler.java?rev=439271&view=auto
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/AbstractXMLByteStreamCompiler.java (added)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/AbstractXMLByteStreamCompiler.java Fri Sep  1 04:25:40 2006
@@ -0,0 +1,322 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.components.sax;
+
+import java.util.HashMap;
+
+import org.apache.cocoon.xml.XMLConsumer;
+import org.xml.sax.Attributes;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+
+/**
+ * This a simple xml compiler which outputs a byte array.
+ * If you want to reuse this instance, make sure to call {@link #recycle()}
+ * inbetween two compilation tasks.
+ *
+ * @version $Id: XMLByteStreamCompiler.java 439255 2006-09-01 10:00:00Z giacomo $
+ */
+public abstract class AbstractXMLByteStreamCompiler implements XMLConsumer, XMLByteStreamConstants {
+
+    private HashMap map;
+    private int mapCount;
+
+    protected AbstractXMLByteStreamCompiler() {
+        this.map = new HashMap();
+        this.initOutput();
+    }
+
+    protected void initOutput() {
+        this.mapCount = 0;
+        this.map.clear();
+    }
+
+    public void recycle() {
+        this.initOutput();
+    }
+
+    public void startDocument() throws SAXException {
+        this.writeEvent(START_DOCUMENT);
+    }
+
+    public void endDocument() throws SAXException {
+        this.writeEvent(END_DOCUMENT);
+    }
+
+    public void startPrefixMapping(java.lang.String prefix, java.lang.String uri) throws SAXException {
+        this.writeEvent(START_PREFIX_MAPPING);
+        this.writeString(prefix);
+        this.writeString(uri);
+    }
+
+    public void endPrefixMapping(String prefix) throws SAXException {
+       this.writeEvent(END_PREFIX_MAPPING);
+       this.writeString(prefix);
+    }
+
+    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
+        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 {
+        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 {
+        this.writeEvent(CHARACTERS);
+        this.writeChars(ch, start, length);
+    }
+
+    public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
+        this.writeEvent(IGNORABLE_WHITESPACE);
+        this.writeChars(ch, start, length);
+    }
+
+    public void processingInstruction(String target, String data) throws SAXException {
+        this.writeEvent(PROCESSING_INSTRUCTION);
+        this.writeString(target);
+        this.writeString(data);
+    }
+
+    public void setDocumentLocator(Locator locator) {
+        try {
+            this.writeEvent(LOCATOR);
+            String publicId = locator.getPublicId();
+            String systemId = locator.getSystemId();
+            this.writeString(publicId!=null?publicId:"");
+            this.writeString(systemId!=null?systemId:"");
+            this.write(locator.getLineNumber());
+            this.write(locator.getColumnNumber());
+        } catch (Exception e) {
+             throw new DocumentLocatorException("Error while handling locator", e);
+        }
+    }
+
+    public void skippedEntity(java.lang.String name) throws SAXException {
+        this.writeEvent(SKIPPED_ENTITY);
+        this.writeString(name);
+    }
+
+    /**
+     * SAX Event Handling: LexicalHandler
+     */
+    public void startDTD(String name, String publicId, String systemId) throws SAXException {
+        this.writeEvent(START_DTD);
+        this.writeString(name);
+        this.writeString(publicId!=null?publicId:"");
+        this.writeString(systemId!=null?systemId:"");
+    }
+
+    /**
+     * SAX Event Handling: LexicalHandler
+     */
+    public void endDTD() throws SAXException {
+        this.writeEvent(END_DTD);
+    }
+
+    /**
+     * SAX Event Handling: LexicalHandler
+     */
+    public void startEntity(String name) throws SAXException {
+        this.writeEvent(START_ENTITY);
+        this.writeString(name);
+    }
+
+    /**
+     * SAX Event Handling: LexicalHandler
+     */
+    public void endEntity(String name) throws SAXException {
+        this.writeEvent(END_ENTITY);
+        this.writeString(name);
+    }
+
+    /**
+     * SAX Event Handling: LexicalHandler
+     */
+    public void startCDATA() throws SAXException {
+        this.writeEvent(START_CDATA);
+    }
+
+    /**
+     * SAX Event Handling: LexicalHandler
+     */
+    public void endCDATA() throws SAXException {
+        this.writeEvent(END_CDATA);
+    }
+
+
+    /**
+     * SAX Event Handling: LexicalHandler
+     */
+    public void comment(char ary[], int start, int length) throws SAXException {
+        try {
+            this.writeEvent(COMMENT);
+            this.writeChars(ary, start, length);
+        } catch (Exception e) {
+            throw new SAXException(e);
+        }
+    }
+
+    public final void writeEvent( final int event) {
+        this.write(event);
+    }
+
+    public final void writeAttributes( final int attributes) throws SAXException {
+        if (attributes > 0xFFFF) throw new SAXException("Too many attributes");
+        this.write((attributes >>> 8) & 0xFF);
+        this.write((attributes >>> 0) & 0xFF);
+    }
+
+    public final void writeString( final String str) throws SAXException {
+        Integer index = (Integer) map.get(str);
+        if (index == null) {
+            map.put(str, new Integer(mapCount++));
+            int length = str.length();
+            this.writeChars(str.toCharArray(), 0, length);
+        }
+        else {
+            int i = index.intValue();
+
+            if (i > 0xFFFF) throw new SAXException("Index too large");
+
+            this.write(((i >>> 8) & 0xFF) | 0x80);
+            this.write((i >>> 0) & 0xFF);
+        }
+    }
+
+    public final void writeChars( final char[] ch, final int start, final int length) {
+        int utflen = 0;
+        int c;
+
+        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;
+            }
+        }
+
+        if (utflen >= 0x00007FFF) {
+            write((byte)0x7F);
+            write((byte)0xFF);
+            write((byte) ((utflen >>> 24) & 0xFF));
+            write((byte) ((utflen >>> 16) & 0xFF));
+            write((byte) ((utflen >>>  8) & 0xFF));
+            write((byte) ((utflen >>>  0) & 0xFF));
+        }
+        else {
+            write((byte) ((utflen >>> 8) & 0xFF));
+            write((byte) ((utflen >>> 0) & 0xFF));
+        }
+
+        for (int i = 0; i < length; i++) {
+            c = ch[i + start];
+            if ((c >= 0x0001) && (c <= 0x007F)) {
+                write((byte) c);
+            }
+            else if (c > 0x07FF) {
+                write((byte) (0xE0 | ((c >> 12) & 0x0F)));
+                write((byte) (0x80 | ((c >>  6) & 0x3F)));
+                write((byte) (0x80 | ((c >>  0) & 0x3F)));
+            }
+            else {
+                write((byte) (0xC0 | ((c >>  6) & 0x1F)));
+                write((byte) (0x80 | ((c >>  0) & 0x3F)));
+            }
+        }
+
+
+/*
+        if (length == 0) return;
+
+        assure( (int) (buf.length + length * utfRatioAverage) );
+
+        int utflentotal = 0;
+
+        bufCount += 2;
+        int bufStart = bufCount;
+
+        for (int i = 0; i < length; i++) {
+            int c = ch[i + start];
+            int l = bufCount-bufStart;
+
+            if (l+3 >= 0x7FFF) {
+                buf[bufStart-2] = (byte) ((l >>> 8) & 0xFF);
+                buf[bufStart-1] = (byte) ((l >>> 0) & 0xFF);
+                utflentotal += l;
+                bufCount += 2;
+                bufStart = bufCount;
+            }
+
+            if ((c >= 0x0001) && (c <= 0x007F)) {
+                assure(bufCount+1);
+                buf[bufCount++] = (byte)c;
+            }
+            else if (c > 0x07FF) {
+                assure(bufCount+3);
+                buf[bufCount++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
+                buf[bufCount++] = (byte) (0x80 | ((c >>  6) & 0x3F));
+                buf[bufCount++] = (byte) (0x80 | ((c >>  0) & 0x3F));
+            }
+            else {
+                assure(bufCount+2);
+                buf[bufCount++] = (byte) (0xC0 | ((c >>  6) & 0x1F));
+                buf[bufCount++] = (byte) (0x80 | ((c >>  0) & 0x3F));
+            }
+        }
+
+        int l = bufCount-bufStart;
+        buf[bufStart-2] = (byte) ((l >>> 8) & 0xFF);
+        buf[bufStart-1] = (byte) ((l >>> 0) & 0xFF);
+        utflentotal += l;
+
+        utfRatioAverage = (utfRatioAverage + (utflentotal / length) / 2);
+*/
+    }
+
+/*  JH (2003-11-20): seems to be never used
+
+    private void write( final byte[] b ) {
+        int newcount = this.bufCount + b.length;
+        assure(newcount);
+        System.arraycopy(b, 0, this.buf, this.bufCount, b.length);
+        this.bufCount = newcount;
+    }
+*/
+
+    abstract protected void write( final int b );
+}
+

Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/AbstractXMLByteStreamCompiler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/AbstractXMLByteStreamInterpreter.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/AbstractXMLByteStreamInterpreter.java?rev=439271&view=auto
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/AbstractXMLByteStreamInterpreter.java (added)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/AbstractXMLByteStreamInterpreter.java Fri Sep  1 04:25:40 2006
@@ -0,0 +1,315 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.components.sax;
+
+import java.io.IOException;
+import java.util.ArrayList;
+
+import org.apache.cocoon.xml.DefaultLexicalHandler;
+import org.apache.cocoon.xml.XMLConsumer;
+import org.apache.cocoon.xml.XMLProducer;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.ext.LexicalHandler;
+import org.xml.sax.helpers.AttributesImpl;
+import org.xml.sax.helpers.DefaultHandler;
+
+/**
+ * This a simple xml compiler which takes a byte array as input.
+ * If you want to reuse this interpreter make sure to call first {@link #recycle()}
+ * and then set the new consumer for the sax events.
+ *
+ * @version $Id: XMLByteStreamInterpreter.java 439255 2006-09-01 10:00:00Z giacomo $
+ */
+public abstract class AbstractXMLByteStreamInterpreter implements XMLProducer, XMLByteStreamConstants {
+
+    private ArrayList list = new ArrayList();
+
+    protected static final ContentHandler EMPTY_CONTENT_HANDLER = new DefaultHandler();
+
+    /** The <code>ContentHandler</code> receiving SAX events. */
+    protected ContentHandler contentHandler = EMPTY_CONTENT_HANDLER;
+
+    /** The <code>LexicalHandler</code> receiving SAX events. */
+    protected LexicalHandler lexicalHandler = DefaultLexicalHandler.NULL_HANDLER;
+
+    /**
+     * Set the <code>XMLConsumer</code> that will receive XML data.
+     * <br>
+     * This method will simply call <code>setContentHandler(consumer)</code>
+     * and <code>setLexicalHandler(consumer)</code>.
+     */
+    public void setConsumer(XMLConsumer consumer) {
+        setContentHandler(consumer);
+        setLexicalHandler(consumer);
+    }
+
+    /**
+     * Set the <code>ContentHandler</code> that will receive XML data.
+     * <br>
+     * Subclasses may retrieve this <code>ContentHandler</code> instance
+     * accessing the protected <code>super.contentHandler</code> field.
+     */
+    public void setContentHandler(ContentHandler handler) {
+        this.contentHandler = handler;
+    }
+
+    /**
+     * Set the <code>LexicalHandler</code> that will receive XML data.
+     * <br>
+     * Subclasses may retrieve this <code>LexicalHandler</code> instance
+     * accessing the protected <code>super.lexicalHandler</code> field.
+     */
+    public void setLexicalHandler(LexicalHandler handler) {
+        this.lexicalHandler = handler;
+    }
+
+    public void recycle() {
+        this.contentHandler = EMPTY_CONTENT_HANDLER;
+        this.lexicalHandler = DefaultLexicalHandler.NULL_HANDLER;
+        this.list.clear();
+    }
+
+    /**
+     * This method needs to be used by sub classes to start the parsing of the byte stream
+     * 
+     * @throws SAXException
+     */
+    protected void parse() throws SAXException {
+        this.list.clear();
+        this.checkProlog();
+        int event = -1;
+        int lastEvent = -1;
+        while ( ( event = readEvent() ) != -1 ) {
+            lastEvent = event;
+            switch (this.readEvent()) {
+                case START_DOCUMENT:
+                    contentHandler.startDocument();
+                    break;
+                case END_DOCUMENT:
+                    contentHandler.endDocument();
+                    break;
+                case START_PREFIX_MAPPING:
+                    contentHandler.startPrefixMapping(this.readString(), this.readString());
+                    break;
+                case END_PREFIX_MAPPING:
+                    contentHandler.endPrefixMapping(this.readString());
+                    break;
+                case START_ELEMENT:
+                    int attributes = this.readAttributes();
+                    AttributesImpl atts = new AttributesImpl();
+                    for (int i = 0; i < attributes; i++) {
+                        atts.addAttribute(this.readString(), this.readString(), this.readString(), this.readString(), this.readString());
+                    }
+                    contentHandler.startElement(this.readString(), this.readString(), this.readString(), atts);
+                    break;
+                case END_ELEMENT:
+                    contentHandler.endElement(this.readString(), this.readString(), this.readString());
+                    break;
+                case CHARACTERS:
+                    char[] chars = this.readChars();
+                    int len = chars.length;
+                    while (len > 0 && chars[len-1]==0) len--;
+                    if (len > 0) contentHandler.characters(chars, 0, len);
+                    break;
+                case IGNORABLE_WHITESPACE:
+                    char[] spaces = this.readChars();
+                    len = spaces.length;
+                    while (len > 0 && spaces[len-1]==0) len--;
+                    if (len > 0) contentHandler.characters(spaces, 0, len);
+                    break;
+                case PROCESSING_INSTRUCTION:
+                    contentHandler.processingInstruction(this.readString(), this.readString());
+                    break;
+                case COMMENT:
+                    chars = this.readChars();
+                    len = chars.length;
+                    while (len > 0 && chars[len-1]==0) len--;
+                    if (len > 0) lexicalHandler.comment(chars, 0, len);
+                    break;
+                case LOCATOR:
+                    {
+                    String publicId = this.readString();
+                    String systemId = this.readString();
+                    int lineNumber = this.read();
+                    int columnNumber = this.read();
+                    org.xml.sax.helpers.LocatorImpl locator = new org.xml.sax.helpers.LocatorImpl();
+                    locator.setPublicId(publicId);
+                    locator.setSystemId(systemId);
+                    locator.setLineNumber(lineNumber);
+                    locator.setColumnNumber(columnNumber);
+                    contentHandler.setDocumentLocator(locator);
+                    }
+                    break;
+                case START_DTD:
+                    lexicalHandler.startDTD(this.readString(), 
+                                            this.readString(), 
+                                            this.readString());
+                    break;
+                case END_DTD:
+                    lexicalHandler.endDTD();
+                    break;
+                case START_CDATA:
+                    lexicalHandler.startCDATA();
+                    break;
+                case END_CDATA:
+                    lexicalHandler.endCDATA();
+                    break;
+                case SKIPPED_ENTITY:
+                    contentHandler.skippedEntity( this.readString() );
+                    break;
+                case START_ENTITY:
+                    lexicalHandler.startEntity( this.readString() );
+                    break;
+                case END_ENTITY:
+                    lexicalHandler.endEntity( this.readString() );
+                    break;
+                default:
+                    throw new SAXException ("parsing error: event not supported: " + event);
+            }
+        }
+        if( lastEvent != END_DOCUMENT )
+        {
+            throw new SAXException ("parsing error: premature end of stream (lastEvent was " + lastEvent + ")." );
+        }
+    }
+
+    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.");
+    }
+
+    protected 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.readWord();
+        int index = length & 0x00007FFF;
+        if (length >= 0x00008000) {
+            return (String) list.get(index);
+        }
+        else {
+            if (length == 0x00007FFF) {
+                length = this.readLong();
+            }
+            char[] chars = this.readChars(length);
+            int len = chars.length;
+            if (len > 0) {
+                while (chars[len-1]==0) len--;
+            }
+            String str;
+            if (len == 0) {
+                str = "";
+            } else {
+                str = new String(chars, 0, len);
+            }
+            list.add(str);
+            return str;
+        }
+    }
+
+    /**
+     * The returned char array might contain any number of zero bytes
+     * at the end
+     */
+    private char[] readChars() throws SAXException {
+        int length = this.readWord();
+        if (length == 0x00007FFF) {
+            length = this.readLong();
+        }
+        return this.readChars(length);
+    }
+
+    protected abstract int read() throws SAXException;
+    protected abstract int read(byte[] b) throws SAXException;
+
+    /**
+     * 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 = 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 = bytearr[count-1];
+                    str[i++] = (char) (((c & 0x1F) << 6) | (char2 & 0x3F));
+                    break;
+                case 14:
+                    // 1110 xxxx  10xx xxxx  10xx xxxx
+                    count += 3;
+                    char2 = bytearr[count-2];
+                    char3 = 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 {
+        final int bytesRead = this.read( b );
+        if (bytesRead < b.length ) {
+            throw new SAXException("End of is reached.");
+        }
+    }
+
+    private int readWord() throws SAXException {
+        int ch1 = this.read();
+        int ch2 = this.read();
+        return ((ch1 << 8) + (ch2 << 0));
+    }
+
+    private int readLong() throws SAXException {
+        int ch1 = this.read();
+        int ch2 = this.read();
+        int ch3 = this.read();
+        int ch4 = this.read();
+        return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
+    }
+}

Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/AbstractXMLByteStreamInterpreter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/XMLByteStreamCompiler.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/XMLByteStreamCompiler.java?rev=439271&r1=439270&r2=439271&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/XMLByteStreamCompiler.java (original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/XMLByteStreamCompiler.java Fri Sep  1 04:25:40 2006
@@ -15,12 +15,6 @@
  */
 package org.apache.cocoon.components.sax;
 
-import java.util.HashMap;
-
-import org.apache.cocoon.xml.XMLConsumer;
-import org.xml.sax.Attributes;
-import org.xml.sax.Locator;
-import org.xml.sax.SAXException;
 
 /**
  * This a simple xml compiler which outputs a byte array.
@@ -29,10 +23,7 @@
  *
  * @version $Id$
  */
-public final class XMLByteStreamCompiler implements XMLConsumer, XMLByteStreamConstants {
-
-    private HashMap map;
-    private int mapCount;
+public final class XMLByteStreamCompiler extends AbstractXMLByteStreamCompiler {
 
     /** The buffer for the compile xml byte stream. */
     private byte buf[];
@@ -44,14 +35,13 @@
 
 
     public XMLByteStreamCompiler() {
-        this.map = new HashMap();
+        super();
         this.bufCountAverage = 2000;
         this.initOutput();
     }
 
-    private void initOutput() {
-        this.mapCount = 0;
-        this.map.clear();
+    protected void initOutput() {
+        super.initOutput();
         this.buf = new byte[bufCountAverage];
         this.buf[0] = (byte)'C';
         this.buf[1] = (byte)'X';
@@ -64,7 +54,7 @@
 
     public void recycle() {
         bufCountAverage = (bufCountAverage + bufCount) / 2;
-        this.initOutput();
+        super.recycle();
     }
 
 
@@ -77,281 +67,7 @@
         return newbuf;
     }
 
-    public void startDocument() throws SAXException {
-        this.writeEvent(START_DOCUMENT);
-    }
-
-    public void endDocument() throws SAXException {
-        this.writeEvent(END_DOCUMENT);
-    }
-
-    public void startPrefixMapping(java.lang.String prefix, java.lang.String uri) throws SAXException {
-        this.writeEvent(START_PREFIX_MAPPING);
-        this.writeString(prefix);
-        this.writeString(uri);
-    }
-
-    public void endPrefixMapping(String prefix) throws SAXException {
-       this.writeEvent(END_PREFIX_MAPPING);
-       this.writeString(prefix);
-    }
-
-    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
-        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 {
-        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 {
-        this.writeEvent(CHARACTERS);
-        this.writeChars(ch, start, length);
-    }
-
-    public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
-        this.writeEvent(IGNORABLE_WHITESPACE);
-        this.writeChars(ch, start, length);
-    }
-
-    public void processingInstruction(String target, String data) throws SAXException {
-        this.writeEvent(PROCESSING_INSTRUCTION);
-        this.writeString(target);
-        this.writeString(data);
-    }
-
-    public void setDocumentLocator(Locator locator) {
-        try {
-            this.writeEvent(LOCATOR);
-            String publicId = locator.getPublicId();
-            String systemId = locator.getSystemId();
-            this.writeString(publicId!=null?publicId:"");
-            this.writeString(systemId!=null?systemId:"");
-            this.write(locator.getLineNumber());
-            this.write(locator.getColumnNumber());
-        } catch (Exception e) {
-             throw new DocumentLocatorException("Error while handling locator", e);
-        }
-    }
-
-    public void skippedEntity(java.lang.String name) throws SAXException {
-        this.writeEvent(SKIPPED_ENTITY);
-        this.writeString(name);
-    }
-
-    /**
-     * SAX Event Handling: LexicalHandler
-     */
-    public void startDTD(String name, String publicId, String systemId) throws SAXException {
-        this.writeEvent(START_DTD);
-        this.writeString(name);
-        this.writeString(publicId!=null?publicId:"");
-        this.writeString(systemId!=null?systemId:"");
-    }
-
-    /**
-     * SAX Event Handling: LexicalHandler
-     */
-    public void endDTD() throws SAXException {
-        this.writeEvent(END_DTD);
-    }
-
-    /**
-     * SAX Event Handling: LexicalHandler
-     */
-    public void startEntity(String name) throws SAXException {
-        this.writeEvent(START_ENTITY);
-        this.writeString(name);
-    }
-
-    /**
-     * SAX Event Handling: LexicalHandler
-     */
-    public void endEntity(String name) throws SAXException {
-        this.writeEvent(END_ENTITY);
-        this.writeString(name);
-    }
-
-    /**
-     * SAX Event Handling: LexicalHandler
-     */
-    public void startCDATA() throws SAXException {
-        this.writeEvent(START_CDATA);
-    }
-
-    /**
-     * SAX Event Handling: LexicalHandler
-     */
-    public void endCDATA() throws SAXException {
-        this.writeEvent(END_CDATA);
-    }
-
-
-    /**
-     * SAX Event Handling: LexicalHandler
-     */
-    public void comment(char ary[], int start, int length) throws SAXException {
-        try {
-            this.writeEvent(COMMENT);
-            this.writeChars(ary, start, length);
-        } catch (Exception e) {
-            throw new SAXException(e);
-        }
-    }
-
-    public final void writeEvent( final int event) {
-        this.write(event);
-    }
-
-    public final void writeAttributes( final int attributes) throws SAXException {
-        if (attributes > 0xFFFF) throw new SAXException("Too many attributes");
-        this.write((attributes >>> 8) & 0xFF);
-        this.write((attributes >>> 0) & 0xFF);
-    }
-
-    public final void writeString( final String str) throws SAXException {
-        Integer index = (Integer) map.get(str);
-        if (index == null) {
-            map.put(str, new Integer(mapCount++));
-            int length = str.length();
-            this.writeChars(str.toCharArray(), 0, length);
-        }
-        else {
-            int i = index.intValue();
-
-            if (i > 0xFFFF) throw new SAXException("Index too large");
-
-            this.write(((i >>> 8) & 0xFF) | 0x80);
-            this.write((i >>> 0) & 0xFF);
-        }
-    }
-
-    public final void writeChars( final char[] ch, final int start, final int length) {
-        int utflen = 0;
-        int c;
-
-        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;
-            }
-        }
-
-        if (utflen >= 0x00007FFF) {
-            assure(bufCount + utflen + 6);
-
-            buf[bufCount++] = (byte)0x7F;
-            buf[bufCount++] = (byte)0xFF;
-
-            buf[bufCount++] = (byte) ((utflen >>> 24) & 0xFF);
-            buf[bufCount++] = (byte) ((utflen >>> 16) & 0xFF);
-            buf[bufCount++] = (byte) ((utflen >>>  8) & 0xFF);
-            buf[bufCount++] = (byte) ((utflen >>>  0) & 0xFF);
-        }
-        else {
-            assure(bufCount + utflen + 2);
-
-            buf[bufCount++] = (byte) ((utflen >>> 8) & 0xFF);
-            buf[bufCount++] = (byte) ((utflen >>> 0) & 0xFF);
-        }
-
-        for (int i = 0; i < length; i++) {
-            c = ch[i + start];
-            if ((c >= 0x0001) && (c <= 0x007F)) {
-                buf[bufCount++] = (byte) c;
-            }
-            else if (c > 0x07FF) {
-                buf[bufCount++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
-                buf[bufCount++] = (byte) (0x80 | ((c >>  6) & 0x3F));
-                buf[bufCount++] = (byte) (0x80 | ((c >>  0) & 0x3F));
-            }
-            else {
-                buf[bufCount++] = (byte) (0xC0 | ((c >>  6) & 0x1F));
-                buf[bufCount++] = (byte) (0x80 | ((c >>  0) & 0x3F));
-            }
-        }
-
-
-/*
-        if (length == 0) return;
-
-        assure( (int) (buf.length + length * utfRatioAverage) );
-
-        int utflentotal = 0;
-
-        bufCount += 2;
-        int bufStart = bufCount;
-
-        for (int i = 0; i < length; i++) {
-            int c = ch[i + start];
-            int l = bufCount-bufStart;
-
-            if (l+3 >= 0x7FFF) {
-                buf[bufStart-2] = (byte) ((l >>> 8) & 0xFF);
-                buf[bufStart-1] = (byte) ((l >>> 0) & 0xFF);
-                utflentotal += l;
-                bufCount += 2;
-                bufStart = bufCount;
-            }
-
-            if ((c >= 0x0001) && (c <= 0x007F)) {
-                assure(bufCount+1);
-                buf[bufCount++] = (byte)c;
-            }
-            else if (c > 0x07FF) {
-                assure(bufCount+3);
-                buf[bufCount++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
-                buf[bufCount++] = (byte) (0x80 | ((c >>  6) & 0x3F));
-                buf[bufCount++] = (byte) (0x80 | ((c >>  0) & 0x3F));
-            }
-            else {
-                assure(bufCount+2);
-                buf[bufCount++] = (byte) (0xC0 | ((c >>  6) & 0x1F));
-                buf[bufCount++] = (byte) (0x80 | ((c >>  0) & 0x3F));
-            }
-        }
-
-        int l = bufCount-bufStart;
-        buf[bufStart-2] = (byte) ((l >>> 8) & 0xFF);
-        buf[bufStart-1] = (byte) ((l >>> 0) & 0xFF);
-        utflentotal += l;
-
-        utfRatioAverage = (utfRatioAverage + (utflentotal / length) / 2);
-*/
-    }
-
-/*  JH (2003-11-20): seems to be never used
-
-    private void write( final byte[] b ) {
-        int newcount = this.bufCount + b.length;
-        assure(newcount);
-        System.arraycopy(b, 0, this.buf, this.bufCount, b.length);
-        this.bufCount = newcount;
-    }
-*/
-
-    private void write( final int b ) {
+    protected void write( final int b ) {
         int newcount = this.bufCount + 1;
         assure(newcount);
         this.buf[this.bufCount] = (byte)b;

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/XMLByteStreamInterpreter.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/XMLByteStreamInterpreter.java?rev=439271&r1=439270&r2=439271&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/XMLByteStreamInterpreter.java (original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/sax/XMLByteStreamInterpreter.java Fri Sep  1 04:25:40 2006
@@ -15,16 +15,7 @@
  */
 package org.apache.cocoon.components.sax;
 
-import java.util.ArrayList;
-
-import org.apache.cocoon.xml.DefaultLexicalHandler;
-import org.apache.cocoon.xml.XMLConsumer;
-import org.apache.cocoon.xml.XMLProducer;
-import org.xml.sax.ContentHandler;
 import org.xml.sax.SAXException;
-import org.xml.sax.ext.LexicalHandler;
-import org.xml.sax.helpers.AttributesImpl;
-import org.xml.sax.helpers.DefaultHandler;
 
 /**
  * This a simple xml compiler which takes a byte array as input.
@@ -33,294 +24,35 @@
  *
  * @version $Id$
  */
-public final class XMLByteStreamInterpreter implements XMLProducer, XMLByteStreamConstants {
+public final class XMLByteStreamInterpreter extends AbstractXMLByteStreamInterpreter {
 
-    private ArrayList list = new ArrayList();
     private byte[] input;
     private int currentPos;
 
-    protected static final ContentHandler EMPTY_CONTENT_HANDLER = new DefaultHandler();
-
-    /** The <code>ContentHandler</code> receiving SAX events. */
-    protected ContentHandler contentHandler = EMPTY_CONTENT_HANDLER;
-
-    /** The <code>LexicalHandler</code> receiving SAX events. */
-    protected LexicalHandler lexicalHandler = DefaultLexicalHandler.NULL_HANDLER;
-
-    /**
-     * Set the <code>XMLConsumer</code> that will receive XML data.
-     * <br>
-     * This method will simply call <code>setContentHandler(consumer)</code>
-     * and <code>setLexicalHandler(consumer)</code>.
-     */
-    public void setConsumer(XMLConsumer consumer) {
-        setContentHandler(consumer);
-        setLexicalHandler(consumer);
-    }
-
-    /**
-     * Set the <code>ContentHandler</code> that will receive XML data.
-     * <br>
-     * Subclasses may retrieve this <code>ContentHandler</code> instance
-     * accessing the protected <code>super.contentHandler</code> field.
-     */
-    public void setContentHandler(ContentHandler handler) {
-        this.contentHandler = handler;
-    }
-
-    /**
-     * Set the <code>LexicalHandler</code> that will receive XML data.
-     * <br>
-     * Subclasses may retrieve this <code>LexicalHandler</code> instance
-     * accessing the protected <code>super.lexicalHandler</code> field.
-     */
-    public void setLexicalHandler(LexicalHandler handler) {
-        this.lexicalHandler = handler;
-    }
-
     public void recycle() {
-        this.contentHandler = EMPTY_CONTENT_HANDLER;
-        this.lexicalHandler = DefaultLexicalHandler.NULL_HANDLER;
-        this.list.clear();
         this.input = null;
+        super.recycle();
     }
 
     public void deserialize(Object saxFragment) throws SAXException {
         if (!(saxFragment instanceof byte[])) {
             throw new SAXException("XMLDeserializer needs byte array for deserialization.");
         }
-        this.list.clear();
         this.input = (byte[])saxFragment;
         this.currentPos = 0;
-        this.checkProlog();
         this.parse();
     }
 
-    private void parse() throws SAXException {
-        while ( currentPos < input.length) {
-            switch (this.readEvent()) {
-                case START_DOCUMENT:
-                    contentHandler.startDocument();
-                    break;
-                case END_DOCUMENT:
-                    contentHandler.endDocument();
-                    break;
-                case START_PREFIX_MAPPING:
-                    contentHandler.startPrefixMapping(this.readString(), this.readString());
-                    break;
-                case END_PREFIX_MAPPING:
-                    contentHandler.endPrefixMapping(this.readString());
-                    break;
-                case START_ELEMENT:
-                    int attributes = this.readAttributes();
-                    AttributesImpl atts = new AttributesImpl();
-                    for (int i = 0; i < attributes; i++) {
-                        atts.addAttribute(this.readString(), this.readString(), this.readString(), this.readString(), this.readString());
-                    }
-                    contentHandler.startElement(this.readString(), this.readString(), this.readString(), atts);
-                    break;
-                case END_ELEMENT:
-                    contentHandler.endElement(this.readString(), this.readString(), this.readString());
-                    break;
-                case CHARACTERS:
-                    char[] chars = this.readChars();
-                    int len = chars.length;
-                    while (len > 0 && chars[len-1]==0) len--;
-                    if (len > 0) contentHandler.characters(chars, 0, len);
-                    break;
-                case IGNORABLE_WHITESPACE:
-                    char[] spaces = this.readChars();
-                    len = spaces.length;
-                    while (len > 0 && spaces[len-1]==0) len--;
-                    if (len > 0) contentHandler.characters(spaces, 0, len);
-                    break;
-                case PROCESSING_INSTRUCTION:
-                    contentHandler.processingInstruction(this.readString(), this.readString());
-                    break;
-                case COMMENT:
-                    chars = this.readChars();
-                    len = chars.length;
-                    while (len > 0 && chars[len-1]==0) len--;
-                    if (len > 0) lexicalHandler.comment(chars, 0, len);
-                    break;
-                case LOCATOR:
-                    {
-                    String publicId = this.readString();
-                    String systemId = this.readString();
-                    int lineNumber = this.read();
-                    int columnNumber = this.read();
-                    org.xml.sax.helpers.LocatorImpl locator = new org.xml.sax.helpers.LocatorImpl();
-                    locator.setPublicId(publicId);
-                    locator.setSystemId(systemId);
-                    locator.setLineNumber(lineNumber);
-                    locator.setColumnNumber(columnNumber);
-                    contentHandler.setDocumentLocator(locator);
-                    }
-                    break;
-                case START_DTD:
-                    lexicalHandler.startDTD(this.readString(), 
-                                            this.readString(), 
-                                            this.readString());
-                    break;
-                case END_DTD:
-                    lexicalHandler.endDTD();
-                    break;
-                case START_CDATA:
-                    lexicalHandler.startCDATA();
-                    break;
-                case END_CDATA:
-                    lexicalHandler.endCDATA();
-                    break;
-                case SKIPPED_ENTITY:
-                    contentHandler.skippedEntity( this.readString() );
-                    break;
-                case START_ENTITY:
-                    lexicalHandler.startEntity( this.readString() );
-                    break;
-                case END_ENTITY:
-                    lexicalHandler.endEntity( this.readString() );
-                    break;
-                default:
-                    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.");
-    }
-
-    protected 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.readWord();
-        int index = length & 0x00007FFF;
-        if (length >= 0x00008000) {
-            return (String) list.get(index);
-        }
-        else {
-            if (length == 0x00007FFF) {
-                length = this.readLong();
-            }
-            char[] chars = this.readChars(length);
-            int len = chars.length;
-            if (len > 0) {
-                while (chars[len-1]==0) len--;
-            }
-            String str;
-            if (len == 0) {
-                str = "";
-            } else {
-                str = new String(chars, 0, len);
-            }
-            list.add(str);
-            return str;
-        }
-    }
-
-    /**
-     * The returned char array might contain any number of zero bytes
-     * at the end
-     */
-    private char[] readChars() throws SAXException {
-        int length = this.readWord();
-        if (length == 0x00007FFF) {
-            length = this.readLong();
-        }
-        return this.readChars(length);
-    }
-
-    private int read() throws SAXException {
+    protected int read() throws SAXException {
         if (currentPos >= input.length)
-            throw new SAXException("Reached end of input.");
+            return -1;
         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 = 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 = bytearr[count-1];
-                    str[i++] = (char) (((c & 0x1F) << 6) | (char2 & 0x3F));
-                    break;
-                case 14:
-                    // 1110 xxxx  10xx xxxx  10xx xxxx
-                    count += 3;
-                    char2 = bytearr[count-2];
-                    char3 = 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) {
-            // TC:
-            // >= prevents getting the last byte
-            // 0 1 2 3 4   input.length = 5
-            //     |_ currentPos = 2
-            // b.length = 3
-            // 2 + 3 > 5 ok
-            // 2 + 3 >= 5 wrong
-            // why has this worked before?
-            throw new SAXException("End of input reached.");
-        }
-        System.arraycopy(this.input, this.currentPos, b, 0, b.length);
-        this.currentPos += b.length;
-    }
-
-    private int readWord() throws SAXException {
-        int ch1 = this.read();
-        int ch2 = this.read();
-        return ((ch1 << 8) + (ch2 << 0));
-    }
-
-    private int readLong() throws SAXException {
-        int ch1 = this.read();
-        int ch2 = this.read();
-        int ch3 = this.read();
-        int ch4 = this.read();
-        return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
+    protected int read(byte[] b) throws SAXException {
+        final int bytesRead = (this.currentPos + b.length > this.input.length ? this.input.length - this.currentPos : b.length);
+        System.arraycopy(this.input, this.currentPos, b, 0, bytesRead);
+        this.currentPos += bytesRead;
+        return bytesRead;
     }
 }



Mime
View raw message