use of org.eclipse.wst.sse.core.internal.exceptions.MalformedInputExceptionWithDetail in project webtools.sourceediting by eclipse.
the class AbstractDocumentLoader method readInputStream.
/**
* Very mechanical method, just to read the characters, once the reader is
* correctly created. Can throw MalFormedInputException.
*/
private StringBuffer readInputStream(Reader reader) throws IOException {
int fBlocksRead = 0;
StringBuffer buffer = new StringBuffer();
int numRead = 0;
try {
char[] tBuff = new char[CodedIO.MAX_BUF_SIZE];
while (numRead != -1) {
numRead = reader.read(tBuff, 0, tBuff.length);
if (numRead > 0) {
buffer.append(tBuff, 0, numRead);
fBlocksRead++;
}
}
} catch (MalformedInputException e) {
throw new MalformedInputExceptionWithDetail(fEncodingMemento.getJavaCharsetName(), fBlocksRead * CodedIO.MAX_BUF_SIZE + numRead + e.getInputLength());
} catch (UnmappableCharacterException e) {
throw new MalformedInputExceptionWithDetail(fEncodingMemento.getJavaCharsetName(), fBlocksRead * CodedIO.MAX_BUF_SIZE + numRead + e.getInputLength());
}
return buffer;
}
use of org.eclipse.wst.sse.core.internal.exceptions.MalformedInputExceptionWithDetail in project webtools.sourceediting by eclipse.
the class AbstractDocumentLoader method reload.
public void reload(IEncodedDocument encodedDocument, Reader inputStreamReader) throws IOException {
if (inputStreamReader == null) {
// $NON-NLS-1$
throw new IllegalArgumentException("stream reader can not be null");
}
int READ_BUFFER_SIZE = 8192;
int MAX_BUFFERED_SIZE_FOR_RESET_MARK = 200000;
// temp .... eventually we'lll only read as needed
BufferedReader bufferedReader = new BufferedReader(inputStreamReader, MAX_BUFFERED_SIZE_FOR_RESET_MARK);
bufferedReader.mark(MAX_BUFFERED_SIZE_FOR_RESET_MARK);
StringBuffer buffer = new StringBuffer();
try {
int numRead = 0;
char[] tBuff = new char[READ_BUFFER_SIZE];
while ((numRead = bufferedReader.read(tBuff, 0, tBuff.length)) != -1) {
buffer.append(tBuff, 0, numRead);
}
// remember -- we didn't open stream ... so we don't close it
} catch (MalformedInputException e) {
// int pos = e.getInputLength();
EncodingMemento localEncodingMemento = getEncodingMemento();
boolean couldReset = true;
String encodingNameInError = localEncodingMemento.getJavaCharsetName();
if (encodingNameInError == null) {
encodingNameInError = localEncodingMemento.getDetectedCharsetName();
}
try {
bufferedReader.reset();
} catch (IOException resetException) {
// the only errro that can occur during reset is an
// IOException
// due to already being past the rest mark. In that case, we
// throw more generic message
couldReset = false;
}
// -1 can be used by UI layer as a code that "position could not
// be
// determined"
int charPostion = -1;
if (couldReset) {
charPostion = getCharPostionOfFailure(bufferedReader);
// getCharPostionOfFailure(new InputStreamReader(inStream,
// javaEncodingNameInError));
}
// is
throw new MalformedInputExceptionWithDetail(encodingNameInError, CodedIO.getAppropriateJavaCharset(encodingNameInError), charPostion, !couldReset, MAX_BUFFERED_SIZE_FOR_RESET_MARK);
}
StringBuffer stringbuffer = buffer;
encodedDocument.set(stringbuffer.toString());
}
use of org.eclipse.wst.sse.core.internal.exceptions.MalformedInputExceptionWithDetail in project webtools.sourceediting by eclipse.
the class FormatActionDelegate method format.
protected void format(IProgressMonitor monitor, IFile file) {
if (monitor == null || monitor.isCanceled())
return;
try {
monitor.beginTask("", 100);
IContentDescription contentDescription = file.getContentDescription();
monitor.worked(5);
if (contentDescription != null) {
IContentType contentType = contentDescription.getContentType();
IStructuredFormatProcessor formatProcessor = getFormatProcessor(contentType.getId());
if (formatProcessor != null && (monitor == null || !monitor.isCanceled())) {
String message = NLS.bind(SSEUIMessages.FormatActionDelegate_3, new String[] { file.getFullPath().toString().substring(1) });
monitor.subTask(message);
formatProcessor.setProgressMonitor(monitor);
formatProcessor.formatFile(file);
}
}
monitor.worked(95);
monitor.done();
} catch (MalformedInputExceptionWithDetail e) {
String message = NLS.bind(SSEUIMessages.FormatActionDelegate_5, new String[] { file.getFullPath().toString() });
fErrorStatus.add(new Status(IStatus.ERROR, SSEUIPlugin.ID, IStatus.ERROR, message, e));
} catch (IOException e) {
String message = NLS.bind(SSEUIMessages.FormatActionDelegate_4, new String[] { file.getFullPath().toString() });
fErrorStatus.add(new Status(IStatus.ERROR, SSEUIPlugin.ID, IStatus.ERROR, message, e));
} catch (CoreException e) {
String message = NLS.bind(SSEUIMessages.FormatActionDelegate_4, new String[] { file.getFullPath().toString() });
fErrorStatus.add(new Status(IStatus.ERROR, SSEUIPlugin.ID, IStatus.ERROR, message, e));
}
}
use of org.eclipse.wst.sse.core.internal.exceptions.MalformedInputExceptionWithDetail in project webtools.sourceediting by eclipse.
the class XMLMalformedInputTests method doTestMalformedInput.
/**
* Tests for a file, filename that should throw a
* MalformedInputExceptionWithDetail at character, expectedPosition. This
* happens when no encoding is specified, so the default is used, but
* there are characters that the default encoding does not recognize
*/
void doTestMalformedInput(String filename, IResourceCharsetDetector detector, int expectedPosition) throws IOException {
Exception foundException = null;
int badCharPosition = -1;
File file = TestsPlugin.getTestFile(filename);
if (!file.exists())
throw new IllegalArgumentException(filename + " was not found");
InputStream inputStream = new FileInputStream(file);
InputStream istream = getMarkSupportedStream(inputStream);
detector.set(istream);
// IEncodedDocument doc =
// detector.createNewStructuredDocument(filename, istream);
EncodingMemento encodingMemento = ((XMLResourceEncodingDetector) detector).getEncodingMemento();
String foundIANAEncoding = encodingMemento.getJavaCharsetName();
Charset charset = Charset.forName(foundIANAEncoding);
CharsetDecoder charsetDecoder = charset.newDecoder();
charsetDecoder.onMalformedInput(CodingErrorAction.REPORT);
charsetDecoder.onUnmappableCharacter(CodingErrorAction.REPORT);
istream.close();
inputStream.close();
// now, try reading as per encoding
inputStream = new FileInputStream(file);
// skip BOM for this case
// System.out.println(inputStream.read());
// System.out.println(inputStream.read());
// System.out.println(inputStream.read());
InputStreamReader reader = new InputStreamReader(inputStream, charsetDecoder);
try {
// just try reading ... should throw exception
// exception)
readInputStream(reader);
} catch (MalformedInputException e) {
// as expected, now do detailed checking.
inputStream.close();
istream.close();
inputStream = new FileInputStream(file);
charsetDecoder = charset.newDecoder();
charsetDecoder.onMalformedInput(CodingErrorAction.REPORT);
charsetDecoder.onUnmappableCharacter(CodingErrorAction.REPORT);
reader = new InputStreamReader(inputStream, charsetDecoder);
istream = getMarkSupportedStream(inputStream);
try {
handleMalFormedInput_DetailChecking(reader, foundIANAEncoding);
} catch (MalformedInputExceptionWithDetail se) {
foundException = se;
badCharPosition = se.getCharPosition();
}
} finally {
if (istream != null) {
istream.close();
}
if (inputStream != null) {
inputStream.close();
}
}
// handle adjustments here for VM differnces:
// for now its either 49 or 49 + 2 BOMs (51)
// can be smarting later.
assertTrue("MalformedInputException was not thrown as expected for filename: " + filename + " Exception thrown:" + foundException, foundException instanceof MalformedInputExceptionWithDetail);
assertTrue("Wrong character position detected in MalformedInputException. Expected: " + expectedPosition + " Found: " + badCharPosition, (badCharPosition == expectedPosition) || badCharPosition == expectedPosition - 2);
}
use of org.eclipse.wst.sse.core.internal.exceptions.MalformedInputExceptionWithDetail in project webtools.sourceediting by eclipse.
the class FormatHandler method format.
protected void format(IProgressMonitor monitor, IFile file) {
if (monitor == null || monitor.isCanceled())
return;
try {
monitor.beginTask("", 100);
IContentDescription contentDescription = file.getContentDescription();
monitor.worked(5);
if (contentDescription != null) {
IContentType contentType = contentDescription.getContentType();
IStructuredFormatProcessor formatProcessor = getFormatProcessor(contentType.getId());
if (formatProcessor != null && (monitor == null || !monitor.isCanceled())) {
String message = NLS.bind(SSEUIMessages.FormatActionDelegate_3, new String[] { file.getFullPath().toString().substring(1) });
monitor.subTask(message);
formatProcessor.setProgressMonitor(new SubProgressMonitor(monitor, 95));
formatProcessor.formatFile(file);
}
}
monitor.done();
} catch (MalformedInputExceptionWithDetail e) {
String message = NLS.bind(SSEUIMessages.FormatActionDelegate_5, new String[] { file.getFullPath().toString() });
fErrorStatus.add(new Status(IStatus.ERROR, SSEUIPlugin.ID, IStatus.ERROR, message, e));
} catch (IOException e) {
String message = NLS.bind(SSEUIMessages.FormatActionDelegate_4, new String[] { file.getFullPath().toString() });
fErrorStatus.add(new Status(IStatus.ERROR, SSEUIPlugin.ID, IStatus.ERROR, message, e));
} catch (CoreException e) {
String message = NLS.bind(SSEUIMessages.FormatActionDelegate_4, new String[] { file.getFullPath().toString() });
fErrorStatus.add(new Status(IStatus.ERROR, SSEUIPlugin.ID, IStatus.ERROR, message, e));
}
}
Aggregations