001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.ide;
011:
012: import java.nio.charset.Charset;
013: import java.nio.charset.IllegalCharsetNameException;
014: import java.util.ArrayList;
015: import java.util.Collection;
016: import java.util.Collections;
017: import java.util.Iterator;
018: import java.util.List;
019:
020: import org.eclipse.core.resources.ResourcesPlugin;
021: import org.eclipse.core.runtime.content.IContentDescription;
022: import org.eclipse.osgi.util.NLS;
023: import org.eclipse.ui.WorkbenchEncoding;
024: import org.eclipse.ui.internal.WorkbenchPlugin;
025: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
026: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
027:
028: /**
029: * IDEEncoding is a utility class for managing encoding information that
030: * includes user preferences from the IDE and core resources.
031: * <p>
032: * This class provides all its functionality via static methods.
033: * It is not intended to be instantiated or subclassed.
034: * </p>
035: *
036: * @see org.eclipse.ui.WorkbenchEncoding
037: * @see org.eclipse.core.resources.ResourcesPlugin
038: * @since 3.1
039: */
040: public final class IDEEncoding {
041:
042: private IDEEncoding() {
043: // prevent instantiation
044: }
045:
046: //The preference for the user entered encodings.
047: private static String IDE_ENCODINGS_PREFERENCE = "IDE_ENCODINGS_PREFERENCE"; //$NON-NLS-1$
048:
049: private static String PREFERENCE_SEPARATOR = "'"; //$NON-NLS-1$
050:
051: /**
052: * Display constant for the UTF 8 byte order marker for resources.
053: */
054: public static String BOM_UTF_8 = "UTF-8 (BOM)";//$NON-NLS-1$
055:
056: /**
057: * Display constant for the UTF 16 big endian byte order marker for
058: * resources.
059: */
060: public static String BOM_UTF_16BE = "UTF-16 Big-Endian (BOM)";//$NON-NLS-1$
061:
062: /**
063: * Display constant for the UTF 16 little endian byte order marker for
064: * resources.
065: */
066: public static String BOM_UTF_16LE = "UTF-16 Little-Endian (BOM)";//$NON-NLS-1$
067:
068: /**
069: * Get all of the available encodings including any that were saved as a
070: * preference in the IDE or in core resources.
071: *
072: * @return List of String
073: */
074: public static List getIDEEncodings() {
075: List encodings = getIDEEncodingsPreference();
076: encodings.addAll(WorkbenchEncoding.getDefinedEncodings());
077:
078: String enc = getResourceEncoding();
079:
080: if (!(enc == null || encodings.contains(enc))) {
081: encodings.add(enc);
082: }
083:
084: Collections.sort(encodings);
085: return encodings;
086: }
087:
088: /**
089: * Get the current value of the encoding preference. If the value is not set
090: * return <code>null</code>.
091: *
092: * @return String
093: */
094: public static String getResourceEncoding() {
095: String preference = ResourcesPlugin.getPlugin()
096: .getPluginPreferences().getString(
097: ResourcesPlugin.PREF_ENCODING);
098: if (preference == null || preference.length() == 0) {
099: 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 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 value,
127: Collection encodings) {
128: boolean addValue = (value != null);
129:
130: StringBuffer result = new StringBuffer();
131:
132: Iterator currentEncodings = encodings.iterator();
133:
134: while (currentEncodings.hasNext()) {
135: String string = (String) currentEncodings.next();
136: result.append(string);
137: result.append(PREFERENCE_SEPARATOR);
138: if (addValue && string.equals(value)) {
139: addValue = false;
140: }
141: }
142:
143: if (addValue) {
144: result.append(value);
145: }
146:
147: IDEWorkbenchPlugin.getDefault().getPreferenceStore().setValue(
148: IDE_ENCODINGS_PREFERENCE, result.toString());
149: }
150:
151: /**
152: * Get the value of the encodings preference.
153: *
154: * @return List
155: */
156: private static List getIDEEncodingsPreference() {
157:
158: boolean updateRequired = false;
159:
160: String encodings = IDEWorkbenchPlugin.getDefault()
161: .getPreferenceStore().getString(
162: IDE_ENCODINGS_PREFERENCE);
163:
164: if (encodings == null || encodings.length() == 0) {
165: return new ArrayList();
166: }
167:
168: String[] preferenceEncodings = encodings
169: .split(PREFERENCE_SEPARATOR);
170: ArrayList result = new ArrayList();
171:
172: //Drop any encodings that are not valid
173: for (int i = 0; i < preferenceEncodings.length; i++) {
174: String string = preferenceEncodings[i];
175: boolean isSupported;
176: try {
177: isSupported = Charset.isSupported(string);
178: } catch (IllegalCharsetNameException e) {
179: isSupported = false;
180: }
181: if (isSupported) {
182: result.add(string);
183: } else {
184: WorkbenchPlugin
185: .log(NLS
186: .bind(
187: IDEWorkbenchMessages.WorkbenchEncoding_invalidCharset,
188: string));
189: updateRequired = true;
190: }
191:
192: }
193:
194: if (updateRequired) {
195: writeEncodingsPreference(null, result);
196: }
197: return result;
198:
199: }
200:
201: /**
202: * Clear the IDE encodings preference.
203: */
204: public static void clearUserEncodings() {
205: IDEWorkbenchPlugin.getDefault().getPreferenceStore()
206: .setToDefault(IDE_ENCODINGS_PREFERENCE);
207: }
208:
209: /**
210: * Get the displayable string for the byte order marking from the supplied
211: * file description.
212: *
213: * @param description
214: * The description to query. May be <code>null</code>.
215: * @return String or <code>null</code> if the byte order mark cannot be
216: * found or the description is <code>null</code>.
217: * @see IContentDescription#getProperty(org.eclipse.core.runtime.QualifiedName)
218: */
219: public static String getByteOrderMarkLabel(
220: IContentDescription description) {
221:
222: if (description == null) {
223: return null;
224: }
225:
226: byte[] bom = (byte[]) description
227: .getProperty(IContentDescription.BYTE_ORDER_MARK);
228: if (bom == null) {
229: return null;
230: }
231: if (bom == IContentDescription.BOM_UTF_8) {
232: return IDEEncoding.BOM_UTF_8;
233: }
234: if (bom == IContentDescription.BOM_UTF_16BE) {
235: return IDEEncoding.BOM_UTF_16BE;
236: }
237: if (bom == IContentDescription.BOM_UTF_16LE) {
238: return IDEEncoding.BOM_UTF_16LE;
239: }
240:
241: return null;
242: }
243:
244: }
|