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());
}
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());
}
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());
}
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());
}
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);
}
Aggregations