Search in sources :

Example 11 with XMLResourceEncodingDetector

use of org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector in project webtools.sourceediting by eclipse.

the class XMLEncodingTests method testXMLNormalNonDefault.

public void testXMLNormalNonDefault() throws IOException {
    String filename = fileLocation + "NormalNonDefault.xml";
    String ianaInFile = "ISO-8859-1";
    doTestFileStream(filename, ianaInFile, new XMLResourceEncodingDetector());
}
Also used : XMLResourceEncodingDetector(org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector)

Example 12 with XMLResourceEncodingDetector

use of org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector in project webtools.sourceediting by eclipse.

the class XMLEncodingTests method testXMLIllformedNormalNonDefault.

public void testXMLIllformedNormalNonDefault() throws IOException {
    String filename = fileLocation + "IllformedNormalNonDefault.xml";
    String ianaInFile = "ISO-8859-1";
    doTestFileStream(filename, ianaInFile, new XMLResourceEncodingDetector());
}
Also used : XMLResourceEncodingDetector(org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector)

Example 13 with XMLResourceEncodingDetector

use of org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector in project webtools.sourceediting by eclipse.

the class XMLEncodingTests method testXMLNoEncoding.

public void testXMLNoEncoding() throws IOException {
    String filename = fileLocation + "NoEncoding.xml";
    doTestFileStream(filename, "UTF-8", new XMLResourceEncodingDetector());
}
Also used : XMLResourceEncodingDetector(org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector)

Example 14 with XMLResourceEncodingDetector

use of org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector in project webtools.sourceediting by eclipse.

the class XMLEncodingTests method testUTF83ByteBOM.

public void testUTF83ByteBOM() throws IOException {
    String filename = fileLocation + "UTF8With3ByteBOM.xml";
    doTestFileStream(filename, "UTF-8", new XMLResourceEncodingDetector());
}
Also used : XMLResourceEncodingDetector(org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector)

Example 15 with XMLResourceEncodingDetector

use of org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector 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

XMLResourceEncodingDetector (org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector)15 EncodingMemento (org.eclipse.wst.sse.core.internal.encoding.EncodingMemento)3 BufferedInputStream (java.io.BufferedInputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Charset (java.nio.charset.Charset)1 CharsetDecoder (java.nio.charset.CharsetDecoder)1 MalformedInputException (java.nio.charset.MalformedInputException)1 MalformedInputExceptionWithDetail (org.eclipse.wst.sse.core.internal.exceptions.MalformedInputExceptionWithDetail)1