KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > core > SiteMapIterator


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.core;
24
25 import java.util.*;
26 import org.meshcms.util.*;
27
28 /**
29  * Iterator for the site map.
30  */

31 public class SiteMapIterator implements Iterator {
32   private boolean skipHiddenSubPages;
33   
34   private SiteMap siteMap;
35   private SiteInfo siteInfo;
36   private Iterator iter;
37   private PageInfo nextPage;
38   private boolean nextPageChecked;
39   
40   /**
41    * Creates an iterator for the full site map of the given website.
42    */

43   public SiteMapIterator(WebSite webSite) {
44     this(webSite, Path.ROOT);
45   }
46   
47   /**
48    * Creates an iterator for the full a submap of the given website, starting
49    * from the specified root path.
50    */

51   public SiteMapIterator(WebSite webSite, Path root) {
52     siteMap = webSite.getSiteMap();
53     siteInfo = webSite.getSiteInfo();
54     iter = siteMap.getPagesList(root).iterator();
55   }
56   
57   public boolean hasNext() {
58     return findNextPage();
59   }
60   
61   public Object JavaDoc next() {
62     if (!findNextPage()) {
63       throw new NoSuchElementException();
64     }
65     
66     return getNextPage();
67   }
68   
69   public void remove() {
70     throw new UnsupportedOperationException JavaDoc("Site map is readonly");
71   }
72   
73   /**
74    * Returns the next page (same as {@link #next}, but returns null when there
75    * are no more pages.
76    */

77   public PageInfo getNextPage() {
78     findNextPage();
79     nextPageChecked = false;
80     return nextPage;
81   }
82   
83   private boolean findNextPage() {
84     if (!nextPageChecked) {
85       if (skipHiddenSubPages && nextPage != null &&
86           siteInfo.getHideSubmenu(nextPage.getPath())) {
87         int level = nextPage.getLevel();
88         nextPage = null;
89         
90         while (nextPage == null && iter.hasNext()) {
91           nextPage = (PageInfo) iter.next();
92           
93           if (nextPage.getLevel() > level) {
94             nextPage = null;
95           }
96         }
97       } else {
98         if (iter.hasNext()) {
99           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 }
119
Popular Tags