Coverage report

  %line %branch
org.apache.jetspeed.rewriter.RulesetRewriterImpl
0% 
0% 

 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  0
 public class RulesetRewriterImpl extends BasicRewriter implements RulesetRewriter
 37  
 {
 38  0
     protected final static Log log = LogFactory.getLog(RulesetRewriterImpl.class);
 39  
     
 40  0
     private Ruleset ruleset = null;
 41  0
     private boolean removeComments = false;
 42  
 
 43  
     public boolean shouldStripTag(String tagid)
 44  
     {        
 45  0
         if (null == ruleset)
 46  
         {
 47  0
             return false;
 48  
         }
 49  
         
 50  0
         Tag tag = ruleset.getTag(tagid.toUpperCase());
 51  0
         if (null == tag)
 52  
         {
 53  0
             return false;
 54  
         }
 55  0
         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  0
         if (null == ruleset)
 64  
         {
 65  0
             return false;
 66  
         }
 67  
         
 68  0
         Tag tag = ruleset.getTag(tagid.toUpperCase());
 69  0
         if (null == tag)
 70  
         {
 71  0
             return false;
 72  
         }
 73  0
         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  0
         this.ruleset = ruleset;
 82  0
     }
 83  
     
 84  
     /* (non-Javadoc)
 85  
      * @see org.apache.jetspeed.cps.rewriter.RulesetRewriter#getRuleset()
 86  
      */
 87  
     public Ruleset getRuleset()
 88  
     {
 89  0
         return this.ruleset;
 90  
     }
 91  
 
 92  
     /* (non-Javadoc)
 93  
      * @see org.apache.jetspeed.cps.rewriter.Rewriter#shouldRemoveComments()
 94  
      */
 95  
     public boolean shouldRemoveComments()
 96  
     {
 97  0
         if (null == ruleset)
 98  
         {
 99  0
             return false;
 100  
         }
 101  
         
 102  0
         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  0
         if (null == ruleset)
 111  
         {
 112  0
             return;
 113  
         }
 114  
         
 115  0
          Tag tag = ruleset.getTag(tagid.toUpperCase());
 116  0
         if (null == tag)
 117  
         {
 118  0
              return;
 119  
         }
 120  
 
 121  0
         Iterator attribRules = tag.getAttributes().iterator();
 122  0
         while (attribRules.hasNext())
 123  
         {
 124  0
             Attribute attribute = (Attribute)attribRules.next();
 125  0
             String name = attribute.getId();
 126  0
             String value = attributes.getValue(name);
 127  
  
 128  0
             if (value != null) // && name.equalsIgnoreCase(attribute.getId()))
 129  
             {
 130  0
                 Rule rule = attribute.getRule();
 131  0
                 if (null == rule)
 132  
                 {
 133  0
                     continue;
 134  
                 }
 135  
                 
 136  0
                 if (!rule.shouldRewrite(value))
 137  
                 {
 138  0
                     continue;
 139  
                 }                                        
 140  
                 
 141  0
                 String rewritten = rewriteUrl(value, tag.getId(), name, attributes);
 142  0
                 if (null != rewritten) // return null indicates "don't rewrite" 
 143  
                 {
 144  0
                     if (rule.getSuffix() != null)
 145  
                     {
 146  0
                         rewritten = rewritten.concat(rule.getSuffix());
 147  
                     }
 148  
                     
 149  0
                     attributes.addAttribute(name, rewritten);
 150  
                                         
 151  0
                     if (rule.getPopup())
 152  
                     {
 153  0
                         attributes.addAttribute("TARGET", "_BLANK");                        
 154  
                     }
 155  
                 }
 156  
             }            
 157  0
         }
 158  0
     }
 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  0
         return getBaseRelativeUrl(url);
 174  
     }
 175  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.