Lines 430-476
Link Here
|
430 |
c = FileEncodingQuery.getEncoding(this.getDataObject().getPrimaryFile()); |
430 |
c = FileEncodingQuery.getEncoding(this.getDataObject().getPrimaryFile()); |
431 |
} |
431 |
} |
432 |
final FileObject fo = this.getDataObject().getPrimaryFile(); |
432 |
final FileObject fo = this.getDataObject().getPrimaryFile(); |
433 |
if (!warnedEncodingFiles.contains(fo)) { |
433 |
final Reader r; |
434 |
if (!checkIfCharsetCanDecodeFile(fo, c)) { |
434 |
if (warnedEncodingFiles.contains(fo)) { |
435 |
throw new UserQuestionException(NbBundle.getMessage(DataObject.class, "MSG_EncodingProblem", c)) { |
435 |
r = new InputStreamReader (stream, c); |
436 |
@Override |
436 |
} else { |
437 |
public void confirmed() throws IOException { |
437 |
CharsetDecoder decoder = c.newDecoder(); |
438 |
warnedEncodingFiles.add(fo); |
438 |
decoder.reset(); |
439 |
} |
439 |
r = new InputStreamReader (stream, decoder); |
440 |
}; |
|
|
441 |
} |
442 |
} |
440 |
} |
443 |
final Reader r = new InputStreamReader (stream, c); |
441 |
try { |
444 |
kit.read(r, doc, 0); |
442 |
kit.read(r, doc, 0); |
|
|
443 |
} catch (CharacterCodingException e) { |
444 |
ERR.log(Level.FINE, "Encoding problem using " + c, e); // NOI18N |
445 |
doc.remove(0, doc.getLength()); |
446 |
createAndThrowIncorrectCharsetUQE(fo, c); |
447 |
} catch (IllegalStateException e) { |
448 |
ERR.log(Level.FINE, "Encoding problem using " + c, e); // NOI18N |
449 |
doc.remove(0, doc.getLength()); |
450 |
createAndThrowIncorrectCharsetUQE(fo, c); |
451 |
} |
445 |
} |
452 |
} |
446 |
|
453 |
|
447 |
private static boolean checkIfCharsetCanDecodeFile(FileObject fo, Charset charset) { |
454 |
private static boolean createAndThrowIncorrectCharsetUQE(final FileObject fo, Charset charset) throws UserQuestionException { |
448 |
try { |
455 |
throw new UserQuestionException(NbBundle.getMessage(DataObject.class, "MSG_EncodingProblem", charset)) { |
449 |
int BUF_SIZE = 1024*4; |
456 |
@Override |
450 |
|
457 |
public void confirmed() throws IOException { |
451 |
BufferedInputStream input = new BufferedInputStream(fo.getInputStream(), BUF_SIZE); |
458 |
warnedEncodingFiles.add(fo); |
452 |
try { |
|
|
453 |
CharsetDecoder decoder = charset.newDecoder(); |
454 |
decoder.reset(); |
455 |
try { |
456 |
BufferedReader reader = new BufferedReader(new InputStreamReader(fo.getInputStream(), decoder), BUF_SIZE); |
457 |
char[] buf = new char[BUF_SIZE]; |
458 |
while (reader.read(buf) > 0) {} |
459 |
reader.close(); |
460 |
} catch (CharacterCodingException e) { |
461 |
ERR.log(Level.FINE, "Encoding problem using " + charset, e); // NOI18N |
462 |
return false; |
463 |
} catch (IllegalStateException e) { |
464 |
ERR.log(Level.FINE, "Encoding problem using " + charset, e); // NOI18N |
465 |
return false; |
466 |
} |
467 |
} finally { |
468 |
input.close(); |
469 |
} |
459 |
} |
470 |
} catch (IOException ex) { |
460 |
}; |
471 |
ERR.log(Level.FINE, "Encoding problem using " + charset, ex); // NOI18N |
|
|
472 |
} |
473 |
return true; |
474 |
} |
461 |
} |
475 |
|
462 |
|
476 |
private static Set<FileObject> warnedEncodingFiles = new WeakSet<FileObject>(); |
463 |
private static Set<FileObject> warnedEncodingFiles = new WeakSet<FileObject>(); |