Coverage Report - org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguageStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
FaceletViewDeclarationLanguageStrategy
0%
0/38
0%
0/30
3
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.myfaces.view.facelets;
 20  
 
 21  
 import java.util.regex.Pattern;
 22  
 
 23  
 import javax.faces.application.ViewHandler;
 24  
 import javax.faces.context.ExternalContext;
 25  
 import javax.faces.context.FacesContext;
 26  
 import javax.faces.view.ViewDeclarationLanguage;
 27  
 
 28  
 import org.apache.myfaces.view.ViewDeclarationLanguageStrategy;
 29  
 
 30  
 /**
 31  
  * @author Simon Lessard (latest modification by $Author$)
 32  
  * @version $Revision$ $Date$
 33  
  * 
 34  
  * @since 2.0
 35  
  */
 36  0
 public class FaceletViewDeclarationLanguageStrategy implements ViewDeclarationLanguageStrategy
 37  
 {
 38  
     private Pattern _acceptPatterns;
 39  
     private String _extension;
 40  
 
 41  
     private ViewDeclarationLanguage _language;
 42  
 
 43  
     public FaceletViewDeclarationLanguageStrategy()
 44  0
     {
 45  0
         FacesContext context = FacesContext.getCurrentInstance();
 46  0
         ExternalContext eContext = context.getExternalContext();
 47  
 
 48  0
         _acceptPatterns = loadAcceptPattern(eContext);
 49  
 
 50  0
         _extension = loadFaceletExtension(eContext);
 51  
 
 52  0
         _language = new FaceletViewDeclarationLanguage(context, this);
 53  0
     }
 54  
 
 55  
     /**
 56  
      * {@inheritDoc}
 57  
      */
 58  
     public ViewDeclarationLanguage getViewDeclarationLanguage()
 59  
     {
 60  0
         return _language;
 61  
     }
 62  
 
 63  
     /**
 64  
      * {@inheritDoc}
 65  
      */
 66  
     public boolean handles(String viewId)
 67  
     {
 68  0
         if (viewId == null)
 69  
         {
 70  0
             return false;
 71  
         }
 72  
         // Check extension first as it's faster than mappings
 73  0
         if (viewId.endsWith(_extension))
 74  
         {
 75  
             // If the extension matches, it's a Facelet viewId.
 76  0
             return true;
 77  
         }
 78  
 
 79  
         // Otherwise, try to match the view identifier with the facelet mappings
 80  0
         return _acceptPatterns != null && _acceptPatterns.matcher(viewId).matches();
 81  
     }
 82  
 
 83  
     /**
 84  
      * Load and compile a regular expression pattern built from the Facelet view mapping parameters.
 85  
      * 
 86  
      * @param context
 87  
      *            the application's external context
 88  
      * 
 89  
      * @return the compiled regular expression
 90  
      */
 91  
     private Pattern loadAcceptPattern(ExternalContext context)
 92  
     {
 93  0
         assert context != null;
 94  
 
 95  0
         String mappings = context.getInitParameter(ViewHandler.FACELETS_VIEW_MAPPINGS_PARAM_NAME);
 96  0
         if(mappings == null)    //consider alias facelets.VIEW_MAPPINGS
 97  
         {
 98  0
             mappings = context.getInitParameter("facelets.VIEW_MAPPINGS");
 99  
         }
 100  0
         if (mappings == null)
 101  
         {
 102  0
             return null;
 103  
         }
 104  
 
 105  
         // Make sure the mappings contain something
 106  0
         mappings = mappings.trim();
 107  0
         if (mappings.length() == 0)
 108  
         {
 109  0
             return null;
 110  
         }
 111  
 
 112  0
         return Pattern.compile(toRegex(mappings));
 113  
     }
 114  
 
 115  
     private String loadFaceletExtension(ExternalContext context)
 116  
     {
 117  0
         assert context != null;
 118  
 
 119  0
         String suffix = context.getInitParameter(ViewHandler.FACELETS_SUFFIX_PARAM_NAME);
 120  0
         if (suffix == null)
 121  
         {
 122  0
             suffix = ViewHandler.DEFAULT_FACELETS_SUFFIX;
 123  
         }
 124  
         else
 125  
         {
 126  0
             suffix = suffix.trim();
 127  0
             if (suffix.length() == 0)
 128  
             {
 129  0
                 suffix = ViewHandler.DEFAULT_FACELETS_SUFFIX;
 130  
             }
 131  
         }
 132  
 
 133  0
         return suffix;
 134  
     }
 135  
 
 136  
     /**
 137  
      * Convert the specified mapping string to an equivalent regular expression.
 138  
      * 
 139  
      * @param mappings
 140  
      *            le mapping string
 141  
      * 
 142  
      * @return an uncompiled regular expression representing the mappings
 143  
      */
 144  
     private String toRegex(String mappings)
 145  
     {
 146  0
         assert mappings != null;
 147  
 
 148  
         // Get rid of spaces
 149  0
         mappings = mappings.replaceAll("\\s", "");
 150  
 
 151  
         // Escape '.'
 152  0
         mappings = mappings.replaceAll("\\.", "\\\\.");
 153  
 
 154  
         // Change '*' to '.*' to represent any match
 155  0
         mappings = mappings.replaceAll("\\*", ".*");
 156  
 
 157  
         // Split the mappings by changing ';' to '|'
 158  0
         mappings = mappings.replaceAll(";", "|");
 159  
 
 160  0
         return mappings;
 161  
     }
 162  
 }