1 30 31 package de.susebox.jtopas.impl; 32 33 import java.util.regex.Pattern ; 37 import java.util.regex.Matcher ; 38 import java.util.regex.PatternSyntaxException ; 39 40 import de.susebox.jtopas.TokenizerProperty; 41 import de.susebox.jtopas.Flags; 42 import de.susebox.jtopas.TokenizerException; 43 44 import de.susebox.jtopas.spi.PatternHandler; 45 import de.susebox.jtopas.spi.DataProvider; 46 47 48 52 59 public class PatternMatcher implements PatternHandler { 60 61 65 75 public PatternMatcher(TokenizerProperty prop, int globalFlags) throws NullPointerException { 76 _globalFlags = globalFlags; 77 setProperty(prop); 78 } 79 80 81 85 91 public boolean hasPattern() { 92 return true; 93 } 94 95 108 public PatternHandler.Result matches(DataProvider dataProvider) 109 throws TokenizerException, NullPointerException 110 { 111 try { 113 String [] groups; 114 115 _matcher.reset(new DataProviderCharSequence(dataProvider)); 116 if (_matcher.lookingAt()) { 117 if (_property.isFlagSet(Flags.F_RETURN_IMAGE_PARTS, (_globalFlags & Flags.F_RETURN_IMAGE_PARTS) != 0)) { 118 groups = new String [_matcher.groupCount() + 1]; 120 for (int index = 0; index < groups.length; ++index) { 121 groups[index] = _matcher.group(index); 122 } 123 } else { 124 groups = new String [] {}; 125 } 126 return new LocalResult(_property, _matcher.end(), groups); 127 } else { 128 return null; 129 } 130 } catch (Exception ex) { 131 throw new TokenizerException(ex); 132 } 133 } 134 135 136 140 147 public void setProperty(TokenizerProperty prop) throws NullPointerException { 148 if (prop == null) { 150 throw new NullPointerException ("No property given."); 151 } else if (prop.getImages() == null || prop.getImages().length < 1 || prop.getImages()[0] == null) { 152 throw new NullPointerException ("Property contains no pattern image."); 153 } 154 155 int flags = Pattern.MULTILINE | Pattern.DOTALL; 157 158 if (prop.isFlagSet(Flags.F_NO_CASE, (_globalFlags & Flags.F_NO_CASE) != 0)) { 159 flags |= Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE; 160 } 161 _matcher = Pattern.compile(prop.getImages()[0], flags).matcher(""); 162 163 _property = prop; 165 } 166 167 172 public TokenizerProperty getProperty() { 173 return _property; 174 } 175 176 177 181 184 private final class LocalResult implements PatternHandler.Result { 185 186 193 protected LocalResult(TokenizerProperty prop, int lengthOfMatch, String [] groups) { 194 _property = prop; 195 _lengthOfMatch = lengthOfMatch; 196 _groups = groups; 197 } 198 199 204 public String [] getGroups() throws TokenizerException { 205 return _groups; 206 } 207 208 213 public int getLengthOfMatch() { 214 return _lengthOfMatch; 215 } 216 217 223 public TokenizerProperty getProperty() { 224 return _property; 225 } 226 227 private TokenizerProperty _property; 229 private int _lengthOfMatch; 230 private String [] _groups; 231 } 232 233 237 private final class DataProviderCharSequence implements CharSequence { 238 239 244 public DataProviderCharSequence(DataProvider dataProvider) { 245 this(dataProvider, dataProvider.getStartPosition(), dataProvider.getLength()); 246 } 247 248 255 private DataProviderCharSequence(DataProvider dataProvider, int start, int length) { 256 _dataProvider = dataProvider; 257 _start = start; 258 _length = length; 259 } 260 261 273 public char charAt(int index) throws ArrayIndexOutOfBoundsException { 274 return _dataProvider.getCharAt(_start + index - _dataProvider.getStartPosition()); 275 } 276 277 283 public int length() { 284 return _length; 285 } 286 287 299 public CharSequence subSequence(int start, int end) { 300 if (start < 0 || end < 0 || end > length() || start > end) { 301 throw new IndexOutOfBoundsException (); 302 } 303 return new DataProviderCharSequence(_dataProvider, _start + start, end - start); 304 } 305 306 311 public String toString() { 312 int realStart = _start - _dataProvider.getStartPosition(); 313 314 return _dataProvider.toString().substring(realStart, realStart + _length); 315 } 316 317 private DataProvider _dataProvider = null; 319 private int _start = 0; 320 private int _length = 0; 321 } 322 323 324 private TokenizerProperty _property = null; 328 private Matcher _matcher = null; 329 private int _globalFlags = 0; 330 } 331 332 | Popular Tags |