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  
20  package org.apache.myfaces.application;
21  
22  import java.util.Deque;
23  import java.util.Iterator;
24  import java.util.LinkedList;
25  import java.util.List;
26  import java.util.Set;
27  import javax.faces.application.ResourceVisitOption;
28  import javax.faces.context.FacesContext;
29  import org.apache.myfaces.shared.resource.ContractResourceLoader;
30  import org.apache.myfaces.shared.resource.ResourceHandlerSupport;
31  import org.apache.myfaces.shared.resource.ResourceLoader;
32  
33  /**
34   *
35   * @author lu4242
36   */
37  public class ViewResourceIterator implements Iterator<String>
38  {
39      private ResourceHandlerSupport support;
40      private Deque<ResourceLoader> stack = new LinkedList<ResourceLoader>();
41      private Deque<String> basePathStack = new LinkedList<String>();
42      private Iterator<String> currentIterator = null;
43      private String origBasePath;
44      private int maxDepth;
45      private ResourceVisitOption[] options;
46      private FacesContext facesContext;
47  
48      /**
49       * register a set 
50       */
51      private Set<String> pathSet;
52  
53      public ViewResourceIterator(FacesContext facesContext, ResourceHandlerSupport support, 
54              String localePrefix, List<String> contracts, String contractPreferred, 
55              String path, int maxDepth, ResourceVisitOption... options) 
56      {
57          this.support = support;
58          this.origBasePath = path;
59          this.maxDepth = maxDepth;
60          this.options = options;
61          this.facesContext = facesContext;
62  
63          String basePath = this.origBasePath.endsWith("/") ? this.origBasePath : this.origBasePath+"/";
64          
65          if (contractPreferred != null)
66          {
67              for (ContractResourceLoader loader : support.getContractResourceLoaders())
68              {
69                  if (localePrefix != null)
70                  {
71                      stack.add(loader);
72                      basePathStack.add(basePath+localePrefix+"/"+contractPreferred);
73                  }
74                  stack.add(loader);
75                  basePathStack.add(basePath+"/"+contractPreferred);
76              }
77          }
78          if (!contracts.isEmpty())
79          {
80              for (ContractResourceLoader loader : support.getContractResourceLoaders())
81              {
82                  for (String contract : contracts)
83                  {
84                      if (localePrefix != null)
85                      {
86                          stack.add(loader);
87                          basePathStack.add(basePath+localePrefix+"/"+contract);
88                      }
89                      stack.add(loader);
90                      basePathStack.add(basePath+contract);
91                  }
92              }
93          }
94          
95          for (ResourceLoader loader : support.getViewResourceLoaders())
96          {
97              if (localePrefix != null)
98              {
99                  stack.add(loader);
100                 basePathStack.add(basePath+localePrefix);
101             }
102             stack.add(loader);
103             basePathStack.add(basePath);
104         }
105     }
106 
107     @Override
108     public boolean hasNext()
109     {
110         boolean next = false;
111         do 
112         {
113             if (currentIterator == null)
114             {
115                 ResourceLoader loader = null;
116                 String basePath = null;
117                 do
118                 {
119                     do
120                     {
121                         loader = (ResourceLoader) stack.pop();
122                         basePath = basePathStack.pop();
123                         
124                         if (loader != null)
125                         {
126                             currentIterator = loader.iterator(facesContext, basePath, maxDepth, options);
127                         }
128                     } 
129                     while (currentIterator == null && !stack.isEmpty());
130                     if (currentIterator != null)
131                     {
132                         boolean hasNext = currentIterator.hasNext();
133                         if (!hasNext)
134                         {
135                             currentIterator = null;
136                         }
137                     }
138                 }
139                 while (currentIterator == null && !stack.isEmpty());
140                 if (currentIterator == null)
141                 {
142                     return false;
143                 }
144             }
145             next = currentIterator.hasNext();
146             if (!next && !stack.isEmpty())
147             {
148                 currentIterator = null;
149             }
150         }
151         while (currentIterator == null);
152         return next;
153     }
154 
155     @Override
156     public String next()
157     {
158         if (hasNext())
159         {
160             if (currentIterator != null)
161             {
162                 return currentIterator.next();
163             }
164             else
165             {
166                 //Should not happen, return null
167                 return null;
168             }
169         }
170         else
171         {
172             return null;
173         }
174         /*
175         if (currentIterator == null)
176         {
177             ResourceLoader loader = (ResourceLoader) stack.pop();
178             if (loader != null)
179             {
180                 currentIterator = loader.iterator(facesContext, basePath, maxDepth, options);
181             }
182             else
183             {
184                 return null;
185             }
186         }
187         return currentIterator.next();
188         */
189     }
190 }