1 11 package org.eclipse.ui; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.lang.reflect.Method ; 15 import java.util.ArrayList ; 16 import java.util.Collections ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 20 import org.eclipse.core.runtime.IConfigurationElement; 21 import org.eclipse.core.runtime.Platform; 22 import org.eclipse.osgi.util.NLS; 23 import org.eclipse.ui.internal.WorkbenchMessages; 24 import org.eclipse.ui.internal.WorkbenchPlugin; 25 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; 26 import org.eclipse.ui.internal.registry.RegistryReader; 27 28 34 public class WorkbenchEncoding { 35 36 40 private static Method CharsetIsSupportedMethod = null; 41 42 static { 43 try { 44 Class charsetClass = Class.forName("java.nio.charset.Charset"); CharsetIsSupportedMethod = charsetClass.getMethod("isSupported", new Class [] { String .class }); } 47 catch (Exception e) { 48 } 50 51 } 52 53 private static class EncodingsRegistryReader extends RegistryReader { 54 55 private List encodings; 56 57 61 public EncodingsRegistryReader(List definedEncodings) { 62 super(); 63 encodings = definedEncodings; 64 } 65 66 71 protected boolean readElement(IConfigurationElement element) { 72 String name = element.getAttribute(IWorkbenchRegistryConstants.ATT_NAME); 73 if (name != null) { 74 encodings.add(name); 75 } 76 return true; 77 } 78 } 79 80 85 public static String getWorkbenchDefaultEncoding() { 86 return System.getProperty("file.encoding", "UTF-8"); } 88 89 95 public static List getDefinedEncodings() { 96 List definedEncodings = Collections.synchronizedList(new ArrayList ()); 97 EncodingsRegistryReader reader = new EncodingsRegistryReader(definedEncodings); 98 99 reader.readRegistry(Platform.getExtensionRegistry(), PlatformUI.PLUGIN_ID, 100 IWorkbenchRegistryConstants.PL_ENCODINGS); 101 102 String [] encodings = new String [definedEncodings.size()]; 104 List invalid = new ArrayList (); 105 definedEncodings.toArray(encodings); 106 for (int i = 0; i < encodings.length; i++) { 107 if (!isSupported(encodings[i])) { 108 invalid.add(encodings[i]); 109 } 110 } 111 112 Iterator invalidIterator = invalid.iterator(); 113 while (invalidIterator.hasNext()) { 114 String next = (String ) invalidIterator.next(); 115 WorkbenchPlugin.log(NLS.bind(WorkbenchMessages.WorkbenchEncoding_invalidCharset, next )); 116 definedEncodings.remove(next); 117 118 } 119 120 return definedEncodings; 121 } 122 123 130 private static boolean isSupported(String encoding) { 131 if (CharsetIsSupportedMethod == null) { 132 return true; 133 } 134 try { 135 Object o = CharsetIsSupportedMethod.invoke(null, new Object [] { encoding }); 136 return Boolean.TRUE.equals(o); 137 } catch (IllegalArgumentException e) { 138 } catch (IllegalAccessException e) { 140 } catch (InvocationTargetException e) { 142 return false; 147 } 148 return true; 149 } 150 } 151 | Popular Tags |