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