View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
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" 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  package org.jdiagnose.library.web.breadcrumb;
17  
18  import java.util.Iterator;
19  import java.util.Locale;
20  import java.util.Map;
21  import java.util.Properties;
22  
23  import javax.servlet.http.HttpServletRequest;
24  
25  /***
26   * @author jmccrindle
27   */
28  public class PropertiesCrumbFactory extends CrumbFactoryWrapper implements CrumbFactory {
29  
30      private Properties properties = null;
31  
32      /***
33       * 
34       */
35      public PropertiesCrumbFactory() {
36          super();
37      }
38      
39      /***
40       * @param crumbFactory
41       */
42      public PropertiesCrumbFactory(CrumbFactory crumbFactory) {
43          super(crumbFactory);
44      }
45      
46      /* (non-Javadoc)
47       * @see org.jdiagnose.library.web.breadcrumb.CrumbFactory#getCrumb(javax.servlet.http.HttpServletRequest, java.lang.String)
48       */
49      public Crumb getCrumb(HttpServletRequest request, final String url) {
50          final String simpleDescription = getSimpleDescription(url);
51          if(simpleDescription != null) {
52              return new Crumb() {
53                  public String getUrl() {
54                      return url;
55                  }
56                  public String getDescription(Locale locale) {
57                      return simpleDescription;
58                  }
59              };
60          }
61          CrumbFactory delegate = getCrumbFactory();
62          if(delegate != null ) {
63              return delegate.getCrumb(request, url);
64          } else {
65              return null;
66          }
67      }
68  
69      protected String getSimpleDescription(String url) {
70          for (Iterator propIterator = properties.entrySet().iterator(); propIterator.hasNext();) {
71              Map.Entry entry = (Map.Entry) propIterator.next();
72              String key = (String) entry.getKey();
73              if(url.endsWith(key)) {
74                  return (String) entry.getValue();
75              }
76          }
77          return null;
78      }
79  
80      public void setProperties(Properties properties) {
81          this.properties = properties;
82      }
83  }