KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > text > TagRule


1 /*******************************************************************************
2  * Copyright (c) 2002, 2006 GEBIT Gesellschaft fuer EDV-Beratung
3  * und Informatik-Technologien mbH,
4  * Berlin, Duesseldorf, Frankfurt (Germany) and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * GEBIT Gesellschaft fuer EDV-Beratung und Informatik-Technologien mbH - initial API and implementation
12  * IBM Corporation - bug 24108, bug 111740
13  * John-Mason P. Shackelford - bug 51215
14  *******************************************************************************/

15
16 package org.eclipse.ant.internal.ui.editor.text;
17
18 /*
19  * This file originates from an internal package of Eclipse's
20  * Manifest Editor. It has been copied by GEBIT to here in order to
21  * permanently use those features. It has been renamed and edited by GEBIT
22  * after copying.
23  */

24
25 import org.eclipse.jface.text.rules.ICharacterScanner;
26 import org.eclipse.jface.text.rules.IToken;
27 import org.eclipse.jface.text.rules.MultiLineRule;
28
29 public class TagRule extends MultiLineRule {
30
31     public TagRule(IToken token) {
32         super("<", ">", token); //$NON-NLS-1$ //$NON-NLS-2$
33
}
34
35     protected boolean sequenceDetected(ICharacterScanner scanner, char[] sequence, boolean eofAllowed) {
36         int c = scanner.read();
37         if (sequence[0] == '<') {
38             if (c == '?') {
39                 // processing instruction - abort
40
scanner.unread();
41                 return false;
42             }
43             if (c == '!') {
44                 scanner.unread();
45                 // comment - abort
46
return false;
47             }
48         } else if (sequence[0] == '>') {
49             scanner.unread();
50         }
51
52         return super.sequenceDetected(scanner, sequence, eofAllowed);
53     }
54
55     /*
56      * (non-Javadoc)
57      *
58      * @see org.eclipse.jface.text.rules.PatternRule#endSequenceDetected(org.eclipse.jface.text.rules.ICharacterScanner)
59      */

60     protected boolean endSequenceDetected(ICharacterScanner scanner) {
61         int c;
62         while ((c = scanner.read()) != ICharacterScanner.EOF) {
63             if (c == fEscapeCharacter) {
64                 // Skip the escaped character.
65
scanner.read();
66             } else if (c == '>') {
67                 endOfTagDetected(scanner);
68                 return true;
69             }
70         }
71         
72         scanner.unread();
73         return false;
74     }
75
76     private void endOfTagDetected(ICharacterScanner scanner) {
77         int c;
78         int scanAhead = 0;
79         int endOfTagOffset = 0;
80         while ((c = scanner.read()) != ICharacterScanner.EOF && c != '<') {
81             scanAhead++;
82             if (c == '>') {
83                 endOfTagOffset = scanAhead;
84             }
85         }
86
87         if (c == '<') {
88             int rewind = (scanAhead - endOfTagOffset) + 1;
89             for (int i = 0; i < rewind; i++) {
90                 scanner.unread();
91             }
92         }
93     }
94 }
95
Popular Tags