Search in sources :

Example 11 with EncodingMemento

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

the class JSPEncodingTests method doTestFileStream.

private void doTestFileStream(String filename, String expectedIANAEncoding, IResourceCharsetDetector detector) throws IOException {
    File file = JSPEncodingTestsPlugin.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 = ((JSPResourceEncodingDetector) 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 : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) EncodingMemento(org.eclipse.wst.sse.core.internal.encoding.EncodingMemento) JSPResourceEncodingDetector(org.eclipse.jst.jsp.core.internal.contenttype.JSPResourceEncodingDetector) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 12 with EncodingMemento

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

the class CSSEncodingTester method doTestFileStream.

private void doTestFileStream(String filename, String expectedIANAEncoding, IResourceCharsetDetector detector) throws IOException {
    File file = CSSEncodingTestsPlugin.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 = ((CSSResourceEncodingDetector) detector).getEncodingMemento();
        String foundIANAEncoding = null;
        if (encodingMemento != null) {
            foundIANAEncoding = encodingMemento.getDetectedCharsetName();
        }
        // 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;
        if (expectedIANAEncoding == null) {
            expectedIANAResult = expectedIANAEncoding == foundIANAEncoding;
        } else {
            expectedIANAResult = expectedIANAEncoding.equalsIgnoreCase(foundIANAEncoding);
        }
        assertTrue("encoding test file " + filename + " expected: " + expectedIANAEncoding + " found: " + foundIANAEncoding, expectedIANAResult);
        // throws exception.
        if (foundIANAEncoding != null) {
            ensureCanRead(filename, foundIANAEncoding, istream);
        }
    } finally {
        if (istream != null) {
            istream.close();
        }
        if (inputStream != null) {
            inputStream.close();
        }
    }
}
Also used : CSSResourceEncodingDetector(org.eclipse.wst.css.core.internal.contenttype.CSSResourceEncodingDetector) 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 13 with EncodingMemento

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

the class HTMLEncodingTests method doTestFileStream.

private void doTestFileStream(String filename, String expectedIANAEncoding, IResourceCharsetDetector detector) throws IOException {
    File file = HTMLEncodingTestsPlugin.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 = ((HTMLResourceEncodingDetector) detector).getEncodingMemento();
        String foundIANAEncoding = null;
        if (encodingMemento != null) {
            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;
        if (expectedIANAEncoding == null) {
            expectedIANAResult = (expectedIANAEncoding == foundIANAEncoding);
        } else {
            expectedIANAResult = expectedIANAEncoding.equalsIgnoreCase(foundIANAEncoding);
        }
        assertTrue("encoding test file " + filename + " expected: " + expectedIANAEncoding + " found: " + foundIANAEncoding, expectedIANAResult);
        // throws exception.
        if (expectedIANAEncoding != null) {
            ensureCanRead(filename, foundIANAEncoding, istream);
        }
    } finally {
        if (istream != null) {
            istream.close();
        }
        if (inputStream != null) {
            inputStream.close();
        }
    }
}
Also used : HTMLResourceEncodingDetector(org.eclipse.wst.html.core.internal.contenttype.HTMLResourceEncodingDetector) 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 14 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 15 with EncodingMemento

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

the class ContentDescriberForXML method handleCalculations.

/**
 * @param description
 * @param detector
 * @throws IOException
 */
private int handleCalculations(int result, IContentDescription description, IResourceCharsetDetector detector) throws IOException {
    int returnResult = result;
    EncodingMemento encodingMemento = ((XMLResourceEncodingDetector) detector).getEncodingMemento();
    if (description != null) {
        // TODO: I need to verify to see if this BOM work is always done
        // by text type.
        Object detectedByteOrderMark = encodingMemento.getUnicodeBOM();
        if (detectedByteOrderMark != null) {
            Object existingByteOrderMark = description.getProperty(IContentDescription.BYTE_ORDER_MARK);
            // need to "push" up into base.
            if (!detectedByteOrderMark.equals(existingByteOrderMark))
                description.setProperty(IContentDescription.BYTE_ORDER_MARK, detectedByteOrderMark);
        }
        if (!encodingMemento.isValid()) {
            // note: after setting here, its the mere presence of
            // IContentDescriptionExtended.UNSUPPORTED_CHARSET
            // in the resource's description that can be used to determine
            // if invalid in those cases, the "detected" property contains
            // an "appropriate default" to use.
            description.setProperty(IContentDescriptionExtended.UNSUPPORTED_CHARSET, encodingMemento.getInvalidEncoding());
            description.setProperty(IContentDescriptionExtended.APPROPRIATE_DEFAULT, encodingMemento.getAppropriateDefault());
        }
        Object detectedCharset = encodingMemento.getDetectedCharsetName();
        Object javaCharset = encodingMemento.getJavaCharsetName();
        // we always include detected, if its different than java
        handleDetectedSpecialCase(description, detectedCharset, javaCharset);
        if (javaCharset != null) {
            Object existingCharset = description.getProperty(IContentDescription.CHARSET);
            if (javaCharset.equals(existingCharset)) {
                handleDetectedSpecialCase(description, detectedCharset, javaCharset);
            } else {
                // we may need to add what we found, but only need to add
                // if different from the default
                Object defaultCharset = detector.getSpecDefaultEncoding();
                if (defaultCharset != null) {
                    if (!defaultCharset.equals(javaCharset)) {
                        description.setProperty(IContentDescription.CHARSET, javaCharset);
                    }
                } else {
                    // assuming if there is no spec default, we always
                    // need to add.
                    // TODO: this is probably a dead branch in current
                    // code, should re-examine for removal.
                    description.setProperty(IContentDescription.CHARSET, javaCharset);
                }
            }
        }
    }
    returnResult = determineValidity(detector, returnResult);
    return returnResult;
}
Also used : EncodingMemento(org.eclipse.wst.sse.core.internal.encoding.EncodingMemento)

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