/* * JBoss, Home of Professional Open Source. * * See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing. * * See the AUTHORS.txt file distributed with this work for a full listing of individual contributors. */ package org.teiid.query.ui.sqleditor.sql; import org.eclipse.jface.text.rules.ICharacterScanner; import org.eclipse.jface.text.rules.IToken; import org.eclipse.jface.text.rules.IWordDetector; import org.eclipse.jface.text.rules.Token; import org.eclipse.jface.text.rules.WordRule; /** * @author SDelap * Extends Word Rule by upper case-ing the token before attempting to get it from the map * thus returning tokens for all cases * * @since 8.0 */ public class CaseInsensitiveWordRule extends WordRule { /** * Creates a rule which, with the help of an word detector, will return the token * associated with the detected word. If no token has been associated, the scanner * will be rolled back and an undefined token will be returned in order to allow * any subsequent rules to analyze the characters. * * @param detector the word detector to be used by this rule, may not be <code>null</code> * * @see #addWord */ protected StringBuffer fBuffer= new StringBuffer(); public CaseInsensitiveWordRule(IWordDetector detector) { this(detector, Token.UNDEFINED); } /** * Creates a rule which, with the help of an word detector, will return the token * associated with the detected word. If no token has been associated, the * specified default token will be returned. * * @param detector the word detector to be used by this rule, may not be <code>null</code> * @param defaultToken the default token to be returned on success * if nothing else is specified, may not be <code>null</code> * * @see #addWord */ public CaseInsensitiveWordRule(IWordDetector detector, IToken defaultToken) { super(detector, defaultToken); } //Upper cases word before attempting to get out of the map @Override public IToken evaluate(ICharacterScanner scanner) { int c= scanner.read(); if (fDetector.isWordStart((char) c)) { if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) { fBuffer.setLength(0); do { fBuffer.append((char) c); c= scanner.read(); } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c)); scanner.unread(); IToken token= (IToken) fWords.get(fBuffer.toString().toUpperCase()); if (token != null) return token; if (fDefaultToken.isUndefined()) unreadBuffer(scanner); return fDefaultToken; } } scanner.unread(); return Token.UNDEFINED; } /* * Returns the characters in the buffer to the scanner. * Had to override since fBuffer is not protected * @param scanner the scanner to be used */ @Override protected void unreadBuffer(ICharacterScanner scanner) { for (int i= fBuffer.length() - 1; i >= 0; i--) scanner.unread(); } }