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;
18  
19  import java.util.Iterator;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import org.apache.jetspeed.rewriter.rules.Attribute;
25  import org.apache.jetspeed.rewriter.rules.Rule;
26  import org.apache.jetspeed.rewriter.rules.Ruleset;
27  import org.apache.jetspeed.rewriter.rules.Tag;
28  
29  
30  /***
31   * RuleBasedRewriter
32   *
33   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
34   * @version $Id: RulesetRewriterImpl.java 517121 2007-03-12 07:45:49Z ate $
35   */
36  public class RulesetRewriterImpl extends BasicRewriter implements RulesetRewriter
37  {
38      protected final static Log log = LogFactory.getLog(RulesetRewriterImpl.class);
39      
40      private Ruleset ruleset = null;
41      private boolean removeComments = false;
42  
43      public boolean shouldStripTag(String tagid)
44      {        
45          if (null == ruleset)
46          {
47              return false;
48          }
49          
50          Tag tag = ruleset.getTag(tagid.toUpperCase());
51          if (null == tag)
52          {
53              return false;
54          }
55          return tag.getStrip();        
56      }
57              
58      /* (non-Javadoc)
59       * @see org.apache.jetspeed.cps.rewriter.Rewriter#shouldRemoveTag(java.lang.String)
60       */
61      public boolean shouldRemoveTag(String tagid)
62      {        
63          if (null == ruleset)
64          {
65              return false;
66          }
67          
68          Tag tag = ruleset.getTag(tagid.toUpperCase());
69          if (null == tag)
70          {
71              return false;
72          }
73          return tag.getRemove();
74      }
75  
76      /* (non-Javadoc)
77       * @see org.apache.jetspeed.cps.rewriter.RulesetRewriter#setRuleset(org.apache.jetspeed.cps.rewriter.rules.Ruleset)
78       */
79      public void setRuleset(Ruleset ruleset)
80      {
81          this.ruleset = ruleset;
82      }
83      
84      /* (non-Javadoc)
85       * @see org.apache.jetspeed.cps.rewriter.RulesetRewriter#getRuleset()
86       */
87      public Ruleset getRuleset()
88      {
89          return this.ruleset;
90      }
91  
92      /* (non-Javadoc)
93       * @see org.apache.jetspeed.cps.rewriter.Rewriter#shouldRemoveComments()
94       */
95      public boolean shouldRemoveComments()
96      {
97          if (null == ruleset)
98          {
99              return false;
100         }
101         
102         return ruleset.getRemoveComments();                
103     }
104 
105     /* (non-Javadoc)
106      * @see org.apache.jetspeed.syndication.services.crawler.rewriter.Rewriter#convertTagEvent(java.lang.String, org.xml.sax.Attributes)
107      */
108     public void enterConvertTagEvent(String tagid, MutableAttributes attributes)
109     {
110         if (null == ruleset)
111         {
112             return;
113         }
114         
115          Tag tag = ruleset.getTag(tagid.toUpperCase());
116         if (null == tag)
117         {
118              return;
119         }
120 
121         Iterator attribRules = tag.getAttributes().iterator();
122         while (attribRules.hasNext())
123         {
124             Attribute attribute = (Attribute)attribRules.next();
125             String name = attribute.getId();
126             String value = attributes.getValue(name);
127  
128             if (value != null) // && name.equalsIgnoreCase(attribute.getId()))
129             {
130                 Rule rule = attribute.getRule();
131                 if (null == rule)
132                 {
133                     continue;
134                 }
135                 
136                 if (!rule.shouldRewrite(value))
137                 {
138                     continue;
139                 }                                        
140                 
141                 String rewritten = rewriteUrl(value, tag.getId(), name, attributes);
142                 if (null != rewritten) // return null indicates "don't rewrite" 
143                 {
144                     if (rule.getSuffix() != null)
145                     {
146                         rewritten = rewritten.concat(rule.getSuffix());
147                     }
148                     
149                     attributes.addAttribute(name, rewritten);
150                                         
151                     if (rule.getPopup())
152                     {
153                         attributes.addAttribute("TARGET", "_BLANK");                        
154                     }
155                 }
156             }            
157         }
158     }
159     
160     /***
161      * rewriteURL
162      * 
163      * @param url
164      * @param tag
165      * @param attribute
166      * @param otherAttributes
167      * @return the modified url which is a portlet action
168      * 
169      * Rewrites all urls HREFS with a portlet action
170      */
171     public String rewriteUrl(String url, String tag, String attribute, MutableAttributes otherAttributes)
172     {
173         return getBaseRelativeUrl(url);
174     }
175 }