001    /*
002     * Copyright (c) 2005 Marko Karppinen & Co. LLC
003     *
004     * Permission is hereby granted, free of charge, to any person obtaining a 
005     * copy of this software and associated documentation files (the "Software"), 
006     * to deal in the Software without restriction, including without limitation 
007     * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
008     * and/or sell copies of the Software, and to permit persons to whom the 
009     * Software is furnished to do so, subject to the following conditions:
010     *
011     * The above copyright notice and this permission notice shall be included in 
012     * all copies or substantial portions of the Software.
013     *
014     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
015     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
016     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
017     * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
018     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
019     * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
020     * DEALINGS IN THE SOFTWARE.
021     */
022    
023    package fi.karppinen.xml;
024    
025    import gnu.xml.pipeline.EventConsumer;
026    import gnu.xml.pipeline.EventFilter;
027    
028    import org.xml.sax.Attributes;
029    import org.xml.sax.SAXException;
030    
031    /**
032     * This filter supresses all <code>ContentHandler</code> events except those that happen between 
033     * XHTML 1.x <code>body</code> start and end. The filter assumes that there is exactly one 
034     * <code>body</code> element. In particular, nested <code>body</code> elements are not supported.
035     * 
036     * @version $Id: BodyContentPassFilter.java,v 1.1 2006/10/30 19:57:09 hsivonen Exp $
037     * @author hsivonen
038     */
039    public class BodyContentPassFilter extends EventFilter {
040    
041        private boolean pass = false;
042    
043        /**
044         *  
045         */
046        public BodyContentPassFilter() {
047            super();
048            setContentHandler(this);
049        }
050    
051        /**
052         * @param consumer
053         */
054        public BodyContentPassFilter(EventConsumer consumer) {
055            super(consumer);
056            setContentHandler(this);
057        }
058    
059        /**
060         * @see org.xml.sax.ContentHandler#characters(char[], int, int)
061         */
062        public void characters(char[] ch, int start, int length)
063                throws SAXException {
064            if (pass)
065                super.characters(ch, start, length);
066        }
067    
068        /**
069         * @see org.xml.sax.ContentHandler#endDocument()
070         */
071        public void endDocument() throws SAXException {
072            pass = false;
073        }
074    
075        /**
076         * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
077         *      java.lang.String, java.lang.String)
078         */
079        public void endElement(String uri, String localName, String qName)
080                throws SAXException {
081            if ("body".equals(localName)
082                    && "http://www.w3.org/1999/xhtml".equals(uri)) {
083                pass = false;
084            } else if (pass) {
085                super.endElement(uri, localName, qName);
086            }
087        }
088    
089        /**
090         * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
091         */
092        public void ignorableWhitespace(char[] ch, int start, int length)
093                throws SAXException {
094            if (pass)
095                super.ignorableWhitespace(ch, start, length);
096        }
097    
098        /**
099         * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String,
100         *      java.lang.String)
101         */
102        public void processingInstruction(String target, String data)
103                throws SAXException {
104            if (pass)
105                super.processingInstruction(target, data);
106        }
107    
108        /**
109         * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
110         */
111        public void skippedEntity(String name) throws SAXException {
112            if (pass)
113                super.skippedEntity(name);
114        }
115    
116        /**
117         * @see org.xml.sax.ContentHandler#startDocument()
118         */
119        public void startDocument() throws SAXException {
120            pass = false;
121        }
122    
123        /**
124         * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
125         *      java.lang.String, java.lang.String, org.xml.sax.Attributes)
126         */
127        public void startElement(String uri, String localName, String qName,
128                Attributes atts) throws SAXException {
129            if ("body".equals(localName)
130                    && "http://www.w3.org/1999/xhtml".equals(uri)) {
131                pass = true;
132            } else if (pass) {
133                super.startElement(uri, localName, qName, atts);
134            }
135        }
136    }