KickJava   Java API By Example, From Geeks To Geeks.

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


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.engine;
13
14 import java.io.FileOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.OutputStreamWriter JavaDoc;
17 import java.net.URL JavaDoc;
18 import java.nio.ByteBuffer JavaDoc;
19 import java.nio.charset.Charset JavaDoc;
20
21 import org.eclipse.jdt.internal.ui.JavaPlugin;
22
23
24 /**
25  * Persistent modifiable word-list based dictionary.
26  *
27  * @since 3.0
28  */

29 public class PersistentSpellDictionary extends AbstractSpellDictionary {
30
31     /** The word list location */
32     private final URL JavaDoc fLocation;
33
34     /**
35      * Creates a new persistent spell dictionary.
36      *
37      * @param url
38      * The URL of the word list for this dictionary
39      */

40     public PersistentSpellDictionary(final URL JavaDoc url) {
41         fLocation= url;
42     }
43
44     /*
45      * @see org.eclipse.jdt.ui.text.spelling.engine.AbstractSpellDictionary#acceptsWords()
46      */

47     public boolean acceptsWords() {
48         return true;
49     }
50
51     /*
52      * @see org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellDictionary#addWord(java.lang.String)
53      */

54     public void addWord(final String JavaDoc word) {
55         if (isCorrect(word))
56             return;
57
58         OutputStreamWriter JavaDoc writer= null;
59         try {
60             Charset JavaDoc charset= Charset.forName(getEncoding());
61             ByteBuffer JavaDoc byteBuffer= charset.encode(word + "\n"); //$NON-NLS-1$
62
int size= byteBuffer.limit();
63             final byte[] byteArray;
64             if (byteBuffer.hasArray())
65                 byteArray= byteBuffer.array();
66             else {
67                 byteArray= new byte[size];
68                 byteBuffer.get(byteArray);
69             }
70             
71             FileOutputStream JavaDoc fileStream= new FileOutputStream JavaDoc(fLocation.getPath(), true);
72             
73             // Encoding UTF-16 charset writes a BOM. In which case we need to cut it away if the file isn't empty
74
int bomCutSize= 0;
75             if (!isEmpty() && "UTF-16".equals(charset.name())) //$NON-NLS-1$
76
bomCutSize= 2;
77             
78             fileStream.write(byteArray, bomCutSize, size - bomCutSize);
79         } catch (IOException JavaDoc exception) {
80             JavaPlugin.log(exception);
81             return;
82         } finally {
83             try {
84                 if (writer != null)
85                     writer.close();
86             } catch (IOException JavaDoc e) {
87             }
88         }
89
90         hashWord(word);
91     }
92
93     /*
94      * @see org.eclipse.jdt.internal.ui.text.spelling.engine.AbstractSpellDictionary#getURL()
95      */

96     protected final URL JavaDoc getURL() {
97         return fLocation;
98     }
99 }
100
Popular Tags