efedb0f29dd77241d7dfa89525b9741281a6ffa2
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / xml / ui / internal / text / EntityRule.java
1 /*
2  * Copyright (c) 2002-2004 Widespace, OU and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *     Igor Malinin - initial contribution
10  *
11  * $Id: EntityRule.java,v 1.1 2004-09-02 18:28:03 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.xml.ui.internal.text;
15
16 import org.eclipse.jface.text.rules.ICharacterScanner;
17 import org.eclipse.jface.text.rules.IRule;
18 import org.eclipse.jface.text.rules.IToken;
19 import org.eclipse.jface.text.rules.Token;
20
21 /**
22  * Rule detecting XML or DTD entities.
23  * 
24  * @author Igor Malinin
25  */
26 public class EntityRule implements IRule {
27
28         private static NameDetector detector = new NameDetector();
29
30         private char start;
31
32         private IToken token;
33
34         public EntityRule(char start, IToken token) {
35                 this.start = start;
36                 this.token = token;
37         }
38
39         public IToken evaluate(ICharacterScanner scanner) {
40                 int ch = scanner.read();
41
42                 if (ch == start) {
43                         ch = scanner.read();
44                         if (ch == ICharacterScanner.EOF) {
45                                 scanner.unread();
46                                 return token;
47                         }
48                         if (ch == ';') {
49                                 return token;
50                         }
51                         if (!detector.isWordStart((char) ch)) {
52                                 scanner.unread();
53                                 return token;
54                         }
55
56                         while (true) {
57                                 ch = scanner.read();
58                                 if (ch == ICharacterScanner.EOF) {
59                                         scanner.unread();
60                                         return token;
61                                 }
62                                 if (ch == ';') {
63                                         return token;
64                                 }
65                                 if (!detector.isWordPart((char) ch)) {
66                                         scanner.unread();
67                                         return token;
68                                 }
69                         }
70                 }
71                 scanner.unread();
72
73                 return Token.UNDEFINED;
74         }
75 }