1 18 package org.apache.beehive.netui.core.urltemplates; 19 20 import java.util.Iterator ; 21 import java.util.regex.Matcher ; 22 import java.util.regex.Pattern ; 23 24 25 29 public class TemplateTokenizer implements Iterator 30 { 31 32 private static final String PATTERN = "\\{url:\\w+\\}"; 33 private static final Pattern pattern = Pattern.compile( PATTERN ); 34 35 private CharSequence _template; 36 private Matcher _matcher; 37 private int _endPrevios = 0; 38 39 private String _token; 42 43 private String _literal; 46 47 public TemplateTokenizer( CharSequence template ) 48 { 49 _template = template; 50 _matcher = pattern.matcher( _template ); 51 } 52 53 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 _literal = _template.subSequence( _endPrevios, _template.length() ).toString(); 76 _endPrevios = _template.length(); 77 78 _matcher = null; 80 } 81 return _literal != null || _token != null; 82 } 83 84 87 public Object next() 88 { 89 String 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 108 public boolean isTokenNext() 109 { 110 return _literal == null && _token != null; 111 } 112 113 116 public void remove() 117 { 118 throw new UnsupportedOperationException (); 119 } 120 } 121 | Popular Tags |