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;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.List;
24  
25  import javax.faces.view.facelets.FaceletHandler;
26  
27  /**
28   * This class was created to gather some code from latest Facelets not existing in latest JSF 2.0 spec.
29   * Also, since it was created on the fly while converting, it's highly possible that methods in this class
30   * should be moved in a more logical location and/or removed.
31   *
32   * @author Simon Lessard (latest modification by $Author$)
33   * @version $Revision$ $Date$
34   *
35   * @since 2.0
36   */
37  public final class TagHandlerUtils
38  {
39  
40      /**
41       * Find the first occurence of a tag handler that is instanceof T
42       *
43       * @since 2.0.1
44       * @param <T>
45       * @param nextHandler
46       * @param type
47       * @return
48       */
49      public static <T> T findFirstNextByType(FaceletHandler nextHandler, Class<T> type)
50      {
51          if (type.isAssignableFrom(nextHandler.getClass()))
52          {
53              return (T)nextHandler;
54          }
55          else if (nextHandler instanceof javax.faces.view.facelets.CompositeFaceletHandler)
56          {
57              for (FaceletHandler handler :
58                      ((javax.faces.view.facelets.CompositeFaceletHandler)nextHandler).getHandlers())
59              {
60                  if (type.isAssignableFrom(handler.getClass()))
61                  {
62                      return (T)handler;
63                  }
64              }
65          }
66          return null;
67      }
68  
69      /**
70       * From TagHandler:
71       * protected final <T> Iterator<T> findNextByType(Class<T> type)
72       *
73       * @param type
74       * @return
75       */
76      @SuppressWarnings("unchecked")
77      public static <T> Collection<T> findNextByType(FaceletHandler nextHandler, Class<T> type)
78      {
79          List<T> found = new ArrayList<T>();
80          if (type.isAssignableFrom(nextHandler.getClass()))
81          {
82              found.add((T)nextHandler);
83          }
84          else if (nextHandler instanceof javax.faces.view.facelets.CompositeFaceletHandler)
85          {
86              for (FaceletHandler handler :
87                      ((javax.faces.view.facelets.CompositeFaceletHandler)nextHandler).getHandlers())
88              {
89                  if (type.isAssignableFrom(handler.getClass()))
90                  {
91                      found.add((T)handler);
92                  }
93              }
94          }
95  
96          return found;
97      }
98  
99      private TagHandlerUtils()
100     {
101 
102     }
103 
104     public static  Collection<FaceletHandler> findNextByType(FaceletHandler nextHandler, Class<?> ... type1)
105     {
106         List<FaceletHandler> found = new ArrayList<FaceletHandler>();
107         boolean isAssignable = false;
108         for (int i = 0; i < type1.length && !isAssignable; i++)
109         {
110             isAssignable = type1[i].isAssignableFrom(nextHandler.getClass());
111         }
112         if (isAssignable)
113         {
114             found.add((FaceletHandler)nextHandler);
115         }
116         else if (nextHandler instanceof javax.faces.view.facelets.CompositeFaceletHandler)
117         {
118             for (FaceletHandler handler :
119                     ((javax.faces.view.facelets.CompositeFaceletHandler)nextHandler).getHandlers())
120             {
121                 isAssignable = false;
122                 for (int i = 0; i < type1.length && !isAssignable; i++)
123                 {
124                     isAssignable = type1[i].isAssignableFrom(handler.getClass());
125                 }
126                 if (isAssignable)
127                 {
128                     found.add((FaceletHandler)handler);
129                 }
130             }
131         }
132 
133         return found;
134     }
135 
136     public static  Collection<FaceletHandler> findNextByType(FaceletHandler nextHandler, Class<?> type1, Class<?> type2)
137     {
138         List<FaceletHandler> found = new ArrayList<FaceletHandler>();
139         if (type1.isAssignableFrom(nextHandler.getClass()) || type2.isAssignableFrom(nextHandler.getClass()))
140         {
141             found.add((FaceletHandler)nextHandler);
142         }
143         else if (nextHandler instanceof javax.faces.view.facelets.CompositeFaceletHandler)
144         {
145             for (FaceletHandler handler :
146                     ((javax.faces.view.facelets.CompositeFaceletHandler)nextHandler).getHandlers())
147             {
148                 if (type1.isAssignableFrom(handler.getClass()) || type2.isAssignableFrom(handler.getClass()))
149                 {
150                     found.add((FaceletHandler)handler);
151                 }
152             }
153         }
154         
155         return found;
156     }
157 }