You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by re...@apache.org on 2008/06/18 19:09:45 UTC
svn commit: r669228 - in /cocoon/whiteboard/corona/trunk/corona-sitemap/src:
main/java/org/apache/cocoon/corona/sitemap/matcher/
main/java/org/apache/cocoon/corona/sitemap/node/
test/java/org/apache/cocoon/corona/sitemap/node/
Author: reinhard
Date: Wed Jun 18 10:09:45 2008
New Revision: 669228
URL: http://svn.apache.org/viewvc?rev=669228&view=rev
Log:
introduce a matcher interface (a first step towards the support of custom matcher implementations)
Added:
cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/
cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/ContainsMatcher.java (with props)
cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/EqualsMatcher.java (with props)
cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/Matcher.java (with props)
cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/RegexpMatcher.java (with props)
cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/WildcardMatcher.java (with props)
Modified:
cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/node/MatchNode.java
cocoon/whiteboard/corona/trunk/corona-sitemap/src/test/java/org/apache/cocoon/corona/sitemap/node/MatchNodeTest.java
Added: cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/ContainsMatcher.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/ContainsMatcher.java?rev=669228&view=auto
==============================================================================
--- cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/ContainsMatcher.java (added)
+++ cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/ContainsMatcher.java Wed Jun 18 10:09:45 2008
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.corona.sitemap.matcher;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class ContainsMatcher implements Matcher {
+
+ public Map<String, String> match(String expression, String testValue) {
+ if (testValue == null) {
+ return null;
+ }
+
+ if (testValue.contains(expression)) {
+ Map<String, String> result = new HashMap<String, String>();
+ result.put("0", testValue);
+ return result;
+ }
+
+ return null;
+ }
+
+}
Propchange: cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/ContainsMatcher.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/ContainsMatcher.java
------------------------------------------------------------------------------
svn:keywords = Id
Propchange: cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/ContainsMatcher.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/EqualsMatcher.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/EqualsMatcher.java?rev=669228&view=auto
==============================================================================
--- cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/EqualsMatcher.java (added)
+++ cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/EqualsMatcher.java Wed Jun 18 10:09:45 2008
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.corona.sitemap.matcher;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class EqualsMatcher implements Matcher {
+
+ public Map<String, String> match(String expression, String testValue) {
+ if (testValue == null) {
+ return null;
+ }
+
+ if (testValue.equals(expression)) {
+ Map<String, String> result = new HashMap<String, String>();
+ result.put("0", testValue);
+ return result;
+ }
+
+ return null;
+ }
+
+}
Propchange: cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/EqualsMatcher.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/EqualsMatcher.java
------------------------------------------------------------------------------
svn:keywords = Id
Propchange: cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/EqualsMatcher.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/Matcher.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/Matcher.java?rev=669228&view=auto
==============================================================================
--- cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/Matcher.java (added)
+++ cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/Matcher.java Wed Jun 18 10:09:45 2008
@@ -0,0 +1,24 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.corona.sitemap.matcher;
+
+import java.util.Map;
+
+public interface Matcher {
+
+ Map<String, String> match(String testValue, String expression);
+}
Propchange: cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/Matcher.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/Matcher.java
------------------------------------------------------------------------------
svn:keywords = Id
Propchange: cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/Matcher.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/RegexpMatcher.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/RegexpMatcher.java?rev=669228&view=auto
==============================================================================
--- cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/RegexpMatcher.java (added)
+++ cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/RegexpMatcher.java Wed Jun 18 10:09:45 2008
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.corona.sitemap.matcher;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+public class RegexpMatcher implements Matcher {
+
+ private static Map<String, Pattern> patterns = new HashMap<String, Pattern>();
+
+ public Map<String, String> match(String expression, String testValue) {
+ if (testValue == null) {
+ return null;
+ }
+
+ Pattern pattern = patterns.get(expression);
+ if (pattern == null) {
+ pattern = Pattern.compile(expression);
+ patterns.put(expression, pattern);
+ }
+
+ java.util.regex.Matcher matcher = null;
+ if (testValue.startsWith("/")) {
+ matcher = pattern.matcher(testValue.substring(1));
+ } else {
+ matcher = pattern.matcher(testValue);
+ }
+
+ if (matcher.matches()) {
+ Map<String, String> result = new HashMap<String, String>();
+ for (int i = 0; i <= matcher.groupCount(); i++) {
+ result.put(Integer.toString(i), matcher.group(i));
+ }
+ return result;
+ }
+
+ return null;
+ }
+
+}
Propchange: cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/RegexpMatcher.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/RegexpMatcher.java
------------------------------------------------------------------------------
svn:keywords = Id
Propchange: cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/RegexpMatcher.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/WildcardMatcher.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/WildcardMatcher.java?rev=669228&view=auto
==============================================================================
--- cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/WildcardMatcher.java (added)
+++ cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/WildcardMatcher.java Wed Jun 18 10:09:45 2008
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.corona.sitemap.matcher;
+
+import java.util.Map;
+
+import org.apache.cocoon.corona.sitemap.util.WildcardMatcherHelper;
+
+public class WildcardMatcher implements Matcher {
+
+ public Map<String, String> match(String expression, String testValue) {
+ if (testValue == null) {
+ return null;
+ }
+
+ if (testValue.startsWith("/")) {
+ return WildcardMatcherHelper.match(expression, testValue.substring(1));
+ }
+
+ return WildcardMatcherHelper.match(expression, testValue);
+ }
+
+}
Propchange: cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/WildcardMatcher.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/WildcardMatcher.java
------------------------------------------------------------------------------
svn:keywords = Id
Propchange: cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/matcher/WildcardMatcher.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/node/MatchNode.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/node/MatchNode.java?rev=669228&r1=669227&r2=669228&view=diff
==============================================================================
--- cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/node/MatchNode.java (original)
+++ cocoon/whiteboard/corona/trunk/corona-sitemap/src/main/java/org/apache/cocoon/corona/sitemap/node/MatchNode.java Wed Jun 18 10:09:45 2008
@@ -18,15 +18,16 @@
*/
package org.apache.cocoon.corona.sitemap.node;
-import java.util.HashMap;
+import java.util.LinkedList;
import java.util.Map;
-import java.util.Map.Entry;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import org.apache.cocoon.corona.sitemap.Invocation;
+import org.apache.cocoon.corona.sitemap.matcher.ContainsMatcher;
+import org.apache.cocoon.corona.sitemap.matcher.EqualsMatcher;
+import org.apache.cocoon.corona.sitemap.matcher.Matcher;
+import org.apache.cocoon.corona.sitemap.matcher.RegexpMatcher;
+import org.apache.cocoon.corona.sitemap.matcher.WildcardMatcher;
import org.apache.cocoon.corona.sitemap.node.annotations.Parameter;
-import org.apache.cocoon.corona.sitemap.util.WildcardMatcherHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -35,8 +36,6 @@
private final Log logger = LogFactory.getLog(this.getClass());
- private static final Map<String, Pattern> patterns = new HashMap<String, Pattern>();
-
@Parameter
private String name;
@@ -58,7 +57,7 @@
@Parameter
private String wildcard;
- private boolean inMatchingMode;
+ private MatcherContext matcherContext;
/**
* {@inheritDoc}
@@ -69,8 +68,8 @@
public InvocationResult invoke(Invocation invocation) {
if (invocation.isErrorInvocation() && !this.hasHandleErrorsParent(this.getParent())) {
if (this.logger.isDebugEnabled()) {
- this.logger
- .debug("Stop here because it's an error invocation and this node is defined outside of <handle-errors>.");
+ this.logger.debug("Stop here because it's an error invocation "
+ + "and this node is defined outside of <handle-errors>.");
}
return InvocationResult.NONE;
}
@@ -81,12 +80,9 @@
testValue = invocation.getRequestURI();
}
- // find out the matching method
- Map<String, String> matchingAttribute = this.prepareMatching();
- this.inMatchingMode = matchingAttribute.size() == 1;
-
+ this.matcherContext = this.lookupMatcherContext();
// if there are NO matching attributes (regexp, equals, ...) set, execute the children
- if (!this.inMatchingMode) {
+ if (this.matcherContext == null) {
InvocationResult invocationResult = super.invoke(invocation);
if (invocationResult.isProcessed()) {
@@ -97,7 +93,7 @@
}
// if there are matching attributes, invoke the matcher and execute the children afterwards
- Map<String, String> matches = this.getMatches(matchingAttribute, testValue);
+ Map<String, String> matches = this.matcherContext.matcher.match(this.matcherContext.value, testValue);
if (matches == null) {
// do not ask our children, there was no match
return InvocationResult.NONE;
@@ -114,166 +110,48 @@
return InvocationResult.NONE;
}
- protected boolean hasHandleErrorsParent(SitemapNode parent) {
- SitemapNode currentNode = parent;
-
- while (currentNode != null) {
- if (currentNode instanceof ErrorNode) {
- return true;
- }
- currentNode = currentNode.getParent();
- }
-
- return false;
- }
-
- protected Map<String, String> getMatches(Map<String, String> matchingAttributes, String value) {
- String matchingAttributeType = null;
- String matchingAttributeValue = null;
- Map<String, String> matches = null;
- if (matchingAttributes.size() == 1) {
- for (Entry<String, String> attribute : matchingAttributes.entrySet()) {
- matchingAttributeType = attribute.getKey();
- matchingAttributeValue = attribute.getValue();
- }
-
- if ("wildcard".equals(matchingAttributeType)) {
- if (this.logger.isDebugEnabled()) {
- this.logger.debug("Invoking wildcard matcher: test=" + value + ", wildcard="
- + matchingAttributeValue);
- }
-
- matches = getWildcardMatches(value, matchingAttributeValue);
- } else if ("regexp".equals(matchingAttributeType)) {
- if (this.logger.isDebugEnabled()) {
- this.logger.debug("Invoking regexp matcher: test=" + value + ", regexp=" + matchingAttributeValue);
- }
-
- matches = getReqexpMatches(value, matchingAttributeValue);
- } else if ("contains".equals(matchingAttributeType)) {
- if (this.logger.isDebugEnabled()) {
- this.logger.debug("Invoking contains matcher: test=" + value + ", contains="
- + matchingAttributeValue);
- }
-
- matches = getContainsMatches(value, matchingAttributeValue);
- } else if ("pattern".equals(matchingAttributeType)) {
- if (this.logger.isDebugEnabled()) {
- this.logger.debug("Invoking wildcard matcher: test=" + value + ", pattern="
- + matchingAttributeValue);
- }
-
- matches = getWildcardMatches(value, matchingAttributeValue);
- } else if ("equals".equals(matchingAttributeType)) {
- if (this.logger.isDebugEnabled()) {
- this.logger.debug("Invoking equals matcher: test=" + value + ", equals=" + matchingAttributeValue);
- }
-
- matches = getEqualsMatches(value, matchingAttributeValue);
- }
- }
-
- return matches;
- }
-
- protected Map<String, String> prepareMatching() {
+ protected MatcherContext lookupMatcherContext() {
// determine the matching type and check if there are conflicting match attributes
- Map<String, String> matchingAttributes = new HashMap<String, String>();
+ LinkedList<MatcherContext> matcherContextList = new LinkedList<MatcherContext>();
if (this.pattern != null) {
- matchingAttributes.put("pattern", this.pattern);
+ matcherContextList.add(new MatcherContext(new WildcardMatcher(), this.pattern));
}
if (this.regexp != null) {
- matchingAttributes.put("regexp", this.regexp);
+ matcherContextList.add(new MatcherContext(new RegexpMatcher(), this.regexp));
}
if (this.equals != null) {
- matchingAttributes.put("equals", this.equals);
+ matcherContextList.add(new MatcherContext(new EqualsMatcher(), this.equals));
}
if (this.contains != null) {
- matchingAttributes.put("contains", this.contains);
+ matcherContextList.add(new MatcherContext(new ContainsMatcher(), this.contains));
}
if (this.wildcard != null) {
- matchingAttributes.put("wildcard", this.wildcard);
+ matcherContextList.add(new MatcherContext(new WildcardMatcher(), this.wildcard));
}
- if (matchingAttributes.size() > 1) {
+ if (matcherContextList.size() > 1) {
String message = "Only one matching attribute (regexp, equals, contains, wildcard, pattern) can be set: "
- + matchingAttributes;
+ + matcherContextList;
this.logger.error(message);
throw new RuntimeException(message);
}
- return matchingAttributes;
+ return matcherContextList.isEmpty() ? null : matcherContextList.getFirst();
}
- private static Map<String, String> getContainsMatches(String value, String matchingAttributeValue) {
- if (value == null) {
- return null;
- }
-
- if (value.contains(matchingAttributeValue)) {
- Map<String, String> result = new HashMap<String, String>();
- result.put("0", value);
- return result;
- }
-
- return null;
- }
-
- private static Map<String, String> getEqualsMatches(String value, String matchingAttributeValue) {
- if (value == null) {
- return null;
- }
-
- if (value.equals(matchingAttributeValue)) {
- Map<String, String> result = new HashMap<String, String>();
- result.put("0", value);
- return result;
- }
-
- return null;
- }
-
- private static Map<String, String> getReqexpMatches(String value, String matchingAttributeValue) {
- if (value == null) {
- return null;
- }
-
- Pattern pattern = patterns.get(matchingAttributeValue);
- if (pattern == null) {
- pattern = Pattern.compile(matchingAttributeValue);
- patterns.put(matchingAttributeValue, pattern);
- }
-
- Matcher matcher = null;
- if (value.startsWith("/")) {
- matcher = pattern.matcher(value.substring(1));
- } else {
- matcher = pattern.matcher(value);
- }
+ protected boolean hasHandleErrorsParent(SitemapNode parent) {
+ SitemapNode currentNode = parent;
- if (matcher.matches()) {
- Map<String, String> result = new HashMap<String, String>();
- for (int i = 0; i <= matcher.groupCount(); i++) {
- result.put(Integer.toString(i), matcher.group(i));
+ while (currentNode != null) {
+ if (currentNode instanceof ErrorNode) {
+ return true;
}
- return result;
- }
-
- return null;
- }
-
- private static Map<String, String> getWildcardMatches(String value, String wildcard) {
- if (value == null) {
- return null;
- }
-
- if (value.startsWith("/")) {
- return WildcardMatcherHelper.match(wildcard, value.substring(1));
+ currentNode = currentNode.getParent();
}
- return WildcardMatcherHelper.match(wildcard, value);
+ return false;
}
public boolean isInMatchingMode() {
- return this.inMatchingMode;
+ return this.matcherContext != null;
}
public void setValue(String value) {
@@ -283,4 +161,23 @@
public String getValue() {
return this.value;
}
+
+ protected static class MatcherContext {
+
+ public Matcher matcher;
+ public String value;
+
+ public MatcherContext(Matcher matcher, String testValue) {
+ super();
+ this.matcher = matcher;
+ this.value = testValue;
+ }
+
+ @Override
+ public String toString() {
+ // TODO Auto-generated method stub
+ return "matcher=" + this.matcher + ", testValue=" + this.value;
+ }
+ }
+
}
Modified: cocoon/whiteboard/corona/trunk/corona-sitemap/src/test/java/org/apache/cocoon/corona/sitemap/node/MatchNodeTest.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/corona/trunk/corona-sitemap/src/test/java/org/apache/cocoon/corona/sitemap/node/MatchNodeTest.java?rev=669228&r1=669227&r2=669228&view=diff
==============================================================================
--- cocoon/whiteboard/corona/trunk/corona-sitemap/src/test/java/org/apache/cocoon/corona/sitemap/node/MatchNodeTest.java (original)
+++ cocoon/whiteboard/corona/trunk/corona-sitemap/src/test/java/org/apache/cocoon/corona/sitemap/node/MatchNodeTest.java Wed Jun 18 10:09:45 2008
@@ -22,6 +22,7 @@
import junitx.util.PrivateAccessor;
+import org.apache.cocoon.corona.sitemap.node.MatchNode.MatcherContext;
import org.junit.Test;
public class MatchNodeTest {
@@ -30,7 +31,8 @@
public void regexpMatchingAttribute() throws Exception {
MatchNode matchNode = new MatchNode();
PrivateAccessor.setField(matchNode, "regexp", "([a-zA-Z\\-]+)/(.*)");
- Map<String, String> matches = matchNode.getMatches(matchNode.prepareMatching(), "abc/44");
+ MatcherContext matcherContext = matchNode.lookupMatcherContext();
+ Map<String, String> matches = matcherContext.matcher.match(matcherContext.value, "abc/44");
assertTrue(matches.containsValue("abc/44"));
assertTrue(matches.containsValue("abc"));
assertTrue(matches.containsValue("44"));
@@ -41,7 +43,8 @@
public void regexpMatchingAttributeWithSlash() throws Exception {
MatchNode matchNode = new MatchNode();
PrivateAccessor.setField(matchNode, "regexp", "([a-zA-Z\\-]+)/(.*)");
- Map<String, String> matches = matchNode.getMatches(matchNode.prepareMatching(), "/abc/44");
+ MatcherContext matcherContext = matchNode.lookupMatcherContext();
+ Map<String, String> matches = matcherContext.matcher.match(matcherContext.value, "abc/44");
assertTrue(matches.containsValue("abc/44"));
assertTrue(matches.containsValue("abc"));
assertTrue(matches.containsValue("44"));
@@ -52,7 +55,8 @@
public void containsMatchingAttribute() throws Exception {
MatchNode matchNode = new MatchNode();
PrivateAccessor.setField(matchNode, "contains", "123");
- Map<String, String> matches = matchNode.getMatches(matchNode.prepareMatching(), "000123456");
+ MatcherContext matcherContext = matchNode.lookupMatcherContext();
+ Map<String, String> matches = matcherContext.matcher.match(matcherContext.value, "000123456");
assertTrue(matches.containsValue("000123456"));
assertEquals(1, matches.size());
}
@@ -61,7 +65,8 @@
public void equalsMatchingAttribute() throws Exception {
MatchNode matchNode = new MatchNode();
PrivateAccessor.setField(matchNode, "equals", "123");
- Map<String, String> matches = matchNode.getMatches(matchNode.prepareMatching(), "123");
+ MatcherContext matcherContext = matchNode.lookupMatcherContext();
+ Map<String, String> matches = matcherContext.matcher.match(matcherContext.value, "123");
assertTrue(matches.containsValue("123"));
assertEquals(1, matches.size());
}
@@ -70,7 +75,8 @@
public void wildcardMatchingPathAttribute() throws Exception {
MatchNode matchNode = new MatchNode();
PrivateAccessor.setField(matchNode, "wildcard", "abc/*/*");
- Map<String, String> matches = matchNode.getMatches(matchNode.prepareMatching(), "abc/def/ghi");
+ MatcherContext matcherContext = matchNode.lookupMatcherContext();
+ Map<String, String> matches = matcherContext.matcher.match(matcherContext.value, "abc/def/ghi");
assertTrue(matches.containsValue("abc/def/ghi"));
assertTrue(matches.containsValue("def"));
assertTrue(matches.containsValue("ghi"));
@@ -81,7 +87,8 @@
public void wildcardMatchingPathAttributeWithSlash() throws Exception {
MatchNode matchNode = new MatchNode();
PrivateAccessor.setField(matchNode, "wildcard", "abc/*/*");
- Map<String, String> matches = matchNode.getMatches(matchNode.prepareMatching(), "/abc/def/ghi");
+ MatcherContext matcherContext = matchNode.lookupMatcherContext();
+ Map<String, String> matches = matcherContext.matcher.match(matcherContext.value, "abc/def/ghi");
assertTrue(matches.containsValue("abc/def/ghi"));
assertTrue(matches.containsValue("def"));
assertTrue(matches.containsValue("ghi"));
@@ -91,8 +98,8 @@
@Test
public void noMatchingAttribute() {
MatchNode matchNode = new MatchNode();
- Map<String, String> matches = matchNode.getMatches(matchNode.prepareMatching(), "123");
- assertNull("If there is no match, null has to be returned.", matches);
+ MatcherContext matcherContext = matchNode.lookupMatcherContext();
+ assertNull("If there is no match attribute, no matcher can be found.", matcherContext);
}
@Test
@@ -101,7 +108,7 @@
PrivateAccessor.setField(matchNode, "pattern", "123");
PrivateAccessor.setField(matchNode, "equals", "123");
try {
- matchNode.getMatches(matchNode.prepareMatching(), "123");
+ matchNode.lookupMatcherContext();
fail();
} catch (Exception e) {
// expected