View Javadoc

1   /*
2    * Copyright 2007 Hippo
3    *
4    * Licensed under the Apache License, Version 2.0 (the  "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" 
12   * BASIS, 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  package nl.hippo.portal.cms.site;
17  
18  import java.util.HashMap;
19  import java.util.List;
20  import java.util.Map;
21  import java.util.StringTokenizer;
22  import java.util.TreeMap;
23  
24  import org.apache.log4j.Logger;
25  
26  /***
27   * @version $Id: SiteMapImpl.java 13521 2008-09-17 14:50:38Z jhoffman $
28   *
29   */
30  public class SiteMapImpl implements SiteMap {
31  
32      private static Logger logger = Logger.getLogger(SiteMap.class);
33  
34      private static final class SrcItem {
35          private SiteMapItem smi;
36  
37          private Map children;
38      }
39  
40      private final String name;
41  
42      private final Map idMap = new HashMap();
43  
44      private final SiteMapItemImpl root = new SiteMapItemImpl(this);
45  
46      private final SrcItem srcRoot = new SrcItem();
47  
48      public SiteMapImpl() {
49          this("");
50      }
51  
52      public SiteMapImpl(String name) {
53          this.name = name;
54      }
55  
56      /* (non-Javadoc)
57       * @see nl.hippo.portal.cms.site.SiteMap#getName()
58       */
59      public String getName() {
60          return name;
61      }
62  
63      /* (non-Javadoc)
64       * @see nl.hippo.portal.cms.site.SiteMap#getRoot()
65       */
66      public SiteMapItem getRoot() {
67          return root;
68      }
69  
70      public SiteMapItem addSiteMapItem(String id, SiteMapItem siteMapItem) {
71          if (id != null) {
72              idMap.put(id, siteMapItem);
73              return siteMapItem;
74          }
75          return null;
76      }
77  
78      public SiteMapItemImpl addSiteMapItem(String id, String path, String title, String src, String template,
79              boolean hidden, boolean menuItem, boolean linkable) {
80          if (id != null && idMap.get(id) != null) {
81              logger.warn("Duplicate SiteMapItem with id " + id + ". Failed to add item: id=" + id 
82              		+ ", path="  + path + ", title=" + title + ", src=" + src + ", template=" + template
83              		+ ", hidden=" + hidden + ", menuItem=" + menuItem +", linkable=" + linkable);
84              return null;
85          }
86          StringTokenizer st = new StringTokenizer(path, "/");
87          SiteMapItemImpl smi = root;
88          boolean moreTokens = st.hasMoreTokens();
89          while (moreTokens) {
90              String name = st.nextToken();
91              moreTokens = st.hasMoreTokens();
92              SiteMapItemImpl child = (SiteMapItemImpl) smi.getChild(name);
93              if (child != null) {
94                  smi = child;
95              } else {
96                  smi = new SiteMapItemImpl(smi, id, name, hidden, this);
97              }
98              if (!moreTokens) {
99                  if (smi.isLeaf()) {
100                     logger.warn("Duplicate SiteMapItem with path " + path + ". Failed to add item: id=" 
101                     		+ id + ", path="  + path + ", title=" + title + ", src=" + src + ", template=" + template
102                     		+ ", hidden=" + hidden + ", menuItem=" + menuItem +", linkable=" + linkable);
103                     return null;
104                 }
105                 if (linkable) {
106                     smi.makeLeaf(title, src, template, menuItem);
107                 } else {
108                     smi.makeNotLinkableLeaf(title, menuItem);
109                 }
110             }
111         }
112         if (src != null) {
113             st = new StringTokenizer(src, "/");
114             SrcItem si = srcRoot;
115             moreTokens = st.hasMoreTokens();
116             while (moreTokens) {
117                 String name = st.nextToken();
118                 moreTokens = st.hasMoreTokens();
119 
120                 if (si.children == null) {
121                     si.children = new TreeMap();
122                 }
123                 SrcItem child = (SrcItem) si.children.get(name);
124                 if (child != null) {
125                     si = child;
126                 } else {
127                     child = new SrcItem();
128                     si.children.put(name, child);
129                     si = child;
130                 }
131                 if (!moreTokens) {
132                     if (si.smi == null || si.smi.getLevel() < smi.getLevel()) {
133                         si.smi = smi;
134                     }
135                 }
136             }
137         }
138         addSiteMapItem(id, smi);
139         return smi;
140     }
141 
142     /* (non-Javadoc)
143      * @see nl.hippo.portal.cms.site.SiteMap#getById(java.lang.String)
144      */
145     public SiteMapItem getById(String id) {
146         return (SiteMapItem) idMap.get(id);
147     }
148 
149     /* (non-Javadoc)
150      * @see nl.hippo.portal.cms.site.SiteMap#match(java.lang.String)
151      */
152     public SiteMapItem getByPaths(String[] paths) {
153         SiteMapItem item = getRoot();
154         for (int i = 0; i < paths.length && item != null; i++) {
155             if (paths[i] == null) {
156                 item = null;
157             } else {
158                 item = item.getChild(paths[i]);
159             }
160         }
161         return item != null && item.isLeaf() ? item : null;
162     }
163 
164     /* (non-Javadoc)
165      * @see nl.hippo.portal.cms.site.SiteMap#match(java.lang.String)
166      */
167     public SiteMapItem getByPath(String path) {
168         StringTokenizer st = new StringTokenizer(path, "/");
169         SiteMapItem smi = getRoot();
170         SiteMapItem leaf = null;
171         while (st.hasMoreTokens()) {
172             SiteMapItem child = smi.getChild(st.nextToken());
173             if (child != null) {
174                 smi = child;
175                 if (smi.isLeaf()) {
176                     leaf = smi;
177                 }
178             } else {
179                 leaf = null;
180                 break;
181             }
182         }
183         return leaf;
184     }
185 
186     /* (non-Javadoc)
187      * @see nl.hippo.portal.cms.site.SiteMap#match(java.lang.String)
188      */
189     public SiteMapItem matchPath(String path) {
190         StringTokenizer st = new StringTokenizer(path, "/");
191         SiteMapItem smi = getRoot();
192         SiteMapItem leaf = null;
193         while (st.hasMoreTokens()) {
194             SiteMapItem child = smi.getChild(st.nextToken());
195             if (child != null) {
196                 smi = child;
197                 if (smi.isLeaf()) {
198                     leaf = smi;
199                 }
200             } else {
201                 break;
202             }
203         }
204         return leaf;
205     }
206 
207     /* (non-Javadoc)
208      * @see nl.hippo.portal.cms.site.SiteMap#matchSrc(java.lang.String)
209      */
210     public SiteMapItem matchSrc(String src) {
211         StringTokenizer st = new StringTokenizer(src, "/");
212         SrcItem si = srcRoot;
213         SiteMapItem leaf = null;
214         while (st.hasMoreTokens() && si.children != null) {
215             SrcItem child = (SrcItem) si.children.get(st.nextToken());
216             if (child != null) {
217                 si = child;
218                 if (si.smi != null) {
219                     leaf = si.smi;
220                 }
221             } else {
222                 break;
223             }
224         }
225         return leaf;
226     }
227 
228     public List getSiteMapItemsByField(String field, String value) {
229         return getRoot().getSiteMapItemsByField(field, value);
230     }
231 
232     public SiteMapItem getFirstMatchingSiteMapItem(String field, String value) {
233         return getRoot().getFirstMatchingSiteMapItem(field, value);
234     }
235 
236 }