1 package com.jdon.util.jdom; 2 55 56 import java.util.Stack ; 57 58 import org.xml.sax.Attributes ; 59 import org.xml.sax.SAXException ; 60 import org.xml.sax.XMLReader ; 61 62 63 77 public class DataUnformatFilter extends XMLFilterBase 78 { 79 80 81 82 86 87 90 public DataUnformatFilter() 91 { 92 } 93 94 95 102 public DataUnformatFilter(XMLReader xmlreader) 103 { 104 super(xmlreader); 105 } 106 107 108 109 113 114 120 public void reset () 121 { 122 state = SEEN_NOTHING; 123 stateStack = new Stack (); 124 whitespace = new StringBuffer (); 125 } 126 127 128 129 133 134 143 public void startDocument () 144 throws SAXException 145 { 146 reset(); 147 super.startDocument(); 148 } 149 150 151 162 public void startElement (String uri, String localName, 163 String qName, Attributes atts) 164 throws SAXException 165 { 166 clearWhitespace(); 167 stateStack.push(SEEN_ELEMENT); 168 state = SEEN_NOTHING; 169 super.startElement(uri, localName, qName, atts); 170 } 171 172 173 183 public void endElement (String uri, String localName, String qName) 184 throws SAXException 185 { 186 if (state == SEEN_ELEMENT) { 187 clearWhitespace(); 188 } else { 189 emitWhitespace(); 190 } 191 state = stateStack.pop(); 192 super.endElement(uri, localName, qName); 193 } 194 195 196 206 public void characters (char ch[], int start, int length) 207 throws SAXException 208 { 209 if (state != SEEN_DATA) { 210 211 212 213 int end = start + length; 214 while (end-- > start) { 215 if (!isXMLWhitespace(ch[end])) 216 break; 217 } 218 219 224 225 if (end < start) { 226 saveWhitespace(ch, start, length); 227 } else { 228 state = SEEN_DATA; 229 emitWhitespace(); 230 } 231 } 232 233 234 235 if (state == SEEN_DATA) { 236 super.characters(ch, start, length); 237 } 238 } 239 240 241 251 public void ignorableWhitespace (char ch[], int start, int length) 252 throws SAXException 253 { 254 emitWhitespace(); 255 } 257 258 259 268 public void processingInstruction (String target, String data) 269 throws SAXException 270 { 271 emitWhitespace(); 272 super.processingInstruction(target, data); 273 } 274 275 276 277 281 282 285 protected void saveWhitespace (char[] ch, int start, int length) { 286 whitespace.append(ch, start, length); 287 } 288 289 290 293 protected void emitWhitespace () 294 throws SAXException 295 { 296 char[] data = new char[whitespace.length()]; 297 whitespace.getChars(0, data.length, data, 0); 298 whitespace.setLength(0); 299 super.characters(data, 0, data.length); 300 } 301 302 303 306 protected void clearWhitespace () { 307 whitespace.setLength(0); 308 } 309 310 311 314 private boolean isXMLWhitespace (char c) 315 { 316 return c == ' ' || c == '\t' || c == '\r' || c == '\n'; 317 } 318 319 320 321 322 326 private static final Object SEEN_NOTHING = new Object (); 327 private static final Object SEEN_ELEMENT = new Object (); 328 private static final Object SEEN_DATA = new Object (); 329 330 331 335 private Object state = SEEN_NOTHING; 336 private Stack stateStack = new Stack (); 337 338 private StringBuffer whitespace = new StringBuffer (); 339 340 } 341 342 | Popular Tags |