View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.rewriter.html.neko;
18  
19  import org.apache.jetspeed.rewriter.Rewriter;
20  import org.apache.xerces.xni.Augmentations;
21  import org.apache.xerces.xni.QName;
22  import org.apache.xerces.xni.XMLAttributes;
23  import org.apache.xerces.xni.XMLString;
24  import org.apache.xerces.xni.XNIException;
25  import org.cyberneko.html.filters.ElementRemover;
26  
27  /***
28   * <p>
29   * CallbackElementRemover
30   * </p>
31   * <p>
32   *  Extended version of the NekoHTML ElementRemover which provides
33   *  tag stripping/removal based on Rewriter settings.
34   * </p>
35   * 
36   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
37   * @version $Id: CallbackElementRemover.java 517719 2007-03-13 15:05:48Z ate $
38   *  
39   */
40  public class CallbackElementRemover extends ElementRemover
41  {
42  
43      private Rewriter rewriter;
44  
45      /***
46       * Construct with reference to the rewriter context to consult for rewriting advice
47       */
48      public CallbackElementRemover( Rewriter rewriter )
49      {
50          super();
51          
52          this.rewriter = rewriter;
53      }
54      
55      
56      // Base Class Protocol
57      
58      /***
59       * <p>
60       * comment
61       * </p>
62       * 
63       * @see org.apache.xerces.xni.XMLDocumentHandler#comment(org.apache.xerces.xni.XMLString text, org.apache.xerces.xni.Augmentations augs)
64       * @param text
65       * @param augs
66       * @throws org.apache.xerces.xni.XNIException
67       */
68      public void comment(XMLString text,Augmentations augs) throws XNIException
69      {
70          if (rewriter.shouldRemoveComments())
71              return;
72          super.comment(text,augs);
73      }
74  
75      /***
76       * <p>
77       * emptyElement
78       * </p>
79       * 
80       * @see org.apache.xerces.xni.XMLDocumentHandler#emptyElement(org.apache.xerces.xni.QName,
81       *      org.apache.xerces.xni.XMLAttributes,
82       *      org.apache.xerces.xni.Augmentations)
83       * @param element
84       * @param arg1
85       * @param arg2
86       * @throws org.apache.xerces.xni.XNIException
87       */
88      public void emptyElement( QName element, XMLAttributes attrs, Augmentations arg2 ) throws XNIException
89      {
90          processTag(element,attrs) ;
91          super.emptyElement(element, attrs, arg2);
92      }
93  
94      /***
95       * <p>
96       * startElement
97       * </p>
98       * 
99       * @see org.apache.xerces.xni.XMLDocumentHandler#startElement(org.apache.xerces.xni.QName,
100      *      org.apache.xerces.xni.XMLAttributes,
101      *      org.apache.xerces.xni.Augmentations)
102      * @param element
103      * @param arg1
104      * @param arg2
105      * @throws org.apache.xerces.xni.XNIException
106      */
107     public void startElement( QName element, XMLAttributes attrs, Augmentations arg2 ) throws XNIException
108     {
109         processTag(element,attrs);
110         super.startElement(element, attrs, arg2);
111     }
112     
113     
114     // Support Methods
115 
116     /***
117      * <p>
118      * processTag
119      * </p>
120      * 
121      * @param tag
122      */
123     protected void processTag(QName element, XMLAttributes attrs)
124     {
125         String tag = element.rawname.toLowerCase();
126         if (fRemovedElements.contains(tag))
127         {
128             // alread removed
129             return ;
130         }
131         else if (rewriter.shouldStripTag(tag))
132         {
133             // first time for this tag...
134             // strip - remove tag and any text associated with it
135             removeElement(tag);
136             return ;
137         }
138         else if (rewriter.shouldRemoveTag(tag))
139         {
140             // BOZO - block intentially left EMPTY
141             
142             // first time for this tag...
143             // remove - no directive necessary, the default behavior of ElementRemover is to drop tags that it does not know about (but the assocated text will remain)
144             return ;
145         }
146 
147         // OTHERWISE - explicitly accept (keep tag and associated text)
148         // NOTE: even if fAcceptedElements contains the tag already, we need to reset the attribute names for this invocation context
149         rewriter.enterConvertTagEvent(tag,new XMLAttributesWrapper(attrs));
150         acceptElement(tag,getAttributeNames(attrs));
151     }
152     protected String[] getAttributeNames(XMLAttributes attrs)
153     {
154         int length = attrs != null ? attrs.getLength() : 0 ;
155         String[] names = length > 0 ? new String[ length ] : null ;
156         
157         for( int i = 0, limit = length;i<limit;i++)
158         {
159             names[i] = attrs.getQName(i) ;
160         }
161         return names ;
162     }
163 }