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.shared.resource;
21  
22  import java.util.Deque;
23  import java.util.Iterator;
24  import java.util.LinkedList;
25  import java.util.Set;
26  import javax.faces.application.ResourceVisitOption;
27  import javax.faces.context.FacesContext;
28  
29  /**
30   *
31   */
32  public class ExternalContextResourceLoaderIterator implements Iterator<String>
33  {
34      private FacesContext facesContext;
35      private String basePath;
36      private int maxDepth;
37      private ResourceVisitOption[] options;
38      private Deque<String> stack = new LinkedList<String>();
39  
40      public ExternalContextResourceLoaderIterator(FacesContext facesContext, String path,
41               int maxDepth, ResourceVisitOption... options)
42      {
43          this.facesContext = facesContext;
44          this.basePath = path;
45          this.maxDepth = maxDepth;
46          this.options = options;
47  
48          Set<String> paths = facesContext.getExternalContext().getResourcePaths(basePath);
49  
50          if (paths == null)
51          {
52              //empty stack means empty iterator.
53          }
54          else
55          {
56              for (String p : paths)
57              {
58                  if (p.startsWith("/WEB-INF") && isTopLevelViewsOnly(options))
59                  {
60                      // skip
61                  }
62                  else if (p.startsWith("/META-INF") && isTopLevelViewsOnly(options))
63                  {
64                      // skip 
65                  }
66                  else
67                  {
68                      stack.add(p);
69                  }
70              }
71          }
72      }
73  
74      private boolean isTopLevelViewsOnly(ResourceVisitOption... options) 
75      {
76          boolean isTopLevelViewsOnly = false;
77          
78          for (ResourceVisitOption option : options) 
79          {
80              if(option == ResourceVisitOption.TOP_LEVEL_VIEWS_ONLY) 
81              {
82                  isTopLevelViewsOnly = true;
83                  break;
84              }
85          }
86          
87          return isTopLevelViewsOnly;
88      }
89  
90      @Override
91      public boolean hasNext()
92      {
93          if (!stack.isEmpty())
94          {
95              String path = stack.peek();
96              do 
97              {
98                  if (ResourceLoaderUtils.isDirectory(path))
99                  {
100                     path = stack.pop();
101                     int depth = ResourceLoaderUtils.getDepth(path);
102                     if (depth < maxDepth)
103                     {
104                         Set<String> list = facesContext.getExternalContext().getResourcePaths(path);
105                         if (list != null)
106                         {
107                             stack.addAll(list);
108                         }
109                     }
110                     if (!stack.isEmpty())
111                     {
112                         path = stack.peek();
113                     }
114                     else
115                     {
116                         path = null;
117                     }
118                 }
119             }
120             while (path != null && ResourceLoaderUtils.isDirectory(path) && !stack.isEmpty());
121 
122             return !stack.isEmpty();
123         }
124         return false;
125     }
126 
127     @Override
128     public String next()
129     {
130         if (!stack.isEmpty())
131         {
132             String path = stack.pop();
133             do 
134             {
135                 if (ResourceLoaderUtils.isDirectory(path))
136                 {
137                     int depth = ResourceLoaderUtils.getDepth(path);
138                     if (depth < maxDepth)
139                     {
140                         Set<String> list = facesContext.getExternalContext().getResourcePaths(path);
141                         if (list != null)
142                         {
143                             stack.addAll(list);
144                         }
145                     }
146                     if (!stack.isEmpty())
147                     {
148                         path = stack.pop();
149                     }
150                     else
151                     {
152                         path = null;
153                     }
154                 }
155             }
156             while (path != null && ResourceLoaderUtils.isDirectory(path) && !stack.isEmpty());
157             if (path != null)
158             {
159                 // Calculate name based on url, basePath.
160                 return path;
161             }
162         }
163         return null;
164     }
165 
166     @Override
167     public void remove()
168     {
169     }
170 }
171