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