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: struberg $)
33   * @version $Revision: 1188235 $ $Date: 2011-10-24 12:09:33 -0500 (Mon, 24 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              {
88                  return true;
89              }
90              if (_currentFDIter != null)
91              {
92                  while (_nextFD == null && _currentFDIter.hasNext())
93                  {
94                      _nextFD = _currentFDIter.next();
95                  }
96              }
97              if (_nextFD == null)
98              {
99                  if (_elResolvers.hasNext())
100                 {
101                     _currentFDIter = _elResolvers.next().getFeatureDescriptors(_context, _base);
102                 }
103                 else
104                 {
105                     return false;
106                 }
107             }
108             return hasNext();
109         }
110 
111         public FeatureDescriptor next()
112         {
113             if (!hasNext())
114             {
115                 throw new NoSuchElementException();
116             }
117             FeatureDescriptor next = _nextFD;
118             _nextFD = null;
119             return next;
120         }
121 
122         public void remove()
123         {
124             throw new UnsupportedOperationException();
125         }
126 
127     }
128 }