001    /*
002     * Copyright (c) 2006 Henri Sivonen
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.iki.hsivonen.xml;
024    
025    import org.xml.sax.Attributes;
026    import org.xml.sax.SAXException;
027    
028    import gnu.xml.pipeline.EventConsumer;
029    import gnu.xml.pipeline.EventFilter;
030    
031    public class IdContentPassFilter extends EventFilter {
032    
033        private int depth;
034    
035        private String id;
036        
037        public IdContentPassFilter(EventConsumer next, String id) {
038            super(next);
039            setContentHandler(this);
040            this.id = id;
041        }
042    
043        /**
044         * @see gnu.xml.pipeline.EventFilter#characters(char[], int, int)
045         */
046        public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
047            if (depth > 0) {
048                super.characters(arg0, arg1, arg2);
049            }
050        }
051    
052        /**
053         * @see gnu.xml.pipeline.EventFilter#endElement(java.lang.String,
054         *      java.lang.String, java.lang.String)
055         */
056        public void endElement(String arg0, String arg1, String arg2)
057                throws SAXException {
058            if (depth > 0) {
059                super.endElement(arg0, arg1, arg2);
060            }
061            depth--;
062        }
063    
064        /**
065         * @see gnu.xml.pipeline.EventFilter#ignorableWhitespace(char[], int, int)
066         */
067        public void ignorableWhitespace(char[] arg0, int arg1, int arg2)
068                throws SAXException {
069            if (depth > 0) {
070                super.ignorableWhitespace(arg0, arg1, arg2);
071            }
072        }
073    
074        /**
075         * @see gnu.xml.pipeline.EventFilter#processingInstruction(java.lang.String,
076         *      java.lang.String)
077         */
078        public void processingInstruction(String arg0, String arg1)
079                throws SAXException {
080            if (depth > 0) {
081                super.processingInstruction(arg0, arg1);
082            }
083        }
084    
085        /**
086         * @see gnu.xml.pipeline.EventFilter#startDocument()
087         */
088        public void startDocument() throws SAXException {
089            depth = -1;
090            super.startDocument();
091        }
092    
093        /**
094         * @see gnu.xml.pipeline.EventFilter#startElement(java.lang.String,
095         *      java.lang.String, java.lang.String, org.xml.sax.Attributes)
096         */
097        public void startElement(String namespaceURI, String localName,
098                String qName, Attributes atts) throws SAXException {
099            if (depth > 0) {
100                super.startElement(namespaceURI, localName,
101                qName, atts);
102                depth++;
103            } else {
104                String idAtt = atts.getValue("", "id");
105                if (id.equals(idAtt)) {
106                    depth = 1;
107                    return;
108                }
109                idAtt = atts.getValue("http://www.w3.org/XML/1998/namespace", "id");
110                if (idAtt == null) {
111                    return;
112                }
113                if (id.equals(idAtt.trim())) {
114                    depth = 1;
115                    return;
116                }
117            }
118        }
119    
120    }