001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.profiler.impl;
018:
019: import java.util.Iterator;
020: import java.util.LinkedList;
021: import java.util.List;
022: import java.util.ListIterator;
023: import java.util.StringTokenizer;
024:
025: import org.apache.jetspeed.profiler.ProfileLocator;
026: import org.apache.jetspeed.profiler.ProfileLocatorProperty;
027: import org.apache.jetspeed.profiler.Profiler;
028: import org.apache.jetspeed.profiler.rules.RuleCriterion;
029:
030: /**
031: * ProfileLocatorImpl
032: *
033: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
034: * @version $Id: JetspeedProfileLocator.java 517719 2007-03-13 15:05:48Z ate $
035: */
036: public class JetspeedProfileLocator implements ProfileLocatorControl {
037: private LinkedList elements = new LinkedList();
038: private String requestPath;
039:
040: public List getElements() {
041: return elements;
042: }
043:
044: public void init(Profiler profiler, String requestPath) {
045: if (requestPath != null)
046: if (requestPath.indexOf("/") != -1)
047: this .requestPath = requestPath;
048: else
049: this .requestPath = "/" + requestPath;
050: else
051: this .requestPath = "/";
052: }
053:
054: public Iterator iterator() {
055: return new ProfileFallbackIterator(this );
056: }
057:
058: public String getValue(String name) {
059: Iterator iter = elements.iterator();
060: while (iter.hasNext()) {
061: ProfileLocatorPropertyImpl element = (ProfileLocatorPropertyImpl) iter
062: .next();
063: String elementName = element.getName();
064: if (elementName != null && elementName.equals(name)) {
065: return element.getValue();
066: }
067: }
068: return null;
069: }
070:
071: public boolean isControl(String name) {
072: Iterator iter = elements.iterator();
073: while (iter.hasNext()) {
074: ProfileLocatorPropertyImpl element = (ProfileLocatorPropertyImpl) iter
075: .next();
076: String elementName = element.getName();
077: if (elementName != null && elementName.equals(name)) {
078: return element.isControl();
079: }
080: }
081: return false;
082: }
083:
084: public boolean isNavigation(String name) {
085: Iterator iter = elements.iterator();
086: while (iter.hasNext()) {
087: ProfileLocatorPropertyImpl element = (ProfileLocatorPropertyImpl) iter
088: .next();
089: String elementName = element.getName();
090: if (elementName != null && elementName.equals(name)) {
091: return element.isNavigation();
092: }
093: }
094: return false;
095: }
096:
097: public void add(RuleCriterion criterion, boolean isControl,
098: boolean isNavigation, String value) {
099: elements.add(new ProfileLocatorPropertyImpl(criterion,
100: isControl, isNavigation, value));
101: }
102:
103: public void add(String name, boolean isControl,
104: boolean isNavigation, String value) {
105: elements.add(new ProfileLocatorPropertyImpl(name, isControl,
106: isNavigation, value));
107: }
108:
109: public void add(String name, String value) {
110: add(name, true, false, value);
111: }
112:
113: public void createFromLocatorPath(String path) {
114: elements.clear();
115: StringTokenizer tokenizer = new StringTokenizer(path,
116: ProfileLocator.PATH_SEPARATOR);
117: while (tokenizer.hasMoreTokens()) {
118: String name = tokenizer.nextToken();
119: if (tokenizer.hasMoreTokens()) {
120: String value = tokenizer.nextToken();
121: this .add(name, true, false, value);
122: }
123: }
124: }
125:
126: public String getLocatorPath() {
127: StringBuffer key = new StringBuffer();
128: ListIterator it = elements.listIterator();
129: while (it.hasNext()) {
130: ProfileLocatorPropertyImpl element = (ProfileLocatorPropertyImpl) it
131: .next();
132: key.append(element.getName());
133: key.append(ProfileLocator.PATH_SEPARATOR);
134: key.append(element.getValue());
135: if (it.hasNext()) {
136: key.append(ProfileLocator.PATH_SEPARATOR);
137: }
138: }
139: return key.toString();
140: }
141:
142: public String getLocatorPath(ProfileLocatorProperty[] properties) {
143: StringBuffer key = new StringBuffer();
144: if (properties != null)
145: for (int i = 0; (i < properties.length); i++) {
146: if (i > 0)
147: key.append(ProfileLocator.PATH_SEPARATOR);
148: key.append(properties[i].getName());
149: key.append(ProfileLocator.PATH_SEPARATOR);
150: key.append(properties[i].getValue());
151: }
152: return key.toString();
153: }
154:
155: public String toString() {
156: return getRequestPath() + ProfileLocator.PATH_SEPARATOR
157: + getLocatorPath();
158: }
159:
160: public String getRequestPath() {
161: return requestPath;
162: }
163: }
|