1 8 9 package net.sourceforge.chaperon.test; 10 11 import org.apache.commons.logging.Log; 12 13 import org.xml.sax.*; 14 15 21 public class WhitespaceFilter implements ContentHandler 22 { 23 private ContentHandler handler = null; 24 private StringBuffer buffer = null; 25 private Log log = null; 26 27 32 public WhitespaceFilter(ContentHandler handler) 33 { 34 this.handler = handler; 35 } 36 37 public WhitespaceFilter(ContentHandler handler, Log log) 38 { 39 this.handler = handler; 40 this.log = log; 41 } 42 43 public void setLog(Log log) 44 { 45 this.log = log; 46 } 47 48 51 public void setDocumentLocator(Locator locator) 52 { 53 if (log!=null) 54 log.debug("[SAX] DocumentLocator "+locator); 55 56 handler.setDocumentLocator(locator); 57 } 58 59 62 public void startDocument() throws SAXException 63 { 64 if (log!=null) 65 log.debug("[SAX] StartDocument"); 66 67 handler.startDocument(); 68 } 69 70 73 public void endDocument() throws SAXException 74 { 75 if (log!=null) 76 log.debug("[SAX] EndDocument"); 77 78 handler.endDocument(); 79 } 80 81 84 public void characters(char[] c, int start, int len) 85 { 86 if (log!=null) 87 log.debug("[SAX] Characters "+new String (c, start, len)); 88 89 if (buffer==null) 90 buffer = new StringBuffer (); 91 92 buffer.append(c, start, len); 93 } 94 95 98 public void ignorableWhitespace(char[] c, int start, int len) 99 { 100 if (log!=null) 101 log.debug("[SAX] Ignorable Whitespace "+new String (c, start, len)); 102 103 } 105 106 109 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) 110 throws SAXException 111 { 112 if (log!=null) 113 log.debug("[SAX] Start Element "+localName+" Namespace "+namespaceURI); 114 115 pushText(); 116 handler.startElement(namespaceURI, localName, qName, atts); 117 } 118 119 122 public void endElement(String namespaceURI, String localName, String raw) 123 throws SAXException 124 { 125 if (log!=null) 126 log.debug("[SAX] End Element "+localName+" Namespace "+namespaceURI); 127 128 pushText(); 129 handler.endElement(namespaceURI, localName, raw); 130 } 131 132 135 public void processingInstruction(String target, String data) 136 throws SAXException 137 { 138 if (log!=null) 139 log.debug("[SAX] Processing Instruction Target "+target+" Data "+data); 140 141 pushText(); 142 handler.processingInstruction(target, data); 143 } 144 145 148 public void startPrefixMapping(String prefix, String uri) 149 throws SAXException 150 { 151 if (log!=null) 152 log.debug("[SAX] Start Prefix Mapping "+prefix+" Namespace "+uri); 153 154 handler.startPrefixMapping(prefix, uri); 155 } 156 157 160 public void endPrefixMapping(String prefix) throws SAXException 161 { 162 if (log!=null) 163 log.debug("[SAX] Start Prefix Mapping "+prefix); 164 165 handler.endPrefixMapping(prefix); 166 } 167 168 public void skippedEntity(String name) throws SAXException 169 { 170 if (log!=null) 171 log.debug("[SAX] Skipped Entity "+name); 172 173 handler.skippedEntity(name); 174 } 175 176 public void pushText() throws SAXException 177 { 178 if (buffer!=null) 179 { 180 String text = buffer.toString(); 181 182 StringBuffer normalized = new StringBuffer (); 183 184 for (int i = 0; i<text.length(); i++) 185 { 186 if (Character.isWhitespace(text.charAt(i))) 187 { 188 normalized.append(' '); 189 while (((i+1)<text.length()) && (Character.isWhitespace(text.charAt(i+1)))) 190 i++; 191 } 192 else 193 normalized.append(text.charAt(i)); 194 } 195 196 text = normalized.toString().trim(); 197 198 if (text.length()>0) 199 handler.characters(text.toCharArray(), 0, text.length()); 200 201 buffer = null; 202 } 203 } 204 } 205 | Popular Tags |