KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > propertiesfileeditor > PropertiesFilePartitionScanner


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.propertiesfileeditor;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.text.rules.EndOfLineRule;
18 import org.eclipse.jface.text.rules.ICharacterScanner;
19 import org.eclipse.jface.text.rules.IPredicateRule;
20 import org.eclipse.jface.text.rules.IToken;
21 import org.eclipse.jface.text.rules.IWordDetector;
22 import org.eclipse.jface.text.rules.RuleBasedPartitionScanner;
23 import org.eclipse.jface.text.rules.SingleLineRule;
24 import org.eclipse.jface.text.rules.Token;
25 import org.eclipse.jface.text.rules.WordRule;
26
27 /**
28  * This scanner recognizes the comments, property keys and property values.
29  *
30  * @since 3.1
31  */

32 public class PropertiesFilePartitionScanner extends RuleBasedPartitionScanner implements IPropertiesFilePartitions {
33
34     /**
35      * Detector for empty comments.
36      */

37     static class EmptyCommentDetector implements IWordDetector {
38
39         /*
40          * @see IWordDetector#isWordStart
41          */

42         public boolean isWordStart(char c) {
43             return (c == '#');
44         }
45
46         /*
47          * @see IWordDetector#isWordPart
48          */

49         public boolean isWordPart(char c) {
50             return (c == '#');
51         }
52     }
53
54
55     /**
56      * Word rule for empty comments.
57      */

58     static class EmptyCommentRule extends WordRule implements IPredicateRule {
59
60         private IToken fSuccessToken;
61         /**
62          * Constructor for EmptyCommentRule.
63          * @param successToken the success token
64          */

65         public EmptyCommentRule(IToken successToken) {
66             super(new EmptyCommentDetector());
67             fSuccessToken= successToken;
68             addWord("#", fSuccessToken); //$NON-NLS-1$
69
}
70
71         /*
72          * @see IPredicateRule#evaluate(ICharacterScanner, boolean)
73          */

74         public IToken evaluate(ICharacterScanner scanner, boolean resume) {
75             return evaluate(scanner);
76         }
77
78         /*
79          * @see IPredicateRule#getSuccessToken()
80          */

81         public IToken getSuccessToken() {
82             return fSuccessToken;
83         }
84     }
85
86
87     /**
88      * Creates the partitioner and sets up the appropriate rules.
89      */

90     public PropertiesFilePartitionScanner() {
91         super();
92
93         IToken comment= new Token(COMMENT);
94         IToken propertyValue= new Token(PROPERTY_VALUE);
95         IToken key= new Token(IDocument.DEFAULT_CONTENT_TYPE);
96
97         List JavaDoc rules= new ArrayList JavaDoc();
98
99         // Add rule for leading white space.
100
rules.add(new LeadingWhitespacePredicateRule(key, "\t")); //$NON-NLS-1$
101
rules.add(new LeadingWhitespacePredicateRule(key, " ")); //$NON-NLS-1$
102

103         // Add rules for comments.
104
rules.add(new EndOfLineRule("#", comment, (char) 0, true)); //$NON-NLS-1$
105
rules.add(new EndOfLineRule("!", comment, (char) 0, true)); //$NON-NLS-1$
106

107         // Add rules for property values.
108
rules.add(new SingleLineRule("=", null, propertyValue, '\\', true, true)); //$NON-NLS-1$
109
rules.add(new SingleLineRule(":", null, propertyValue, '\\', true, true)); //$NON-NLS-1$
110
rules.add(new SingleLineRule(" ", null, propertyValue, '\\', true, true)); //$NON-NLS-1$
111
rules.add(new SingleLineRule("\t", null, propertyValue, '\\', true, true)); //$NON-NLS-1$
112

113         // Add special case word rule.
114
EmptyCommentRule wordRule= new EmptyCommentRule(comment);
115         rules.add(wordRule);
116
117         IPredicateRule[] result= new IPredicateRule[rules.size()];
118         rules.toArray(result);
119         setPredicateRules(result);
120     }
121 }
122
Popular Tags