use of org.eclipse.ui.texteditor.ResourceMarkerAnnotationModel in project eclipse.platform.text by eclipse.
the class FileDocumentProvider method doSaveDocument.
@Override
protected void doSaveDocument(IProgressMonitor monitor, Object element, IDocument document, boolean overwrite) throws CoreException {
if (element instanceof IFileEditorInput) {
IFileEditorInput input = (IFileEditorInput) element;
String encoding = null;
FileInfo info = (FileInfo) getElementInfo(element);
IFile file = input.getFile();
encoding = getCharsetForNewFile(file, document, info);
if (info != null && info.fBOM == IContentDescription.BOM_UTF_16LE && CHARSET_UTF_16.equals(encoding))
encoding = CHARSET_UTF_16LE;
Charset charset;
try {
charset = Charset.forName(encoding);
} catch (UnsupportedCharsetException ex) {
String message = NLSUtility.format(TextEditorMessages.DocumentProvider_error_unsupported_encoding_message_arg, encoding);
IStatus s = new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, message, ex);
throw new CoreException(s);
} catch (IllegalCharsetNameException ex) {
String message = NLSUtility.format(TextEditorMessages.DocumentProvider_error_illegal_encoding_message_arg, encoding);
IStatus s = new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, message, ex);
throw new CoreException(s);
}
CharsetEncoder encoder = charset.newEncoder();
encoder.onMalformedInput(CodingErrorAction.REPLACE);
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
InputStream stream;
try {
byte[] bytes;
ByteBuffer byteBuffer = encoder.encode(CharBuffer.wrap(document.get()));
if (byteBuffer.hasArray())
bytes = byteBuffer.array();
else {
bytes = new byte[byteBuffer.limit()];
byteBuffer.get(bytes);
}
stream = new ByteArrayInputStream(bytes, 0, byteBuffer.limit());
} catch (CharacterCodingException ex) {
Assert.isTrue(ex instanceof UnmappableCharacterException);
String message = NLSUtility.format(TextEditorMessages.DocumentProvider_error_charset_mapping_failed_message_arg, encoding);
IStatus s = new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, EditorsUI.CHARSET_MAPPING_FAILED, message, null);
throw new CoreException(s);
}
/*
* XXX:
* This is a workaround for a corresponding bug in Java readers and writer,
* see http://developer.java.sun.com/developer/bugParade/bugs/4508058.html
*/
if (info != null && info.fBOM == IContentDescription.BOM_UTF_8 && CHARSET_UTF_8.equals(encoding))
stream = new SequenceInputStream(new ByteArrayInputStream(IContentDescription.BOM_UTF_8), stream);
if (info != null && info.fBOM == IContentDescription.BOM_UTF_16LE && CHARSET_UTF_16LE.equals(encoding))
stream = new SequenceInputStream(new ByteArrayInputStream(IContentDescription.BOM_UTF_16LE), stream);
if (file.exists()) {
if (info != null && !overwrite)
checkSynchronizationState(info.fModificationStamp, file);
// inform about the upcoming content change
fireElementStateChanging(element);
try {
file.setContents(stream, overwrite, true, monitor);
} catch (CoreException x) {
// inform about failure
fireElementStateChangeFailed(element);
throw x;
} catch (RuntimeException x) {
// inform about failure
fireElementStateChangeFailed(element);
throw x;
}
if (info != null) {
ResourceMarkerAnnotationModel model = (ResourceMarkerAnnotationModel) info.fModel;
if (model != null)
model.updateMarkers(info.fDocument);
info.fModificationStamp = computeModificationStamp(file);
}
} else {
SubMonitor subMonitor = SubMonitor.convert(monitor, TextEditorMessages.FileDocumentProvider_task_saving, 2);
ContainerCreator creator = new ContainerCreator(file.getWorkspace(), file.getParent().getFullPath());
creator.createContainer(subMonitor.split(1));
file.create(stream, false, subMonitor.split(1));
}
} else {
super.doSaveDocument(monitor, element, document, overwrite);
}
}
use of org.eclipse.ui.texteditor.ResourceMarkerAnnotationModel in project linuxtools by eclipse.
the class DockerDocumentProvider method doSaveDocument.
@Override
protected void doSaveDocument(IProgressMonitor monitor, Object element, IDocument document, boolean overwrite) throws CoreException {
if (element instanceof FileStoreEditorInput) {
String encoding = null;
ElementInfo info = getElementInfo(element);
Path filePath = Paths.get(((FileStoreEditorInput) element).getURI());
encoding = getEncoding(element);
Charset charset;
try {
charset = Charset.forName(encoding);
} catch (UnsupportedCharsetException ex) {
String message = NLS.bind(Messages.DockerDocumentProvider_encoding_not_supported, encoding);
IStatus s = new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, message, ex);
throw new CoreException(s);
} catch (IllegalCharsetNameException ex) {
String message = NLS.bind(Messages.DockerDocumentProvider_encoding_not_legal, encoding);
IStatus s = new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, message, ex);
throw new CoreException(s);
}
CharsetEncoder encoder = charset.newEncoder();
encoder.onMalformedInput(CodingErrorAction.REPLACE);
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
InputStream stream;
try {
byte[] bytes;
ByteBuffer byteBuffer = encoder.encode(CharBuffer.wrap(document.get()));
if (byteBuffer.hasArray())
bytes = byteBuffer.array();
else {
bytes = new byte[byteBuffer.limit()];
byteBuffer.get(bytes);
}
stream = new ByteArrayInputStream(bytes, 0, byteBuffer.limit());
} catch (CharacterCodingException ex) {
Assert.isTrue(ex instanceof UnmappableCharacterException);
String message = NLS.bind(Messages.DockerDocumentProvider_cannot_be_mapped + Messages.DockerDocumentProvider_chars_not_supported, encoding);
IStatus s = new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, EditorsUI.CHARSET_MAPPING_FAILED, message, null);
throw new CoreException(s);
}
if (Files.exists(filePath)) {
// inform about the upcoming content change
fireElementStateChanging(element);
try (FileWriter fw = new FileWriter(filePath.toFile());
InputStreamReader istream = new InputStreamReader(stream)) {
char[] bb = new char[1024];
int nRead = istream.read(bb);
while (nRead > 0) {
fw.write(bb, 0, nRead);
nRead = istream.read(bb);
}
} catch (RuntimeException | IOException x) {
// inform about failure
fireElementStateChangeFailed(element);
throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, x.getMessage()));
}
if (info != null) {
ResourceMarkerAnnotationModel model = (ResourceMarkerAnnotationModel) info.fModel;
if (model != null)
model.updateMarkers(info.fDocument);
}
} else {
try {
Files.createFile(filePath);
try (FileWriter fw = new FileWriter(filePath.toFile());
InputStreamReader istream = new InputStreamReader(stream)) {
char[] bb = new char[1024];
int nRead = istream.read(bb);
while (nRead > 0) {
fw.write(bb, 0, nRead);
nRead = istream.read(bb);
}
} catch (IOException x) {
throw x;
}
} catch (IOException e) {
throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage()));
} finally {
monitor.done();
}
}
} else {
super.doSaveDocument(monitor, element, document, overwrite);
}
}
Aggregations