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.io.IOException;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import javax.el.ELException;
26  import javax.faces.FacesException;
27  import javax.faces.application.Resource;
28  import javax.faces.component.UIViewRoot;
29  import javax.faces.context.FacesContext;
30  import javax.faces.context.ResponseWriter;
31  
32  import org.apache.myfaces.view.ViewDeclarationLanguageStrategy;
33  import org.apache.myfaces.view.facelets.FaceletFactory;
34  import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage;
35  import org.apache.myfaces.view.facelets.compiler.Compiler;
36  
37  public class MockMyFacesFaceletViewDeclarationLanguage extends FaceletViewDeclarationLanguage
38  {
39      private String _renderedViewId;
40      private Map<Resource, Resource> _scriptComponentResources;
41  
42      public MockMyFacesFaceletViewDeclarationLanguage(FacesContext context)
43      {
44          super(context);
45      }
46  
47      public MockMyFacesFaceletViewDeclarationLanguage(FacesContext context,
48              ViewDeclarationLanguageStrategy strategy)
49      {
50          super(context, strategy);
51      }
52      
53      @Override
54      public void buildView(FacesContext context, UIViewRoot view)
55              throws IOException
56      {
57          _renderedViewId = null;
58          super.buildView(context, view);
59      }
60  
61      public void buildView(FacesContext context, UIViewRoot view, String xmlFile) throws IOException
62      {
63          _renderedViewId = xmlFile;
64          view.setViewId(xmlFile);
65          super.buildView(context, view);
66      }
67      
68      @Override
69      public String getRenderedViewId(FacesContext context, String actionId)
70      {
71          if (_renderedViewId != null)
72          {
73              return _renderedViewId;//super.getRenderedViewId(context, actionId);
74          }
75          else
76          {
77              return super.getRenderedViewId(context, actionId);
78          }
79      }    
80  
81      @Override
82      public String calculateViewId(FacesContext context, String viewId)
83      {
84          String calculatedViewId = super.calculateViewId(context, viewId);
85          if (calculatedViewId == null)
86          {
87              //can't calculate it, just passthrough the received one
88              calculatedViewId = viewId;
89          }
90          return calculatedViewId;
91      }
92  
93      @Override
94      public Compiler createCompiler(FacesContext context)
95      {
96          return super.createCompiler(context);
97      }
98  
99      @Override
100     public FaceletFactory createFaceletFactory(FacesContext context,
101             Compiler compiler)
102     {
103         return super.createFaceletFactory(context, compiler);
104     }
105 
106     @Override
107     public ResponseWriter createResponseWriter(FacesContext context)
108             throws IOException, FacesException
109     {
110         return super.createResponseWriter(context);
111     }
112 
113     @Override
114     public String getDefaultSuffix(FacesContext context)
115             throws FacesException
116     {
117         return super.getDefaultSuffix(context);
118     }
119 
120     @Override
121     public String getResponseContentType(FacesContext context, String orig)
122     {
123         return super.getResponseContentType(context, orig);
124     }
125 
126     @Override
127     public String getResponseEncoding(FacesContext context, String orig)
128     {
129         return super.getResponseEncoding(context, orig);
130     }
131 
132     @Override
133     public void handleFaceletNotFound(FacesContext context, String viewId)
134             throws FacesException, IOException
135     {
136         super.handleFaceletNotFound(context, viewId);
137     }
138 
139     @Override
140     public void handleRenderException(FacesContext context, Exception e)
141             throws IOException, ELException, FacesException
142     {
143         super.handleRenderException(context, e);
144     }
145 
146     @Override
147     public void initialize(FacesContext context)
148     {
149         super.initialize(context);
150     }
151 
152     @Override
153     public void loadDecorators(FacesContext context, Compiler compiler)
154     {
155         super.loadDecorators(context, compiler);
156     }
157 
158     @Override
159     public void loadLibraries(FacesContext context, Compiler compiler)
160     {
161         super.loadLibraries(context, compiler);
162     }
163 
164     @Override
165     public void loadOptions(FacesContext context, Compiler compiler)
166     {
167         super.loadOptions(context, compiler);
168     }
169 
170     @Override
171     public void sendSourceNotFound(FacesContext context, String message)
172     {
173         super.sendSourceNotFound(context, message);
174     }
175 
176     @Override
177     public Resource getScriptComponentResource(FacesContext context,
178             Resource componentResource)
179     {
180         if (_scriptComponentResources != null)
181         {
182             Resource installedResource = _scriptComponentResources.get(componentResource);
183             if (installedResource != null)
184             {
185                 // if we have a Resource installed for this componentResource, return it
186                 return installedResource;
187             }
188         }
189         return super.getScriptComponentResource(context, componentResource);
190     }
191     
192     /**
193      * This method sets the scriptResource for a given componentResource so that
194      * a call to getScriptComponentResource() with the given componentResource
195      * will return the installed scriptResource.
196      * @param componentResource
197      * @param scriptResource
198      */
199     public void setScriptComponentResource(Resource componentResource, Resource scriptResource)
200     {
201         if (_scriptComponentResources == null)
202         {
203             _scriptComponentResources = new HashMap<Resource, Resource>();
204         }
205         _scriptComponentResources.put(componentResource, scriptResource);
206     }
207 
208 }