Coverage Report - org.apache.myfaces.mc.test.core.mock.MockMyFacesFaceletViewDeclarationLanguageStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
MockMyFacesFaceletViewDeclarationLanguageStrategy
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.mc.test.core.mock;
 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 Leonardo Uribe 
 32  
  * @since 2.0
 33  
  */
 34  0
 public class MockMyFacesFaceletViewDeclarationLanguageStrategy implements ViewDeclarationLanguageStrategy
 35  
 {
 36  
     private Pattern _acceptPatterns;
 37  
     private String _extension;
 38  
 
 39  
     private ViewDeclarationLanguage _language;
 40  
 
 41  
     public MockMyFacesFaceletViewDeclarationLanguageStrategy()
 42  0
     {
 43  0
         FacesContext context = FacesContext.getCurrentInstance();
 44  0
         ExternalContext eContext = context.getExternalContext();
 45  
 
 46  0
         _acceptPatterns = loadAcceptPattern(eContext);
 47  
 
 48  0
         _extension = loadFaceletExtension(eContext);
 49  
 
 50  0
         _language = new MockMyFacesFaceletViewDeclarationLanguage(context, this);
 51  0
     }
 52  
 
 53  
     /**
 54  
      * {@inheritDoc}
 55  
      */
 56  
     public ViewDeclarationLanguage getViewDeclarationLanguage()
 57  
     {
 58  0
         return _language;
 59  
     }
 60  
 
 61  
     /**
 62  
      * {@inheritDoc}
 63  
      */
 64  
     public boolean handles(String viewId)
 65  
     {
 66  0
         if (viewId == null)
 67  
         {
 68  0
             return false;
 69  
         }
 70  
         // Check extension first as it's faster than mappings
 71  0
         if (viewId.endsWith(_extension))
 72  
         {
 73  
             // If the extension matches, it's a Facelet viewId.
 74  0
             return true;
 75  
         }
 76  
 
 77  
         // Otherwise, try to match the view identifier with the facelet mappings
 78  0
         return _acceptPatterns != null && _acceptPatterns.matcher(viewId).matches();
 79  
     }
 80  
 
 81  
     /**
 82  
      * Load and compile a regular expression pattern built from the Facelet view mapping parameters.
 83  
      * 
 84  
      * @param context
 85  
      *            the application's external context
 86  
      * 
 87  
      * @return the compiled regular expression
 88  
      */
 89  
     private Pattern loadAcceptPattern(ExternalContext context)
 90  
     {
 91  0
         assert context != null;
 92  
 
 93  0
         String mappings = context.getInitParameter(ViewHandler.FACELETS_VIEW_MAPPINGS_PARAM_NAME);
 94  0
         if(mappings == null)    //consider alias facelets.VIEW_MAPPINGS
 95  
         {
 96  0
             mappings = context.getInitParameter("facelets.VIEW_MAPPINGS");
 97  
         }
 98  0
         if (mappings == null)
 99  
         {
 100  0
             return null;
 101  
         }
 102  
 
 103  
         // Make sure the mappings contain something
 104  0
         mappings = mappings.trim();
 105  0
         if (mappings.length() == 0)
 106  
         {
 107  0
             return null;
 108  
         }
 109  
 
 110  0
         return Pattern.compile(toRegex(mappings));
 111  
     }
 112  
 
 113  
     private String loadFaceletExtension(ExternalContext context)
 114  
     {
 115  0
         assert context != null;
 116  
 
 117  0
         String suffix = context.getInitParameter(ViewHandler.FACELETS_SUFFIX_PARAM_NAME);
 118  0
         if (suffix == null)
 119  
         {
 120  0
             suffix = ViewHandler.DEFAULT_FACELETS_SUFFIX;
 121  
         }
 122  
         else
 123  
         {
 124  0
             suffix = suffix.trim();
 125  0
             if (suffix.length() == 0)
 126  
             {
 127  0
                 suffix = ViewHandler.DEFAULT_FACELETS_SUFFIX;
 128  
             }
 129  
         }
 130  
 
 131  0
         return suffix;
 132  
     }
 133  
 
 134  
     /**
 135  
      * Convert the specified mapping string to an equivalent regular expression.
 136  
      * 
 137  
      * @param mappings
 138  
      *            le mapping string
 139  
      * 
 140  
      * @return an uncompiled regular expression representing the mappings
 141  
      */
 142  
     private String toRegex(String mappings)
 143  
     {
 144  0
         assert mappings != null;
 145  
 
 146  
         // Get rid of spaces
 147  0
         mappings = mappings.replaceAll("\\s", "");
 148  
 
 149  
         // Escape '.'
 150  0
         mappings = mappings.replaceAll("\\.", "\\\\.");
 151  
 
 152  
         // Change '*' to '.*' to represent any match
 153  0
         mappings = mappings.replaceAll("\\*", ".*");
 154  
 
 155  
         // Split the mappings by changing ';' to '|'
 156  0
         mappings = mappings.replaceAll(";", "|");
 157  
 
 158  0
         return mappings;
 159  
     }
 160  
 }