Index: apichanges.xml =================================================================== RCS file: /cvs/projects/queries/apichanges.xml,v retrieving revision 1.6 diff -u -r1.6 apichanges.xml --- apichanges.xml 30 Jun 2006 21:27:31 -0000 1.6 +++ apichanges.xml 9 Feb 2007 14:28:38 -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: manifest.mf =================================================================== RCS file: /cvs/projects/queries/manifest.mf,v retrieving revision 1.11 diff -u -r1.11 manifest.mf --- manifest.mf 12 Dec 2005 15:40:22 -0000 1.11 +++ manifest.mf 9 Feb 2007 14:28:38 -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: src/org/netbeans/api/queries/FileEncodingQuery.java =================================================================== RCS file: src/org/netbeans/api/queries/FileEncodingQuery.java diff -N src/org/netbeans/api/queries/FileEncodingQuery.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/netbeans/api/queries/FileEncodingQuery.java 9 Feb 2007 14:28:38 -0000 @@ -0,0 +1,94 @@ +/* + * 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; + } + } + String _encoding = System.getProperty("file.encoding"); //NOI18N + assert _encoding != null; + encoding = Charset.forName(_encoding); + assert encoding != null; + return encoding; + } + + /** + * 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: src/org/netbeans/spi/queries/FileEncodingQueryImplementation.java =================================================================== RCS file: src/org/netbeans/spi/queries/FileEncodingQueryImplementation.java diff -N src/org/netbeans/spi/queries/FileEncodingQueryImplementation.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/netbeans/spi/queries/FileEncodingQueryImplementation.java 9 Feb 2007 14:28:39 -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); + +}