| 1 31 32 package org.opencms.util; 33 34 import org.opencms.main.CmsLog; 35 36 import java.util.Hashtable ; 37 import java.util.Set ; 38 import java.util.TreeSet ; 39 import java.util.Vector ; 40 41 import org.apache.commons.logging.Log; 42 43 import org.htmlparser.Attribute; 44 import org.htmlparser.Node; 45 import org.htmlparser.NodeFilter; 46 import org.htmlparser.PrototypicalNodeFactory; 47 import org.htmlparser.Tag; 48 import org.htmlparser.lexer.Page; 49 import org.htmlparser.scanners.Scanner; 50 import org.htmlparser.util.NodeList; 51 import org.htmlparser.util.ParserException; 52 import org.htmlparser.util.SimpleNodeIterator; 53 import org.htmlparser.visitors.NodeVisitor; 54 55 77 public final class CmsHtmlTagRemoveFactory extends PrototypicalNodeFactory { 78 79 99 private static final class CmsInvisibleTag implements Tag { 100 101 102 private static final long serialVersionUID = -3397880117291165819L; 103 104 105 private Tag m_decorated; 106 107 117 CmsInvisibleTag(Tag delegate) { 118 119 m_decorated = delegate; 120 } 121 122 125 public void accept(NodeVisitor visitor) { 126 127 NodeList children = m_decorated.getChildren(); 129 if (children == null) { 130 return; 131 } 132 SimpleNodeIterator itChildren = children.elements(); 133 while (itChildren.hasMoreNodes()) { 134 itChildren.nextNode().accept(visitor); 135 } 136 } 137 138 141 public boolean breaksFlow() { 142 143 return m_decorated.breaksFlow(); 144 } 145 146 149 public Object clone() throws CloneNotSupportedException { 150 151 return m_decorated.clone(); 152 } 153 154 158 public void collectInto(NodeList arg0, NodeFilter arg1) { 159 160 m_decorated.collectInto(arg0, arg1); 161 } 162 163 166 public void doSemanticAction() throws ParserException { 167 168 m_decorated.doSemanticAction(); 169 } 170 171 174 public String getAttribute(String arg0) { 175 176 return m_decorated.getAttribute(arg0); 177 } 178 179 182 public Attribute getAttributeEx(String arg0) { 183 184 return m_decorated.getAttributeEx(arg0); 185 } 186 187 191 public Hashtable getAttributes() { 192 193 return m_decorated.getAttributes(); 194 } 195 196 199 public Vector getAttributesEx() { 200 201 return m_decorated.getAttributesEx(); 202 } 203 204 207 public NodeList getChildren() { 208 209 return m_decorated.getChildren(); 210 } 211 212 215 public String [] getEnders() { 216 217 return m_decorated.getEnders(); 218 } 219 220 223 public int getEndingLineNumber() { 224 225 return m_decorated.getEndingLineNumber(); 226 } 227 228 231 public int getEndPosition() { 232 233 return m_decorated.getEndPosition(); 234 } 235 236 239 public Tag getEndTag() { 240 241 return m_decorated.getEndTag(); 242 } 243 244 247 public String [] getEndTagEnders() { 248 249 return m_decorated.getEndTagEnders(); 250 } 251 252 255 public String [] getIds() { 256 257 return m_decorated.getIds(); 258 } 259 260 263 public Page getPage() { 264 265 return m_decorated.getPage(); 266 } 267 268 271 public Node getParent() { 272 273 return m_decorated.getParent(); 274 } 275 276 279 public String getRawTagName() { 280 281 return m_decorated.getRawTagName(); 282 } 283 284 287 public int getStartingLineNumber() { 288 289 return m_decorated.getStartingLineNumber(); 290 } 291 292 295 public int getStartPosition() { 296 297 return m_decorated.getStartPosition(); 298 } 299 300 303 public String getTagName() { 304 305 return m_decorated.getTagName(); 306 } 307 308 311 public String getText() { 312 313 return m_decorated.getText(); 314 } 315 316 319 public Scanner getThisScanner() { 320 321 return m_decorated.getThisScanner(); 322 } 323 324 327 public boolean isEmptyXmlTag() { 328 329 return m_decorated.isEmptyXmlTag(); 330 } 331 332 335 public boolean isEndTag() { 336 337 return m_decorated.isEndTag(); 338 } 339 340 343 public void removeAttribute(String arg0) { 344 345 m_decorated.removeAttribute(arg0); 346 } 347 348 351 public void setAttribute(String arg0, String arg1) { 352 353 m_decorated.setAttribute(arg0, arg1); 354 } 355 356 359 public void setAttribute(String arg0, String arg1, char arg2) { 360 361 m_decorated.setAttribute(arg0, arg1, arg2); 362 } 363 364 367 public void setAttributeEx(Attribute arg0) { 368 369 m_decorated.setAttributeEx(arg0); 370 } 371 372 378 public void setAttributes(Hashtable arg0) { 379 380 m_decorated.setAttributes(arg0); 381 } 382 383 386 public void setAttributesEx(Vector arg0) { 387 388 m_decorated.setAttributesEx(arg0); 389 } 390 391 394 public void setChildren(NodeList arg0) { 395 396 m_decorated.setChildren(arg0); 397 } 398 399 402 public void setEmptyXmlTag(boolean arg0) { 403 404 m_decorated.setEmptyXmlTag(arg0); 405 } 406 407 410 public void setEndPosition(int arg0) { 411 412 m_decorated.setEndPosition(arg0); 413 } 414 415 418 public void setEndTag(Tag arg0) { 419 420 m_decorated.setEndTag(arg0); 421 } 422 423 426 public void setPage(Page arg0) { 427 428 m_decorated.setPage(arg0); 429 } 430 431 434 public void setParent(Node arg0) { 435 436 m_decorated.setParent(arg0); 437 } 438 439 442 public void setStartPosition(int arg0) { 443 444 m_decorated.setStartPosition(arg0); 445 } 446 447 450 public void setTagName(String arg0) { 451 452 m_decorated.setTagName(arg0); 453 } 454 455 458 public void setText(String arg0) { 459 460 m_decorated.setText(arg0); 461 } 462 463 466 public void setThisScanner(Scanner arg0) { 467 468 m_decorated.setThisScanner(arg0); 469 } 470 471 474 public String toHtml() { 475 476 return m_decorated.toHtml(); 477 } 478 479 482 public String toPlainTextString() { 483 484 return m_decorated.toPlainTextString(); 485 } 486 487 490 public String toString() { 491 492 return m_decorated.toString(); 493 } 494 495 } 496 497 498 private static final Log LOG = CmsLog.getLog(CmsHtmlTagRemoveFactory.class); 499 500 501 private static final long serialVersionUID = 6961158563666656633L; 502 503 504 private Set m_invisibleTags; 505 506 511 public CmsHtmlTagRemoveFactory() { 512 513 super(); 514 m_invisibleTags = new TreeSet (); 515 } 516 517 530 public boolean addTagRemoval(Tag tag) { 531 532 boolean result = false; 533 String tagName = tag.getTagName(); 534 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(tagName)) { 535 result = m_invisibleTags.add(tagName.toLowerCase()); 536 } 537 return result; 538 } 539 540 544 public Tag createTagNode(Page arg0, int arg1, int arg2, Vector arg3) { 545 546 try { 547 String tagName = ((Attribute)arg3.get(0)).getName().toLowerCase(); 548 if (tagName.charAt(0) == '/') { 550 tagName = tagName.substring(1); 551 } 552 Tag result = super.createTagNode(arg0, arg1, arg2, arg3); 553 if (m_invisibleTags.contains(tagName)) { 554 result = new CmsInvisibleTag(result); 555 } 556 return result; 557 } catch (RuntimeException rte) { 558 if (LOG.isErrorEnabled()) { 559 LOG.error(rte); 562 } 563 throw rte; 564 } 565 } 566 } | Popular Tags |