KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > util > CmsHtmlTagRemoveFactory


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/util/CmsHtmlTagRemoveFactory.java,v $
3  * Date : $Date: 2006/07/20 13:46:39 $
4  * Version: $Revision: 1.3 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2006 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.util;
33
34 import org.opencms.main.CmsLog;
35
36 import java.util.Hashtable JavaDoc;
37 import java.util.Set JavaDoc;
38 import java.util.TreeSet JavaDoc;
39 import java.util.Vector JavaDoc;
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 /**
56  *
57  * A tag factory for htmlparser that is able to "remove tags".
58  * <p>
59  *
60  * Create an instance, add the {@link org.htmlparser.Tag} instances to remove and assign this
61  * factory to the {@link org.htmlparser.Parser} before starting a visit. A demo usage is shown in
62  * {@link org.opencms.workplace.tools.content.CmsTagReplaceParser}.
63  * <p>
64  *
65  * The tags are not actually removed: They are linked in the document object model tree of the HTML
66  * that the parser generates. They just will not accept any {@link NodeVisitor} instances and
67  * therefore be invisible in any output a visitor will generate from the visited tree.
68  * <p>
69  *
70  * @author Achim Westermann
71  *
72  * @version $Revision: 1.3 $
73  *
74  * @since 6.1.8
75  *
76  */

77 public final class CmsHtmlTagRemoveFactory extends PrototypicalNodeFactory {
78
79     /**
80      *
81      * A Tag implementation that will not accept any {@link NodeVisitor} stopping by.
82      * <p>
83      *
84      * When visiting the corresponding tree of tags, this tag will be there but the visitor will not
85      * see it as it is not accepted. This allows "elimination" of this tag in the output the visitor
86      * generates from the document object model (e.g. HTML code again).
87      * <p>
88      *
89      * Potential child tags will be visible to visitors (unless they are instances of this class).
90      * <p>
91      *
92      * @author Achim Westermann
93      *
94      * @version $Revision: 1.3 $
95      *
96      * @since 6.1.8
97      *
98      */

99     private static final class CmsInvisibleTag implements Tag {
100
101         /** Generated serial version UID. */
102         private static final long serialVersionUID = -3397880117291165819L;
103
104         /** The real underlying tag. */
105         private Tag m_decorated;
106
107         /**
108          * Constructor with the delegate to wrap.
109          * <p>
110          *
111          * Every property is accessed transparently from the delegate, except that visitors are not
112          * welcome.
113          * <p>
114          *
115          * @param delegate the tag to hide.
116          */

117         CmsInvisibleTag(Tag delegate) {
118
119             m_decorated = delegate;
120         }
121
122         /**
123          * @see org.htmlparser.nodes.TagNode#accept(org.htmlparser.visitors.NodeVisitor)
124          */

125         public void accept(NodeVisitor visitor) {
126
127             // be invisible but show the children (if they like visits)
128
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         /**
139          * @see org.htmlparser.Tag#breaksFlow()
140          */

141         public boolean breaksFlow() {
142
143             return m_decorated.breaksFlow();
144         }
145
146         /**
147          * @see org.htmlparser.Node#clone()
148          */

149         public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
150
151             return m_decorated.clone();
152         }
153
154         /**
155          * @see org.htmlparser.Node#collectInto(org.htmlparser.util.NodeList,
156          * org.htmlparser.NodeFilter)
157          */

158         public void collectInto(NodeList arg0, NodeFilter arg1) {
159
160             m_decorated.collectInto(arg0, arg1);
161         }
162
163         /**
164          * @see org.htmlparser.Node#doSemanticAction()
165          */

166         public void doSemanticAction() throws ParserException {
167
168             m_decorated.doSemanticAction();
169         }
170
171         /**
172          * @see org.htmlparser.Tag#getAttribute(java.lang.String)
173          */

174         public String JavaDoc getAttribute(String JavaDoc arg0) {
175
176             return m_decorated.getAttribute(arg0);
177         }
178
179         /**
180          * @see org.htmlparser.Tag#getAttributeEx(java.lang.String)
181          */

182         public Attribute getAttributeEx(String JavaDoc arg0) {
183
184             return m_decorated.getAttributeEx(arg0);
185         }
186
187         /**
188          * @deprecated
189          * @see org.htmlparser.Tag#getAttributes()
190          */

191         public Hashtable JavaDoc getAttributes() {
192
193             return m_decorated.getAttributes();
194         }
195
196         /**
197          * @see org.htmlparser.Tag#getAttributesEx()
198          */

199         public Vector JavaDoc getAttributesEx() {
200
201             return m_decorated.getAttributesEx();
202         }
203
204         /**
205          * @see org.htmlparser.Node#getChildren()
206          */

207         public NodeList getChildren() {
208
209             return m_decorated.getChildren();
210         }
211
212         /**
213          * @see org.htmlparser.Tag#getEnders()
214          */

215         public String JavaDoc[] getEnders() {
216
217             return m_decorated.getEnders();
218         }
219
220         /**
221          * @see org.htmlparser.Tag#getEndingLineNumber()
222          */

223         public int getEndingLineNumber() {
224
225             return m_decorated.getEndingLineNumber();
226         }
227
228         /**
229          * @see org.htmlparser.Node#getEndPosition()
230          */

231         public int getEndPosition() {
232
233             return m_decorated.getEndPosition();
234         }
235
236         /**
237          * @see org.htmlparser.Tag#getEndTag()
238          */

239         public Tag getEndTag() {
240
241             return m_decorated.getEndTag();
242         }
243
244         /**
245          * @see org.htmlparser.Tag#getEndTagEnders()
246          */

247         public String JavaDoc[] getEndTagEnders() {
248
249             return m_decorated.getEndTagEnders();
250         }
251
252         /**
253          * @see org.htmlparser.Tag#getIds()
254          */

255         public String JavaDoc[] getIds() {
256
257             return m_decorated.getIds();
258         }
259
260         /**
261          * @see org.htmlparser.Node#getPage()
262          */

263         public Page getPage() {
264
265             return m_decorated.getPage();
266         }
267
268         /**
269          * @see org.htmlparser.Node#getParent()
270          */

271         public Node getParent() {
272
273             return m_decorated.getParent();
274         }
275
276         /**
277          * @see org.htmlparser.Tag#getRawTagName()
278          */

279         public String JavaDoc getRawTagName() {
280
281             return m_decorated.getRawTagName();
282         }
283
284         /**
285          * @see org.htmlparser.Tag#getStartingLineNumber()
286          */

287         public int getStartingLineNumber() {
288
289             return m_decorated.getStartingLineNumber();
290         }
291
292         /**
293          * @see org.htmlparser.Node#getStartPosition()
294          */

295         public int getStartPosition() {
296
297             return m_decorated.getStartPosition();
298         }
299
300         /**
301          * @see org.htmlparser.Tag#getTagName()
302          */

303         public String JavaDoc getTagName() {
304
305             return m_decorated.getTagName();
306         }
307
308         /**
309          * @see org.htmlparser.Node#getText()
310          */

311         public String JavaDoc getText() {
312
313             return m_decorated.getText();
314         }
315
316         /**
317          * @see org.htmlparser.Tag#getThisScanner()
318          */

319         public Scanner getThisScanner() {
320
321             return m_decorated.getThisScanner();
322         }
323
324         /**
325          * @see org.htmlparser.Tag#isEmptyXmlTag()
326          */

327         public boolean isEmptyXmlTag() {
328
329             return m_decorated.isEmptyXmlTag();
330         }
331
332         /**
333          * @see org.htmlparser.Tag#isEndTag()
334          */

335         public boolean isEndTag() {
336
337             return m_decorated.isEndTag();
338         }
339
340         /**
341          * @see org.htmlparser.Tag#removeAttribute(java.lang.String)
342          */

343         public void removeAttribute(String JavaDoc arg0) {
344
345             m_decorated.removeAttribute(arg0);
346         }
347
348         /**
349          * @see org.htmlparser.Tag#setAttribute(java.lang.String, java.lang.String)
350          */

351         public void setAttribute(String JavaDoc arg0, String JavaDoc arg1) {
352
353             m_decorated.setAttribute(arg0, arg1);
354         }
355
356         /**
357          * @see org.htmlparser.Tag#setAttribute(java.lang.String, java.lang.String, char)
358          */

359         public void setAttribute(String JavaDoc arg0, String JavaDoc arg1, char arg2) {
360
361             m_decorated.setAttribute(arg0, arg1, arg2);
362         }
363
364         /**
365          * @see org.htmlparser.Tag#setAttributeEx(org.htmlparser.Attribute)
366          */

367         public void setAttributeEx(Attribute arg0) {
368
369             m_decorated.setAttributeEx(arg0);
370         }
371
372         /**
373          *
374          * @deprecated
375          *
376          * @see org.htmlparser.Tag#setAttributes(java.util.Hashtable)
377          */

378         public void setAttributes(Hashtable JavaDoc arg0) {
379
380             m_decorated.setAttributes(arg0);
381         }
382
383         /**
384          * @see org.htmlparser.Tag#setAttributesEx(java.util.Vector)
385          */

386         public void setAttributesEx(Vector JavaDoc arg0) {
387
388             m_decorated.setAttributesEx(arg0);
389         }
390
391         /**
392          * @see org.htmlparser.Node#setChildren(org.htmlparser.util.NodeList)
393          */

394         public void setChildren(NodeList arg0) {
395
396             m_decorated.setChildren(arg0);
397         }
398
399         /**
400          * @see org.htmlparser.Tag#setEmptyXmlTag(boolean)
401          */

402         public void setEmptyXmlTag(boolean arg0) {
403
404             m_decorated.setEmptyXmlTag(arg0);
405         }
406
407         /**
408          * @see org.htmlparser.Node#setEndPosition(int)
409          */

410         public void setEndPosition(int arg0) {
411
412             m_decorated.setEndPosition(arg0);
413         }
414
415         /**
416          * @see org.htmlparser.Tag#setEndTag(org.htmlparser.Tag)
417          */

418         public void setEndTag(Tag arg0) {
419
420             m_decorated.setEndTag(arg0);
421         }
422
423         /**
424          * @see org.htmlparser.Node#setPage(org.htmlparser.lexer.Page)
425          */

426         public void setPage(Page arg0) {
427
428             m_decorated.setPage(arg0);
429         }
430
431         /**
432          * @see org.htmlparser.Node#setParent(org.htmlparser.Node)
433          */

434         public void setParent(Node arg0) {
435
436             m_decorated.setParent(arg0);
437         }
438
439         /**
440          * @see org.htmlparser.Node#setStartPosition(int)
441          */

442         public void setStartPosition(int arg0) {
443
444             m_decorated.setStartPosition(arg0);
445         }
446
447         /**
448          * @see org.htmlparser.Tag#setTagName(java.lang.String)
449          */

450         public void setTagName(String JavaDoc arg0) {
451
452             m_decorated.setTagName(arg0);
453         }
454
455         /**
456          * @see org.htmlparser.Node#setText(java.lang.String)
457          */

458         public void setText(String JavaDoc arg0) {
459
460             m_decorated.setText(arg0);
461         }
462
463         /**
464          * @see org.htmlparser.Tag#setThisScanner(org.htmlparser.scanners.Scanner)
465          */

466         public void setThisScanner(Scanner arg0) {
467
468             m_decorated.setThisScanner(arg0);
469         }
470
471         /**
472          * @see org.htmlparser.Node#toHtml()
473          */

474         public String JavaDoc toHtml() {
475
476             return m_decorated.toHtml();
477         }
478
479         /**
480          * @see org.htmlparser.Node#toPlainTextString()
481          */

482         public String JavaDoc toPlainTextString() {
483
484             return m_decorated.toPlainTextString();
485         }
486
487         /**
488          * @see org.htmlparser.Node#toString()
489          */

490         public String JavaDoc toString() {
491
492             return m_decorated.toString();
493         }
494
495     }
496
497     /** The log object for this class. */
498     private static final Log LOG = CmsLog.getLog(CmsHtmlTagRemoveFactory.class);
499
500     /** Generated serial version UID. */
501     private static final long serialVersionUID = 6961158563666656633L;
502
503     /** The tags to hide from the node visitors. */
504     private Set JavaDoc m_invisibleTags;
505
506     /**
507      * Create a new factory with all tags registered.
508      * <p>
509      *
510      */

511     public CmsHtmlTagRemoveFactory() {
512
513         super();
514         m_invisibleTags = new TreeSet JavaDoc();
515     }
516
517     /**
518      * Add a tag that will be invisible for {@link NodeVisitor} instances.
519      * <p>
520      *
521      * Not only "this" tag will be invisible but all parsed Tags that have the same name (case
522      * insensitive).
523      * <p>
524      *
525      * @param tag the tag that will be invisible for all {@link NodeVisitor} instances.
526      *
527      * @return true if the tag was added to the internal set of tags to remove, false if not (was
528      * contained before, has no name,...).
529      */

530     public boolean addTagRemoval(Tag tag) {
531
532         boolean result = false;
533         String JavaDoc tagName = tag.getTagName();
534         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(tagName)) {
535             result = m_invisibleTags.add(tagName.toLowerCase());
536         }
537         return result;
538     }
539
540     /**
541      * @see org.htmlparser.PrototypicalNodeFactory#createTagNode(org.htmlparser.lexer.Page, int,
542      * int, java.util.Vector)
543      */

544     public Tag createTagNode(Page arg0, int arg1, int arg2, Vector JavaDoc arg3) {
545
546         try {
547             String JavaDoc tagName = ((Attribute)arg3.get(0)).getName().toLowerCase();
548             // end tags have names like "/a"....
549
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 JavaDoc rte) {
558             if (LOG.isErrorEnabled()) {
559                 // log here, as htmlparser 1.5 did swallow exceptions from here and threw NPEs from
560
// other places
561
LOG.error(rte);
562             }
563             throw rte;
564         }
565     }
566 }
Popular Tags