Search in sources :

Example 1 with MalformedInputExceptionWithDetail

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;
}
Also used : UnmappableCharacterException(java.nio.charset.UnmappableCharacterException) MalformedInputException(java.nio.charset.MalformedInputException) MalformedInputExceptionWithDetail(org.eclipse.wst.sse.core.internal.exceptions.MalformedInputExceptionWithDetail)

Example 2 with MalformedInputExceptionWithDetail

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());
}
Also used : EncodingMemento(org.eclipse.wst.sse.core.internal.encoding.EncodingMemento) BufferedReader(java.io.BufferedReader) MalformedInputException(java.nio.charset.MalformedInputException) MalformedInputExceptionWithDetail(org.eclipse.wst.sse.core.internal.exceptions.MalformedInputExceptionWithDetail) IOException(java.io.IOException)

Example 3 with MalformedInputExceptionWithDetail

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));
    }
}
Also used : IStructuredFormatProcessor(org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor) MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) CoreException(org.eclipse.core.runtime.CoreException) MalformedInputExceptionWithDetail(org.eclipse.wst.sse.core.internal.exceptions.MalformedInputExceptionWithDetail) IContentType(org.eclipse.core.runtime.content.IContentType) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 4 with MalformedInputExceptionWithDetail

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);
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) XMLResourceEncodingDetector(org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector) InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) EncodingMemento(org.eclipse.wst.sse.core.internal.encoding.EncodingMemento) MalformedInputExceptionWithDetail(org.eclipse.wst.sse.core.internal.exceptions.MalformedInputExceptionWithDetail) Charset(java.nio.charset.Charset) MalformedInputException(java.nio.charset.MalformedInputException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) MalformedInputException(java.nio.charset.MalformedInputException) File(java.io.File)

Example 5 with MalformedInputExceptionWithDetail

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));
    }
}
Also used : IStructuredFormatProcessor(org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor) MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) CoreException(org.eclipse.core.runtime.CoreException) MalformedInputExceptionWithDetail(org.eclipse.wst.sse.core.internal.exceptions.MalformedInputExceptionWithDetail) IContentType(org.eclipse.core.runtime.content.IContentType) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Aggregations

MalformedInputExceptionWithDetail (org.eclipse.wst.sse.core.internal.exceptions.MalformedInputExceptionWithDetail)5 IOException (java.io.IOException)4 MalformedInputException (java.nio.charset.MalformedInputException)3 CoreException (org.eclipse.core.runtime.CoreException)2 IStatus (org.eclipse.core.runtime.IStatus)2 MultiStatus (org.eclipse.core.runtime.MultiStatus)2 Status (org.eclipse.core.runtime.Status)2 IContentDescription (org.eclipse.core.runtime.content.IContentDescription)2 IContentType (org.eclipse.core.runtime.content.IContentType)2 EncodingMemento (org.eclipse.wst.sse.core.internal.encoding.EncodingMemento)2 IStructuredFormatProcessor (org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Charset (java.nio.charset.Charset)1 CharsetDecoder (java.nio.charset.CharsetDecoder)1 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)1