KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > WorkbenchEncoding


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.lang.reflect.Method JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
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 /**
29  * WorkbenchEncoding is a utility class for plug-ins that want to use the list
30  * of encodings defined by default in the workbench.
31  *
32  * @since 3.1
33  */

34 public class WorkbenchEncoding {
35
36     /**
37      * The method for java.nio.charset.Charset.isSupported(String), or <code>null</code>
38      * if not present. Reflection is used here to allow compilation against JCL Foundation (bug 80053).
39      */

40     private static Method JavaDoc CharsetIsSupportedMethod = null;
41     
42     static {
43         try {
44             Class JavaDoc charsetClass = Class.forName("java.nio.charset.Charset"); //$NON-NLS-1$
45
CharsetIsSupportedMethod = charsetClass.getMethod("isSupported", new Class JavaDoc[] { String JavaDoc.class }); //$NON-NLS-1$
46
}
47         catch (Exception JavaDoc e) {
48             // ignore
49
}
50             
51     }
52     
53     private static class EncodingsRegistryReader extends RegistryReader {
54         
55         private List JavaDoc encodings;
56         
57         /**
58          * Create a new instance of the receiver.
59          * @param definedEncodings
60          */

61         public EncodingsRegistryReader(List JavaDoc definedEncodings) {
62             super();
63             encodings = definedEncodings;
64         }
65
66         /*
67          * (non-Javadoc)
68          *
69          * @see org.eclipse.ui.internal.registry.RegistryReader#readElement(org.eclipse.core.runtime.IConfigurationElement)
70          */

71         protected boolean readElement(IConfigurationElement element) {
72             String JavaDoc name = element.getAttribute(IWorkbenchRegistryConstants.ATT_NAME);
73             if (name != null) {
74                 encodings.add(name);
75             }
76             return true;
77         }
78     }
79
80     /**
81      * Get the default encoding from the virtual machine.
82      *
83      * @return String
84      */

85     public static String JavaDoc getWorkbenchDefaultEncoding() {
86         return System.getProperty("file.encoding", "UTF-8");//$NON-NLS-1$ //$NON-NLS-2$
87
}
88
89     /**
90      * Return the list of encodings defined using the org.eclipse.ui.encodings
91      * extension point.
92      *
93      * @return List of String
94      */

95     public static List JavaDoc getDefinedEncodings() {
96         List JavaDoc definedEncodings = Collections.synchronizedList(new ArrayList JavaDoc());
97         EncodingsRegistryReader reader = new EncodingsRegistryReader(definedEncodings);
98
99         reader.readRegistry(Platform.getExtensionRegistry(), PlatformUI.PLUGIN_ID,
100                 IWorkbenchRegistryConstants.PL_ENCODINGS);
101
102         //Make it an array in case of concurrency issues with Iterators
103
String JavaDoc[] encodings = new String JavaDoc[definedEncodings.size()];
104         List JavaDoc invalid = new ArrayList JavaDoc();
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 JavaDoc invalidIterator = invalid.iterator();
113         while (invalidIterator.hasNext()) {
114             String JavaDoc next = (String JavaDoc) invalidIterator.next();
115             WorkbenchPlugin.log(NLS.bind(WorkbenchMessages.WorkbenchEncoding_invalidCharset, next ));
116             definedEncodings.remove(next);
117
118         }
119
120         return definedEncodings;
121     }
122
123     /**
124      * Returns whether the given encoding is supported in the current runtime.
125      *
126      * @param encoding the encoding to test
127      * @return <code>true</code> if supported or if its support could not be determined,
128      * <code>false</code> if not supported
129      */

130     private static boolean isSupported(String JavaDoc encoding) {
131         if (CharsetIsSupportedMethod == null) {
132             return true;
133         }
134         try {
135             Object JavaDoc o = CharsetIsSupportedMethod.invoke(null, new Object JavaDoc[] { encoding });
136             return Boolean.TRUE.equals(o);
137         } catch (IllegalArgumentException JavaDoc e) {
138             //fall through
139
} catch (IllegalAccessException JavaDoc e) {
140             // fall through
141
} catch (InvocationTargetException JavaDoc e) {
142             // Method.invoke can throw InvocationTargetException if there is
143
// an exception in the invoked method.
144
// Charset.isSupported() is specified to throw IllegalCharsetNameException only
145
// which we want to return false for.
146
return false;
147         }
148         return true;
149     }
150 }
151
Popular Tags