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 AndRequestMatcher implements RequestMatcher {
038:
039: /**
040: *
041: */
042: public AndRequestMatcher() {
043: super ();
044: }
045:
046: public AndRequestMatcher(RequestMatcher[] matchers) {
047: this (Arrays.asList(matchers));
048: }
049:
050: public AndRequestMatcher(Collection matchers) {
051: this .matchers.addAll(matchers);
052: while (this .matchers.remove(null))
053: ;
054: }
055:
056: /**
057: * Puts self to matched if all of contained matchers match.
058: */
059: public List match(HttpServletRequest request) {
060: boolean matchQueryString = false;
061: boolean matched = true;
062: int weight = getWeight();
063: Iterator it = matchers.iterator();
064: while (it.hasNext()) {
065: RequestMatcher rm = (RequestMatcher) it.next();
066: Collection matchResults = rm.match(request);
067: if (matchResults.isEmpty()) {
068: matched = false;
069: break;
070: }
071:
072: Iterator mit = matchResults.iterator();
073: while (mit.hasNext()) {
074: MatchResult mmr = (MatchResult) mit.next();
075: matchQueryString = matchQueryString
076: || mmr.isMatchQueryString();
077: weight += mmr.getWeight();
078: }
079: }
080: List ret = new LinkedList();
081: if (matched) {
082: ret.add(new MatchResult(this , matchQueryString, weight));
083: }
084: return ret;
085: }
086:
087: private List matchers = new LinkedList();
088:
089: public void addMatcher(RequestMatcher matcher) {
090: matchers.add(matcher);
091: }
092:
093: public boolean removeMatcher(RequestMatcher matcher) {
094: return matchers.remove(matcher);
095: }
096:
097: protected List getMatchers() {
098: return matchers;
099: }
100:
101: public int getWeight() {
102: return 0;
103: }
104:
105: public boolean isMatchQueryString() {
106: boolean ret = false;
107: Iterator it = matchers.iterator();
108: while (!ret && it.hasNext()) {
109: ret = ((RequestMatcher) it.next()).isMatchQueryString();
110: }
111: return ret;
112: }
113:
114: public boolean isEmpty() {
115: return matchers.isEmpty();
116: }
117: }
|