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