Check "Content-Encoding" FileObject attribute when we create the Reader. BLD200503161050
authormentlicher@netbeans.org
Tue, 15 Mar 2005 19:29:20 +0000
changeset 594422c1fda0156d
parent 5943 4b217f65949e
child 5945 be136f3287cb
Check "Content-Encoding" FileObject attribute when we create the Reader.
This fixes issue #56318 for Java files.
vcs.advanced/src/org/netbeans/modules/vcs/profiles/commands/AbstractDiffCommand.java
     1.1 --- a/vcs.advanced/src/org/netbeans/modules/vcs/profiles/commands/AbstractDiffCommand.java	Tue Mar 15 16:38:06 2005 +0000
     1.2 +++ b/vcs.advanced/src/org/netbeans/modules/vcs/profiles/commands/AbstractDiffCommand.java	Tue Mar 15 19:29:20 2005 +0000
     1.3 @@ -14,14 +14,19 @@
     1.4  package org.netbeans.modules.vcs.profiles.commands;
     1.5  
     1.6  import java.io.File;
     1.7 +import java.io.FileInputStream;
     1.8  import java.io.FileNotFoundException;
     1.9  import java.io.FileReader;
    1.10 +import java.io.InputStreamReader;
    1.11  import java.io.Reader;
    1.12 +import java.io.UnsupportedEncodingException;
    1.13  import java.util.ArrayList;
    1.14  import java.util.Hashtable;
    1.15  import java.util.List;
    1.16  
    1.17  import org.openide.NotifyDescriptor;
    1.18 +import org.openide.filesystems.FileObject;
    1.19 +import org.openide.filesystems.FileUtil;
    1.20  import org.openide.util.Lookup;
    1.21  import org.openide.util.NbBundle;
    1.22  
    1.23 @@ -38,7 +43,9 @@
    1.24  import org.netbeans.modules.vcscore.commands.VcsCommandExecutor;
    1.25  import org.netbeans.modules.vcscore.util.VariableValueAdjustment;
    1.26  import org.netbeans.modules.vcscore.util.VcsUtilities;
    1.27 +
    1.28  import org.openide.DialogDisplayer;
    1.29 +import org.openide.ErrorManager;
    1.30  
    1.31  import javax.swing.*;
    1.32  
    1.33 @@ -282,7 +289,7 @@
    1.34          String file1 = tmpDir+File.separator+file;
    1.35          String file2;
    1.36          if (tmpDir2 == null) {
    1.37 -            file2 = rootDir+File.separator+dir+File.separator+file;
    1.38 +            file2 = path;
    1.39          } else {
    1.40              file2 = tmpDir2+File.separator+file;
    1.41          }
    1.42 @@ -291,7 +298,7 @@
    1.43                                                     file1Title, file2Title,
    1.44                                                     mimeType, false, true,
    1.45                                                     new File(file1), new File(file2),
    1.46 -                                                   tmpDir, tmpDir2);
    1.47 +                                                   tmpDir, tmpDir2, getEncoding(path));
    1.48          presenter.initWithDiffInfo(diffInfo);
    1.49          diffInfo.setPresentingComponent(diffComponent);
    1.50          presenter.setVisualizer((DiffVisualizer) Lookup.getDefault().lookup(DiffVisualizer.class));
    1.51 @@ -380,6 +387,21 @@
    1.52      
    1.53      public abstract void outputData(String[] elements);
    1.54      
    1.55 +    private String getEncoding(String absolutePath) {
    1.56 +        Object encoding = null;
    1.57 +        try {
    1.58 +            FileObject fo = FileUtil.toFileObject(new File(absolutePath));
    1.59 +            if (fo != null) {
    1.60 +                encoding = fo.getAttribute("Content-Encoding");
    1.61 +            }
    1.62 +        } catch (IllegalArgumentException iaex) {} // Ignore
    1.63 +        if (encoding != null) {
    1.64 +            return encoding.toString();
    1.65 +        } else {
    1.66 +            return null;
    1.67 +        }
    1.68 +    }
    1.69 +    
    1.70      private static class DiffInfo extends DiffPresenter.Info {
    1.71          
    1.72          private Difference[] diffs;
    1.73 @@ -387,24 +409,41 @@
    1.74          private File file2;
    1.75          private File tmpDir;
    1.76          private File tmpDir2;
    1.77 +        private String encoding;
    1.78          
    1.79          public DiffInfo(Difference[] diffs, String name1, String name2, String title1, String title2,
    1.80                          String mimeType, boolean chooseProviders, boolean chooseVisualizers,
    1.81 -                        File file1, File file2, File tmpDir, File tmpDir2) {
    1.82 +                        File file1, File file2, File tmpDir, File tmpDir2, String encoding) {
    1.83              super(name1, name2, title1, title2, mimeType, chooseProviders, chooseVisualizers);
    1.84              this.file1 = file1;
    1.85              this.file2 = file2;
    1.86              this.tmpDir = tmpDir;
    1.87              this.tmpDir2 = tmpDir2;
    1.88              this.diffs = diffs;
    1.89 +            this.encoding = encoding;
    1.90          }
    1.91          
    1.92          public Reader createFirstReader() throws FileNotFoundException {
    1.93 -            return new FileReader(file1);
    1.94 +            return createEncodedFileReader(file1);
    1.95          }
    1.96          
    1.97          public Reader createSecondReader() throws FileNotFoundException {
    1.98 -            return new FileReader(file2);
    1.99 +            return createEncodedFileReader(file2);
   1.100 +        }
   1.101 +        
   1.102 +        private Reader createEncodedFileReader(File file) throws FileNotFoundException {
   1.103 +            Reader r = null;
   1.104 +            if (encoding != null) {
   1.105 +                try {
   1.106 +                    r = new InputStreamReader(new FileInputStream(file), encoding);
   1.107 +                } catch (UnsupportedEncodingException ueex) {
   1.108 +                    ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Unknown encoding attribute '"+encoding+"' of "+file.getAbsolutePath());
   1.109 +                }
   1.110 +            }
   1.111 +            if (r == null) {
   1.112 +                r = new InputStreamReader(new FileInputStream(file));
   1.113 +            }
   1.114 +            return r;
   1.115          }
   1.116          
   1.117          public Difference[] getDifferences() {