KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > ide > IDEEncoding


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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.ui.ide;
12
13 import java.nio.charset.Charset JavaDoc;
14 import java.nio.charset.IllegalCharsetNameException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.eclipse.core.resources.ResourcesPlugin;
22 import org.eclipse.core.runtime.content.IContentDescription;
23 import org.eclipse.osgi.util.NLS;
24 import org.eclipse.ui.WorkbenchEncoding;
25 import org.eclipse.ui.internal.WorkbenchPlugin;
26 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
27 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
28
29 /**
30  * IDEEncoding is a utility class for managing encoding information that
31  * includes user preferences from the IDE and core resources.
32  * <p>
33  * This class provides all its functionality via static methods.
34  * It is not intended to be instantiated or subclassed.
35  * </p>
36  *
37  * @see org.eclipse.ui.WorkbenchEncoding
38  * @see org.eclipse.core.resources.ResourcesPlugin
39  * @since 3.1
40  */

41 public final class IDEEncoding {
42     
43     private IDEEncoding () {
44         // prevent instantiation
45
}
46
47     //The preference for the user entered encodings.
48
private static String JavaDoc IDE_ENCODINGS_PREFERENCE = "IDE_ENCODINGS_PREFERENCE"; //$NON-NLS-1$
49

50     private static String JavaDoc PREFERENCE_SEPARATOR = "'"; //$NON-NLS-1$
51

52     /**
53      * Display constant for the UTF 8 byte order marker for resources.
54      */

55     public static String JavaDoc BOM_UTF_8 = "UTF-8 (BOM)";//$NON-NLS-1$
56

57     /**
58      * Display constant for the UTF 16 big endian byte order marker for
59      * resources.
60      */

61     public static String JavaDoc BOM_UTF_16BE = "UTF-16 Big-Endian (BOM)";//$NON-NLS-1$
62

63     /**
64      * Display constant for the UTF 16 little endian byte order marker for
65      * resources.
66      */

67     public static String JavaDoc BOM_UTF_16LE = "UTF-16 Little-Endian (BOM)";//$NON-NLS-1$
68

69     /**
70      * Get all of the available encodings including any that were saved as a
71      * preference in the IDE or in core resources.
72      *
73      * @return List of String
74      */

75     public static List JavaDoc getIDEEncodings() {
76         List JavaDoc encodings = getIDEEncodingsPreference();
77         encodings.addAll(WorkbenchEncoding.getDefinedEncodings());
78
79         String JavaDoc enc = getResourceEncoding();
80
81         if (!(enc == null || encodings.contains(enc))) {
82             encodings.add(enc);
83         }
84
85         Collections.sort(encodings);
86         return encodings;
87     }
88
89     /**
90      * Get the current value of the encoding preference. If the value is not set
91      * return <code>null</code>.
92      *
93      * @return String
94      */

95     public static String JavaDoc getResourceEncoding() {
96         String JavaDoc preference = ResourcesPlugin.getPlugin().getPluginPreferences().getString(
97                 ResourcesPlugin.PREF_ENCODING);
98         if (preference == null || preference.length() == 0) {
99             return null;
100         }
101         return preference;
102     }
103
104     /**
105      * Add value to the list of workbench encodings.
106      *
107      * @param value
108      */

109     public static void addIDEEncoding(String JavaDoc value) {
110
111         if (WorkbenchEncoding.getDefinedEncodings().contains(value)) {
112             return;
113         }
114
115         writeEncodingsPreference(value, getIDEEncodingsPreference());
116
117     }
118
119     /**
120      * Write the encodings preference. If value is not null
121      * and not already in the list of currentEncodings add
122      * it to the list.
123      * @param value String or <code>null</code>
124      * @param encodings The list of encodings to write
125      */

126     private static void writeEncodingsPreference(String JavaDoc value, Collection JavaDoc encodings) {
127         boolean addValue = (value != null);
128
129         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
130
131         Iterator JavaDoc currentEncodings = encodings.iterator();
132
133         while (currentEncodings.hasNext()) {
134             String JavaDoc string = (String JavaDoc) currentEncodings.next();
135             result.append(string);
136             result.append(PREFERENCE_SEPARATOR);
137             if (addValue && string.equals(value)) {
138                 addValue = false;
139             }
140         }
141
142         if (addValue) {
143             result.append(value);
144         }
145
146         IDEWorkbenchPlugin.getDefault().getPreferenceStore().setValue(IDE_ENCODINGS_PREFERENCE,
147                 result.toString());
148     }
149
150     /**
151      * Get the value of the encodings preference.
152      *
153      * @return List
154      */

155     private static List JavaDoc getIDEEncodingsPreference() {
156         
157         boolean updateRequired = false;
158         
159         String JavaDoc encodings = IDEWorkbenchPlugin.getDefault().getPreferenceStore().getString(
160                 IDE_ENCODINGS_PREFERENCE);
161
162         if (encodings == null || encodings.length() == 0) {
163             return new ArrayList JavaDoc();
164         }
165
166         String JavaDoc[] preferenceEncodings = encodings.split(PREFERENCE_SEPARATOR);
167         ArrayList JavaDoc result = new ArrayList JavaDoc();
168
169         //Drop any encodings that are not valid
170
for (int i = 0; i < preferenceEncodings.length; i++) {
171             String JavaDoc string = preferenceEncodings[i];
172             boolean isSupported;
173             try {
174                 isSupported = Charset.isSupported(string);
175             } catch (IllegalCharsetNameException JavaDoc e) {
176                 isSupported = false;
177             }
178             if (isSupported) {
179                 result.add(string);
180             } else{
181                 WorkbenchPlugin.log(NLS.bind(IDEWorkbenchMessages.WorkbenchEncoding_invalidCharset, string));
182                 updateRequired = true;
183             }
184                 
185         }
186         
187         if(updateRequired) {
188             writeEncodingsPreference(null, result);
189         }
190         return result;
191
192     }
193
194     /**
195      * Clear the IDE encodings preference.
196      */

197     public static void clearUserEncodings() {
198         IDEWorkbenchPlugin.getDefault().getPreferenceStore().setToDefault(IDE_ENCODINGS_PREFERENCE);
199     }
200
201     /**
202      * Get the displayable string for the byte order marking from the supplied
203      * file description.
204      *
205      * @param description
206      * The description to query. May be <code>null</code>.
207      * @return String or <code>null</code> if the byte order mark cannot be
208      * found or the description is <code>null</code>.
209      * @see IContentDescription#getProperty(org.eclipse.core.runtime.QualifiedName)
210      */

211     public static String JavaDoc getByteOrderMarkLabel(IContentDescription description) {
212
213         if (description == null) {
214             return null;
215         }
216
217         byte[] bom = (byte[]) description.getProperty(IContentDescription.BYTE_ORDER_MARK);
218         if (bom == null) {
219             return null;
220         }
221         if (bom == IContentDescription.BOM_UTF_8) {
222             return IDEEncoding.BOM_UTF_8;
223         }
224         if (bom == IContentDescription.BOM_UTF_16BE) {
225             return IDEEncoding.BOM_UTF_16BE;
226         }
227         if (bom == IContentDescription.BOM_UTF_16LE) {
228             return IDEEncoding.BOM_UTF_16LE;
229         }
230
231         return null;
232     }
233
234 }
235
Popular Tags