1 /******************************************************************************* 2 * Copyright (c) 2004, 2018 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.ui; 15 16 import java.nio.charset.Charset; 17 import java.nio.charset.IllegalCharsetNameException; 18 import java.util.ArrayList; 19 import java.util.Collections; 20 import java.util.List; 21 import org.eclipse.core.runtime.IConfigurationElement; 22 import org.eclipse.core.runtime.Platform; 23 import org.eclipse.osgi.util.NLS; 24 import org.eclipse.ui.internal.WorkbenchMessages; 25 import org.eclipse.ui.internal.WorkbenchPlugin; 26 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; 27 import org.eclipse.ui.internal.registry.RegistryReader; 28 29 /** 30 * WorkbenchEncoding is a utility class for plug-ins that want to use the list 31 * of encodings defined by default in the workbench. 32 * 33 * @since 3.1 34 */ 35 public class WorkbenchEncoding { 36 37 private static class EncodingsRegistryReader extends RegistryReader { 38 39 private List<String> encodings; 40 41 /** 42 * Create a new instance of the receiver. 43 * 44 * @param definedEncodings the list of encodings 45 */ EncodingsRegistryReader(List<String> definedEncodings)46 public EncodingsRegistryReader(List<String> definedEncodings) { 47 super(); 48 encodings = definedEncodings; 49 } 50 51 @Override readElement(IConfigurationElement element)52 protected boolean readElement(IConfigurationElement element) { 53 String name = element.getAttribute(IWorkbenchRegistryConstants.ATT_NAME); 54 if (name != null) { 55 encodings.add(name); 56 } 57 return true; 58 } 59 } 60 61 /** 62 * Get the default encoding from the virtual machine. 63 * 64 * @return String 65 */ getWorkbenchDefaultEncoding()66 public static String getWorkbenchDefaultEncoding() { 67 return System.getProperty("file.encoding", "UTF-8");//$NON-NLS-1$ //$NON-NLS-2$ 68 } 69 70 /** 71 * Return the list of encodings defined using the org.eclipse.ui.encodings 72 * extension point. 73 * 74 * @return List of String 75 */ getDefinedEncodings()76 public static List<String> getDefinedEncodings() { 77 List<String> definedEncodings = Collections.synchronizedList(new ArrayList<>()); 78 EncodingsRegistryReader reader = new EncodingsRegistryReader(definedEncodings); 79 80 reader.readRegistry(Platform.getExtensionRegistry(), PlatformUI.PLUGIN_ID, 81 IWorkbenchRegistryConstants.PL_ENCODINGS); 82 83 // Make it an array in case of concurrency issues with Iterators 84 String[] encodings = new String[definedEncodings.size()]; 85 List<String> invalid = new ArrayList<>(); 86 definedEncodings.toArray(encodings); 87 for (String encoding : encodings) { 88 try { 89 if (!Charset.isSupported(encoding)) { 90 invalid.add(encoding); 91 } 92 } catch (IllegalCharsetNameException e) { 93 invalid.add(encoding); 94 } 95 } 96 97 for (String next : invalid) { 98 WorkbenchPlugin.log(NLS.bind(WorkbenchMessages.WorkbenchEncoding_invalidCharset, next)); 99 definedEncodings.remove(next); 100 101 } 102 103 return definedEncodings; 104 } 105 } 106