View Javadoc

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;
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  public class MockMyFacesFaceletViewDeclarationLanguageStrategy implements ViewDeclarationLanguageStrategy
35  {
36      private Pattern _acceptPatterns;
37      private String _extension;
38  
39      private ViewDeclarationLanguage _language;
40  
41      public MockMyFacesFaceletViewDeclarationLanguageStrategy()
42      {
43          FacesContext context = FacesContext.getCurrentInstance();
44          ExternalContext eContext = context.getExternalContext();
45  
46          _acceptPatterns = loadAcceptPattern(eContext);
47  
48          _extension = loadFaceletExtension(eContext);
49  
50          _language = new MockMyFacesFaceletViewDeclarationLanguage(context, this);
51      }
52  
53      /**
54       * {@inheritDoc}
55       */
56      public ViewDeclarationLanguage getViewDeclarationLanguage()
57      {
58          return _language;
59      }
60  
61      /**
62       * {@inheritDoc}
63       */
64      public boolean handles(String viewId)
65      {
66          if (viewId == null)
67          {
68              return false;
69          }
70          // Check extension first as it's faster than mappings
71          if (viewId.endsWith(_extension))
72          {
73              // If the extension matches, it's a Facelet viewId.
74              return true;
75          }
76  
77          // Otherwise, try to match the view identifier with the facelet mappings
78          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          assert context != null;
92  
93          String mappings = context.getInitParameter(ViewHandler.FACELETS_VIEW_MAPPINGS_PARAM_NAME);
94          if(mappings == null)    //consider alias facelets.VIEW_MAPPINGS
95          {
96              mappings = context.getInitParameter("facelets.VIEW_MAPPINGS");
97          }
98          if (mappings == null)
99          {
100             return null;
101         }
102 
103         // Make sure the mappings contain something
104         mappings = mappings.trim();
105         if (mappings.length() == 0)
106         {
107             return null;
108         }
109 
110         return Pattern.compile(toRegex(mappings));
111     }
112 
113     private String loadFaceletExtension(ExternalContext context)
114     {
115         assert context != null;
116 
117         String suffix = context.getInitParameter(ViewHandler.FACELETS_SUFFIX_PARAM_NAME);
118         if (suffix == null)
119         {
120             suffix = ViewHandler.DEFAULT_FACELETS_SUFFIX;
121         }
122         else
123         {
124             suffix = suffix.trim();
125             if (suffix.length() == 0)
126             {
127                 suffix = ViewHandler.DEFAULT_FACELETS_SUFFIX;
128             }
129         }
130 
131         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         assert mappings != null;
145 
146         // Get rid of spaces
147         mappings = mappings.replaceAll("\\s", "");
148 
149         // Escape '.'
150         mappings = mappings.replaceAll("\\.", "\\\\.");
151 
152         // Change '*' to '.*' to represent any match
153         mappings = mappings.replaceAll("\\*", ".*");
154 
155         // Split the mappings by changing ';' to '|'
156         mappings = mappings.replaceAll(";", "|");
157 
158         return mappings;
159     }
160 }