01: /*
02: * Copyright 2004-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: *
16: */
17:
18: package org.jpublish.page.servletcontext;
19:
20: import org.apache.commons.logging.Log;
21: import org.apache.commons.logging.LogFactory;
22: import org.jpublish.JPublishRuntimeException;
23: import org.jpublish.util.PathUtilities;
24:
25: import java.util.Iterator;
26:
27: /** Iterator which iterates through the Page objects known to the specified
28: PageManager. This implementation wraps an iterator which returns paths
29: and loads the Page object for each path. The wrapped Iterator must return
30: Strings.
31:
32: @author Anthony Eden
33: @since 2.0
34: */
35:
36: public class ServletContextPageIterator implements Iterator {
37:
38: private static final Log log = LogFactory
39: .getLog(ServletContextPageIterator.class);
40:
41: private ServletContextPageManager pageManager;
42: private Iterator iter;
43:
44: /** Construct a new ServletContextPageIterator.
45:
46: @param pageManager The PageManager
47: @param iter The Iterator to wrap
48: */
49:
50: public ServletContextPageIterator(
51: ServletContextPageManager pageManager, Iterator iter) {
52: this .pageManager = pageManager;
53: this .iter = iter;
54: }
55:
56: /** Returns true if the iteration has more elements. (In other words,
57: returns true if next would return an element rather than throwing
58: an exception.)
59:
60: @return True if the iteration has more elements
61: */
62:
63: public boolean hasNext() {
64: return iter.hasNext();
65: }
66:
67: /** Returns the next element in the iteration.
68:
69: @return The next element in the iteration
70: */
71:
72: public Object next() {
73: String root = PathUtilities.toResourcePath(pageManager
74: .getRoot());
75: String path = (String) iter.next();
76: String relativePath = path.substring(root.length());
77:
78: try {
79: return pageManager.getPage(relativePath);
80: } catch (Exception e) {
81: log.error("Error getting page: " + relativePath);
82: throw new JPublishRuntimeException(e.getMessage(), e);
83: }
84: }
85:
86: /** Removes from the underlying collection the last element returned by
87: the iterator (optional operation). This method can be called only
88: once per call to next. The behavior of an iterator is unspecified
89: if the underlying collection is modified while the iteration is in
90: progress in any way other than by calling this method.
91:
92: @throws UnsupportedOperationException
93: */
94:
95: public void remove() {
96: throw new UnsupportedOperationException();
97: }
98:
99: }
|