1 22 23 package org.xquark.util; 24 25 import org.xml.sax.Attributes ; 26 import org.xml.sax.SAXException ; 27 import org.xml.sax.XMLReader ; 28 29 33 public class CharNormalizeFilter extends DefaultXMLFilter 34 { 35 private static final String RCSRevision = "$Revision: 1.1 $"; 36 private static final String RCSName = "$Name: $"; 37 private boolean lastEventIsChar = false; 38 private StringBuffer chars = new StringBuffer (); 39 private boolean removeWhitespace = false; 41 47 public CharNormalizeFilter(XMLReader reader,boolean removeWhitespace) { 48 super(reader); 49 this.removeWhitespace = removeWhitespace; 50 } 51 52 public void comment (char ch[], int start, int length) 56 throws SAXException 57 { 58 saveWaitingChars(); 59 super.comment(ch, start, length); 60 } 61 62 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) 66 throws SAXException 67 { 68 saveWaitingChars(); 69 super.startElement(namespaceURI, localName, qName, atts); 70 } 71 72 public void endElement (String uri, String localName, String rawName) 73 throws SAXException 74 { 75 saveWaitingChars(); 76 super.endElement(uri, localName, rawName); 77 } 78 79 public void processingInstruction (String target, String data) 80 throws SAXException 81 { 82 saveWaitingChars(); 83 super.processingInstruction(target, data); 84 } 85 86 public void characters (char ch[], int start, int length) 87 throws SAXException 88 { 89 lastEventIsChar = true; 90 91 if (removeWhitespace && isWhitespace(ch, start, length)) 93 return; 94 95 chars.append(ch, start, length); 97 } 98 99 103 private void saveWaitingChars() throws SAXException 104 { 105 if (chars.length() > 0) { 106 char array[] = chars.toString().toCharArray(); 107 super.characters(array, 0, array.length); 108 109 lastEventIsChar = false; 110 chars.setLength(0); 111 } 112 } 113 114 119 private boolean isWhitespace(char ch[], int start, int length) 120 { 121 int i, max = start+length; 122 for (i = start; i < max; i++) 123 { 124 switch (ch[i]) 125 { 126 case 0x20 : case 0x9 : case 0xD : case 0xA : 127 break; 128 default : 129 return false; 130 } 131 } 132 return true; } 134 135 } 136 | Popular Tags |