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.DefaultFilter;
26  
27  /***
28   * <p>
29   * URLRewriterFilter
30   * </p>
31   * <p>
32   * 
33   * </p>
34   * 
35   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
36   * @version $Id: URLRewriterFilter.java 517121 2007-03-12 07:45:49Z ate $
37   *  
38   */
39  public class URLRewriterFilter extends DefaultFilter
40  {
41      
42      private Rewriter rewriter; 
43      
44      
45      /***
46       *  
47       */
48      public URLRewriterFilter(Rewriter rewriter )
49      {
50          super();
51          this.rewriter = rewriter;
52      }
53          
54  
55      /***
56       * <p>
57       * startElement
58       * </p>
59       * 
60       * @see org.apache.xerces.xni.XMLDocumentHandler#startElement(org.apache.xerces.xni.QName,
61       *      org.apache.xerces.xni.XMLAttributes,
62       *      org.apache.xerces.xni.Augmentations)
63       * @param element
64       * @param attrs
65       * @param augs
66       * @throws org.apache.xerces.xni.XNIException
67       */
68      public void startElement( QName element, XMLAttributes attrs, Augmentations augs ) throws XNIException
69      {
70          if (false == rewriter.enterSimpleTagEvent(element.rawname, new XMLAttributesWrapper(attrs)))
71          {
72              doRewrite(element, attrs);
73              String appended = rewriter.exitSimpleTagEvent(element.rawname, new XMLAttributesWrapper(attrs));
74              if (null != appended)
75              {
76                //TODO: implement this!
77              }
78          }
79          
80          super.startElement(element, attrs, augs);
81      }
82  
83      /***
84       * <p>
85       * doRewrite
86       * </p>
87       *
88       * @param element
89       * @param attrs
90       */
91      protected void doRewrite( QName element, XMLAttributes attrs )
92      {
93          if (element.rawname.equals("A"))
94          {            
95              rewriteAttribute("href", attrs);
96          }
97          else if (element.rawname.equals("FORM"))
98          {            
99              rewriteAttribute("action", attrs);
100         }
101     }
102 
103     protected void rewriteAttribute( String attrName, XMLAttributes attributes )
104     {
105 
106         String uri = attributes.getValue(attrName);
107         
108         
109         if (uri != null)
110         {
111                // attributes.setValue(attributes.getIndex(attrName), urlGenerator.createUrl(uri));
112          
113         }
114 
115     }
116     /***
117      * <p>
118      * emptyElement
119      * </p>
120      *
121      * @see org.apache.xerces.xni.XMLDocumentHandler#emptyElement(org.apache.xerces.xni.QName, org.apache.xerces.xni.XMLAttributes, org.apache.xerces.xni.Augmentations)
122      * @param arg0
123      * @param arg1
124      * @param arg2
125      * @throws org.apache.xerces.xni.XNIException
126      */
127     public void emptyElement( QName element, XMLAttributes attrs, Augmentations arg2 ) throws XNIException
128     {
129         doRewrite(element, attrs);
130         super.emptyElement(element, attrs, arg2);
131     }
132     /***
133      * <p>
134      * comment
135      * </p>
136      *
137      * @see org.apache.xerces.xni.XMLDocumentHandler#comment(org.apache.xerces.xni.XMLString, org.apache.xerces.xni.Augmentations)
138      * @param comment
139      * @param augs
140      * @throws org.apache.xerces.xni.XNIException
141      */
142     public void comment( XMLString comment, Augmentations augs ) throws XNIException
143     {
144         if (!rewriter.shouldRemoveComments())
145         {
146             super.comment(comment, augs);                  
147         }
148         
149     }
150     /***
151      * <p>
152      * endElement
153      * </p>
154      *
155      * @see org.apache.xerces.xni.XMLDocumentHandler#endElement(org.apache.xerces.xni.QName, org.apache.xerces.xni.Augmentations)
156      * @param arg0
157      * @param arg1
158      * @throws org.apache.xerces.xni.XNIException
159      */
160     public void endElement( QName element, Augmentations augs ) throws XNIException
161     {
162         super.endElement(element, augs);
163     }
164     /***
165      * <p>
166      * characters
167      * </p>
168      *
169      * @see org.apache.xerces.xni.XMLDocumentHandler#characters(org.apache.xerces.xni.XMLString, org.apache.xerces.xni.Augmentations)
170      * @param arg0
171      * @param arg1
172      * @throws org.apache.xerces.xni.XNIException
173      */
174     public void characters( XMLString text, Augmentations arg1 ) throws XNIException
175     {        
176         if (!(text.ch[0] == '>') && ! rewriter.enterText(text.ch, text.offset))
177         {                            
178             super.characters(text, arg1);
179         }         
180     }
181 }