001: /*******************************************************************************
002: * Copyright (c) 2004, 2007 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;
011:
012: import java.lang.reflect.InvocationTargetException;
013: import java.lang.reflect.Method;
014: import java.util.ArrayList;
015: import java.util.Collections;
016: import java.util.Iterator;
017: import java.util.List;
018:
019: import org.eclipse.core.runtime.IConfigurationElement;
020: import org.eclipse.core.runtime.Platform;
021: import org.eclipse.osgi.util.NLS;
022: import org.eclipse.ui.internal.WorkbenchMessages;
023: import org.eclipse.ui.internal.WorkbenchPlugin;
024: import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
025: import org.eclipse.ui.internal.registry.RegistryReader;
026:
027: /**
028: * WorkbenchEncoding is a utility class for plug-ins that want to use the list
029: * of encodings defined by default in the workbench.
030: *
031: * @since 3.1
032: */
033: public class WorkbenchEncoding {
034:
035: /**
036: * The method for java.nio.charset.Charset.isSupported(String), or <code>null</code>
037: * if not present. Reflection is used here to allow compilation against JCL Foundation (bug 80053).
038: */
039: private static Method CharsetIsSupportedMethod = null;
040:
041: static {
042: try {
043: Class charsetClass = Class
044: .forName("java.nio.charset.Charset"); //$NON-NLS-1$
045: CharsetIsSupportedMethod = charsetClass.getMethod(
046: "isSupported", new Class[] { String.class }); //$NON-NLS-1$
047: } catch (Exception e) {
048: // ignore
049: }
050:
051: }
052:
053: private static class EncodingsRegistryReader extends RegistryReader {
054:
055: private List encodings;
056:
057: /**
058: * Create a new instance of the receiver.
059: * @param definedEncodings
060: */
061: public EncodingsRegistryReader(List definedEncodings) {
062: super ();
063: encodings = definedEncodings;
064: }
065:
066: /*
067: * (non-Javadoc)
068: *
069: * @see org.eclipse.ui.internal.registry.RegistryReader#readElement(org.eclipse.core.runtime.IConfigurationElement)
070: */
071: protected boolean readElement(IConfigurationElement element) {
072: String name = element
073: .getAttribute(IWorkbenchRegistryConstants.ATT_NAME);
074: if (name != null) {
075: encodings.add(name);
076: }
077: return true;
078: }
079: }
080:
081: /**
082: * Get the default encoding from the virtual machine.
083: *
084: * @return String
085: */
086: public static String getWorkbenchDefaultEncoding() {
087: return System.getProperty("file.encoding", "UTF-8");//$NON-NLS-1$ //$NON-NLS-2$
088: }
089:
090: /**
091: * Return the list of encodings defined using the org.eclipse.ui.encodings
092: * extension point.
093: *
094: * @return List of String
095: */
096: public static List getDefinedEncodings() {
097: List definedEncodings = Collections
098: .synchronizedList(new ArrayList());
099: EncodingsRegistryReader reader = new EncodingsRegistryReader(
100: definedEncodings);
101:
102: reader.readRegistry(Platform.getExtensionRegistry(),
103: PlatformUI.PLUGIN_ID,
104: IWorkbenchRegistryConstants.PL_ENCODINGS);
105:
106: //Make it an array in case of concurrency issues with Iterators
107: String[] encodings = new String[definedEncodings.size()];
108: List invalid = new ArrayList();
109: definedEncodings.toArray(encodings);
110: for (int i = 0; i < encodings.length; i++) {
111: if (!isSupported(encodings[i])) {
112: invalid.add(encodings[i]);
113: }
114: }
115:
116: Iterator invalidIterator = invalid.iterator();
117: while (invalidIterator.hasNext()) {
118: String next = (String) invalidIterator.next();
119: WorkbenchPlugin.log(NLS.bind(
120: WorkbenchMessages.WorkbenchEncoding_invalidCharset,
121: next));
122: definedEncodings.remove(next);
123:
124: }
125:
126: return definedEncodings;
127: }
128:
129: /**
130: * Returns whether the given encoding is supported in the current runtime.
131: *
132: * @param encoding the encoding to test
133: * @return <code>true</code> if supported or if its support could not be determined,
134: * <code>false</code> if not supported
135: */
136: private static boolean isSupported(String encoding) {
137: if (CharsetIsSupportedMethod == null) {
138: return true;
139: }
140: try {
141: Object o = CharsetIsSupportedMethod.invoke(null,
142: new Object[] { encoding });
143: return Boolean.TRUE.equals(o);
144: } catch (IllegalArgumentException e) {
145: //fall through
146: } catch (IllegalAccessException e) {
147: // fall through
148: } catch (InvocationTargetException e) {
149: // Method.invoke can throw InvocationTargetException if there is
150: // an exception in the invoked method.
151: // Charset.isSupported() is specified to throw IllegalCharsetNameException only
152: // which we want to return false for.
153: return false;
154: }
155: return true;
156: }
157: }
|