cziegeler 01/09/06 04:10:54 Modified: src/org/apache/cocoon/components/source SitemapSource.java URLSource.java src/org/apache/cocoon/xml ContentHandlerWrapper.java Log: Better handling of LexicalHandlers for toSAX Revision Changes Path 1.19 +12 -27 xml-cocoon2/src/org/apache/cocoon/components/source/SitemapSource.java Index: SitemapSource.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/components/source/SitemapSource.java,v retrieving revision 1.18 retrieving revision 1.19 diff -u -r1.18 -r1.19 --- SitemapSource.java 2001/09/04 14:44:12 1.18 +++ SitemapSource.java 2001/09/06 11:10:54 1.19 @@ -34,6 +34,7 @@ import org.xml.sax.ContentHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; +import org.xml.sax.ext.LexicalHandler; import org.w3c.dom.Node; import java.io.ByteArrayInputStream; @@ -47,7 +48,7 @@ * Description of a source which is defined by a pipeline. * * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> - * @version CVS $Revision: 1.18 $ $Date: 2001/09/04 14:44:12 $ + * @version CVS $Revision: 1.19 $ $Date: 2001/09/06 11:10:54 $ */ public final class SitemapSource @@ -202,22 +203,6 @@ } /** - * Is this a file, then the <code>getFile</code> method can - * return a <code>File</code> object for this source. - */ - public boolean isFile() { - return false; - } - - /** - * Return a <code>File</code> object if this is a local file - * (if the <code>isFile</code> method returns true). - */ - public File getFile() { - return null; - } - - /** * Refresh this object and update the last modified date * and content length. */ @@ -276,14 +261,22 @@ } /** - * Stream content to the consumer + * Stream content to the content handler */ - public void toSAX(XMLConsumer consumer) + public void toSAX(ContentHandler contentHandler) throws SAXException { if (this.exception != null) { throw this.exception; } try { + XMLConsumer consumer; + if (contentHandler instanceof XMLConsumer) { + consumer = (XMLConsumer)contentHandler; + } else if (contentHandler instanceof LexicalHandler) { + consumer = new ContentHandlerWrapper(contentHandler, (LexicalHandler)contentHandler); + } else { + consumer = new ContentHandlerWrapper(contentHandler); + } if (this.redirectSource != null) { this.redirectSource.toSAX(consumer); } else { @@ -298,14 +291,6 @@ } finally { reset(); } - } - - /** - * Stream content to the content handler - */ - public void toSAX(ContentHandler contentHandler) - throws SAXException { - this.toSAX(new ContentHandlerWrapper(contentHandler)); } private void reset() { 1.11 +9 -6 xml-cocoon2/src/org/apache/cocoon/components/source/URLSource.java Index: URLSource.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/components/source/URLSource.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- URLSource.java 2001/08/25 19:40:32 1.10 +++ URLSource.java 2001/09/06 11:10:54 1.11 @@ -17,6 +17,7 @@ import org.xml.sax.ContentHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; +import org.xml.sax.ext.LexicalHandler; import java.io.File; import java.io.FileInputStream; @@ -30,7 +31,7 @@ * Description of a source which is described by an URL. * * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> - * @version CVS $Revision: 1.10 $ $Date: 2001/08/25 19:40:32 $ + * @version CVS $Revision: 1.11 $ $Date: 2001/09/06 11:10:54 $ */ public final class URLSource implements ModifiableSource { @@ -290,17 +291,19 @@ * Stream content to a content handler or to an XMLConsumer */ public void toSAX(ContentHandler handler) - throws SAXException - { + throws SAXException { Parser parser = null; try { parser = (Parser)this.manager.lookup(Parser.ROLE); - if (handler instanceof XMLConsumer) + if (handler instanceof XMLConsumer) { parser.setConsumer((XMLConsumer)handler); - else + } else { parser.setContentHandler(handler); - + if (handler instanceof LexicalHandler) { + parser.setLexicalHandler((LexicalHandler)handler); + } + } parser.parse(this.getInputSource()); } catch (Exception e){ throw new SAXException("Exception in URLSource.stream()", e); 1.3 +129 -38 xml-cocoon2/src/org/apache/cocoon/xml/ContentHandlerWrapper.java Index: ContentHandlerWrapper.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/xml/ContentHandlerWrapper.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- ContentHandlerWrapper.java 2001/08/20 13:55:18 1.2 +++ ContentHandlerWrapper.java 2001/09/06 11:10:54 1.3 @@ -13,6 +13,7 @@ import org.xml.sax.ContentHandler; import org.xml.sax.Locator; import org.xml.sax.SAXException; +import org.xml.sax.ext.LexicalHandler; /** * This class is an utility class "wrapping" around a SAX version 2.0 @@ -22,13 +23,16 @@ * * @author <a href="mailto:[EMAIL PROTECTED]">Davanum Srinivas</a> * (Apache Software Foundation, Computer Associates) - * @version CVS $Revision: 1.2 $ $Date: 2001/08/20 13:55:18 $ + * @version CVS $Revision: 1.3 $ $Date: 2001/09/06 11:10:54 $ */ public class ContentHandlerWrapper extends AbstractXMLConsumer implements Recyclable { /** The current <code>ContentHandler</code>. */ - protected ContentHandler documentHandler; + protected ContentHandler contentHandler; + /** The optional <code>LexicalHandler</code> */ + protected LexicalHandler lexicalHandler; + /** * Create a new <code>ContentHandlerWrapper</code> instance. */ @@ -38,10 +42,20 @@ /** * Create a new <code>ContentHandlerWrapper</code> instance. + */ + public ContentHandlerWrapper(ContentHandler contentHandler) { + this(); + this.setContentHandler(contentHandler); + } + + /** + * Create a new <code>ContentHandlerWrapper</code> instance. */ - public ContentHandlerWrapper(ContentHandler document) { + public ContentHandlerWrapper(ContentHandler contentHandler, + LexicalHandler lexicalHandler) { this(); - this.setContentHandler(document); + this.setContentHandler(contentHandler); + this.setLexicalHandler(lexicalHandler); } /** @@ -49,23 +63,36 @@ * * @exception IllegalStateException If the <code>ContentHandler</code> * was already set. + */ + public void setContentHandler(ContentHandler contentHandler) + throws IllegalStateException { + if (this.contentHandler!=null) throw new IllegalStateException(); + this.contentHandler=contentHandler; + } + + /** + * Set the <code>LexicalHandler</code> that will receive XML data. + * + * @exception IllegalStateException If the <code>LexicalHandler</code> + * was already set. */ - public void setContentHandler(ContentHandler document) + public void setLexicalHandler(LexicalHandler lexicalHandler) throws IllegalStateException { - if (this.documentHandler!=null) throw new IllegalStateException(); - this.documentHandler=document; + if (this.lexicalHandler!=null) throw new IllegalStateException(); + this.lexicalHandler=lexicalHandler; } public void recycle () { - this.documentHandler = null; + this.contentHandler = null; + this.lexicalHandler = null; } /** * Receive an object for locating the origin of SAX document events. */ public void setDocumentLocator (Locator locator) { - if (this.documentHandler==null) return; - else this.documentHandler.setDocumentLocator(locator); + if (this.contentHandler==null) return; + else this.contentHandler.setDocumentLocator(locator); } /** @@ -73,9 +100,9 @@ */ public void startDocument () throws SAXException { - if (this.documentHandler==null) + if (this.contentHandler==null) throw new SAXException("ContentHandler not set"); - this.documentHandler.startDocument(); + this.contentHandler.startDocument(); } /** @@ -83,9 +110,7 @@ */ public void endDocument () throws SAXException { - if (this.documentHandler==null) - throw new SAXException("ContentHandler not set"); - this.documentHandler.endDocument(); + this.contentHandler.endDocument(); } /** @@ -93,9 +118,9 @@ */ public void startPrefixMapping(String prefix, String uri) throws SAXException { - if (this.documentHandler==null) + if (this.contentHandler==null) throw new SAXException("ContentHandler not set"); - this.documentHandler.startPrefixMapping(prefix, uri); + this.contentHandler.startPrefixMapping(prefix, uri); } /** @@ -103,9 +128,7 @@ */ public void endPrefixMapping(String prefix) throws SAXException { - if (this.documentHandler==null) - throw new SAXException("ContentHandler not set"); - this.documentHandler.endPrefixMapping(prefix); + this.contentHandler.endPrefixMapping(prefix); } /** @@ -113,9 +136,7 @@ */ public void startElement(String uri, String loc, String raw, Attributes a) throws SAXException { - if (this.documentHandler==null) - throw new SAXException("ContentHandler not set"); - this.documentHandler.startElement(uri, loc, raw, a); + this.contentHandler.startElement(uri, loc, raw, a); } @@ -124,9 +145,7 @@ */ public void endElement(String uri, String loc, String raw) throws SAXException { - if (this.documentHandler==null) - throw new SAXException("ContentHandler not set"); - this.documentHandler.endElement(uri, loc, raw); + this.contentHandler.endElement(uri, loc, raw); } /** @@ -134,9 +153,7 @@ */ public void characters(char ch[], int start, int len) throws SAXException { - if (this.documentHandler==null) - throw new SAXException("ContentHandler not set"); - this.documentHandler.characters(ch,start,len); + this.contentHandler.characters(ch,start,len); } /** @@ -144,9 +161,7 @@ */ public void ignorableWhitespace(char ch[], int start, int len) throws SAXException { - if (this.documentHandler==null) - throw new SAXException("ContentHandler not set"); - this.documentHandler.ignorableWhitespace(ch,start,len); + this.contentHandler.ignorableWhitespace(ch,start,len); } /** @@ -154,9 +169,7 @@ */ public void processingInstruction(String target, String data) throws SAXException { - if (this.documentHandler==null) - throw new SAXException("ContentHandler not set"); - this.documentHandler.processingInstruction(target,data); + this.contentHandler.processingInstruction(target,data); } /** @@ -167,8 +180,86 @@ */ public void skippedEntity(String name) throws SAXException { - if (this.documentHandler==null) - throw new SAXException("ContentHandler not set"); - this.documentHandler.skippedEntity(name); + this.contentHandler.skippedEntity(name); + } + + /** + * Report the start of DTD declarations, if any. + * + * @param name The document type name. + * @param publicId The declared public identifier for the external DTD + * subset, or null if none was declared. + * @param systemId The declared system identifier for the external DTD + * subset, or null if none was declared. + */ + public void startDTD(String name, String publicId, String systemId) + throws SAXException { + if (this.lexicalHandler != null) + this.lexicalHandler.startDTD(name, publicId, systemId); + } + + /** + * Report the end of DTD declarations. + */ + public void endDTD() + throws SAXException { + if (this.lexicalHandler != null) + this.lexicalHandler.endDTD(); + } + + /** + * Report the beginning of an entity. + * + * @param name The name of the entity. If it is a parameter entity, the + * name will begin with '%'. + */ + public void startEntity(String name) + throws SAXException { + if (this.lexicalHandler != null) + this.lexicalHandler.startEntity(name); + } + + /** + * Report the end of an entity. + * + * @param name The name of the entity that is ending. + */ + public void endEntity(String name) + throws SAXException { + if (this.lexicalHandler != null) + this.lexicalHandler.endEntity(name); } + + /** + * Report the start of a CDATA section. + */ + public void startCDATA() + throws SAXException { + if (this.lexicalHandler != null) + this.lexicalHandler.startCDATA(); + } + + /** + * Report the end of a CDATA section. + */ + public void endCDATA() + throws SAXException { + if (this.lexicalHandler != null) + this.lexicalHandler.endCDATA(); + } + + + /** + * Report an XML comment anywhere in the document. + * + * @param ch An array holding the characters in the comment. + * @param start The starting position in the array. + * @param len The number of characters to use from the array. + */ + public void comment(char ch[], int start, int len) + throws SAXException { + if (this.lexicalHandler != null) + this.lexicalHandler.comment(ch, start, len); + } + } ---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]