1
/*******************************************************************************
2
* Copyright (c) 2007, 2009 Red Hat, Inc.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Red Hat - initial API and implementation
10
*******************************************************************************/
12
package org.eclipse.linuxtools.internal.rpm.ui.editor.rules;
14
import org.eclipse.jface.text.rules.ICharacterScanner;
15
import org.eclipse.jface.text.rules.IPredicateRule;
16
import org.eclipse.jface.text.rules.IToken;
17
import org.eclipse.jface.text.rules.Token;
19
public class AuthorEmailRule implements IPredicateRule {
21
/** Buffer used for pattern detection */
22
private StringBuilder fBuffer = new StringBuilder();
24
/** The success token */
27
protected final static char START_CHAR = '<';
29
protected final static char END_CHAR = '>';
31
protected final static char[] INTER_CHARS = { '@', '.' };
33
protected final static int STATE_START = 0;
35
protected final static int STATE_OPENED = 1;
37
protected final static int STATE_AT = 2;
39
protected final static int STATE_PERIOD = 3;
41
protected final static int STATE_DONE = 4;
43
public AuthorEmailRule(IToken token) {
47
public IToken getSuccessToken() {
51
public IToken evaluate(ICharacterScanner scanner, boolean resume) {
53
* whether we think we're reading the ending sequence, i.e. the next
56
int state = STATE_START;
62
fBuffer.append((char) c);
64
// we have reached the end of file or line prematurely, this is not
66
if (c == ICharacterScanner.EOF || (char) c == '\n') {
67
unreadBuffer(scanner, fBuffer);
68
return Token.UNDEFINED;
71
// we encountered the opening character at the beginning
72
if (state == STATE_START && (char) c == START_CHAR) {
74
} else if (state == STATE_OPENED) {
75
// we encountered the first neccessary intermediary char
76
if ((char) c == INTER_CHARS[0]) {
80
// check if we have a valid char
81
if (! (Character.isLetterOrDigit((char) c) || c == '.' || c == '_' || c == '-' || c == '@')){
82
unreadBuffer(scanner, fBuffer);
83
return Token.UNDEFINED;
86
// we just keep reading
88
} else if (state == STATE_AT) {
89
// we encountered the second neccessary intermediary char
90
if ((char) c == INTER_CHARS[1]) {
94
// check if we have a valid char
95
if (! (Character.isLetterOrDigit((char) c) || c == '.' || c == '_' || c == '-')){
96
unreadBuffer(scanner, fBuffer);
97
return Token.UNDEFINED;
99
// we just keep reading
100
} else if (state == STATE_PERIOD) {
101
// the last char before the ending char cannot be a '.'
102
if ((char) c == END_CHAR && fBuffer.charAt(fBuffer.length() - 1) != '.')
104
else if ((char) c == END_CHAR){
105
unreadBuffer(scanner, fBuffer);
106
return Token.UNDEFINED;
109
unreadBuffer(scanner, fBuffer);
110
return Token.UNDEFINED;
113
} while (state != STATE_DONE);
115
// we've gone through all states until we've reached STATE_DONE, success
119
public IToken evaluate(ICharacterScanner scanner) {
120
return evaluate(scanner, false);
124
* Returns the characters in the buffer to the scanner.
127
* the scanner to be used
129
protected void unreadBuffer(ICharacterScanner scanner, StringBuilder buffer) {
130
for (int i = buffer.length() - 1; i >= 0; i--)