KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > core > urltemplates > TemplateTokenizer


1 /*
2  * Copyright 2005 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  * $Header:$
17  */

18 package org.apache.beehive.netui.core.urltemplates;
19
20 import java.util.Iterator JavaDoc;
21 import java.util.regex.Matcher JavaDoc;
22 import java.util.regex.Pattern JavaDoc;
23
24
25 /**
26  * The identified tokens and the text between the
27  * matching tokens in the template are all returned.
28  */

29 public class TemplateTokenizer implements Iterator JavaDoc
30 {
31
32     private static final String JavaDoc PATTERN = "\\{url:\\w+\\}";
33     private static final Pattern JavaDoc pattern = Pattern.compile( PATTERN );
34
35     private CharSequence JavaDoc _template;
36     private Matcher JavaDoc _matcher;
37     private int _endPrevios = 0;
38
39     // The current matched token value. If non-null and literal == null,
40
// should be returned at the next call to next()
41
private String JavaDoc _token;
42
43     // The current literal string. If non-null, non-empty, should be
44
// returned at the next call to next()
45
private String JavaDoc _literal;
46
47     public TemplateTokenizer( CharSequence JavaDoc template )
48     {
49         _template = template;
50         _matcher = pattern.matcher( _template );
51     }
52
53     /**
54      * Returns true if there are more literals or tokens/delimiters.
55      */

56     public boolean hasNext()
57     {
58         if ( _matcher == null )
59         {
60             return false;
61         }
62         if ( _literal != null || _token != null )
63         {
64             return true;
65         }
66         if ( _matcher.find() )
67         {
68             _literal = _template.subSequence( _endPrevios, _matcher.start() ).toString();
69             _token = _matcher.group();
70             _endPrevios = _matcher.end();
71         }
72         else if ( _endPrevios < _template.length() )
73         {
74             // We're at the end
75
_literal = _template.subSequence( _endPrevios, _template.length() ).toString();
76             _endPrevios = _template.length();
77
78             // Remove the matcher so it doesn't reset itself
79
_matcher = null;
80         }
81         return _literal != null || _token != null;
82     }
83
84     /**
85      * Returns the next literal string or token/delimiter.
86      */

87     public Object JavaDoc next()
88     {
89         String JavaDoc result = null;
90
91         if ( _literal != null )
92         {
93             result = _literal;
94             _literal = null;
95         }
96         else if ( _token != null )
97         {
98             result = _token;
99             _token = null;
100         }
101         return result;
102     }
103
104     /**
105      * Returns true if the call to next() will return a token rather
106      * than a literal.
107      */

108     public boolean isTokenNext()
109     {
110         return _literal == null && _token != null;
111     }
112
113     /**
114      * Not supported.
115      */

116     public void remove()
117     {
118         throw new UnsupportedOperationException JavaDoc();
119     }
120 }
121
Popular Tags