Skip to content
This repository has been archived by the owner on Apr 10, 2021. It is now read-only.

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

Open
bennycode opened this issue Nov 14, 2014 · 7 comments

Comments

@bennycode
Copy link
Member

No description provided.

@bennycode bennycode added this to the v1.0.0 milestone Nov 14, 2014
@bennycode
Copy link
Member Author

See #21

@bennycode
Copy link
Member Author

"\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
Copy link
Member Author

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
Copy link
Member Author

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 bennycode self-assigned this Nov 28, 2014
@bennycode
Copy link
Member Author

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

@bennycode
Copy link
Member Author

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

@bennycode
Copy link
Member Author

@bennycode bennycode modified the milestones: v1.1.0, v1.0.0 Feb 6, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant