KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > matching > AbstractRegexpMatcher


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.matching;
17
18 import org.apache.avalon.framework.parameters.Parameters;
19 import org.apache.avalon.framework.thread.ThreadSafe;
20 import org.apache.cocoon.sitemap.PatternException;
21 import org.apache.cocoon.sitemap.SitemapParameters;
22 import org.apache.regexp.RE;
23 import org.apache.regexp.RECompiler;
24 import org.apache.regexp.REProgram;
25 import org.apache.regexp.RESyntaxException;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  * Base class for all matchers using a regular expression pattern.
32  *
33  * @author <a HREF="mailto:Giacomo.Pati@pwr.ch">Giacomo Pati</a>
34  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
35  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
36  * @version CVS $Id: AbstractRegexpMatcher.java 30932 2004-07-29 17:35:38Z vgritsenko $
37  */

38
39 public abstract class AbstractRegexpMatcher extends AbstractPreparableMatcher implements ThreadSafe {
40
41     /**
42      * Compile the pattern in a <code>org.apache.regexp.REProgram</code>.
43      */

44     public Object JavaDoc preparePattern(String JavaDoc pattern) throws PatternException {
45         // if pattern is null, return null to allow throwing a located exception in preparedMatch()
46
if (pattern == null) {
47             return null;
48         }
49
50         if (pattern.length() == 0) {
51             pattern = "^$";
52             if (getLogger().isWarnEnabled()) {
53                 getLogger().warn("The empty pattern string was rewritten to '^$'"
54                                  + " to match for empty strings. If you intended"
55                                  + " to match all strings, please change your"
56                                  + " pattern to '.*'");
57             }
58         }
59
60         try {
61             RECompiler compiler = new RECompiler();
62             REProgram program = compiler.compile(pattern);
63             return program;
64
65         } catch (RESyntaxException rse) {
66             getLogger().debug("Failed to compile the pattern '" + pattern + "'", rse);
67             throw new PatternException(rse.getMessage(), rse);
68         }
69     }
70
71     /**
72      * Match the prepared pattern against the value returned by {@link #getMatchString(Map, Parameters)}.
73      */

74     public Map JavaDoc preparedMatch(Object JavaDoc preparedPattern, Map JavaDoc objectModel, Parameters parameters) throws PatternException {
75
76         if(preparedPattern == null) {
77             throw new PatternException("A pattern is needed at " + SitemapParameters.getStatementLocation(parameters));
78         }
79
80         RE re = new RE((REProgram)preparedPattern);
81         String JavaDoc match = getMatchString(objectModel, parameters);
82
83         if (match == null)
84             return null;
85
86         if(re.match(match)) {
87             /* Handle parenthesised subexpressions. XXX: could be faster if we count
88              * parens *outside* the generated code.
89              * Note: *ONE* based, not zero, zero contains complete match
90              */

91             int parenCount = re.getParenCount();
92             Map JavaDoc map = new HashMap JavaDoc();
93             for (int paren = 0; paren <= parenCount; paren++) {
94                 map.put(Integer.toString(paren), re.getParen(paren));
95             }
96
97             return map;
98         }
99
100         return null;
101     }
102
103     /**
104      * Get the string to test against the regular expression. To be defined
105      * by concrete subclasses.
106      */

107     protected abstract String JavaDoc getMatchString(Map JavaDoc objectModel, Parameters parameters);
108 }
109
Popular Tags