]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/text/spelling/engine/PersistentSpellDictionary.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / text / spelling / engine / PersistentSpellDictionary.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *     Benjamin Muskalla <b.muskalla@gmx.net> - [spell checking][implementation] PersistentSpellDictionary closes wrong stream - https://bugs.eclipse.org/bugs/show_bug.cgi?id=236421
11  *******************************************************************************/
12 package org.eclipse.jdt.internal.ui.text.spelling.engine;
13
14 import java.io.FileOutputStream;
15 import java.io.IOException;
16 import java.net.URL;
17 import java.nio.ByteBuffer;
18 import java.nio.charset.Charset;
19
20 import org.eclipse.jdt.internal.ui.JavaPlugin;
21
22
23 /**
24  * Persistent modifiable word-list based dictionary.
25  *
26  * @since 3.0
27  */
28 public class PersistentSpellDictionary extends AbstractSpellDictionary {
29
30         /** The word list location */
31         private final URL fLocation;
32
33         /**
34          * Creates a new persistent spell dictionary.
35          *
36          * @param url the URL of the word list for this dictionary
37          */
38         public PersistentSpellDictionary(final URL url) {
39                 fLocation= url;
40         }
41
42         /*
43          * @see org.eclipse.jdt.ui.text.spelling.engine.AbstractSpellDictionary#acceptsWords()
44          */
45         @Override
46         public boolean acceptsWords() {
47                 return true;
48         }
49
50         /*
51          * @see org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellDictionary#addWord(java.lang.String)
52          */
53         @Override
54         public void addWord(final String word) {
55                 if (isCorrect(word))
56                         return;
57
58                 FileOutputStream fileStream= null;
59                 try {
60                         Charset charset= Charset.forName(getEncoding());
61                         ByteBuffer 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                         fileStream= new FileOutputStream(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 exception) {
80                         JavaPlugin.log(exception);
81                         return;
82                 } finally {
83                         try {
84                                 if (fileStream != null)
85                                         fileStream.close();
86                         } catch (IOException e) {
87                         }
88                 }
89
90                 hashWord(word);
91         }
92
93         /*
94          * @see org.eclipse.jdt.internal.ui.text.spelling.engine.AbstractSpellDictionary#getURL()
95          */
96         @Override
97         protected final URL getURL() {
98                 return fLocation;
99         }
100 }