This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 42638
Collapse All | Expand All

(-)apichanges.xml (-1 / +21 lines)
Lines 81-87 Link Here
81
    <!-- ACTUAL CHANGES BEGIN HERE: -->
81
    <!-- ACTUAL CHANGES BEGIN HERE: -->
82
82
83
    <changes>
83
    <changes>
84
84
      <change id="FileEncodingQuery">
85
        <api name="general"/>
86
        <summary>Added support for obtaining encoding of files</summary>
87
        <version major="1" minor="9"/>
88
        <date day="8" month="2" year="2007"/>
89
        <author login="tzezula"/>
90
        <compatibility addition="yes" binary="compatible" semantic="compatible" source="compatible">
91
        </compatibility>
92
        <description>
93
          <p>
94
            Added a query (<code>FileEncodingQuery</code>) to find out the encoding of the file. The query
95
            delegates to the instances of the SPI interface (<code>FileEncodingQueryImplementation</code>),
96
            when the SPI implementations don't know anything about the file encoding it returns the encoding
97
            corresponding to the file.encoding property. The API also provides getter and setter for default
98
            project encoding which should be used by the project generator.
99
          </p>
100
        </description>
101
        <class package="org.netbeans.api.queries" name="FileEncodingQuery"/>
102
        <class package="org.netbeans.spi.queries" name="FileEncodingQueryImplementation"/>
103
        <issue number="83343"/>
104
        </change>
85
        <change id="fileinfo-NonRecursiveFolder">
105
        <change id="fileinfo-NonRecursiveFolder">
86
            <api name="general"/>
106
            <api name="general"/>
87
            <summary>Added <code>fileinfo</code> package with <code>NonRecursiveFolder</code> interface.</summary>
107
            <summary>Added <code>fileinfo</code> package with <code>NonRecursiveFolder</code> interface.</summary>
(-)manifest.mf (-1 / +1 lines)
Lines 1-5 Link Here
1
Manifest-Version: 1.0
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.netbeans.modules.queries/1
2
OpenIDE-Module: org.netbeans.modules.queries/1
3
OpenIDE-Module-Specification-Version: 1.8
3
OpenIDE-Module-Specification-Version: 1.9
4
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/queries/Bundle.properties
4
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/queries/Bundle.properties
5
5
(-)src/org/netbeans/api/queries/FileEncodingQuery.java (+94 lines)
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
        String _encoding = System.getProperty("file.encoding");     //NOI18N
61
        assert _encoding != null;        
62
        encoding = Charset.forName(_encoding);
63
        assert encoding != null;
64
        return encoding;        
65
    }   
66
    
67
    /**
68
     * Returns the encoding which should be used for newly created projects.
69
     * The typical user of this method is a code generating new projects.
70
     * The returned value is a last used encoding set for project.
71
     * @return the default encoding 
72
     * 
73
     */
74
    public static Charset getDefaultEncoding () {
75
        Preferences prefs = NbPreferences.forModule(FileEncodingQuery.class);
76
        String defaultEncoding = prefs.get (DEFAULT_ENCODING,UTF_8);
77
        return Charset.forName(defaultEncoding);
78
    }
79
    
80
    /**
81
     * Sets the encoding which should be used for newly created projects.
82
     * The typical user of this method is a project customizer, when the
83
     * user sets a new encoding the customizer code should update the defaul
84
     * encoding by this method.
85
     * @param encoding the new default encoding
86
     * 
87
     */
88
    public static void setDefaultEncoding (final Charset encoding) {
89
        assert encoding != null;
90
        Preferences prefs = NbPreferences.forModule(FileEncodingQuery.class);
91
        prefs.put(DEFAULT_ENCODING, encoding.name());
92
    }
93
    
94
}
(-)src/org/netbeans/spi/queries/FileEncodingQueryImplementation.java (+52 lines)
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.spi.queries;
20
21
import java.nio.charset.Charset;
22
import org.openide.filesystems.FileObject;
23
24
/**
25
 * Information about encoding of a file.
26
 * <p>
27
 * A default implementations are registered by the
28
 * <code>org.netbeans.modules.projectapi</code> module which firstly looks up the
29
 * implementation of this interface in the <code>DataObject</code> lookup. When
30
 * available it delegates to it. When the implementation isn't available in the
31
 * <code>DataObject</code> lookup or it returns null it tries to find a
32
 * project corresponding to the file and checks whether that project has an
33
 * implementation of this interface in its lookup. If so, it delegates to 
34
 * that implementation. Therefore it is not generally necessary
35
 * for a project type provider nor data loader to register its own global implementation of
36
 * this query. 
37
 * </p> 
38
 * @see org.netbeans.api.queries.FileEncodingQuery
39
 * @since org.netbeans.modules.queries/1 1.9
40
 * @author Tomas Zezula
41
 */
42
public interface FileEncodingQueryImplementation {
43
    
44
    /**
45
     * Returns encoding of a given file.
46
     * @param file to find an encoding for
47
     * @return encoding which should be used for given file
48
     * or null when nothing is known about the file encoding.
49
     */
50
    public Charset getEncoding (FileObject file);
51
    
52
}

Return to bug 42638