Stuart McCulloch | 26e7a5a | 2011-10-17 10:31:43 +0000 | [diff] [blame^] | 1 | package aQute.libg.sax.filters; |
| 2 | |
| 3 | import org.xml.sax.Attributes; |
| 4 | import org.xml.sax.SAXException; |
| 5 | |
| 6 | import aQute.libg.sax.ContentFilterImpl; |
| 7 | |
| 8 | public abstract class ElementSelectionFilter extends ContentFilterImpl{ |
| 9 | |
| 10 | int depth = 0; |
| 11 | int hiddenDepth = -1; |
| 12 | |
| 13 | protected abstract boolean select(int depth, String uri, String localName, String qName, Attributes attribs); |
| 14 | |
| 15 | @Override |
| 16 | public final void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { |
| 17 | if (hiddenDepth < 0) { |
| 18 | boolean allow = select(depth, uri, localName, qName, atts); |
| 19 | if (allow) |
| 20 | super.startElement(uri, localName, qName, atts); |
| 21 | else |
| 22 | hiddenDepth = 0; |
| 23 | } else { |
| 24 | hiddenDepth ++; |
| 25 | } |
| 26 | depth++; |
| 27 | } |
| 28 | |
| 29 | @Override |
| 30 | public final void endElement(String uri, String localName, String qName) throws SAXException { |
| 31 | if (hiddenDepth < 0) { |
| 32 | super.endElement(uri, localName, qName); |
| 33 | } else { |
| 34 | hiddenDepth --; |
| 35 | } |
| 36 | depth --; |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public void characters(char[] ch, int start, int length) throws SAXException { |
| 41 | if (hiddenDepth < 0) super.characters(ch, start, length); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { |
| 46 | if (hiddenDepth < 0) super.ignorableWhitespace(ch, start, length); |
| 47 | } |
| 48 | |
| 49 | } |