Coverage report

  %line %branch
net.sf.infrared.aspects.jsp.JspContext
82% 
88% 

 1  
 /* 
 2  
  * Copyright 2005 Tavant Technologies and Contributors
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License")
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  * 
 16  
  *
 17  
  *
 18  
  * Original Author:  binil.thomas (Tavant Technologies)
 19  
  * Contributor(s):   -;
 20  
  *
 21  
  */
 22  
 package net.sf.infrared.aspects.jsp;
 23  
 
 24  
 import java.io.File;
 25  
 import java.util.Properties;
 26  
 import java.util.regex.Matcher;
 27  
 import java.util.regex.Pattern;
 28  
 
 29  
 import net.sf.infrared.aspects.AbstractExecutionContext;
 30  
 
 31  
 public class JspContext extends AbstractExecutionContext {
 32  1
     private static Properties cache = new Properties();
 33  
     
 34  
     private static final String JASPER_PREFIX = "org.apache.jsp.";
 35  
     
 36  
     private static final String WEBLOGIC_PREFIX = "jsp_servlet._";
 37  
     
 38  
     private String jspName;
 39  
     
 40  
     // private String jspClassName;
 41  
     
 42  
     
 43  
     
 44  
     public JspContext(String jspClassName) {
 45  1
         super("Jsp");
 46  1
         if (jspClassName.startsWith(JASPER_PREFIX)) {
 47  0
             jspName = JspContext.getJasperJspName(jspClassName);
 48  1
         } else if (jspClassName.startsWith(WEBLOGIC_PREFIX)) {
 49  0
             jspName = JspContext.getWeblogicJspName(jspClassName);
 50  
         } else {
 51  1
             jspName = jspClassName;
 52  
         }        
 53  1
     }
 54  
     
 55  
     public JspContext(Class jspClass) {
 56  0
         this(jspClass.getName());
 57  0
     }
 58  
     
 59  
     public JspContext(String name, String layer) {
 60  1
         super(layer);
 61  
         
 62  1
         this.jspName = name;
 63  
         // this.jspClassName = name;
 64  1
     }
 65  
     
 66  
     public String getName() {
 67  0
         if (jspName == null) {                                            
 68  
             // @TODO make it work for other JSP engines
 69  
             // @TODO consider making this a strategy
 70  
             
 71  
         }
 72  0
         return jspName;
 73  
     }
 74  
     
 75  
     public String toString() {
 76  0
         return "Jsp " + getName();
 77  
     }
 78  
     
 79  
     public boolean equals(Object o) {
 80  1
         if (o == null) {
 81  0
             return false;
 82  
         }
 83  
         
 84  1
         if (o == this) {
 85  0
             return true;
 86  
         }
 87  
         
 88  1
         if (! (o instanceof JspContext) ) {
 89  0
             return false;
 90  
         }
 91  
         
 92  1
         JspContext other = (JspContext) o;
 93  
         
 94  1
         return other.jspName.equals( this.jspName );
 95  
     }
 96  
     
 97  
     public int hashCode() {
 98  0
         return 7 * jspName.hashCode();
 99  
     }
 100  
     
 101  
     //@TODO uses Java 1.4 APIs; if someone needs to use InfraRED with 1.3, we can rewrite these
 102  
     // portions with Jakarta Regexp package
 103  
     
 104  
     static String getJasperJspName(String jspClassName) {
 105  5
         String jspName = (String) cache.get(jspClassName);
 106  5
         if (jspName != null) {
 107  0
             return jspName;
 108  
         }
 109  5
         jspName = jspClassName.substring(JASPER_PREFIX.length());
 110  
         
 111  
         // jasper replaces a directory seperator with a "."
 112  5
         jspName = jspName.replace('.', '/');
 113  
         
 114  
         // jasper replaces special characters with a mangle character,
 115  
         // which is '_' followed by the hexadecimal value of the character
 116  5
         Pattern pattern = Pattern.compile("_[a-f0-9]{4}");
 117  5
         Matcher matcher = pattern.matcher(jspName);
 118  5
         int findFrom = 0;
 119  12
         while (matcher.find(findFrom)) {          
 120  7
             findFrom = matcher.start() + 1;
 121  7
             String mangledSpecialChar = matcher.group();
 122  7
             mangledSpecialChar = mangledSpecialChar.substring(1, mangledSpecialChar.length());
 123  7
             String unmangedSpecialChar = JspContext.getUnmangedSpecialChar(mangledSpecialChar);            
 124  7
             jspName = jspName.replaceAll("_" + mangledSpecialChar, unmangedSpecialChar);            
 125  7
             matcher = pattern.matcher(jspName);
 126  
         }
 127  
         
 128  
         // jasper replaces '.' with a '_'
 129  5
         jspName = jspName.substring(0, jspName.length() - 4);
 130  5
         jspName = jspName + ".jsp";
 131  5
         cache.put(jspClassName, jspName);
 132  5
         return jspName;
 133  
     }
 134  
     
 135  
     static String getWeblogicJspName(String jspClassName) {
 136  3
         String jspName = (String) cache.get(jspClassName);
 137  3
         if (jspName != null) {
 138  0
             return jspName;
 139  
         }
 140  
         
 141  3
         jspName = jspClassName.substring(WEBLOGIC_PREFIX.length());
 142  
         
 143  
         // weblogic replaces a directory seperator with a "._"        
 144  3
         jspName = jspName.replaceAll("\\._", "/");
 145  
         
 146  
         // weblogic replaces every special character with _[xxx]_ where xxx is the integer
 147  
         // value of the special character
 148  3
         Pattern pattern = Pattern.compile("_[0-9]+_");
 149  3
         Matcher matcher = pattern.matcher(jspName);
 150  7
         while (matcher.find()) {
 151  4
             String specialCharMangle = matcher.group();
 152  4
             specialCharMangle = specialCharMangle.substring(1, specialCharMangle.length() - 1);
 153  4
             int ch = Integer.parseInt(specialCharMangle);            
 154  4
             jspName = jspName.replaceAll("_" + specialCharMangle + "_", String.valueOf((char) ch));
 155  4
             matcher = pattern.matcher(jspName);
 156  
         }
 157  
         
 158  
         // weblogic adds leading _ to file & directory names
 159  3
         jspName = jspName.replaceAll("/_", "/");
 160  3
         jspName = jspName.replaceAll("^_", "");
 161  3
         jspName = jspName + ".jsp";
 162  
         
 163  3
         cache.put(jspClassName, jspName);
 164  3
         return jspName;
 165  
     }
 166  
     
 167  
     // @TODO most likely there is some better way to figure this out using shift operators
 168  
     // fix this later!
 169  
     static String getUnmangedSpecialChar(String mangled) {                        
 170  7
         char ch = mangled.class="keyword">charAt(0);
 171  7
         int hex0 = Character.digit(ch, 16);
 172  7
         ch = mangled.charAt(1);
 173  7
         int hex1 = Character.digit(ch, 16);
 174  7
         ch = mangled.charAt(2);
 175  7
         int hex2 = Character.digit(ch, 16);
 176  7
         ch = mangled.charAt(3);
 177  7
         int hex3 = Character.digit(ch, 16);
 178  7
         int sum = (4096 * hex0) + (256 * hex1) + (16 * hex2) + hex3; 
 179  
         
 180  7
         return new String(class="keyword">new char[] {(char) sum});
 181  
     }
 182  
 }

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