1 7 8 package org.cyberneko.html.filters; 9 10 import java.util.Hashtable ; 11 12 import org.apache.xerces.xni.Augmentations; 13 import org.apache.xerces.xni.NamespaceContext; 14 import org.apache.xerces.xni.QName; 15 import org.apache.xerces.xni.XMLAttributes; 16 import org.apache.xerces.xni.XMLLocator; 17 import org.apache.xerces.xni.XMLResourceIdentifier; 18 import org.apache.xerces.xni.XMLString; 19 import org.apache.xerces.xni.XNIException; 20 21 85 public class ElementRemover 86 extends DefaultFilter { 87 88 92 93 protected static final Object NULL = new Object (); 94 95 99 101 102 protected Hashtable fAcceptedElements = new Hashtable (); 103 104 105 protected Hashtable fRemovedElements = new Hashtable (); 106 107 109 110 protected int fElementDepth; 111 112 113 protected int fRemovalElementDepth; 114 115 119 129 public void acceptElement(String element, String [] attributes) { 130 Object key = element.toLowerCase(); 131 Object value = NULL; 132 if (attributes != null) { 133 String [] newarray = new String [attributes.length]; 134 for (int i = 0; i < attributes.length; i++) { 135 newarray[i] = attributes[i].toLowerCase(); 136 } 137 value = attributes; 138 } 139 fAcceptedElements.put(key, value); 140 } 142 150 public void removeElement(String element) { 151 Object key = element.toLowerCase(); 152 Object value = NULL; 153 fRemovedElements.put(key, value); 154 } 156 160 162 163 public void startDocument(XMLLocator locator, String encoding, 164 NamespaceContext nscontext, Augmentations augs) 165 throws XNIException { 166 fElementDepth = 0; 167 fRemovalElementDepth = Integer.MAX_VALUE; 168 super.startDocument(locator, encoding, nscontext, augs); 169 } 171 173 174 public void startDocument(XMLLocator locator, String encoding, Augmentations augs) 175 throws XNIException { 176 startDocument(locator, encoding, null, augs); 177 } 179 180 public void startPrefixMapping(String prefix, String uri, Augmentations augs) 181 throws XNIException { 182 if (fElementDepth <= fRemovalElementDepth) { 183 super.startPrefixMapping(prefix, uri, augs); 184 } 185 } 187 188 public void startElement(QName element, XMLAttributes attributes, Augmentations augs) 189 throws XNIException { 190 if (fElementDepth <= fRemovalElementDepth && handleOpenTag(element, attributes)) { 191 super.startElement(element, attributes, augs); 192 } 193 fElementDepth++; 194 } 196 197 public void emptyElement(QName element, XMLAttributes attributes, Augmentations augs) 198 throws XNIException { 199 if (fElementDepth <= fRemovalElementDepth && handleOpenTag(element, attributes)) { 200 super.emptyElement(element, attributes, augs); 201 } 202 } 204 205 public void comment(XMLString text, Augmentations augs) 206 throws XNIException { 207 if (fElementDepth <= fRemovalElementDepth) { 208 super.comment(text, augs); 209 } 210 } 212 213 public void processingInstruction(String target, XMLString data, Augmentations augs) 214 throws XNIException { 215 if (fElementDepth <= fRemovalElementDepth) { 216 super.processingInstruction(target, data, augs); 217 } 218 } 220 221 public void characters(XMLString text, Augmentations augs) 222 throws XNIException { 223 if (fElementDepth <= fRemovalElementDepth) { 224 super.characters(text, augs); 225 } 226 } 228 229 public void ignorableWhitespace(XMLString text, Augmentations augs) 230 throws XNIException { 231 if (fElementDepth <= fRemovalElementDepth) { 232 super.ignorableWhitespace(text, augs); 233 } 234 } 236 237 public void startGeneralEntity(String name, XMLResourceIdentifier id, String encoding, Augmentations augs) 238 throws XNIException { 239 if (fElementDepth <= fRemovalElementDepth) { 240 super.startGeneralEntity(name, id, encoding, augs); 241 } 242 } 244 245 public void textDecl(String version, String encoding, Augmentations augs) 246 throws XNIException { 247 if (fElementDepth <= fRemovalElementDepth) { 248 super.textDecl(version, encoding, augs); 249 } 250 } 252 253 public void endGeneralEntity(String name, Augmentations augs) 254 throws XNIException { 255 if (fElementDepth <= fRemovalElementDepth) { 256 super.endGeneralEntity(name, augs); 257 } 258 } 260 261 public void startCDATA(Augmentations augs) throws XNIException { 262 if (fElementDepth <= fRemovalElementDepth) { 263 super.startCDATA(augs); 264 } 265 } 267 268 public void endCDATA(Augmentations augs) throws XNIException { 269 if (fElementDepth <= fRemovalElementDepth) { 270 super.endCDATA(augs); 271 } 272 } 274 275 public void endElement(QName element, Augmentations augs) 276 throws XNIException { 277 if (fElementDepth <= fRemovalElementDepth && elementAccepted(element.rawname)) { 278 super.endElement(element, augs); 279 } 280 fElementDepth--; 281 if (fElementDepth == fRemovalElementDepth) { 282 fRemovalElementDepth = Integer.MAX_VALUE; 283 } 284 } 286 287 public void endPrefixMapping(String prefix, Augmentations augs) 288 throws XNIException { 289 if (fElementDepth <= fRemovalElementDepth) { 290 super.endPrefixMapping(prefix, augs); 291 } 292 } 294 298 299 protected boolean elementAccepted(String element) { 300 Object key = element.toLowerCase(); 301 return fAcceptedElements.containsKey(key); 302 } 304 305 protected boolean elementRemoved(String element) { 306 Object key = element.toLowerCase(); 307 return fRemovedElements.containsKey(key); 308 } 310 311 protected boolean handleOpenTag(QName element, XMLAttributes attributes) { 312 if (elementAccepted(element.rawname)) { 313 Object key = element.rawname.toLowerCase(); 314 Object value = fAcceptedElements.get(key); 315 if (value != NULL) { 316 String [] anames = (String [])value; 317 int attributeCount = attributes.getLength(); 318 LOOP: for (int i = 0; i < attributeCount; i++) { 319 String aname = attributes.getQName(i).toLowerCase(); 320 for (int j = 0; j < anames.length; j++) { 321 if (anames[j].equals(aname)) { 322 continue LOOP; 323 } 324 } 325 attributes.removeAttributeAt(i--); 326 attributeCount--; 327 } 328 } 329 else { 330 attributes.removeAllAttributes(); 331 } 332 return true; 333 } 334 else if (elementRemoved(element.rawname)) { 335 fRemovalElementDepth = fElementDepth; 336 } 337 return false; 338 } 340 } | Popular Tags |