KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > spelling > PropertiesFileSpellingEngine


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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
12 package org.eclipse.jdt.internal.ui.text.spelling;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Locale JavaDoc;
18
19 import org.eclipse.core.runtime.IProgressMonitor;
20
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.IRegion;
24 import org.eclipse.jface.text.ITypedRegion;
25 import org.eclipse.jface.text.TextUtilities;
26 import org.eclipse.jface.text.TypedRegion;
27
28 import org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector;
29
30 import org.eclipse.jdt.ui.PreferenceConstants;
31
32 import org.eclipse.jdt.internal.ui.JavaPlugin;
33 import org.eclipse.jdt.internal.ui.propertiesfileeditor.IPropertiesFilePartitions;
34 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellChecker;
35
36 /**
37  * Properties file spelling engine
38  *
39  * @since 3.1
40  */

41 public class PropertiesFileSpellingEngine extends SpellingEngine {
42
43     /*
44      * @see org.eclipse.jdt.internal.ui.text.spelling.SpellingEngine#check(org.eclipse.jface.text.IDocument, org.eclipse.jface.text.IRegion[], org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellChecker, org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector, org.eclipse.core.runtime.IProgressMonitor)
45      */

46     protected void check(IDocument document, IRegion[] regions, ISpellChecker checker, ISpellingProblemCollector collector, IProgressMonitor monitor) {
47         SpellEventListener listener= new SpellEventListener(collector, document);
48         boolean isIgnoringAmpersand= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.SPELLING_IGNORE_AMPERSAND_IN_PROPERTIES);
49         try {
50             checker.addListener(listener);
51             List JavaDoc partitionList= new ArrayList JavaDoc();
52             for (int i= 0; i < regions.length; i++)
53                 partitionList.addAll(Arrays.asList(TextUtilities.computePartitioning(document, IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING, regions[i].getOffset(), regions[i].getLength(), false)));
54             ITypedRegion[] partitions= (ITypedRegion[]) partitionList.toArray(new ITypedRegion[partitionList.size()]);
55
56             for (int i= 0; i < partitions.length; i++) {
57                 if (monitor != null && monitor.isCanceled())
58                     return;
59                 if (listener.isProblemsThresholdReached())
60                     return;
61                 
62                 ITypedRegion partition= partitions[i];
63                 if (IPropertiesFilePartitions.COMMENT.equals(partition.getType())) {
64                     for (; i < partitions.length - 1; i++) {
65                         ITypedRegion next= partitions[i+1];
66                         int gapOffset= partition.getOffset() + partition.getLength();
67                         int gapLength= next.getOffset() - gapOffset;
68                         if ((IPropertiesFilePartitions.COMMENT.equals(next.getType()) || isWhitespace(document, next.getOffset(), next.getLength())) && isWhitespace(document, gapOffset, gapLength))
69                             partition= new TypedRegion(partition.getOffset(), next.getOffset() + next.getLength() - partition.getOffset(), partition.getType());
70                         else
71                             break;
72                     }
73                 }
74                 String JavaDoc partitionType= partition.getType();
75                 if (IPropertiesFilePartitions.COMMENT.equals(partitionType) || (!isIgnoringAmpersand && IPropertiesFilePartitions.PROPERTY_VALUE.equals(partitionType))) {
76                     Locale JavaDoc locale= checker.getLocale();
77                     checker.execute(new SpellCheckIterator(document, partition, locale));
78                 } else if (isIgnoringAmpersand && IPropertiesFilePartitions.PROPERTY_VALUE.equals(partitionType)) {
79                     Locale JavaDoc locale= checker.getLocale();
80                     checker.execute(new PropertiesFileSpellCheckIterator(document, partition, locale));
81                 }
82             }
83         } catch (BadLocationException x) {
84             JavaPlugin.log(x);
85         } finally {
86             checker.removeListener(listener);
87         }
88     }
89
90     /**
91      * Returns <code>true</code> iff the given region contains only
92      * whitespace.
93      *
94      * @param document the document
95      * @param offset the region's offset
96      * @param length the region's length
97      * @return <code>true</code> iff the given region contains only
98      * whitespace
99      */

100     private boolean isWhitespace(IDocument document, int offset, int length) {
101         try {
102             for (int i= 0; i < length; i++)
103                 if (!Character.isWhitespace(document.getChar(offset + i)))
104                     return false;
105             return true;
106         } catch (BadLocationException x) {
107             JavaPlugin.log(x);
108             return false;
109         }
110     }
111 }
112
Popular Tags