editorconfig-netbeans icon indicating copy to clipboard operation
editorconfig-netbeans copied to clipboard

Test that files with "charset" declaration are saved with their file marks

Open bennycode opened this issue 10 years ago • 7 comments

bennycode avatar Nov 14 '14 02:11 bennycode

See https://github.com/welovecoding/editorconfig-netbeans/issues/21

bennycode avatar Nov 17 '14 21:11 bennycode

"\uFEFF" will be FF FE which indicates UTF-16LE BOM.

Another option would be:

 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(string.length() * 2 + 2);
 byteArrayOutputStream.write(new byte[]{(byte)0xFF,(byte)0xFE});
 byteArrayOutputStream.write(string.getBytes("UTF-16LE"));
 return byteArrayOutputStream.toByteArray();

bennycode avatar Nov 18 '14 11:11 bennycode

Helpful snippets

DataEditorSupport des = dataObject.getLookup().lookup(DataEditorSupport.class);
-> saveFromKitToStream( StyledDocument myDoc, OutputStream os )
public static EditorKit getEditorKit(String mimePath) {
    Lookup lookup = MimeLookup.getLookup(MimePath.parse(mimePath));
    EditorKit kit = lookup.lookup(EditorKit.class);

    if (kit == null) {
        // Try 'text/plain'
        lookup = MimeLookup.getLookup(MimePath.parse("text/plain"));
        kit = lookup.lookup(EditorKit.class);
    }

    // Don't use the prototype instance straightaway
    return kit != null ? (EditorKit) kit.clone() : new PlainEditorKit();
}
Charset c = charsets.get(this.getDataObject());

if (c == null) {
    c = FileEncodingQuery.getEncoding(this.getDataObject().getPrimaryFile());
}

FilterOutputStream fos = new FilterOutputStream(stream) {
    @Override
    public void close() throws IOException {
        flush();
    }
};

Writer w = new OutputStreamWriter (fos, c);

try {
    kit.write(w, doc, 0, doc.getLength());
} finally {
    w.close();
}
  private void saveFromKitToStream(FileObject fo, StyledDocument doc, Charset c, OutputStream stream) {
    EditorKit kit = NetBeansFileUtil.getEditorKit(fo);

    FilterOutputStream fos = new FilterOutputStream(stream) {
      @Override
      public void close() throws IOException {
        flush();
      }
    };

    Writer w = new OutputStreamWriter(fos, c);

    try {
      kit.write(w, doc, 0, doc.getLength());
    } catch (IOException | BadLocationException ex) {
      Exceptions.printStackTrace(ex);
    } finally {
      try {
        w.close();
      } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
      }
    }
  }

bennycode avatar Nov 23 '14 00:11 bennycode

Approach to set charset for EditorKit reader:

"Another way the character set can be specified is in the document itself. This requires reading the document prior to determining the character set that is desired. To handle this, it is expected that the EditorKit.read operation throw a ChangedCharSetException which will be caught. The read is then restarted with a new Reader that uses the character set specified in the ChangedCharSetException (which is an IOException)"

Source: https://docs.oracle.com/javase/7/docs/api/javax/swing/JEditorPane.html

Code samples: http://www.programcreek.com/java-api-examples/index.php?api=javax.swing.text.ChangedCharSetException

bennycode avatar Nov 24 '14 00:11 bennycode

BOMs in bytes: http://stackoverflow.com/a/1888284/451634

bennycode avatar Nov 29 '14 01:11 bennycode

EOLs can be detected in Swing with: textComponent.getDocument().getProperty( DefaultEditorKit.EndOfLineStringProperty );

bennycode avatar Nov 29 '14 02:11 bennycode

Interesting: http://commons.apache.org/proper/commons-net/jacoco/org.apache.commons.net.io/CRLFLineReader.java.html

bennycode avatar Nov 29 '14 15:11 bennycode