1 11 package org.eclipse.ui.ide; 12 13 import java.nio.charset.Charset ; 14 import java.nio.charset.IllegalCharsetNameException ; 15 import java.util.ArrayList ; 16 import java.util.Collection ; 17 import java.util.Collections ; 18 import java.util.Iterator ; 19 import java.util.List ; 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 41 public final class IDEEncoding { 42 43 private IDEEncoding () { 44 } 46 47 private static String IDE_ENCODINGS_PREFERENCE = "IDE_ENCODINGS_PREFERENCE"; 50 private static String PREFERENCE_SEPARATOR = "'"; 52 55 public static String BOM_UTF_8 = "UTF-8 (BOM)"; 57 61 public static String BOM_UTF_16BE = "UTF-16 Big-Endian (BOM)"; 63 67 public static String BOM_UTF_16LE = "UTF-16 Little-Endian (BOM)"; 69 75 public static List getIDEEncodings() { 76 List encodings = getIDEEncodingsPreference(); 77 encodings.addAll(WorkbenchEncoding.getDefinedEncodings()); 78 79 String 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 95 public static String getResourceEncoding() { 96 String 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 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 126 private static void writeEncodingsPreference(String value, Collection encodings) { 127 boolean addValue = (value != null); 128 129 StringBuffer result = new StringBuffer (); 130 131 Iterator currentEncodings = encodings.iterator(); 132 133 while (currentEncodings.hasNext()) { 134 String string = (String ) 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 155 private static List getIDEEncodingsPreference() { 156 157 boolean updateRequired = false; 158 159 String encodings = IDEWorkbenchPlugin.getDefault().getPreferenceStore().getString( 160 IDE_ENCODINGS_PREFERENCE); 161 162 if (encodings == null || encodings.length() == 0) { 163 return new ArrayList (); 164 } 165 166 String [] preferenceEncodings = encodings.split(PREFERENCE_SEPARATOR); 167 ArrayList result = new ArrayList (); 168 169 for (int i = 0; i < preferenceEncodings.length; i++) { 171 String string = preferenceEncodings[i]; 172 boolean isSupported; 173 try { 174 isSupported = Charset.isSupported(string); 175 } catch (IllegalCharsetNameException 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 197 public static void clearUserEncodings() { 198 IDEWorkbenchPlugin.getDefault().getPreferenceStore().setToDefault(IDE_ENCODINGS_PREFERENCE); 199 } 200 201 211 public static String 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 |