1 37 38 package net.sourceforge.cruisecontrol.util; 39 40 import org.apache.log4j.Logger; 41 import org.xml.sax.Attributes ; 42 import org.xml.sax.SAXException ; 43 import org.xml.sax.XMLReader ; 44 import org.xml.sax.helpers.XMLFilterImpl ; 45 46 54 public class PruneElementFilter extends XMLFilterImpl { 55 56 private static final Logger LOG = Logger.getLogger(PruneElementFilter.class); 57 58 private final String tagName; 59 60 private int depth = 0; 61 62 65 public PruneElementFilter(String tagName) { 66 super(); 67 this.tagName = tagName; 68 } 69 70 74 public PruneElementFilter(String tagName, XMLReader arg0) { 75 super(arg0); 76 this.tagName = tagName; 77 } 78 79 82 public void characters(char[] ch, int start, int length) throws SAXException { 83 if (!isPruning()) { 84 super.characters(ch, start, length); 85 } 86 } 87 88 91 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { 92 if (!isPruning()) { 93 super.ignorableWhitespace(ch, start, length); 94 } 95 } 96 97 100 public void endElement(String uri, String localName, String qName) throws SAXException { 101 if (isPruning()) { 102 depth--; 103 } else { 104 super.endElement(uri, localName, qName); 105 } 106 } 107 108 111 public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { 112 if (!isPruning() && localName.equals(tagName)) { 113 depth = 1; 115 LOG.debug("pruning branch starting with element named " + tagName); 116 } else if (isPruning()) { 117 depth++; 118 } else { 119 super.startElement(uri, localName, qName, atts); 120 } 121 } 122 123 private boolean isPruning() { 124 return depth > 0; 125 } 126 127 } 128 | Popular Tags |