Search in sources :

Example 16 with EncodingMemento

use of org.eclipse.wst.sse.core.internal.encoding.EncodingMemento in project webtools.sourceediting by eclipse.

the class TestStructuredDocument method testSetEncodingMemento.

public void testSetEncodingMemento() throws IOException, CoreException {
    IStructuredModel model = getTestModel();
    try {
        IStructuredDocument sDoc = model.getStructuredDocument();
        EncodingMemento memento = new EncodingMemento();
        sDoc.setEncodingMemento(memento);
        EncodingMemento gottenMemento = sDoc.getEncodingMemento();
        assertEquals("mementos don't match", memento, gottenMemento);
    } finally {
        if (model != null) {
            model.releaseFromEdit();
        }
    }
}
Also used : EncodingMemento(org.eclipse.wst.sse.core.internal.encoding.EncodingMemento) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

Example 17 with EncodingMemento

use of org.eclipse.wst.sse.core.internal.encoding.EncodingMemento in project webtools.sourceediting by eclipse.

the class AbstractResourceEncodingDetector method createEncodingMemento.

/**
 * Note: once this instance is created, trace info still needs to be
 * appended by caller, depending on the context its created.
 */
private void createEncodingMemento(String detectedCharsetName) {
    fEncodingMemento = new EncodingMemento();
    fEncodingMemento.setJavaCharsetName(getAppropriateJavaCharset(detectedCharsetName));
    fEncodingMemento.setDetectedCharsetName(detectedCharsetName);
    // TODO: if detectedCharset and spec default is
    // null, need to use "work
    // bench based" defaults.
    fEncodingMemento.setAppropriateDefault(getSpecDefaultEncoding());
}
Also used : EncodingMemento(org.eclipse.wst.sse.core.internal.encoding.EncodingMemento)

Example 18 with EncodingMemento

use of org.eclipse.wst.sse.core.internal.encoding.EncodingMemento in project webtools.sourceediting by eclipse.

the class AbstractResourceEncodingDetector method getSpecDefaultEncodingMemento.

public EncodingMemento getSpecDefaultEncodingMemento() {
    resetAll();
    EncodingMemento result = null;
    String enc = getSpecDefaultEncoding();
    if (enc != null) {
        createEncodingMemento(enc, EncodingMemento.DEFAULTS_ASSUMED_FOR_EMPTY_INPUT);
        fEncodingMemento.setAppropriateDefault(enc);
        result = fEncodingMemento;
    }
    return result;
}
Also used : EncodingMemento(org.eclipse.wst.sse.core.internal.encoding.EncodingMemento)

Example 19 with EncodingMemento

use of org.eclipse.wst.sse.core.internal.encoding.EncodingMemento in project webtools.sourceediting by eclipse.

the class XMLEncodingTests method doTestFileStream.

private void doTestFileStream(String filename, String expectedIANAEncoding, IResourceCharsetDetector detector) throws IOException {
    File file = TestsPlugin.getTestFile(filename);
    if (!file.exists())
        throw new IllegalArgumentException(filename + " was not found");
    InputStream inputStream = new FileInputStream(file);
    // InputStream inStream = getClass().getResourceAsStream(filename);
    InputStream istream = getMarkSupportedStream(inputStream);
    try {
        detector.set(istream);
        EncodingMemento encodingMemento = ((XMLResourceEncodingDetector) detector).getEncodingMemento();
        String foundIANAEncoding = encodingMemento.getJavaCharsetName();
        // I changed many "equals" to "equalsIgnoreCase" on 11/4/2002,
        // since
        // some issues with SHIFT_JIS vs. Shift_JIS were causing failures.
        // We do want to be tolerant on input, and accept either, but I
        // think
        // that SupportedJavaEncodings needs to be changed to "recommend"
        // Shift_JIS.
        boolean expectedIANAResult = false;
        expectedIANAResult = expectedIANAEncoding.equalsIgnoreCase(foundIANAEncoding);
        assertTrue("encoding test file " + filename + " expected: " + expectedIANAEncoding + " found: " + foundIANAEncoding, expectedIANAResult);
        // a very simple read test ... will cause JUnit error (not fail) if throws exception.
        ensureCanRead(filename, foundIANAEncoding, istream);
    } finally {
        if (istream != null) {
            istream.close();
        }
        if (inputStream != null) {
            inputStream.close();
        }
    }
}
Also used : XMLResourceEncodingDetector(org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) EncodingMemento(org.eclipse.wst.sse.core.internal.encoding.EncodingMemento) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 20 with EncodingMemento

use of org.eclipse.wst.sse.core.internal.encoding.EncodingMemento 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)

Aggregations

EncodingMemento (org.eclipse.wst.sse.core.internal.encoding.EncodingMemento)34 InputStream (java.io.InputStream)6 BufferedInputStream (java.io.BufferedInputStream)5 File (java.io.File)5 FileInputStream (java.io.FileInputStream)5 IOException (java.io.IOException)4 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)3 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)3 XMLResourceEncodingDetector (org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector)3 InputStreamReader (java.io.InputStreamReader)2 MalformedInputException (java.nio.charset.MalformedInputException)2 JSPResourceEncodingDetector (org.eclipse.jst.jsp.core.internal.contenttype.JSPResourceEncodingDetector)2 MalformedInputExceptionWithDetail (org.eclipse.wst.sse.core.internal.exceptions.MalformedInputExceptionWithDetail)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Reader (java.io.Reader)1 Charset (java.nio.charset.Charset)1 CharsetDecoder (java.nio.charset.CharsetDecoder)1 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)1