Search in sources :

Example 1 with UnsupportedCharsetExceptionWithDetail

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;
}
Also used : IContentTypeManager(org.eclipse.core.runtime.content.IContentTypeManager) UnsupportedCharsetExceptionWithDetail(org.eclipse.wst.sse.core.internal.exceptions.UnsupportedCharsetExceptionWithDetail) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 2 with UnsupportedCharsetExceptionWithDetail

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();
    }
}
Also used : UnmappableCharacterException(java.nio.charset.UnmappableCharacterException) CharArrayReader(java.io.CharArrayReader) Reader(java.io.Reader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) Charset(java.nio.charset.Charset) UnsupportedCharsetExceptionWithDetail(org.eclipse.wst.sse.core.internal.exceptions.UnsupportedCharsetExceptionWithDetail) OutputStreamWriter(java.io.OutputStreamWriter) CharsetEncoder(java.nio.charset.CharsetEncoder)

Example 3 with UnsupportedCharsetExceptionWithDetail

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;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) JSPTranslationExtension(org.eclipse.jst.jsp.core.internal.java.JSPTranslationExtension) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) UnsupportedCharsetExceptionWithDetail(org.eclipse.wst.sse.core.internal.exceptions.UnsupportedCharsetExceptionWithDetail) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IOException(java.io.IOException) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)

Example 4 with UnsupportedCharsetExceptionWithDetail

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;
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) UnsupportedCharsetExceptionWithDetail(org.eclipse.wst.sse.core.internal.exceptions.UnsupportedCharsetExceptionWithDetail) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter) IOException(java.io.IOException)

Example 5 with UnsupportedCharsetExceptionWithDetail

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;
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) NullInputStream(org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Charset(java.nio.charset.Charset) UnsupportedCharsetExceptionWithDetail(org.eclipse.wst.sse.core.internal.exceptions.UnsupportedCharsetExceptionWithDetail)

Aggregations

UnsupportedCharsetExceptionWithDetail (org.eclipse.wst.sse.core.internal.exceptions.UnsupportedCharsetExceptionWithDetail)6 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 Reader (java.io.Reader)2 Charset (java.nio.charset.Charset)2 IFile (org.eclipse.core.resources.IFile)2 CoreException (org.eclipse.core.runtime.CoreException)2 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)2 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)2 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)2 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)2 BufferedInputStream (java.io.BufferedInputStream)1 CharArrayReader (java.io.CharArrayReader)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringReader (java.io.StringReader)1