@Test public void test() throws Exception { Blob blob; blob = new ByteArrayBlob(new byte[0], null, "somecharset"); assertNull(NoteImporter.guessEncoding(blob)); blob = new ByteArrayBlob(new byte[0], null, null); assertNull(NoteImporter.guessEncoding(blob)); blob = new ByteArrayBlob(new byte[0], "foo", null); assertNull(NoteImporter.guessEncoding(blob)); blob = new ByteArrayBlob(new byte[] { (byte) 0xe9 }, "text/plain; charset=iso-8859-1; x=y", null); assertEquals("\u00e9", NoteImporter.guessEncoding(blob)); assertEquals("text/plain", blob.getMimeType()); blob = new ByteArrayBlob(new byte[] { (byte) 0xe9 }, "text/plain", null); assertEquals("\u00e9", NoteImporter.guessEncoding(blob)); assertEquals("text/plain", blob.getMimeType()); blob = new ByteArrayBlob(new byte[] { (byte) 0xc3, (byte) 0xa9 }, "text/plain", null); assertEquals("\u00e9", NoteImporter.guessEncoding(blob)); assertEquals("text/plain", blob.getMimeType()); blob = new ByteArrayBlob(new byte[] { (byte) 0xe9 }, "text/plain; charset=utf-8; x=y", null); assertEquals("\u00e9", NoteImporter.guessEncoding(blob)); assertEquals("text/plain", blob.getMimeType()); }
protected static String guessEncoding(Blob blob) throws IOException { if (blob.getEncoding() != null) { return null; } String mimeType = blob.getMimeType(); if (mimeType == null) { return null; } if (!mimeType.startsWith("text/") && !mimeType.startsWith("application/xhtml")) { return null; } byte[] bytes = blob.getByteArray(); List<String> charsets = Arrays.asList("utf-8", "iso-8859-1"); String CSEQ = "charset="; int i = mimeType.indexOf(CSEQ); if (i > 0) { String onlyMimeType = mimeType.substring(0, i).replace(";", "").trim(); blob.setMimeType(onlyMimeType); String charset = mimeType.substring(i + CSEQ.length()); i = charset.indexOf(";"); if (i > 0) { charset = charset.substring(0, i); } charset = charset.trim().replace("\"", ""); charsets = new ArrayList<String>(charsets); charsets.add(0, charset); } for (String charset : charsets) { try { Charset cs = Charset.forName(charset); CharsetDecoder d = cs.newDecoder().onMalformedInput( CodingErrorAction.REPORT).onUnmappableCharacter( CodingErrorAction.REPORT); CharBuffer cb = d.decode(ByteBuffer.wrap(bytes)); return cb.toString(); } catch (IllegalArgumentException e) { } catch (CharacterCodingException e) { } } return null; }
NoteImporter extends AbstractFileImporter { protected static String guessEncoding(Blob blob) throws IOException { if (blob.getEncoding() != null) { return null; } String mimeType = blob.getMimeType(); if (mimeType == null) { return null; } if (!mimeType.startsWith("text/") && !mimeType.startsWith("application/xhtml")) { return null; } byte[] bytes = blob.getByteArray(); List<String> charsets = Arrays.asList("utf-8", "iso-8859-1"); String CSEQ = "charset="; int i = mimeType.indexOf(CSEQ); if (i > 0) { String onlyMimeType = mimeType.substring(0, i).replace(";", "").trim(); blob.setMimeType(onlyMimeType); String charset = mimeType.substring(i + CSEQ.length()); i = charset.indexOf(";"); if (i > 0) { charset = charset.substring(0, i); } charset = charset.trim().replace("\"", ""); charsets = new ArrayList<String>(charsets); charsets.add(0, charset); } for (String charset : charsets) { try { Charset cs = Charset.forName(charset); CharsetDecoder d = cs.newDecoder().onMalformedInput( CodingErrorAction.REPORT).onUnmappableCharacter( CodingErrorAction.REPORT); CharBuffer cb = d.decode(ByteBuffer.wrap(bytes)); return cb.toString(); } catch (IllegalArgumentException e) { } catch (CharacterCodingException e) { } } return null; } }
NoteImporter extends AbstractFileImporter { protected static String guessEncoding(Blob blob) throws IOException { if (blob.getEncoding() != null) { return null; } String mimeType = blob.getMimeType(); if (mimeType == null) { return null; } if (!mimeType.startsWith("text/") && !mimeType.startsWith("application/xhtml")) { return null; } byte[] bytes = blob.getByteArray(); List<String> charsets = Arrays.asList("utf-8", "iso-8859-1"); String CSEQ = "charset="; int i = mimeType.indexOf(CSEQ); if (i > 0) { String onlyMimeType = mimeType.substring(0, i).replace(";", "").trim(); blob.setMimeType(onlyMimeType); String charset = mimeType.substring(i + CSEQ.length()); i = charset.indexOf(";"); if (i > 0) { charset = charset.substring(0, i); } charset = charset.trim().replace("\"", ""); charsets = new ArrayList<String>(charsets); charsets.add(0, charset); } for (String charset : charsets) { try { Charset cs = Charset.forName(charset); CharsetDecoder d = cs.newDecoder().onMalformedInput( CodingErrorAction.REPORT).onUnmappableCharacter( CodingErrorAction.REPORT); CharBuffer cb = d.decode(ByteBuffer.wrap(bytes)); return cb.toString(); } catch (IllegalArgumentException e) { } catch (CharacterCodingException e) { } } return null; }  }
NoteImporter extends AbstractFileImporter { protected static String guessEncoding(Blob blob) throws IOException { if (blob.getEncoding() != null) { return null; } String mimeType = blob.getMimeType(); if (mimeType == null) { return null; } if (!mimeType.startsWith("text/") && !mimeType.startsWith("application/xhtml")) { return null; } byte[] bytes = blob.getByteArray(); List<String> charsets = Arrays.asList("utf-8", "iso-8859-1"); String CSEQ = "charset="; int i = mimeType.indexOf(CSEQ); if (i > 0) { String onlyMimeType = mimeType.substring(0, i).replace(";", "").trim(); blob.setMimeType(onlyMimeType); String charset = mimeType.substring(i + CSEQ.length()); i = charset.indexOf(";"); if (i > 0) { charset = charset.substring(0, i); } charset = charset.trim().replace("\"", ""); charsets = new ArrayList<String>(charsets); charsets.add(0, charset); } for (String charset : charsets) { try { Charset cs = Charset.forName(charset); CharsetDecoder d = cs.newDecoder().onMalformedInput( CodingErrorAction.REPORT).onUnmappableCharacter( CodingErrorAction.REPORT); CharBuffer cb = d.decode(ByteBuffer.wrap(bytes)); return cb.toString(); } catch (IllegalArgumentException e) { } catch (CharacterCodingException e) { } } return null; }  @Override String getDefaultDocType(); @Override boolean isOverwriteByTitle(); @Override void updateDocument(DocumentModel doc, Blob content); }
NoteImporter extends AbstractFileImporter { protected static String guessEncoding(Blob blob) throws IOException { if (blob.getEncoding() != null) { return null; } String mimeType = blob.getMimeType(); if (mimeType == null) { return null; } if (!mimeType.startsWith("text/") && !mimeType.startsWith("application/xhtml")) { return null; } byte[] bytes = blob.getByteArray(); List<String> charsets = Arrays.asList("utf-8", "iso-8859-1"); String CSEQ = "charset="; int i = mimeType.indexOf(CSEQ); if (i > 0) { String onlyMimeType = mimeType.substring(0, i).replace(";", "").trim(); blob.setMimeType(onlyMimeType); String charset = mimeType.substring(i + CSEQ.length()); i = charset.indexOf(";"); if (i > 0) { charset = charset.substring(0, i); } charset = charset.trim().replace("\"", ""); charsets = new ArrayList<String>(charsets); charsets.add(0, charset); } for (String charset : charsets) { try { Charset cs = Charset.forName(charset); CharsetDecoder d = cs.newDecoder().onMalformedInput( CodingErrorAction.REPORT).onUnmappableCharacter( CodingErrorAction.REPORT); CharBuffer cb = d.decode(ByteBuffer.wrap(bytes)); return cb.toString(); } catch (IllegalArgumentException e) { } catch (CharacterCodingException e) { } } return null; }  @Override String getDefaultDocType(); @Override boolean isOverwriteByTitle(); @Override void updateDocument(DocumentModel doc, Blob content);  }