001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.menu.matchers;
024:
025: import java.util.Arrays;
026: import java.util.Collection;
027: import java.util.Iterator;
028: import java.util.LinkedList;
029: import java.util.List;
030:
031: import javax.servlet.http.HttpServletRequest;
032:
033: /**
034: * @author Pavel Vlasov
035: * @version $Revision: 1.2 $
036: */
037: public class OrRequestMatcher implements RequestMatcher {
038:
039: /**
040: *
041: */
042: public OrRequestMatcher() {
043: super ();
044: }
045:
046: public OrRequestMatcher(Collection matchers) {
047: this .matchers.addAll(matchers);
048: while (this .matchers.remove(null))
049: ;
050: }
051:
052: public OrRequestMatcher(RequestMatcher[] matchers) {
053: this (Arrays.asList(matchers));
054: }
055:
056: /**
057: * Puts self to matched if any of contained matchers match.
058: */
059: public List match(HttpServletRequest request) {
060: List ret = new LinkedList();
061: Iterator it = matchers.iterator();
062: while (it.hasNext()) {
063: RequestMatcher matcher = (RequestMatcher) it.next();
064: Collection matchResult = matcher.match(request);
065: if (!matchResult.isEmpty()) {
066: boolean matchQueryString = true;
067: int weight = 0;
068: Iterator mit = matchResult.iterator();
069: while (mit.hasNext()) {
070: MatchResult mmr = (MatchResult) mit.next();
071: matchQueryString = matchQueryString
072: && mmr.isMatchQueryString();
073: weight = Math.max(weight, mmr.getWeight());
074: }
075: ret.add(new MatchResult(this , matchQueryString, weight
076: + getWeight()));
077: break;
078: }
079: }
080: return ret;
081: }
082:
083: private List matchers = new LinkedList();
084:
085: public void addMatcher(RequestMatcher matcher) {
086: matchers.add(matcher);
087: }
088:
089: public boolean removeMatcher(RequestMatcher matcher) {
090: return matchers.remove(matcher);
091: }
092:
093: protected List getMatchers() {
094: return matchers;
095: }
096:
097: public int getWeight() {
098: return 0;
099: }
100:
101: public boolean isMatchQueryString() {
102: boolean ret = false;
103: Iterator it = matchers.iterator();
104: while (!ret && it.hasNext()) {
105: ret = ((RequestMatcher) it.next()).isMatchQueryString();
106: }
107: return ret;
108: }
109:
110: public boolean isEmpty() {
111: return matchers.isEmpty();
112: }
113: }
|