KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > util > matcher > UriPattern


1 package org.sapia.soto.util.matcher;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Map JavaDoc;
5
6 /**
7  * A performant pattern matching implementation that works on top of the
8  * <code>UriPatternHelper</code> class.
9  *
10  * @see org.sapia.soto.util.matcher.UriPatternHelper
11  *
12  * @author Yanick Duchesne
13  *
14  * <dl>
15  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
16  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
17  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
18  * </dl>
19  */

20 public class UriPattern implements Pattern{
21
22     private String JavaDoc _pattern;
23     private int[] _compiled;
24     
25     
26     private UriPattern(String JavaDoc pattern, int[] compiled){
27         _pattern = pattern;
28         _compiled = compiled;
29     }
30
31     /**
32      * @param pattern a string pattern
33      * @return a <code>UriPattern</code> instance.
34      */

35     public static UriPattern parse(String JavaDoc pattern){
36         return new UriPattern(pattern, UriPatternHelper.compilePattern(pattern));
37         
38     }
39     
40     /**
41    * @see org.sapia.soto.util.matcher.Pattern#matches(java.lang.String)
42    */

43   public boolean matches(String JavaDoc str) {
44     return matchResult(str).matched;
45   }
46
47   /**
48    * Tests if the given string matches the pattern represented by this instance.
49    *
50    * @param data a string to test this pattern against.
51    * @return a <code>MatchResult</code>.
52    */

53     public MatchResult matchResult(String JavaDoc data){
54         Map JavaDoc map = new HashMap JavaDoc();
55         MatchResult res = new MatchResult();
56         res.result = map;
57         res.matched = UriPatternHelper.match(map, data, _compiled);
58         return res;
59     }
60     
61     /**
62      * This class represents the result of a pattern-matching operation
63      *
64      * @author Yanick Duchesne
65      *
66      * <dl>
67      * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
68      * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
69      * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
70      * </dl>
71      */

72     public static final class MatchResult{
73         
74         /**
75          * The <code>Map</code> containing the tokens that have been matched. The
76          * token are bound under the index corresponding to the order in which they
77          * have been matched.
78          */

79         public Map JavaDoc result;
80         
81         /**
82          * Returns <code>true</code> if the matching operation was successful.
83          */

84         public boolean matched;
85     }
86
87 }
88
Popular Tags