KickJava   Java API By Example, From Geeks To Geeks.

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


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.matching.helpers.WildcardHelper;
21 import org.apache.cocoon.sitemap.PatternException;
22 import org.apache.cocoon.sitemap.SitemapParameters;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 /**
28  * Base class for wildcard matchers
29  *
30  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
31  * @version CVS $Id: AbstractWildcardMatcher.java 30932 2004-07-29 17:35:38Z vgritsenko $
32  */

33
34 public abstract class AbstractWildcardMatcher extends AbstractPreparableMatcher implements ThreadSafe {
35
36     /**
37      * Compile the pattern in an <code>int[]</code>.
38      */

39     public Object JavaDoc preparePattern(String JavaDoc pattern) {
40         // if pattern is null, return null to allow throwing a located exception in preparedMatch()
41
return pattern == null ? null : WildcardHelper.compilePattern(pattern);
42     }
43
44     /**
45      * Match the prepared pattern against the result of {@link #getMatchString(Map, Parameters)}.
46      */

47     public Map JavaDoc preparedMatch(Object JavaDoc preparedPattern, Map JavaDoc objectModel, Parameters parameters) throws PatternException {
48
49         if(preparedPattern == null) {
50             throw new PatternException("A pattern is needed at " +
51                     SitemapParameters.getStatementLocation(parameters));
52         }
53
54         String JavaDoc match = getMatchString(objectModel, parameters);
55
56         if (match == null) {
57             return null;
58         }
59
60         HashMap JavaDoc map = new HashMap JavaDoc();
61
62         if (WildcardHelper.match(map, match, (int[])preparedPattern)) {
63             return map;
64         } else {
65             return null;
66         }
67     }
68
69     /**
70      * Get the string to test against the wildcard expression. To be defined
71      * by concrete subclasses.
72      */

73     protected abstract String JavaDoc getMatchString(Map JavaDoc objectModel, Parameters parameters);
74 }
75
Popular Tags