1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.propertiesfileeditor;
13 import java.util.ArrayList;
14 import java.util.List;
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;
28 * This scanner recognizes the comments, property keys and property values.
32 public class PropertiesFilePartitionScanner extends RuleBasedPartitionScanner implements IPropertiesFilePartitions {
35 * Detector for empty comments.
37 static class EmptyCommentDetector implements IWordDetector {
40 * @see IWordDetector#isWordStart
42 public boolean isWordStart(char c) {
47 * @see IWordDetector#isWordPart
49 public boolean isWordPart(char c) {
56 * Word rule for empty comments.
58 static class EmptyCommentRule extends WordRule implements IPredicateRule {
60 private IToken fSuccessToken;
62 * Constructor for EmptyCommentRule.
63 * @param successToken the success token
65 public EmptyCommentRule(IToken successToken) {
66 super(new EmptyCommentDetector());
67 fSuccessToken= successToken;
68 addWord("#", fSuccessToken); //$NON-NLS-1$
72 * @see IPredicateRule#evaluate(ICharacterScanner, boolean)
74 public IToken evaluate(ICharacterScanner scanner, boolean resume) {
75 return evaluate(scanner);
79 * @see IPredicateRule#getSuccessToken()
81 public IToken getSuccessToken() {
88 * Creates the partitioner and sets up the appropriate rules.
90 public PropertiesFilePartitionScanner() {
93 IToken comment= new Token(COMMENT);
94 IToken propertyValue= new Token(PROPERTY_VALUE);
95 IToken key= new Token(IDocument.DEFAULT_CONTENT_TYPE);
97 List<IPredicateRule> rules= new ArrayList<IPredicateRule>();
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$
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$
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$
113 // Add special case word rule.
114 EmptyCommentRule wordRule= new EmptyCommentRule(comment);
117 IPredicateRule[] result= new IPredicateRule[rules.size()];
118 rules.toArray(result);
119 setPredicateRules(result);