Index: manifest.mf =================================================================== RCS file: /cvs/openide/loaders/manifest.mf,v retrieving revision 1.30 diff -u -r1.30 manifest.mf --- manifest.mf 7 Feb 2007 10:02:42 -0000 1.30 +++ manifest.mf 14 Mar 2007 10:20:29 -0000 @@ -1,5 +1,5 @@ Manifest-Version: 1.0 OpenIDE-Module: org.openide.loaders -OpenIDE-Module-Specification-Version: 6.1 +OpenIDE-Module-Specification-Version: 6.2 OpenIDE-Module-Localizing-Bundle: org/openide/loaders/Bundle.properties Index: api/apichanges.xml =================================================================== RCS file: /cvs/openide/loaders/api/apichanges.xml,v retrieving revision 1.25 diff -u -r1.25 apichanges.xml --- api/apichanges.xml 7 Feb 2007 10:02:43 -0000 1.25 +++ api/apichanges.xml 14 Mar 2007 10:20:29 -0000 @@ -83,6 +83,24 @@ + + + DataEditorSupport uses FileEncodingQuery to read or write document in correct encoding + + + + + +

+ The DataEditorSupport overides the loadFromStreamToKit and saveFromKitToStream methods. + In these methods it uses FileEncodingQuery to find out the encoding of the file, creates + the Reader or Writer with obtained encoding and calls EditorKit.read or EditorKit.write. +

+
+ + +
+ Integration with scripting and templating Index: nbproject/project.xml =================================================================== RCS file: /cvs/openide/loaders/nbproject/project.xml,v retrieving revision 1.26 diff -u -r1.26 project.xml --- nbproject/project.xml 20 Feb 2007 15:13:51 -0000 1.26 +++ nbproject/project.xml 14 Mar 2007 10:20:30 -0000 @@ -33,6 +33,15 @@ + org.netbeans.modules.queries + + + + 1 + 1.9 + + + org.openide.actions Index: src/org/openide/text/DataEditorSupport.java =================================================================== RCS file: /cvs/openide/loaders/src/org/openide/text/DataEditorSupport.java,v retrieving revision 1.39 diff -u -r1.39 DataEditorSupport.java --- src/org/openide/text/DataEditorSupport.java 28 Oct 2006 21:57:36 -0000 1.39 +++ src/org/openide/text/DataEditorSupport.java 14 Mar 2007 10:20:30 -0000 @@ -23,10 +23,12 @@ import java.beans.*; import java.io.*; import java.lang.ref.Reference; +import java.nio.charset.Charset; import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.text.*; +import org.netbeans.api.queries.FileEncodingQuery; import org.netbeans.modules.openide.loaders.UIException; import org.openide.*; import org.openide.filesystems.*; @@ -272,6 +274,34 @@ return super.canClose(); } + + /** + * @inheritDoc + */ + @Override + protected void loadFromStreamToKit(StyledDocument doc, InputStream stream, EditorKit kit) throws IOException, BadLocationException { + final Charset c = FileEncodingQuery.getEncoding(this.getDataObject().getPrimaryFile()); + final Reader r = new InputStreamReader (stream, c); + try { + kit.read(r, doc, 0); + } finally { + r.close(); + } + } + + /** + * @inheritDoc + */ + @Override + protected void saveFromKitToStream(StyledDocument doc, EditorKit kit, OutputStream stream) throws IOException, BadLocationException { + final Charset c = FileEncodingQuery.getEncoding(this.getDataObject().getPrimaryFile()); + final Writer w = new OutputStreamWriter (stream, c); + try { + kit.write(w, doc, 0, doc.getLength()); + } finally { + w.close(); + } + } /** Saves document. Overrides superclass method, adds checking * for read-only property of saving file and warns user in that case. */ Index: test/unit/src/org/openide/text/DataEditorSupportTest.java =================================================================== RCS file: /cvs/openide/loaders/test/unit/src/org/openide/text/DataEditorSupportTest.java,v retrieving revision 1.6 diff -u -r1.6 DataEditorSupportTest.java --- test/unit/src/org/openide/text/DataEditorSupportTest.java 5 Jan 2007 22:56:21 -0000 1.6 +++ test/unit/src/org/openide/text/DataEditorSupportTest.java 14 Mar 2007 10:20:30 -0000 @@ -25,6 +25,7 @@ import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Method; +import java.nio.charset.Charset; import java.util.Collections; import java.util.HashSet; import java.util.Set; @@ -38,6 +39,7 @@ import org.netbeans.junit.NbTestCase; import org.netbeans.junit.NbTestSuite; +import org.netbeans.spi.queries.FileEncodingQueryImplementation; import org.openide.actions.*; import org.openide.cookies.CloseCookie; import org.openide.cookies.EditCookie; @@ -228,6 +230,17 @@ env.fileLock.releaseLock(); } + public void testFileEncodingQuery () throws Exception { + DES des = support(); + FileEncodingQueryImpl.getDefault().reset(); + StyledDocument doc = des.openDocument(); + assertEquals(des.getDataObject().getPrimaryFile(),FileEncodingQueryImpl.getDefault().getFile()); + FileEncodingQueryImpl.getDefault().reset(); + doc.insertString(doc.getLength(), " Added text.", null); + des.saveDocument(); + assertEquals(des.getDataObject().getPrimaryFile(),FileEncodingQueryImpl.getDefault().getFile()); + } + /** File object that let us know what is happening and delegates to certain * instance variables of the test. */ @@ -392,6 +405,37 @@ } } + + private static final class FileEncodingQueryImpl extends FileEncodingQueryImplementation { + + private static FileEncodingQueryImpl instance; + + private FileObject file; + + private FileEncodingQueryImpl () { + + } + + public Charset getEncoding(FileObject file) { + this.file = file; + return Charset.defaultCharset(); + } + + public void reset () { + this.file = null; + } + + public FileObject getFile () { + return this.file; + } + + public synchronized static FileEncodingQueryImpl getDefault () { + if (instance == null) { + instance = new FileEncodingQueryImpl (); + } + return instance; + } + } public static final class Lkp extends org.openide.util.lookup.AbstractLookup { public Lkp () { @@ -402,6 +446,7 @@ super (ic); ic.add (new Pool ()); + ic.add (FileEncodingQueryImpl.getDefault()); } } // end of Lkp