~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/rules/AuthorEmailRule.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
7
 *
 
8
 * Contributors:
 
9
 *    Red Hat - initial API and implementation
 
10
 *******************************************************************************/
 
11
 
 
12
package org.eclipse.linuxtools.internal.rpm.ui.editor.rules;
 
13
 
 
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;
 
18
 
 
19
public class AuthorEmailRule implements IPredicateRule {
 
20
 
 
21
        /** Buffer used for pattern detection */
 
22
        private StringBuilder fBuffer = new StringBuilder();
 
23
 
 
24
        /** The success token */
 
25
        IToken token;
 
26
 
 
27
        protected final static char START_CHAR = '<';
 
28
 
 
29
        protected final static char END_CHAR = '>';
 
30
 
 
31
        protected final static char[] INTER_CHARS = { '@', '.' };
 
32
 
 
33
        protected final static int STATE_START = 0;
 
34
 
 
35
        protected final static int STATE_OPENED = 1;
 
36
 
 
37
        protected final static int STATE_AT = 2;
 
38
 
 
39
        protected final static int STATE_PERIOD = 3;
 
40
 
 
41
        protected final static int STATE_DONE = 4;
 
42
 
 
43
        public AuthorEmailRule(IToken token) {
 
44
                this.token = token;
 
45
        }
 
46
 
 
47
        public IToken getSuccessToken() {
 
48
                return token;
 
49
        }
 
50
 
 
51
        public IToken evaluate(ICharacterScanner scanner, boolean resume) {
 
52
                /*
 
53
                 * whether we think we're reading the ending sequence, i.e. the next
 
54
                 * section heading
 
55
                 */
 
56
                int state = STATE_START;
 
57
                fBuffer.setLength(0);
 
58
                int c;
 
59
 
 
60
                do {
 
61
                        c = scanner.read();
 
62
                        fBuffer.append((char) c);
 
63
 
 
64
                        // we have reached the end of file or line prematurely, this is not
 
65
                        // considered success
 
66
                        if (c == ICharacterScanner.EOF || (char) c == '\n') {
 
67
                                unreadBuffer(scanner, fBuffer);
 
68
                                return Token.UNDEFINED;
 
69
                        }
 
70
 
 
71
                        // we encountered the opening character at the beginning
 
72
                        if (state == STATE_START && (char) c == START_CHAR) {
 
73
                                state++;
 
74
                        } else if (state == STATE_OPENED) {
 
75
                                // we encountered the first neccessary intermediary char
 
76
                                if ((char) c == INTER_CHARS[0]) {
 
77
                                        state++;
 
78
                                }
 
79
 
 
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;
 
84
                                }
 
85
                                
 
86
                                // we just keep reading
 
87
 
 
88
                        } else if (state == STATE_AT) {
 
89
                                // we encountered the second neccessary intermediary char
 
90
                                if ((char) c == INTER_CHARS[1]) {
 
91
                                        state++;
 
92
                                }
 
93
                                
 
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;
 
98
                                }
 
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) != '.')
 
103
                                        state++;
 
104
                                else if ((char) c == END_CHAR){
 
105
                                        unreadBuffer(scanner, fBuffer);
 
106
                                        return Token.UNDEFINED;
 
107
                                }
 
108
                        } else {
 
109
                                unreadBuffer(scanner, fBuffer);
 
110
                                return Token.UNDEFINED;
 
111
                        }
 
112
 
 
113
                } while (state != STATE_DONE);
 
114
                
 
115
                // we've gone through all states until we've reached STATE_DONE, success
 
116
                return token;
 
117
        }
 
118
 
 
119
        public IToken evaluate(ICharacterScanner scanner) {
 
120
                return evaluate(scanner, false);
 
121
        }
 
122
 
 
123
        /**
 
124
         * Returns the characters in the buffer to the scanner.
 
125
         * 
 
126
         * @param scanner
 
127
         *            the scanner to be used
 
128
         */
 
129
        protected void unreadBuffer(ICharacterScanner scanner, StringBuilder buffer) {
 
130
                for (int i = buffer.length() - 1; i >= 0; i--)
 
131
                        scanner.unread();
 
132
        }
 
133
 
 
134
}