001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017:
018: package org.jpublish.page.xml;
019:
020: import java.io.FileNotFoundException;
021: import java.util.List;
022: import java.util.Iterator;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: import org.jpublish.JPublishRuntimeException;
028:
029: /** Iterator which iterates through the Page objects known to the specified
030: PageManager.
031:
032: @author Anthony Eden
033: @since 1.1
034: */
035:
036: public class XMLPageIterator implements Iterator {
037:
038: private static final Log log = LogFactory
039: .getLog(XMLPageIterator.class);
040:
041: private XMLPageManager pageManager;
042: private Iterator paths;
043: private String basePath;
044:
045: /** Construct a new XMLPageIterator.
046:
047: @param pageManager The XMLPageManager
048: @param paths The list of paths to iterate through
049: @param basePath The required base path
050: */
051:
052: public XMLPageIterator(XMLPageManager pageManager, List paths,
053: String basePath) {
054: this .pageManager = pageManager;
055: this .paths = paths.iterator();
056: this .basePath = basePath;
057: }
058:
059: /** Returns true if the iteration has more elements. (In other words,
060: returns true if next would return an element rather than throwing
061: an exception.)
062:
063: @return True if the iteration has more elements
064: */
065:
066: public boolean hasNext() {
067: return paths.hasNext();
068: }
069:
070: /** Returns the next element in the iteration.
071:
072: @return The next element in the iteration
073: */
074:
075: public Object next() {
076: String path = (String) paths.next();
077: try {
078: return pageManager.getPage(path);
079: } catch (FileNotFoundException e) {
080: log.warn("FileNotFoundException while retrieving path: "
081: + path);
082: return next();
083: } catch (Exception e) {
084: throw new JPublishRuntimeException("Error loading page: "
085: + e.getMessage(), e);
086: }
087: }
088:
089: /** Removes from the underlying collection the last element returned by
090: the iterator (optional operation). This method can be called only
091: once per call to next. The behavior of an iterator is unspecified
092: if the underlying collection is modified while the iteration is in
093: progress in any way other than by calling this method.
094:
095: @throws UnsupportedOperationException
096: */
097:
098: public void remove() {
099: throw new UnsupportedOperationException();
100: }
101:
102: }
|