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.el;
20  
21  import java.beans.FeatureDescriptor;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.Iterator;
26  import java.util.NoSuchElementException;
27  
28  import javax.el.ELContext;
29  import javax.el.ELResolver;
30  
31  /**
32   * @author Mathias Broekelmann (latest modification by $Author: bommel $)
33   * @version $Revision: 1187700 $ $Date: 2011-10-22 07:19:37 -0500 (Sat, 22 Oct 2011) $
34   */
35  public class CompositeELResolver extends javax.el.CompositeELResolver
36  {
37      private Collection<ELResolver> _elResolvers;
38  
39      @Override
40      public Iterator<FeatureDescriptor> getFeatureDescriptors(final ELContext context, final Object base)
41      {
42          Collection<ELResolver> resolvers = _elResolvers;
43          if (resolvers == null)
44          {
45              resolvers = Collections.emptyList();
46          }
47          
48          return new CompositeIterator(context, base, resolvers.iterator());
49      }
50  
51      /**
52       * @param elResolver
53       */
54      @Override
55      public final synchronized void add(final ELResolver elResolver)
56      {
57          super.add(elResolver);
58  
59          if (_elResolvers == null)
60          {
61              _elResolvers = new ArrayList<ELResolver>();
62          }
63  
64          _elResolvers.add(elResolver);
65      }
66  
67      private static class CompositeIterator implements Iterator<FeatureDescriptor>
68      {
69          private final ELContext _context;
70          private final Object _base;
71          private final Iterator<ELResolver> _elResolvers;
72  
73          private FeatureDescriptor _nextFD;
74  
75          private Iterator<FeatureDescriptor> _currentFDIter;
76  
77          public CompositeIterator(final ELContext context, final Object base, final Iterator<ELResolver> elResolvers)
78          {
79              _context = context;
80              _base = base;
81              _elResolvers = elResolvers;
82          }
83  
84          public boolean hasNext()
85          {
86              if (_nextFD != null)
87                  return true;
88              if (_currentFDIter != null)
89              {
90                  while (_nextFD == null && _currentFDIter.hasNext())
91                  {
92                      _nextFD = _currentFDIter.next();
93                  }
94              }
95              if (_nextFD == null)
96              {
97                  if (_elResolvers.hasNext())
98                  {
99                      _currentFDIter = _elResolvers.next().getFeatureDescriptors(_context, _base);
100                 }
101                 else
102                 {
103                     return false;
104                 }
105             }
106             return hasNext();
107         }
108 
109         public FeatureDescriptor next()
110         {
111             if (!hasNext())
112                 throw new NoSuchElementException();
113             FeatureDescriptor next = _nextFD;
114             _nextFD = null;
115             return next;
116         }
117 
118         public void remove()
119         {
120             throw new UnsupportedOperationException();
121         }
122 
123     }
124 }