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.tag.composite;
20  
21  import java.lang.reflect.Method;
22  import java.net.URL;
23  
24  import javax.faces.FacesException;
25  import javax.faces.application.Resource;
26  import javax.faces.application.ResourceHandler;
27  import javax.faces.context.FacesContext;
28  import javax.faces.view.facelets.ComponentConfig;
29  import javax.faces.view.facelets.FaceletHandler;
30  import javax.faces.view.facelets.Tag;
31  import javax.faces.view.facelets.TagConfig;
32  import javax.faces.view.facelets.TagHandler;
33  
34  import org.apache.myfaces.view.facelets.tag.TagLibrary;
35  
36  /**
37   * This class create composite component tag handlers for "http://java.sun.com/jsf/composite/"
38   * namespace. Note that the class that create composite component tag handlers using its own 
39   * namespace defined in facelet taglib .xml file see TagLibraryConfig.TagLibraryImpl
40   * 
41   * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
42   * @version $Revision: 1300254 $ $Date: 2012-03-13 12:38:40 -0500 (Tue, 13 Mar 2012) $
43   */
44  public class CompositeResourceLibrary implements TagLibrary
45  {
46      public final static String NAMESPACE_PREFIX = "http://java.sun.com/jsf/composite/";
47      
48      private final ResourceHandler _resourceHandler;
49      
50      public CompositeResourceLibrary(FacesContext facesContext)
51      {
52          _resourceHandler = facesContext.getApplication().getResourceHandler();
53      }
54      
55      public boolean containsFunction(String ns, String name)
56      {
57          // Composite component tag library does not suport functions
58          return false;
59      }
60  
61      public boolean containsNamespace(String ns)
62      {
63          if (ns != null && ns.startsWith(NAMESPACE_PREFIX))
64          {
65              if (ns.length() > NAMESPACE_PREFIX.length())
66              {
67                  String libraryName = ns.substring(NAMESPACE_PREFIX.length());
68                  return _resourceHandler.libraryExists(libraryName);
69              }
70          }        
71          return false;
72      }
73  
74      public boolean containsTagHandler(String ns, String localName)
75      {
76          if (ns != null && ns.startsWith(NAMESPACE_PREFIX))
77          {
78              if (ns.length() > NAMESPACE_PREFIX.length())
79              {
80                  String libraryName = ns.substring(NAMESPACE_PREFIX.length());
81                  String resourceName = localName + ".xhtml";
82                  Resource compositeComponentResource = _resourceHandler.createResource(resourceName, libraryName);
83                  if (compositeComponentResource != null)
84                  {
85                      URL url = compositeComponentResource.getURL();
86                      return (url != null);
87                  }
88              }
89          }
90          return false;
91      }
92  
93      public Method createFunction(String ns, String name)
94      {
95          // Composite component tag library does not suport functions
96          return null;
97      }
98  
99      public TagHandler createTagHandler(String ns, String localName,
100             TagConfig tag) throws FacesException
101     {
102         if (ns != null && ns.startsWith(NAMESPACE_PREFIX))
103         {
104             if (ns.length() > NAMESPACE_PREFIX.length())
105             {
106                 String libraryName = ns.substring(NAMESPACE_PREFIX.length());
107                 String resourceName = localName + ".xhtml";
108                 // MYFACES-3308 If a composite component exists, it requires to 
109                 // be always resolved. In other words, it should always exists a default.
110                 // The call here for resourceHandler.createResource, just try to get
111                 // the Resource and if it does not exists, it just returns null.
112                 // The intention of this code is just create an instance and pass to
113                 // CompositeComponentResourceTagHandler. Then, its values 
114                 // (resourceName, libraryName) will be used to derive the real instance
115                 // to use in a view, based on the locale used.
116                 Resource compositeComponentResource = new CompositeResouceWrapper(
117                     _resourceHandler.createResource(resourceName, libraryName));
118                 if (compositeComponentResource != null)
119                 {
120                     ComponentConfig componentConfig = new ComponentConfigWrapper(tag,
121                             "javax.faces.NamingContainer", null);
122                     
123                     return new CompositeComponentResourceTagHandler(componentConfig, compositeComponentResource);
124                 }
125             }
126         }
127         return null;
128     }
129 
130     private static class ComponentConfigWrapper implements ComponentConfig
131     {
132 
133         protected final TagConfig parent;
134 
135         protected final String componentType;
136 
137         protected final String rendererType;
138 
139         public ComponentConfigWrapper(TagConfig parent, String componentType,
140                                       String rendererType)
141         {
142             this.parent = parent;
143             this.componentType = componentType;
144             this.rendererType = rendererType;
145         }
146 
147         public String getComponentType()
148         {
149             return this.componentType;
150         }
151 
152         public String getRendererType()
153         {
154             return this.rendererType;
155         }
156 
157         public FaceletHandler getNextHandler()
158         {
159             return this.parent.getNextHandler();
160         }
161 
162         public Tag getTag()
163         {
164             return this.parent.getTag();
165         }
166 
167         public String getTagId()
168         {
169             return this.parent.getTagId();
170         }
171     }
172 }