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 javax.faces.view;
20  
21  import java.beans.BeanInfo;
22  import java.io.IOException;
23  import java.net.MalformedURLException;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.logging.Level;
27  import java.util.logging.Logger;
28  import java.util.stream.Stream;
29  
30  import javax.faces.application.Resource;
31  import javax.faces.application.ResourceVisitOption;
32  import javax.faces.application.ViewVisitOption;
33  import javax.faces.component.UIComponent;
34  import javax.faces.component.UIViewRoot;
35  import javax.faces.context.FacesContext;
36  
37  /**
38   * @since 2.0
39   */
40  public abstract class ViewDeclarationLanguage
41  {
42      /**
43       * @since 2.1
44       */
45      public static final String JSP_VIEW_DECLARATION_LANGUAGE_ID = "java.faces.JSP";
46  
47      /**
48       * @since 2.1
49       */
50      public static final String FACELETS_VIEW_DECLARATION_LANGUAGE_ID = "java.faces.Facelets";
51      
52      public abstract void buildView(FacesContext context, UIViewRoot view) throws IOException;
53  
54      public abstract UIViewRoot createView(FacesContext context, String viewId);
55  
56      public abstract BeanInfo getComponentMetadata(FacesContext context, Resource componentResource);
57  
58      public abstract Resource getScriptComponentResource(FacesContext context, Resource componentResource);
59      
60      public abstract StateManagementStrategy getStateManagementStrategy(FacesContext context, String viewId); 
61  
62      public abstract ViewMetadata getViewMetadata(FacesContext context, String viewId);
63  
64      public abstract void renderView(FacesContext context, UIViewRoot view) throws IOException;
65  
66      public abstract UIViewRoot restoreView(FacesContext context, String viewId);
67      
68      public void retargetAttachedObjects(FacesContext context, UIComponent topLevelComponent,
69                                          List<AttachedObjectHandler> handlers)
70      {
71          throw new UnsupportedOperationException(); 
72      }
73  
74      public void retargetMethodExpressions(FacesContext context, UIComponent topLevelComponent)
75      {
76          throw new UnsupportedOperationException(); 
77      }
78      
79      /**
80       * 
81       * @since 2.1
82       * @return
83       */
84      public String getId()
85      {
86          return this.getClass().getName();
87      }
88      
89      /**
90       * 
91       * @since 2.1
92       * @param facesContext
93       * @param viewId
94       * @return
95       */
96      public boolean viewExists(FacesContext facesContext, String viewId)
97      {
98          try
99          {
100             return facesContext.getExternalContext().getResource(viewId) != null;
101         }
102         catch (MalformedURLException e)
103         {
104             Logger log = Logger.getLogger(ViewDeclarationLanguage.class.getName());
105             if (log.isLoggable(Level.SEVERE))
106             {
107                 log.log(Level.SEVERE, "Malformed URL viewId: "+viewId, e);
108             }
109         }
110         return false;
111     }
112     
113     /**
114      * @since 2.2
115      * @param context
116      * @param taglibURI
117      * @param tagName
118      * @param attributes
119      * @return 
120      */
121     public UIComponent createComponent(FacesContext context,
122                                    String taglibURI,
123                                    String tagName,
124                                    Map<String,Object> attributes)
125     {
126         return null;
127     }
128     
129     /**
130      * @since 2.2
131      * @param context
132      * @param viewId
133      * @return 
134      */
135     public List<String> calculateResourceLibraryContracts(FacesContext context,
136                                                       String viewId)
137     {
138         return null;
139     }
140     
141     /**
142      * @since 2.3
143      * @param facesContext
144      * @param path
145      * @param options
146      * @return 
147      */
148     public Stream<java.lang.String> getViews(FacesContext facesContext, String path, ViewVisitOption... options)
149     {
150         return getViews(facesContext, path, Integer.MAX_VALUE, options);
151     }
152     
153     
154     /**
155      * 
156      * @since 2.3
157      * @param facesContext
158      * @param path
159      * @param maxDepth
160      * @param options
161      * @return 
162      */
163     public Stream<java.lang.String> getViews(FacesContext facesContext, String path, 
164             int maxDepth, ViewVisitOption... options)
165     {
166         // Here by default we follow what spec javadoc says
167         // "...This method works as if invoking it were equivalent to evaluating the expression:
168         //     getViewResources(facesContext, start, Integer.MAX_VALUE, options) ..."
169         // The problem here is ViewVisitOption != ResourceVisitOption. But whatever return
170         // getViews must always have TOP_LEVEL_VIEWS_ONLY, because otherwise it will return 
171         // everything (css, js, ...). There is ViewVisitOption.RETURN_AS_MINIMAL_IMPLICIT_OUTCOME,
172         // but this is a filter on top of the stream.
173         
174         return facesContext.getApplication().getResourceHandler().getViewResources(
175                 facesContext, path, maxDepth, ResourceVisitOption.TOP_LEVEL_VIEWS_ONLY);
176     }
177 }