vgritsenko 2003/12/09 13:03:17
Modified: src/java/org/apache/cocoon/xml SaxBuffer.java
Log:
Add copy constructor;
Add methods for extending classes: addBit(), bits(), isEmpty();
Add toString() method: returns concatenation of all character events;
Change inner classes order.
Revision Changes Path
1.6 +157 -97 cocoon-2.1/src/java/org/apache/cocoon/xml/SaxBuffer.java
Index: SaxBuffer.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/xml/SaxBuffer.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- SaxBuffer.java 20 Nov 2003 14:09:59 -0000 1.5
+++ SaxBuffer.java 9 Dec 2003 21:03:17 -0000 1.6
@@ -60,6 +60,7 @@
import java.util.ArrayList;
import java.util.Iterator;
+import java.util.List;
/**
* A class that can record SAX events and replay them later.
@@ -75,17 +76,36 @@
* <p>Both ContentHandler and LexicalHandler are supported, the only
exception is
* that the setDocumentLocator event is not recorded.
*
+ * @author <a href="mailto:[email protected]">Apache Cocoon Team</a>
* @version CVS $Id$
*/
public class SaxBuffer implements ContentHandler, LexicalHandler, XMLizable,
Recyclable {
- private ArrayList saxbits = new ArrayList();
+
+ /**
+ * Stores list of [EMAIL PROTECTED] SaxBit} objects.
+ */
+ private List saxbits = new ArrayList();
+
+ /**
+ * Creates empty SaxBuffer
+ */
+ public SaxBuffer() {
+ }
+
+ /**
+ * Creates copy of another SaxBuffer
+ */
+ public SaxBuffer(SaxBuffer saxBuffer) {
+ this.saxbits.addAll(saxBuffer.saxbits);
+ }
+
public void skippedEntity(String name) throws SAXException {
saxbits.add(new SkippedEntity(name));
}
public void setDocumentLocator(Locator locator) {
- //don't record this event
+ // don't record this event
}
public void ignorableWhitespace(char ch[], int start, int length) throws
SAXException {
@@ -152,22 +172,70 @@
saxbits.add(new EndEntity(name));
}
+
+ /**
+ * Adds a SaxBit to the bits list
+ */
+ protected final void addBit(SaxBit bit) {
+ saxbits.add(bit);
+ }
+
+ /**
+ * Iterates through the bits list
+ */
+ protected final Iterator bits() {
+ return saxbits.iterator();
+ }
+
+ public boolean isEmpty() {
+ return saxbits.isEmpty();
+ }
+
public void toSAX(ContentHandler contentHandler) throws SAXException {
- Iterator saxbitsIt = saxbits.iterator();
- while (saxbitsIt.hasNext()) {
- SaxBit saxbit = (SaxBit)saxbitsIt.next();
+ for (Iterator i = saxbits.iterator(); i.hasNext();) {
+ SaxBit saxbit = (SaxBit)i.next();
saxbit.send(contentHandler);
}
}
+ /*
+ * NOTE: Used in i18n XML bundle implementation
+ */
+ public String toString() {
+ StringBuffer value = new StringBuffer();
+ for (Iterator i = saxbits.iterator(); i.hasNext();) {
+ SaxBit saxbit = (SaxBit)i.next();
+ if (saxbit instanceof Characters) {
+ ((Characters)saxbit).toString(value);
+ }
+ }
+
+ return value.toString();
+ }
+
public void recycle() {
saxbits.clear();
}
+ /**
+ * SaxBit is a representation of the SAX event. Every SaxBit is
immutable object.
+ */
interface SaxBit {
public void send(ContentHandler contentHandler) throws SAXException;
}
+ final static class StartDocument implements SaxBit {
+ public void send(ContentHandler contentHandler) throws SAXException {
+ contentHandler.startDocument();
+ }
+ }
+
+ final static class EndDocument implements SaxBit {
+ public void send(ContentHandler contentHandler) throws SAXException {
+ contentHandler.endDocument();
+ }
+ }
+
final static class PI implements SaxBit {
private final String target;
private final String data;
@@ -182,69 +250,65 @@
}
}
- final static class StartElement implements SaxBit {
- private final String namespaceURI;
- private final String localName;
- private final String qName;
- private final Attributes attrs;
+ final static class StartDTD implements SaxBit {
+ private final String name;
+ private final String publicId;
+ private final String systemId;
- public StartElement(String namespaceURI, String localName, String
qName, Attributes attrs) {
- this.namespaceURI = namespaceURI;
- this.localName = localName;
- this.qName = qName;
- this.attrs = new org.xml.sax.helpers.AttributesImpl(attrs);
+ public StartDTD(String name, String publicId, String systemId) {
+ this.name = name;
+ this.publicId = publicId;
+ this.systemId = systemId;
}
public void send(ContentHandler contentHandler) throws SAXException {
- contentHandler.startElement(namespaceURI, localName, qName,
attrs);
+ if (contentHandler instanceof LexicalHandler)
+ ((LexicalHandler)contentHandler).startDTD(name, publicId,
systemId);
}
}
- final static class EndPrefixMapping implements SaxBit {
- private final String prefix;
-
- public EndPrefixMapping(String prefix) {
- this.prefix = prefix;
- }
-
+ final static class EndDTD implements SaxBit {
public void send(ContentHandler contentHandler) throws SAXException {
- contentHandler.endPrefixMapping(prefix);
+ if (contentHandler instanceof LexicalHandler)
+ ((LexicalHandler)contentHandler).endDTD();
}
}
- final static class Characters implements SaxBit {
- private final char[] ch;
+ final static class StartEntity implements SaxBit {
+ private final String name;
- public Characters(char[] ch, int start, int length) {
- // make a copy so that we don't hold references to a potentially
large array we don't control
- this.ch = new char[length];
- System.arraycopy(ch, start, this.ch, 0, length);
+ public StartEntity(String name) {
+ this.name = name;
}
public void send(ContentHandler contentHandler) throws SAXException {
- contentHandler.characters(ch, 0, ch.length);
+ if (contentHandler instanceof LexicalHandler)
+ ((LexicalHandler)contentHandler).startEntity(name);
}
}
- final static class EndElement implements SaxBit {
- private final String namespaceURI;
- private final String localName;
- private final String qName;
+ final static class EndEntity implements SaxBit {
+ private final String name;
- public EndElement(String namespaceURI, String localName, String
qName) {
- this.namespaceURI = namespaceURI;
- this.localName = localName;
- this.qName = qName;
+ public EndEntity(String name) {
+ this.name = name;
}
public void send(ContentHandler contentHandler) throws SAXException {
- contentHandler.endElement(namespaceURI, localName, qName);
+ if (contentHandler instanceof LexicalHandler)
+ ((LexicalHandler)contentHandler).endEntity(name);
}
}
- final static class EndDocument implements SaxBit {
+ final static class SkippedEntity implements SaxBit {
+ private final String name;
+
+ public SkippedEntity(String name) {
+ this.name = name;
+ }
+
public void send(ContentHandler contentHandler) throws SAXException {
- contentHandler.endDocument();
+ contentHandler.skippedEntity(name);
}
}
@@ -262,114 +326,110 @@
}
}
- final static class Comment implements SaxBit {
- private final char[] ch;
+ final static class EndPrefixMapping implements SaxBit {
+ private final String prefix;
- public Comment(char[] ch, int start, int length) {
- // make a copy so that we don't hold references to a potentially
large array we don't control
- this.ch = new char[length];
- System.arraycopy(ch, start, this.ch, 0, length);
+ public EndPrefixMapping(String prefix) {
+ this.prefix = prefix;
}
public void send(ContentHandler contentHandler) throws SAXException {
- if (contentHandler instanceof LexicalHandler)
- ((LexicalHandler)contentHandler).comment(ch, 0, ch.length);
+ contentHandler.endPrefixMapping(prefix);
}
}
- final static class StartCDATA implements SaxBit {
- public void send(ContentHandler contentHandler) throws SAXException {
- if (contentHandler instanceof LexicalHandler)
- ((LexicalHandler)contentHandler).startCDATA();
+ final static class StartElement implements SaxBit {
+ private final String namespaceURI;
+ private final String localName;
+ private final String qName;
+ private final Attributes attrs;
+
+ public StartElement(String namespaceURI, String localName, String
qName, Attributes attrs) {
+ this.namespaceURI = namespaceURI;
+ this.localName = localName;
+ this.qName = qName;
+ this.attrs = new org.xml.sax.helpers.AttributesImpl(attrs);
}
- }
- final static class EndCDATA implements SaxBit {
public void send(ContentHandler contentHandler) throws SAXException {
- if (contentHandler instanceof LexicalHandler)
- ((LexicalHandler)contentHandler).endCDATA();
+ contentHandler.startElement(namespaceURI, localName, qName,
attrs);
}
}
- final static class SkippedEntity implements SaxBit {
- private final String name;
+ final static class EndElement implements SaxBit {
+ private final String namespaceURI;
+ private final String localName;
+ private final String qName;
- public SkippedEntity(String name) {
- this.name = name;
+ public EndElement(String namespaceURI, String localName, String
qName) {
+ this.namespaceURI = namespaceURI;
+ this.localName = localName;
+ this.qName = qName;
}
public void send(ContentHandler contentHandler) throws SAXException {
- contentHandler.skippedEntity(name);
+ contentHandler.endElement(namespaceURI, localName, qName);
}
}
- final static class IgnorableWhitespace implements SaxBit {
+ final static class Characters implements SaxBit {
private final char[] ch;
- public IgnorableWhitespace(char[] ch, int start, int length) {
+ public Characters(char[] ch, int start, int length) {
// make a copy so that we don't hold references to a potentially
large array we don't control
this.ch = new char[length];
System.arraycopy(ch, start, this.ch, 0, length);
}
public void send(ContentHandler contentHandler) throws SAXException {
- contentHandler.ignorableWhitespace(ch, 0, ch.length);
+ contentHandler.characters(ch, 0, ch.length);
}
- }
-
- final static class StartDocument implements SaxBit {
- public void send(ContentHandler contentHandler) throws SAXException {
- contentHandler.startDocument();
+
+ public void toString(StringBuffer value) {
+ value.append(ch);
}
}
- final static class StartDTD implements SaxBit {
- private final String name;
- private final String publicId;
- private final String systemId;
+ final static class Comment implements SaxBit {
+ private final char[] ch;
- public StartDTD(String name, String publicId, String systemId) {
- this.name = name;
- this.publicId = publicId;
- this.systemId = systemId;
+ public Comment(char[] ch, int start, int length) {
+ // make a copy so that we don't hold references to a potentially
large array we don't control
+ this.ch = new char[length];
+ System.arraycopy(ch, start, this.ch, 0, length);
}
public void send(ContentHandler contentHandler) throws SAXException {
if (contentHandler instanceof LexicalHandler)
- ((LexicalHandler)contentHandler).startDTD(name, publicId,
systemId);
+ ((LexicalHandler)contentHandler).comment(ch, 0, ch.length);
}
}
- final static class EndDTD implements SaxBit {
+ final static class StartCDATA implements SaxBit {
public void send(ContentHandler contentHandler) throws SAXException {
if (contentHandler instanceof LexicalHandler)
- ((LexicalHandler)contentHandler).endDTD();
+ ((LexicalHandler)contentHandler).startCDATA();
}
}
- final static class StartEntity implements SaxBit {
- private final String name;
-
- public StartEntity(String name) {
- this.name = name;
- }
-
+ final static class EndCDATA implements SaxBit {
public void send(ContentHandler contentHandler) throws SAXException {
if (contentHandler instanceof LexicalHandler)
- ((LexicalHandler)contentHandler).startEntity(name);
+ ((LexicalHandler)contentHandler).endCDATA();
}
}
- final static class EndEntity implements SaxBit {
- private final String name;
+ final static class IgnorableWhitespace implements SaxBit {
+ private final char[] ch;
- public EndEntity(String name) {
- this.name = name;
+ public IgnorableWhitespace(char[] ch, int start, int length) {
+ // make a copy so that we don't hold references to a potentially
large array we don't control
+ this.ch = new char[length];
+ System.arraycopy(ch, start, this.ch, 0, length);
}
public void send(ContentHandler contentHandler) throws SAXException {
- if (contentHandler instanceof LexicalHandler)
- ((LexicalHandler)contentHandler).endEntity(name);
+ contentHandler.ignorableWhitespace(ch, 0, ch.length);
}
}
}