1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
167 return null;
168 }
169 }
170 else
171 {
172 return null;
173 }
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189 }
190 }