1 30 31 package de.susebox.jtopas.spi; 32 33 import java.util.Iterator ; 37 38 import de.susebox.jtopas.TokenizerProperty; 39 import de.susebox.jtopas.TokenizerProperties; 40 import de.susebox.jtopas.TokenizerException; 41 42 43 47 64 public class StandardSequenceHandler implements SequenceHandler { 65 66 73 public StandardSequenceHandler(TokenizerProperties props) { 74 _properties = props; 75 } 76 77 86 public boolean hasSequenceCommentOrString() { 87 if (_properties != null) { 88 return _properties.getSpecialSequences().hasNext(); 89 } else { 90 return false; 91 } 92 } 93 94 110 public TokenizerProperty startsWithSequenceCommentOrString(DataProvider dataProvider) 111 throws NullPointerException 112 { 113 TokenizerProperty prop = null; 114 115 if (_properties != null) { 116 String data = dataProvider.toString(); 117 118 prop = getLongestMatch(data, _properties.getSpecialSequences(), prop); 119 prop = getLongestMatch(data, _properties.getLineComments(), prop); 120 prop = getLongestMatch(data, _properties.getBlockComments(), prop); 121 prop = getLongestMatch(data, _properties.getStrings(), prop); 122 } 123 return prop; 124 } 125 126 141 public int getSequenceMaxLength() { 142 int maxLength = 0; 143 144 if (_properties != null) { 145 maxLength = getSequenceMaxLength(_properties.getSpecialSequences(), maxLength); 146 maxLength = getSequenceMaxLength(_properties.getLineComments(), maxLength); 147 maxLength = getSequenceMaxLength(_properties.getBlockComments(), maxLength); 148 maxLength = getSequenceMaxLength(_properties.getStrings(), maxLength); 149 } 150 return maxLength; 151 } 152 153 162 private int getSequenceMaxLength(Iterator iter, int currentMax) { 163 while (iter.hasNext()) { 164 TokenizerProperty prop = (TokenizerProperty)iter.next(); 165 int len = prop.getImages()[0].length(); 166 167 if (len > currentMax) { 168 currentMax = len; 169 } 170 } 171 return currentMax; 172 } 173 174 175 184 private TokenizerProperty getLongestMatch( 185 String data, 186 Iterator iter, 187 TokenizerProperty currentMatch 188 ) 189 { 190 int currentMax = (currentMatch != null) ? currentMatch.getImages()[0].length() : 0; 191 TokenizerProperty retProp = currentMatch; 192 193 while (iter.hasNext()) { 194 TokenizerProperty prop = (TokenizerProperty)iter.next(); 195 int len = prop.getImages()[0].length(); 196 197 if (len > currentMax) { 198 currentMax = len; 199 retProp = prop; 200 } 201 } 202 return retProp; 203 } 204 205 206 210 214 private TokenizerProperties _properties = null; 215 } 216 | Popular Tags |