001: /*
002: * MeshCMS - A simple CMS based on SiteMesh
003: * Copyright (C) 2004-2007 Luciano Vernaschi
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: *
019: * You can contact the author at http://www.cromoteca.com
020: * and at info@cromoteca.com
021: */
022:
023: package org.meshcms.core;
024:
025: import java.util.*;
026: import org.meshcms.util.*;
027:
028: /**
029: * Iterator for the site map.
030: */
031: public class SiteMapIterator implements Iterator {
032: private boolean skipHiddenSubPages;
033:
034: private SiteMap siteMap;
035: private SiteInfo siteInfo;
036: private Iterator iter;
037: private PageInfo nextPage;
038: private boolean nextPageChecked;
039:
040: /**
041: * Creates an iterator for the full site map of the given website.
042: */
043: public SiteMapIterator(WebSite webSite) {
044: this (webSite, Path.ROOT);
045: }
046:
047: /**
048: * Creates an iterator for the full a submap of the given website, starting
049: * from the specified root path.
050: */
051: public SiteMapIterator(WebSite webSite, Path root) {
052: siteMap = webSite.getSiteMap();
053: siteInfo = webSite.getSiteInfo();
054: iter = siteMap.getPagesList(root).iterator();
055: }
056:
057: public boolean hasNext() {
058: return findNextPage();
059: }
060:
061: public Object next() {
062: if (!findNextPage()) {
063: throw new NoSuchElementException();
064: }
065:
066: return getNextPage();
067: }
068:
069: public void remove() {
070: throw new UnsupportedOperationException("Site map is readonly");
071: }
072:
073: /**
074: * Returns the next page (same as {@link #next}, but returns null when there
075: * are no more pages.
076: */
077: public PageInfo getNextPage() {
078: findNextPage();
079: nextPageChecked = false;
080: return nextPage;
081: }
082:
083: private boolean findNextPage() {
084: if (!nextPageChecked) {
085: if (skipHiddenSubPages && nextPage != null
086: && siteInfo.getHideSubmenu(nextPage.getPath())) {
087: int level = nextPage.getLevel();
088: nextPage = null;
089:
090: while (nextPage == null && iter.hasNext()) {
091: nextPage = (PageInfo) iter.next();
092:
093: if (nextPage.getLevel() > level) {
094: nextPage = null;
095: }
096: }
097: } else {
098: if (iter.hasNext()) {
099: nextPage = (PageInfo) iter.next();
100: } else {
101: nextPage = null;
102: }
103: }
104:
105: nextPageChecked = true;
106: }
107:
108: return nextPage != null;
109: }
110:
111: public boolean isSkipHiddenSubPages() {
112: return skipHiddenSubPages;
113: }
114:
115: public void setSkipHiddenSubPages(boolean skipHiddenSubPages) {
116: this.skipHiddenSubPages = skipHiddenSubPages;
117: }
118: }
|