use of org.eclipse.wst.sse.core.internal.exceptions.UnsupportedCharsetExceptionWithDetail in project webtools.sourceediting by eclipse.
the class CodedReaderCreator method checkForEncodingInContents.
private EncodingMemento checkForEncodingInContents(InputStream limitedStream) throws CoreException, IOException {
EncodingMemento result = null;
// have been set, and no need to get again.
if (fEncodingMemento != null) {
result = fEncodingMemento;
} else {
if (fClientSuppliedStream) {
try {
limitedStream.reset();
IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
IContentDescription contentDescription = contentTypeManager.getDescriptionFor(limitedStream, fFilename, IContentDescription.ALL);
if (contentDescription != null) {
fEncodingMemento = createMemento(contentDescription);
}
result = fEncodingMemento;
} finally {
limitedStream.reset();
}
} else {
// throw new IllegalStateException("unexpected state:
// encodingMemento was null but no input stream supplied by
// client"); //$NON-NLS-1$
result = null;
}
}
if (result != null && !result.isValid() && !forceDefault()) {
throw new UnsupportedCharsetExceptionWithDetail(result);
}
return result;
}
use of org.eclipse.wst.sse.core.internal.exceptions.UnsupportedCharsetExceptionWithDetail in project webtools.sourceediting by eclipse.
the class CodedStreamCreator method dump.
private void dump(OutputStream outputStream, EncodingRule encodingRule, boolean use3ByteBOMifUTF8) throws CoreException, IOException {
getCurrentEncodingMemento();
String javaEncodingName = null;
if (encodingRule == EncodingRule.CONTENT_BASED) {
if (fCurrentEncodingMemento.isValid()) {
javaEncodingName = fCurrentEncodingMemento.getJavaCharsetName();
} else {
throw new UnsupportedCharsetExceptionWithDetail(fCurrentEncodingMemento);
}
} else if (encodingRule == EncodingRule.IGNORE_CONVERSION_ERROR)
javaEncodingName = fCurrentEncodingMemento.getJavaCharsetName();
else if (encodingRule == EncodingRule.FORCE_DEFAULT)
javaEncodingName = fCurrentEncodingMemento.getAppropriateDefault();
// to skip whole check if that's the case.
if (javaEncodingName != null) {
if ((javaEncodingName.equals(UTF_8_CHARSET_NAME) && use3ByteBOMifUTF8) || (javaEncodingName.equals(UTF_8_CHARSET_NAME) && fCurrentEncodingMemento.isUTF83ByteBOMUsed())) {
outputStream.write(UTF3BYTEBOM);
} else if (javaEncodingName.equals(UTF_16LE_CHARSET_NAME)) {
outputStream.write(UTF16LEBOM);
} else if (javaEncodingName.equals(UTF_16BE_CHARSET_NAME)) {
outputStream.write(UTF16BEBOM);
}
}
// TODO add back in line delimiter handling the
// "right" way (updating
// markers, not requiring string, etc. .. may need
// to move to document
// level)
// allTextBuffer =
// handleLineDelimiter(allTextBuffer, document);
Reader reader = getResettableReader();
// be sure to test large "readers" ... we'll need
// to make sure they all
// can reset to initial position (StringReader,
// CharArrayReader, and
// DocumentReader should all work ok).
reader.reset();
// There must be cleaner logic somehow, but the
// idea is that
// javaEncodingName can be null
// if original detected encoding is not valid (and
// if FORCE_DEFAULT was
// not specified). Hence, we WANT the first
// Charset.forName to
// throw appropriate exception.
Charset charset = null;
// this call checks "override" properties file
javaEncodingName = CodedIO.getAppropriateJavaCharset(javaEncodingName);
if (javaEncodingName == null) {
charset = Charset.forName(fCurrentEncodingMemento.getDetectedCharsetName());
} else {
charset = Charset.forName(javaEncodingName);
}
CharsetEncoder charsetEncoder = charset.newEncoder();
if (!(encodingRule == EncodingRule.IGNORE_CONVERSION_ERROR)) {
charsetEncoder.onMalformedInput(CodingErrorAction.REPORT);
charsetEncoder.onUnmappableCharacter(CodingErrorAction.REPORT);
} else {
charsetEncoder.onMalformedInput(CodingErrorAction.REPLACE);
charsetEncoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
}
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, charsetEncoder);
// TODO: this may no longer be needed (and is at
// least wrong spot for
// it).
// if (checkConversion && (!(encodingRule ==
// EncodingRule.IGNORE_CONVERSION_ERROR))) {
// checkConversion(fCurrentEncodingMemento,
// encodingRule);
// }
char[] charbuf = new char[CodedIO.MAX_BUF_SIZE];
int nRead = 0;
try {
while (nRead != -1) {
nRead = reader.read(charbuf, 0, MAX_BUF_SIZE);
if (nRead > 0) {
outputStreamWriter.flush();
outputStreamWriter.write(charbuf, 0, nRead);
}
}
} catch (UnmappableCharacterException e) {
checkConversion(fCurrentEncodingMemento, encodingRule);
} finally {
// since we don't own the original output stream, we
// won't close it ours.
// the caller who passed it to us must close original one
// when appropriate.
// (but we do flush to be sure all up-to-date)
outputStreamWriter.flush();
}
}
use of org.eclipse.wst.sse.core.internal.exceptions.UnsupportedCharsetExceptionWithDetail in project webtools.sourceediting by eclipse.
the class JSPSearchDocument method getJSPTranslation.
/**
* It's not recommended for clients to hold on to this JSPTranslation
* since it's kind of large. If possible, hold on to the
* JSPSearchDocument, which is more of a lightweight proxy.
*
* @return the JSPTranslation for the jsp file, or null if it's an
* unsupported file.
*/
public final JSPTranslationExtension getJSPTranslation() {
JSPTranslationExtension translation = null;
IFile jspFile = getFile();
if (!JSPSearchSupport.isJsp(jspFile))
return translation;
IStructuredModel model = null;
try {
// get existing model for read, then get document from it
IModelManager modelManager = getModelManager();
if (modelManager != null) {
jspFile.refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor());
model = modelManager.getModelForRead(jspFile);
}
// handle unsupported
if (model instanceof IDOMModel) {
IDOMModel xmlModel = (IDOMModel) model;
setupAdapterFactory(xmlModel);
IDOMDocument doc = xmlModel.getDocument();
JSPTranslationAdapter adapter = (JSPTranslationAdapter) doc.getAdapterFor(IJSPTranslation.class);
translation = adapter.getJSPTranslation();
}
} catch (IOException e) {
Logger.logException(e);
} catch (CoreException e) {
Logger.logException(e);
} catch (UnsupportedCharsetExceptionWithDetail e) {
// no need to log this. Just consider it an invalid file for our
// purposes.
// Logger.logException(e);
} finally {
if (model != null)
model.releaseFromRead();
}
return translation;
}
use of org.eclipse.wst.sse.core.internal.exceptions.UnsupportedCharsetExceptionWithDetail in project webtools.sourceediting by eclipse.
the class JsSearchDocument method getJSTranslation.
/**
* It's not recommended for clients to hold on to this JSPTranslation
* since it's kind of large. If possible, hold on to the
* JSPSearchDocument, which is more of a lightweight proxy.
*
* @return the JSPTranslation for the jsp file, or null if it's an
* unsupported file.
*/
public final IJsTranslation getJSTranslation() {
IJsTranslation translation = null;
IFile jspFile = getFile();
if (!JsSearchSupport.isJsp(jspFile)) {
return translation;
}
IStructuredModel model = null;
try {
// get existing model for read, then get document from it
IModelManager modelManager = getModelManager();
if (modelManager != null) {
model = modelManager.getModelForRead(jspFile);
}
// handle unsupported
if (model instanceof IDOMModel) {
IDOMModel xmlModel = (IDOMModel) model;
JsTranslationAdapterFactory.setupAdapterFactory(xmlModel);
IDOMDocument doc = xmlModel.getDocument();
JsTranslationAdapter adapter = (JsTranslationAdapter) doc.getAdapterFor(IJsTranslation.class);
translation = adapter.getJsTranslation(false);
}
} catch (IOException e) {
Logger.logException(e);
} catch (CoreException e) {
Logger.logException(e);
} catch (UnsupportedCharsetExceptionWithDetail e) {
// no need to log this. Just consider it an invalid file for our
// purposes.
// Logger.logException(e);
} finally {
if (model != null) {
model.releaseFromRead();
}
}
return translation;
}
use of org.eclipse.wst.sse.core.internal.exceptions.UnsupportedCharsetExceptionWithDetail in project webtools.sourceediting by eclipse.
the class CodedReaderCreator method getCodedReader.
public Reader getCodedReader() throws CoreException, IOException {
Reader result = null;
// we make a local copy of encoding memento so
// stream won't
// be accessed simultaneously.
EncodingMemento encodingMemento = getEncodingMemento();
// $NON-NLS-1$
Assert.isNotNull(encodingMemento, "Appears reader requested before file or stream set");
InputStream streamToReturn = getResettableStream();
streamToReturn.reset();
// see ... TODO look up bug number
if (encodingMemento.isUnicodeStream()) {
streamToReturn.skip(2);
} else if (encodingMemento.isUTF83ByteBOMUsed()) {
streamToReturn.skip(3);
}
String charsetName = encodingMemento.getJavaCharsetName();
if (charsetName == null) {
charsetName = encodingMemento.getDetectedCharsetName();
}
if (!encodingMemento.isValid() && !forceDefault()) {
throw new UnsupportedCharsetExceptionWithDetail(encodingMemento);
}
if (fEncodingRule == EncodingRule.FORCE_DEFAULT) {
charsetName = encodingMemento.getAppropriateDefault();
}
// [228366] For files that have a unicode BOM, and a charset name of UTF-16, the charset decoder needs "UTF-16LE"
if (CHARSET_UTF_16.equals(charsetName) && encodingMemento.getUnicodeBOM() == IContentDescription.BOM_UTF_16LE)
charsetName = CHARSET_UTF_16LE;
Charset charset = Charset.forName(charsetName);
CharsetDecoder charsetDecoder = charset.newDecoder();
if (fEncodingRule == EncodingRule.IGNORE_CONVERSION_ERROR) {
charsetDecoder.onMalformedInput(CodingErrorAction.REPLACE);
charsetDecoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
} else {
charsetDecoder.onMalformedInput(CodingErrorAction.REPORT);
charsetDecoder.onUnmappableCharacter(CodingErrorAction.REPORT);
}
// more efficient to be buffered, and I know of no
// reason not to return
// that directly.
result = new BufferedReader(new InputStreamReader(streamToReturn, charsetDecoder), CodedIO.MAX_BUF_SIZE);
result.mark(CodedIO.MAX_BUF_SIZE);
return result;
}
Aggregations