Added
Link Here
|
1 |
/* |
2 |
* The contents of this file are subject to the terms of the Common Development |
3 |
* and Distribution License (the License). You may not use this file except in |
4 |
* compliance with the License. |
5 |
* |
6 |
* You can obtain a copy of the License at http://www.netbeans.org/cddl.html |
7 |
* or http://www.netbeans.org/cddl.txt. |
8 |
* |
9 |
* When distributing Covered Code, include this CDDL Header Notice in each file |
10 |
* and include the License file at http://www.netbeans.org/cddl.txt. |
11 |
* If applicable, add the following below the CDDL Header, with the fields |
12 |
* enclosed by brackets [] replaced by your own identifying information: |
13 |
* "Portions Copyrighted [year] [name of copyright owner]" |
14 |
* |
15 |
* The Original Software is NetBeans. The Initial Developer of the Original |
16 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun |
17 |
* Microsystems, Inc. All Rights Reserved. |
18 |
*/ |
19 |
package org.netbeans.api.queries; |
20 |
|
21 |
import java.nio.charset.Charset; |
22 |
import java.util.prefs.Preferences; |
23 |
import org.netbeans.spi.queries.FileEncodingQueryImplementation; |
24 |
import org.openide.filesystems.FileObject; |
25 |
import org.openide.util.Lookup; |
26 |
import org.openide.util.NbPreferences; |
27 |
|
28 |
/** |
29 |
* The query is used for finding encoding of files. |
30 |
* The query should be used when reading or writing files to use the |
31 |
* correct encoding. |
32 |
* @since org.netbeans.modules.queries/1 1.9 |
33 |
* @see FileEncodingQueryImplementation |
34 |
* @author Tomas Zezula |
35 |
*/ |
36 |
public class FileEncodingQuery { |
37 |
|
38 |
private static final String DEFAULT_ENCODING = "default-encoding"; //NOI18N |
39 |
private static final String UTF_8 = "UTF-8"; //NOI18N |
40 |
|
41 |
|
42 |
private FileEncodingQuery() { |
43 |
} |
44 |
|
45 |
|
46 |
/** |
47 |
* Returns encoding of given file. |
48 |
* @param file to find an encoding for |
49 |
* @return encoding which should be used for given file, never returns null. |
50 |
*/ |
51 |
public static Charset getEncoding (FileObject file) { |
52 |
assert file != null; |
53 |
Charset encoding; |
54 |
for (FileEncodingQueryImplementation impl : Lookup.getDefault().lookupAll(FileEncodingQueryImplementation.class)) { |
55 |
encoding = impl.getEncoding(file); |
56 |
if (encoding != null) { |
57 |
return encoding; |
58 |
} |
59 |
} |
60 |
return Charset.defaultCharset(); |
61 |
} |
62 |
|
63 |
/** |
64 |
* Returns the encoding which should be used for newly created projects. |
65 |
* The typical user of this method is a code generating new projects. |
66 |
* The returned value is a last used encoding set for project. |
67 |
* @return the default encoding |
68 |
* |
69 |
*/ |
70 |
public static Charset getDefaultEncoding () { |
71 |
Preferences prefs = NbPreferences.forModule(FileEncodingQuery.class); |
72 |
String defaultEncoding = prefs.get (DEFAULT_ENCODING,UTF_8); |
73 |
return Charset.forName(defaultEncoding); |
74 |
} |
75 |
|
76 |
/** |
77 |
* Sets the encoding which should be used for newly created projects. |
78 |
* The typical user of this method is a project customizer, when the |
79 |
* user sets a new encoding the customizer code should update the defaul |
80 |
* encoding by this method. |
81 |
* @param encoding the new default encoding |
82 |
* |
83 |
*/ |
84 |
public static void setDefaultEncoding (final Charset encoding) { |
85 |
assert encoding != null; |
86 |
Preferences prefs = NbPreferences.forModule(FileEncodingQuery.class); |
87 |
prefs.put(DEFAULT_ENCODING, encoding.name()); |
88 |
} |
89 |
|
90 |
} |