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

(-)projectapi/overview.html (-2 / +3 lines)
Lines 4-11 Link Here
4
4
5
<p>
5
<p>
6
Besides the visible Javadoc, this module permits a project to add implementations
6
Besides the visible Javadoc, this module permits a project to add implementations
7
of {@link org.netbeans.spi.queries.FileBuiltQueryImplementation}
7
of {@link org.netbeans.spi.queries.FileBuiltQueryImplementation},
8
and {@link org.netbeans.spi.queries.SharabilityQueryImplementation}
8
{@link org.netbeans.spi.queries.SharabilityQueryImplementation} and
9
{link org.netbeans.spi.queries.FileEncodingQueryImplementation}
9
into the project lookup (rather than global lookup).
10
into the project lookup (rather than global lookup).
10
The implementations will be consulted only in the case the relevant file
11
The implementations will be consulted only in the case the relevant file
11
belongs to that project (according to {@link org.netbeans.api.project.FileOwnerQuery}).
12
belongs to that project (according to {@link org.netbeans.api.project.FileOwnerQuery}).
(-)projectapi/src/META-INF/services/org.netbeans.spi.queries.FileEncodingQueryImplementation (+4 lines)
Added Link Here
1
org.netbeans.modules.projectapi.ProjectFileEncodingQueryImplementation
2
#position=200
3
org.netbeans.modules.projectapi.DataObjectEncodingQueryImplementation
4
#position=100
(-)projectapi/src/org/netbeans/api/project/Project.java (+1 lines)
Lines 79-84 Link Here
79
     * <li>{@link org.netbeans.spi.project.ProjectConfigurationProvider}</li>
79
     * <li>{@link org.netbeans.spi.project.ProjectConfigurationProvider}</li>
80
     * <li>{@link org.netbeans.spi.queries.FileBuiltQueryImplementation}</li>
80
     * <li>{@link org.netbeans.spi.queries.FileBuiltQueryImplementation}</li>
81
     * <li>{@link org.netbeans.spi.queries.SharabilityQueryImplementation}</li>
81
     * <li>{@link org.netbeans.spi.queries.SharabilityQueryImplementation}</li>
82
     * <li>{@link org.netbeans.spi.queries.FileEncodingQueryImplementation}</li>
82
     * <li><a href="@org-netbeans-modules-projectuiapi@/org/netbeans/spi/project/ui/ProjectOpenedHook.html"><code>ProjectOpenedHook</code></a></li>
83
     * <li><a href="@org-netbeans-modules-projectuiapi@/org/netbeans/spi/project/ui/ProjectOpenedHook.html"><code>ProjectOpenedHook</code></a></li>
83
     * <li><a href="@org-netbeans-modules-projectuiapi@/org/netbeans/spi/project/ui/RecommendedTemplates.html"><code>RecommendedTemplates</code></a></li>
84
     * <li><a href="@org-netbeans-modules-projectuiapi@/org/netbeans/spi/project/ui/RecommendedTemplates.html"><code>RecommendedTemplates</code></a></li>
84
     * <li><a href="@org-netbeans-modules-projectuiapi@/org/netbeans/spi/project/ui/PrivilegedTemplates.html"><code>PrivilegedTemplates</code></a></li>
85
     * <li><a href="@org-netbeans-modules-projectuiapi@/org/netbeans/spi/project/ui/PrivilegedTemplates.html"><code>PrivilegedTemplates</code></a></li>
(-)projectapi/src/org/netbeans/modules/projectapi/DataObjectEncodingQueryImplementation.java (+53 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.modules.projectapi;
20
21
import java.nio.charset.Charset;
22
import org.netbeans.spi.queries.FileEncodingQueryImplementation;
23
import org.openide.filesystems.FileObject;
24
import org.openide.loaders.DataObject;
25
import org.openide.loaders.DataObjectNotFoundException;
26
import org.openide.util.Exceptions;
27
28
/**
29
 *
30
 * @author Tomas Zezula
31
 */
32
public class DataObjectEncodingQueryImplementation implements FileEncodingQueryImplementation {
33
    
34
    /** Creates a new instance of DataObjectEncodingQueryImplementation */
35
    public DataObjectEncodingQueryImplementation() {
36
    }
37
    
38
    public Charset getEncoding(FileObject file) {
39
        assert file != null;
40
        try {
41
            DataObject dobj = DataObject.find(file);
42
            FileEncodingQueryImplementation impl = dobj.getLookup().lookup (FileEncodingQueryImplementation.class);
43
            if (impl == null)  {
44
                return null;
45
            }
46
            return impl.getEncoding(file);
47
        } catch (DataObjectNotFoundException donf) {
48
            Exceptions.printStackTrace(donf);
49
            return null;
50
        }
51
    }
52
53
}
(-)projectapi/src/org/netbeans/modules/projectapi/ProjectFileEncodingQueryImplementation.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.modules.projectapi;
20
21
import java.nio.charset.Charset;
22
import org.netbeans.api.project.FileOwnerQuery;
23
import org.netbeans.api.project.Project;
24
import org.netbeans.spi.queries.FileEncodingQueryImplementation;
25
import org.openide.filesystems.FileObject;
26
27
/**
28
 *
29
 * @author Tomas Zezula
30
 */
31
public class ProjectFileEncodingQueryImplementation implements FileEncodingQueryImplementation {
32
    
33
    
34
    public ProjectFileEncodingQueryImplementation() {
35
    }
36
    
37
    public Charset getEncoding(FileObject file) {
38
        Project p = FileOwnerQuery.getOwner(file);
39
        if (p == null) {
40
            return null;
41
        }
42
        FileEncodingQueryImplementation delegate = p.getLookup().lookup(FileEncodingQueryImplementation.class);
43
        if (delegate == null) {
44
            return null;
45
        }
46
        return delegate.getEncoding(file);
47
    }
48
49
    
50
    
51
    
52
}
(-)queries/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>
(-)queries/arch.xml (-3 / +105 lines)
Lines 22-28 Link Here
22
]>
22
]>
23
23
24
<api-answers
24
<api-answers
25
  question-version="1.24"
25
  question-version="1.29"
26
  author="jglick@netbeans.org"
26
  author="jglick@netbeans.org"
27
>
27
>
28
28
Lines 190-196 Link Here
190
-->
190
-->
191
 <answer id="compat-version">
191
 <answer id="compat-version">
192
  <p>
192
  <p>
193
   The module uses no persistent settings.
193
   The module uses NbPreferences to store the default project encoding. When there is no
194
   default project encoding stored it falls back to UTF-8.
194
  </p>
195
  </p>
195
 </answer>
196
 </answer>
196
197
Lines 239-245 Link Here
239
 <answer id="dep-nb">
240
 <answer id="dep-nb">
240
  <!-- XXX link -->
241
  <!-- XXX link -->
241
  <p>
242
  <p>
242
   Uses the NetBeans Filesystems and Lookup APIs.
243
   Uses the NetBeans Filesystems, Lookup and Preferences APIs.
243
  </p>
244
  </p>
244
 </answer>
245
 </answer>
245
246
Lines 927-931 Link Here
927
  </p>
928
  </p>
928
 </answer>
929
 </answer>
929
930
931
 
932
933
934
935
<!--
936
        <question id="arch-where" when="impl">
937
            Where one can find sources for your module?
938
            <hint>
939
                Please provide link to the CVS web client at
940
                http://www.netbeans.org/download/source_browse.html
941
                or just use tag defaultanswer generate='here'
942
            </hint>
943
        </question>
944
-->
945
 <answer id="arch-where">
946
  <defaultanswer generate='here' />
947
 </answer>
948
949
950
951
<!--
952
        <question id="compat-deprecation" when="init">
953
            How the introduction of your project influences functionality
954
            provided by previous version of the product?
955
            <hint>
956
            If you are planning to deprecate/remove/change any existing APIs,
957
            list them here accompanied with the reason explaining why you
958
            are doing so.
959
            </hint>
960
        </question>
961
-->
962
 <answer id="compat-deprecation">
963
  <p>
964
   Addition of a new API.
965
  </p>
966
 </answer>
967
968
969
970
<!--
971
        <question id="exec-ant-tasks" when="impl">
972
            Do you define or register any ant tasks that other can use?
973
            
974
            <hint>
975
            If you provide an ant task that users can use, you need to be very
976
            careful about its syntax and behaviour, as it most likely forms an
977
	          API for end users and as there is a lot of end users, their reaction
978
            when such API gets broken can be pretty strong.
979
            </hint>
980
        </question>
981
-->
982
 <answer id="exec-ant-tasks">
983
  <p>
984
   No.
985
  </p>
986
 </answer>
987
988
989
990
<!--
991
        <question id="resources-preferences" when="final">
992
            Does your module uses preferences via Preferences API? Does your module use NbPreferences or
993
            or regular JDK Preferences ? Does it read, write or both ? 
994
            Does it share preferences with other modules ? If so, then why ?
995
            <hint>
996
                You may use
997
                    &lt;api type="export" group="preferences"
998
                    name="preference node name" category="private"&gt;
999
                    description of individual keys, where it is used, what it
1000
                    influences, whether the module reads/write it, etc.
1001
                    &lt;/api&gt;
1002
                Due to XML ID restrictions, rather than /org/netbeans/modules/foo give the "name" as org.netbeans.modules.foo.
1003
                Note that if you use NbPreferences this name will then be the same as the code name base of the module.
1004
            </hint>
1005
        </question>
1006
-->
1007
<answer id="resources-preferences">    
1008
  <p>
1009
      The module internally uses <code>NbPreferences</code> to store the default value of encoding of a new project.
1010
      Clients should not access this preferences directly using preferences API but they should use
1011
      the <code>FileEncodingQuery</code> methods.
1012
  </p>
1013
  <api group="preferences" name="org.netbeans.api.queries" type="export" category="private" url="">
1014
      <table>
1015
          <tbody>
1016
              <tr>
1017
                  <th>key</th>
1018
                  <th>description</th>
1019
                  <th>read</th>
1020
                  <th>write</th>
1021
              </tr>
1022
              <tr>
1023
                  <td>default-encoding</td>
1024
                  <td>Default encoding which should be used for new project</td>
1025
                  <td>x</td>
1026
                  <td>x</td>
1027
              </tr>
1028
          </tbody>
1029
      </table>
1030
  </api>
1031
 </answer>
930
1032
931
</api-answers>
1033
</api-answers>
(-)queries/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
(-)queries/src/org/netbeans/api/queries/FileEncodingQuery.java (+90 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
        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
}
(-)queries/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