1 23 24 package org.gjt.sp.jedit; 25 26 import java.lang.reflect.Method ; 28 import java.util.Hashtable ; 29 import java.util.Collections ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.regex.Pattern ; 34 import java.util.regex.PatternSyntaxException ; 35 import org.gjt.sp.jedit.indent.DeepIndentRule; 36 import org.gjt.sp.jedit.indent.IndentRule; 37 import org.gjt.sp.jedit.indent.IndentRuleFactory; 38 import org.gjt.sp.jedit.syntax.TokenMarker; 39 import org.gjt.sp.util.Log; 40 import org.gjt.sp.util.StandardUtilities; 41 43 50 public class Mode 51 { 52 60 public Mode(String name) 61 { 62 this.name = name; 63 props = new Hashtable <String , Object >(); 64 } 66 71 public void init() 72 { 73 try 74 { 75 String filenameGlob = (String )getProperty("filenameGlob"); 76 if(filenameGlob != null && filenameGlob.length() != 0) 77 { 78 filenameRE = Pattern.compile(StandardUtilities.globToRE(filenameGlob), 79 Pattern.CASE_INSENSITIVE); 80 } 81 82 String firstlineGlob = (String )getProperty("firstlineGlob"); 83 if(firstlineGlob != null && firstlineGlob.length() != 0) 84 { 85 firstlineRE = Pattern.compile(StandardUtilities.globToRE(firstlineGlob), 86 Pattern.CASE_INSENSITIVE); 87 } 88 } 89 catch(PatternSyntaxException re) 90 { 91 Log.log(Log.ERROR,this,"Invalid filename/firstline" 92 + " globs in mode " + name); 93 Log.log(Log.ERROR,this,re); 94 } 95 96 marker = null; 103 } 105 109 public TokenMarker getTokenMarker() 110 { 111 loadIfNecessary(); 112 return marker; 113 } 115 120 public void setTokenMarker(TokenMarker marker) 121 { 122 this.marker = marker; 123 } 125 130 public void loadIfNecessary() 131 { 132 if(marker == null) 133 { 134 jEdit.loadMode(this); 135 if (marker == null) 136 Log.log(Log.ERROR, this, "Mode not correctly loaded, token marker is still null"); 137 } 138 } 140 147 public Object getProperty(String key) 148 { 149 String prefix = "mode." + name + '.'; 150 151 String property = jEdit.getProperty(prefix + key); 154 if(property != null) 155 { 156 Object value; 157 try 158 { 159 value = new Integer (property); 160 } 161 catch(NumberFormatException nf) 162 { 163 value = property; 164 } 165 return value; 166 } 167 169 Object value = props.get(key); 170 if(value != null) 171 return value; 172 173 String global = jEdit.getProperty("buffer." + key); 174 if(global != null) 175 { 176 try 177 { 178 return new Integer (global); 179 } 180 catch(NumberFormatException nf) 181 { 182 return global; 183 } 184 } 185 else 186 return null; 187 } 189 196 public boolean getBooleanProperty(String key) 197 { 198 Object value = getProperty(key); 199 if("true".equals(value) || "on".equals(value) || "yes".equals(value)) 200 return true; 201 else 202 return false; 203 } 205 211 public void setProperty(String key, Object value) 212 { 213 props.put(key,value); 214 } 216 222 public void unsetProperty(String key) 223 { 224 props.remove(key); 225 } 227 232 public void setProperties(Map props) 233 { 234 if(props == null) 235 props = new Hashtable <String , Object >(); 236 237 String filenameGlob = (String )this.props.get("filenameGlob"); 241 String firstlineGlob = (String )this.props.get("firstlineGlob"); 242 String filename = (String )this.props.get("file"); 243 this.props = props; 244 if(filenameGlob != null) 245 props.put("filenameGlob",filenameGlob); 246 if(firstlineGlob != null) 247 props.put("firstlineGlob",firstlineGlob); 248 if(filename != null) 249 props.put("file",filename); 250 } 252 262 public boolean accept(String fileName, String firstLine) 263 { 264 if(filenameRE != null && filenameRE.matcher(fileName).matches()) 265 return true; 266 267 if(firstlineRE != null && firstlineRE.matcher(firstLine).matches()) 268 return true; 269 270 return false; 271 } 273 277 public String getName() 278 { 279 return name; 280 } 282 286 public String toString() 287 { 288 return name; 289 } 291 293 public synchronized List <IndentRule> getIndentRules() 294 { 295 if (indentRules == null) 296 { 297 initIndentRules(); 298 } 299 return indentRules; 300 } 301 302 public synchronized boolean isElectricKey(char ch) 303 { 304 if (electricKeys == null) 305 { 306 String [] props = { 307 "indentOpenBrackets", 308 "indentCloseBrackets", 309 "electricKeys" 310 }; 311 312 StringBuilder buf = new StringBuilder (); 313 for(int i = 0; i < props.length; i++) 314 { 315 String prop = (String ) getProperty(props[i]); 316 if (prop != null) 317 buf.append(prop); 318 } 319 320 electricKeys = buf.toString(); 321 } 322 323 return (electricKeys.indexOf(ch) >= 0); 324 } 325 326 private void initIndentRules() 327 { 328 List <IndentRule> rules = new LinkedList <IndentRule>(); 329 330 String [] regexpProps = { 331 "indentNextLine", 332 "indentNextLines" 333 }; 334 335 for(int i = 0; i < regexpProps.length; i++) 336 { 337 IndentRule rule = createRegexpIndentRule(regexpProps[i]); 338 if(rule != null) 339 rules.add(rule); 340 } 341 342 String [] bracketProps = { 343 "indentOpenBracket", 344 "indentCloseBracket", 345 "unalignedOpenBracket", 346 "unalignedCloseBracket", 347 }; 348 349 for(int i = 0; i < bracketProps.length; i++) 350 { 351 createBracketIndentRules(bracketProps[i], rules); 352 } 353 354 String [] finalProps = { 355 "unindentThisLine", 356 "unindentNextLines" 357 }; 358 359 for(int i = 0; i < finalProps.length; i++) 360 { 361 IndentRule rule = createRegexpIndentRule(finalProps[i]); 362 if(rule != null) 363 rules.add(rule); 364 } 365 366 if (getBooleanProperty("deepIndent")) 367 rules.add(new DeepIndentRule()); 368 369 indentRules = Collections.unmodifiableList(rules); 370 } 371 372 private IndentRule createRegexpIndentRule(String prop) 373 { 374 String value = (String ) getProperty(prop); 375 376 try 377 { 378 if(value != null) 379 { 380 Method m = IndentRuleFactory.class.getMethod( 381 prop,new Class [] { String .class }); 382 return (IndentRule)m.invoke(null, value); 383 } 384 } 385 catch(Exception e) 386 { 387 Log.log(Log.ERROR,this,"Bad indent rule " + prop 388 + '=' + value + ':'); 389 Log.log(Log.ERROR,this,e); 390 } 391 392 return null; 393 } 394 395 private void createBracketIndentRules(String prop, 396 List <IndentRule> rules) 397 { 398 String value = (String ) getProperty(prop + 's'); 399 400 try 401 { 402 if(value != null) 403 { 404 for(int i = 0; i < value.length(); i++) 405 { 406 char ch = value.charAt(i); 407 408 Method m = IndentRuleFactory.class.getMethod( 409 prop,new Class [] { char.class }); 410 rules.add((IndentRule) m.invoke(null, ch)); 411 } 412 } 413 } 414 catch(Exception e) 415 { 416 Log.log(Log.ERROR,this,"Bad indent rule " + prop 417 + '=' + value + ':'); 418 Log.log(Log.ERROR,this,e); 419 } 420 } 421 422 424 private String name; 426 private Map <String , Object > props; 427 private Pattern firstlineRE; 428 private Pattern filenameRE; 429 private TokenMarker marker; 430 private List <IndentRule> indentRules; 431 private String electricKeys; 432 } 434 | Popular Tags |