Index: projectapi/overview.html =================================================================== RCS file: /cvs/projects/projectapi/overview.html,v --- projectapi/overview.html 14 Oct 2004 17:26:43 -0000 1.4 +++ projectapi/overview.html 26 Feb 2007 13:34:47 -0000 @@ -4,8 +4,9 @@

Besides the visible Javadoc, this module permits a project to add implementations -of {@link org.netbeans.spi.queries.FileBuiltQueryImplementation} -and {@link org.netbeans.spi.queries.SharabilityQueryImplementation} +of {@link org.netbeans.spi.queries.FileBuiltQueryImplementation}, +{@link org.netbeans.spi.queries.SharabilityQueryImplementation} and +{link org.netbeans.spi.queries.FileEncodingQueryImplementation} into the project lookup (rather than global lookup). The implementations will be consulted only in the case the relevant file belongs to that project (according to {@link org.netbeans.api.project.FileOwnerQuery}). Index: projectapi/src/META-INF/services/org.netbeans.spi.queries.FileEncodingQueryImplementation =================================================================== RCS file: projectapi/src/META-INF/services/org.netbeans.spi.queries.FileEncodingQueryImplementation --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ projectapi/src/META-INF/services/org.netbeans.spi.queries.FileEncodingQueryImplementation 26 Feb 2007 13:34:47 -0000 @@ -0,0 +1,4 @@ +org.netbeans.modules.projectapi.ProjectFileEncodingQueryImplementation +#position=200 +org.netbeans.modules.projectapi.DataObjectEncodingQueryImplementation +#position=100 Index: projectapi/src/org/netbeans/api/project/Project.java =================================================================== RCS file: /cvs/projects/projectapi/src/org/netbeans/api/project/Project.java,v --- projectapi/src/org/netbeans/api/project/Project.java 9 Feb 2007 10:55:10 -0000 1.17 +++ projectapi/src/org/netbeans/api/project/Project.java 26 Feb 2007 13:34:47 -0000 @@ -79,6 +79,7 @@ *

  • {@link org.netbeans.spi.project.ProjectConfigurationProvider}
  • *
  • {@link org.netbeans.spi.queries.FileBuiltQueryImplementation}
  • *
  • {@link org.netbeans.spi.queries.SharabilityQueryImplementation}
  • + *
  • {@link org.netbeans.spi.queries.FileEncodingQueryImplementation}
  • *
  • ProjectOpenedHook
  • *
  • RecommendedTemplates
  • *
  • PrivilegedTemplates
  • Index: projectapi/src/org/netbeans/modules/projectapi/DataObjectEncodingQueryImplementation.java =================================================================== RCS file: projectapi/src/org/netbeans/modules/projectapi/DataObjectEncodingQueryImplementation.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ projectapi/src/org/netbeans/modules/projectapi/DataObjectEncodingQueryImplementation.java 26 Feb 2007 13:34:47 -0000 @@ -0,0 +1,53 @@ +/* + * The contents of this file are subject to the terms of the Common Development + * and Distribution License (the License). You may not use this file except in + * compliance with the License. + * + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html + * or http://www.netbeans.org/cddl.txt. + * + * When distributing Covered Code, include this CDDL Header Notice in each file + * and include the License file at http://www.netbeans.org/cddl.txt. + * If applicable, add the following below the CDDL Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun + * Microsystems, Inc. All Rights Reserved. + */ +package org.netbeans.modules.projectapi; + +import java.nio.charset.Charset; +import org.netbeans.spi.queries.FileEncodingQueryImplementation; +import org.openide.filesystems.FileObject; +import org.openide.loaders.DataObject; +import org.openide.loaders.DataObjectNotFoundException; +import org.openide.util.Exceptions; + +/** + * + * @author Tomas Zezula + */ +public class DataObjectEncodingQueryImplementation implements FileEncodingQueryImplementation { + + /** Creates a new instance of DataObjectEncodingQueryImplementation */ + public DataObjectEncodingQueryImplementation() { + } + + public Charset getEncoding(FileObject file) { + assert file != null; + try { + DataObject dobj = DataObject.find(file); + FileEncodingQueryImplementation impl = dobj.getLookup().lookup (FileEncodingQueryImplementation.class); + if (impl == null) { + return null; + } + return impl.getEncoding(file); + } catch (DataObjectNotFoundException donf) { + Exceptions.printStackTrace(donf); + return null; + } + } + +} Index: projectapi/src/org/netbeans/modules/projectapi/ProjectFileEncodingQueryImplementation.java =================================================================== RCS file: projectapi/src/org/netbeans/modules/projectapi/ProjectFileEncodingQueryImplementation.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ projectapi/src/org/netbeans/modules/projectapi/ProjectFileEncodingQueryImplementation.java 26 Feb 2007 13:34:47 -0000 @@ -0,0 +1,52 @@ +/* + * The contents of this file are subject to the terms of the Common Development + * and Distribution License (the License). You may not use this file except in + * compliance with the License. + * + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html + * or http://www.netbeans.org/cddl.txt. + * + * When distributing Covered Code, include this CDDL Header Notice in each file + * and include the License file at http://www.netbeans.org/cddl.txt. + * If applicable, add the following below the CDDL Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun + * Microsystems, Inc. All Rights Reserved. + */ +package org.netbeans.modules.projectapi; + +import java.nio.charset.Charset; +import org.netbeans.api.project.FileOwnerQuery; +import org.netbeans.api.project.Project; +import org.netbeans.spi.queries.FileEncodingQueryImplementation; +import org.openide.filesystems.FileObject; + +/** + * + * @author Tomas Zezula + */ +public class ProjectFileEncodingQueryImplementation implements FileEncodingQueryImplementation { + + + public ProjectFileEncodingQueryImplementation() { + } + + public Charset getEncoding(FileObject file) { + Project p = FileOwnerQuery.getOwner(file); + if (p == null) { + return null; + } + FileEncodingQueryImplementation delegate = p.getLookup().lookup(FileEncodingQueryImplementation.class); + if (delegate == null) { + return null; + } + return delegate.getEncoding(file); + } + + + + +} Index: queries/apichanges.xml =================================================================== RCS file: /cvs/projects/queries/apichanges.xml,v --- queries/apichanges.xml 30 Jun 2006 21:27:31 -0000 1.6 +++ queries/apichanges.xml 26 Feb 2007 13:34:47 -0000 @@ -81,7 +81,27 @@ - + + + Added support for obtaining encoding of files + + + + + + +

    + Added a query (FileEncodingQuery) to find out the encoding of the file. The query + delegates to the instances of the SPI interface (FileEncodingQueryImplementation), + when the SPI implementations don't know anything about the file encoding it returns the encoding + corresponding to the file.encoding property. The API also provides getter and setter for default + project encoding which should be used by the project generator. +

    +
    + + + +
    Added fileinfo package with NonRecursiveFolder interface. Index: queries/arch.xml =================================================================== RCS file: /cvs/projects/queries/arch.xml,v --- queries/arch.xml 15 Aug 2006 15:21:28 -0000 1.10 +++ queries/arch.xml 26 Feb 2007 13:34:47 -0000 @@ -22,7 +22,7 @@ ]> @@ -190,7 +190,8 @@ -->

    - The module uses no persistent settings. + The module uses NbPreferences to store the default project encoding. When there is no + default project encoding stored it falls back to UTF-8.

    @@ -239,7 +240,7 @@

    - Uses the NetBeans Filesystems and Lookup APIs. + Uses the NetBeans Filesystems, Lookup and Preferences APIs.

    @@ -927,5 +928,106 @@

    + + + + + + + + + + + + + +

    + Addition of a new API. +

    +
    + + + + + +

    + No. +

    +
    + + + + + +

    + The module internally uses NbPreferences to store the default value of encoding of a new project. + Clients should not access this preferences directly using preferences API but they should use + the FileEncodingQuery methods. +

    + + + + + + + + + + + + + + + + +
    keydescriptionreadwrite
    default-encodingDefault encoding which should be used for new projectxx
    +
    +
    Index: queries/manifest.mf =================================================================== RCS file: /cvs/projects/queries/manifest.mf,v --- queries/manifest.mf 12 Dec 2005 15:40:22 -0000 1.11 +++ queries/manifest.mf 26 Feb 2007 13:34:47 -0000 @@ -1,5 +1,5 @@ Manifest-Version: 1.0 OpenIDE-Module: org.netbeans.modules.queries/1 -OpenIDE-Module-Specification-Version: 1.8 +OpenIDE-Module-Specification-Version: 1.9 OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/queries/Bundle.properties Index: queries/src/org/netbeans/api/queries/FileEncodingQuery.java =================================================================== RCS file: queries/src/org/netbeans/api/queries/FileEncodingQuery.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ queries/src/org/netbeans/api/queries/FileEncodingQuery.java 26 Feb 2007 13:34:47 -0000 @@ -0,0 +1,90 @@ +/* + * The contents of this file are subject to the terms of the Common Development + * and Distribution License (the License). You may not use this file except in + * compliance with the License. + * + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html + * or http://www.netbeans.org/cddl.txt. + * + * When distributing Covered Code, include this CDDL Header Notice in each file + * and include the License file at http://www.netbeans.org/cddl.txt. + * If applicable, add the following below the CDDL Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun + * Microsystems, Inc. All Rights Reserved. + */ +package org.netbeans.api.queries; + +import java.nio.charset.Charset; +import java.util.prefs.Preferences; +import org.netbeans.spi.queries.FileEncodingQueryImplementation; +import org.openide.filesystems.FileObject; +import org.openide.util.Lookup; +import org.openide.util.NbPreferences; + +/** + * The query is used for finding encoding of files. + * The query should be used when reading or writing files to use the + * correct encoding. + * @since org.netbeans.modules.queries/1 1.9 + * @see FileEncodingQueryImplementation + * @author Tomas Zezula + */ +public class FileEncodingQuery { + + private static final String DEFAULT_ENCODING = "default-encoding"; //NOI18N + private static final String UTF_8 = "UTF-8"; //NOI18N + + + private FileEncodingQuery() { + } + + + /** + * Returns encoding of given file. + * @param file to find an encoding for + * @return encoding which should be used for given file, never returns null. + */ + public static Charset getEncoding (FileObject file) { + assert file != null; + Charset encoding; + for (FileEncodingQueryImplementation impl : Lookup.getDefault().lookupAll(FileEncodingQueryImplementation.class)) { + encoding = impl.getEncoding(file); + if (encoding != null) { + return encoding; + } + } + return Charset.defaultCharset(); + } + + /** + * Returns the encoding which should be used for newly created projects. + * The typical user of this method is a code generating new projects. + * The returned value is a last used encoding set for project. + * @return the default encoding + * + */ + public static Charset getDefaultEncoding () { + Preferences prefs = NbPreferences.forModule(FileEncodingQuery.class); + String defaultEncoding = prefs.get (DEFAULT_ENCODING,UTF_8); + return Charset.forName(defaultEncoding); + } + + /** + * Sets the encoding which should be used for newly created projects. + * The typical user of this method is a project customizer, when the + * user sets a new encoding the customizer code should update the defaul + * encoding by this method. + * @param encoding the new default encoding + * + */ + public static void setDefaultEncoding (final Charset encoding) { + assert encoding != null; + Preferences prefs = NbPreferences.forModule(FileEncodingQuery.class); + prefs.put(DEFAULT_ENCODING, encoding.name()); + } + +} Index: queries/src/org/netbeans/spi/queries/FileEncodingQueryImplementation.java =================================================================== RCS file: queries/src/org/netbeans/spi/queries/FileEncodingQueryImplementation.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ queries/src/org/netbeans/spi/queries/FileEncodingQueryImplementation.java 26 Feb 2007 13:34:47 -0000 @@ -0,0 +1,52 @@ +/* + * The contents of this file are subject to the terms of the Common Development + * and Distribution License (the License). You may not use this file except in + * compliance with the License. + * + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html + * or http://www.netbeans.org/cddl.txt. + * + * When distributing Covered Code, include this CDDL Header Notice in each file + * and include the License file at http://www.netbeans.org/cddl.txt. + * If applicable, add the following below the CDDL Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun + * Microsystems, Inc. All Rights Reserved. + */ +package org.netbeans.spi.queries; + +import java.nio.charset.Charset; +import org.openide.filesystems.FileObject; + +/** + * Information about encoding of a file. + *

    + * A default implementations are registered by the + * org.netbeans.modules.projectapi module which firstly looks up the + * implementation of this interface in the DataObject lookup. When + * available it delegates to it. When the implementation isn't available in the + * DataObject lookup or it returns null it tries to find a + * project corresponding to the file and checks whether that project has an + * implementation of this interface in its lookup. If so, it delegates to + * that implementation. Therefore it is not generally necessary + * for a project type provider nor data loader to register its own global implementation of + * this query. + *

    + * @see org.netbeans.api.queries.FileEncodingQuery + * @since org.netbeans.modules.queries/1 1.9 + * @author Tomas Zezula + */ +public interface FileEncodingQueryImplementation { + + /** + * Returns encoding of a given file. + * @param file to find an encoding for + * @return encoding which should be used for given file + * or null when nothing is known about the file encoding. + */ + public Charset getEncoding (FileObject file); + +}