View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.profiler.impl;
18  
19  import java.util.Iterator;
20  import java.util.LinkedList;
21  import java.util.List;
22  import java.util.ListIterator;
23  import java.util.StringTokenizer;
24  
25  import org.apache.jetspeed.profiler.ProfileLocator;
26  import org.apache.jetspeed.profiler.ProfileLocatorProperty;
27  import org.apache.jetspeed.profiler.Profiler;
28  import org.apache.jetspeed.profiler.rules.RuleCriterion;
29  
30  /***
31   * ProfileLocatorImpl
32   *
33   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
34   * @version $Id: JetspeedProfileLocator.java 517719 2007-03-13 15:05:48Z ate $
35   */
36  public class JetspeedProfileLocator implements ProfileLocatorControl
37  {    
38      private LinkedList elements = new LinkedList();
39      private String requestPath;
40  
41      public List getElements()
42      {
43          return elements;
44      }
45  
46      public void init(Profiler profiler, String requestPath)
47      {
48          if (requestPath != null)
49              if (requestPath.indexOf("/") != -1)
50                  this.requestPath = requestPath;
51              else
52                  this.requestPath = "/" + requestPath;
53          else
54              this.requestPath = "/";
55      }
56  
57      public Iterator iterator()
58      {    
59          return new ProfileFallbackIterator(this);
60      }
61      
62      public String getValue(String name)
63      {
64          Iterator iter = elements.iterator();
65          while (iter.hasNext())
66          {
67              ProfileLocatorPropertyImpl element = (ProfileLocatorPropertyImpl)iter.next();
68              String elementName = element.getName();
69              if (elementName != null && elementName.equals(name))
70              {
71                  return element.getValue();
72              }
73          }
74          return null;
75      }
76  
77      public boolean isControl(String name)
78      {
79          Iterator iter = elements.iterator();
80          while (iter.hasNext())
81          {
82              ProfileLocatorPropertyImpl element = (ProfileLocatorPropertyImpl)iter.next();
83              String elementName = element.getName();
84              if (elementName != null && elementName.equals(name))
85              {
86                  return element.isControl();
87              }
88          }
89          return false;
90      }
91  
92      public boolean isNavigation(String name)
93      {
94          Iterator iter = elements.iterator();
95          while (iter.hasNext())
96          {
97              ProfileLocatorPropertyImpl element = (ProfileLocatorPropertyImpl)iter.next();
98              String elementName = element.getName();
99              if (elementName != null && elementName.equals(name))
100             {
101                 return element.isNavigation();
102             }
103         }
104         return false;
105     }
106     
107     public void add(RuleCriterion criterion, boolean isControl, boolean isNavigation, String value)
108     {
109         elements.add(new ProfileLocatorPropertyImpl(criterion, isControl, isNavigation, value));
110     }
111 
112     public void add(String name, boolean isControl, boolean isNavigation, String value)
113     {
114         elements.add(new ProfileLocatorPropertyImpl(name, isControl, isNavigation, value));
115     }
116     
117     public void add(String name, String value)
118     {
119         add(name, true, false, value);
120     }
121     
122     public void createFromLocatorPath(String path)
123     {
124         elements.clear();
125         StringTokenizer tokenizer = new StringTokenizer(path, ProfileLocator.PATH_SEPARATOR);
126         while (tokenizer.hasMoreTokens())
127         {
128             String name = tokenizer.nextToken();
129             if (tokenizer.hasMoreTokens())
130             {
131                 String value = tokenizer.nextToken();
132                 this.add(name, true, false, value);
133             }
134         }        
135     }
136                     
137     public String getLocatorPath()
138     {
139         StringBuffer key = new StringBuffer();
140         ListIterator it = elements.listIterator();
141         while (it.hasNext())
142         {
143             ProfileLocatorPropertyImpl element = (ProfileLocatorPropertyImpl)it.next();
144             key.append(element.getName());
145             key.append(ProfileLocator.PATH_SEPARATOR);
146             key.append(element.getValue());
147             if (it.hasNext())
148             {
149                 key.append(ProfileLocator.PATH_SEPARATOR);
150             }
151         }
152         return key.toString();
153     }
154         
155     public String getLocatorPath(ProfileLocatorProperty [] properties)
156     {
157         StringBuffer key = new StringBuffer();
158         if (properties != null)
159             for (int i = 0; (i < properties.length); i++)
160             {
161                 if (i > 0)
162                     key.append(ProfileLocator.PATH_SEPARATOR);
163                 key.append(properties[i].getName());
164                 key.append(ProfileLocator.PATH_SEPARATOR);
165                 key.append(properties[i].getValue());
166             }
167         return key.toString();
168     }
169 
170     public String toString()
171     {
172         return getRequestPath() + ProfileLocator.PATH_SEPARATOR + getLocatorPath();
173     }
174         
175     public String getRequestPath()
176     {
177         return requestPath;
178     }
179 }