Search in sources :

Example 26 with EncodingMemento

use of org.eclipse.wst.sse.core.internal.encoding.EncodingMemento in project webtools.sourceediting by eclipse.

the class ModelManagerImpl method saveStructuredDocument.

public void saveStructuredDocument(IStructuredDocument structuredDocument, IFile iFile, EncodingRule encodingRule) throws UnsupportedEncodingException, CoreException, IOException {
    // $NON-NLS-1$
    Assert.isNotNull(iFile, "file parameter can not be null");
    if (FileBufferModelManager.getInstance().isExistingBuffer(structuredDocument)) {
        ITextFileBuffer buffer = FileBufferModelManager.getInstance().getBuffer(structuredDocument);
        if (iFile.getFullPath().equals(buffer.getLocation()) || (iFile.getLocation() != null && iFile.getLocation().equals(buffer.getLocation())) || (iFile.getLocationURI() != null && buffer.getFileStore() != null && iFile.getLocationURI().equals(buffer.getFileStore().toURI()))) {
            buffer.commit(new NullProgressMonitor(), true);
        }
    } else {
        // IModelHandler handler = calculateType(iFile);
        // IDocumentDumper dumper = handler.getDocumentDumper();
        CodedStreamCreator codedStreamCreator = new CodedStreamCreator();
        Reader reader = new DocumentReader(structuredDocument);
        codedStreamCreator.set(iFile, reader);
        codedStreamCreator.setPreviousEncodingMemento(structuredDocument.getEncodingMemento());
        EncodingMemento encodingMemento = codedStreamCreator.getCurrentEncodingMemento();
        // be sure document's is updated, in case exception is thrown in
        // getCodedByteArrayOutputStream
        structuredDocument.setEncodingMemento(encodingMemento);
        // Convert line delimiters after encoding memento is figured out,
        // but
        // before writing to output stream.
        handleConvertLineDelimiters(structuredDocument, iFile, encodingRule, encodingMemento);
        ByteArrayOutputStream codedByteStream = codedStreamCreator.getCodedByteArrayOutputStream(encodingRule);
        InputStream codedStream = new ByteArrayInputStream(codedByteStream.toByteArray());
        if (iFile.exists())
            iFile.setContents(codedStream, true, true, null);
        else
            iFile.create(codedStream, false, null);
        codedByteStream.close();
        codedStream.close();
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) CodedStreamCreator(org.eclipse.wst.sse.core.internal.encoding.CodedStreamCreator) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) EncodingMemento(org.eclipse.wst.sse.core.internal.encoding.EncodingMemento) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) Reader(java.io.Reader) DocumentReader(org.eclipse.wst.sse.core.internal.document.DocumentReader) InputStreamReader(java.io.InputStreamReader) DocumentReader(org.eclipse.wst.sse.core.internal.document.DocumentReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 27 with EncodingMemento

use of org.eclipse.wst.sse.core.internal.encoding.EncodingMemento in project webtools.sourceediting by eclipse.

the class ContentDescriberForJSON method handleCalculations.

/**
 * @param description
 * @param detector
 * @throws IOException
 */
private void handleCalculations(IContentDescription description, IResourceCharsetDetector detector) throws IOException {
    // note: if we're asked for one, we set them all. I need to be sure if
    // called
    // mulitiple times (one for each, say) that we don't waste time
    // processing same
    // content again.
    EncodingMemento encodingMemento = ((JSONResourceEncodingDetector) detector).getEncodingMemento();
    // TODO: I need to verify to see if this BOM work is always done
    // by text type.
    Object detectedByteOrderMark = encodingMemento.getUnicodeBOM();
    if (detectedByteOrderMark != null) {
        Object existingByteOrderMark = description.getProperty(IContentDescription.BYTE_ORDER_MARK);
        // need to "push" up into base.
        if (!detectedByteOrderMark.equals(existingByteOrderMark))
            description.setProperty(IContentDescription.BYTE_ORDER_MARK, detectedByteOrderMark);
    }
    if (!encodingMemento.isValid()) {
        // note: after setting here, its the mere presence of
        // IContentDescriptionExtended.UNSUPPORTED_CHARSET
        // in the resource's description that can be used to determine if
        // invalid
        // in those cases, the "detected" property contains an
        // "appropriate default" to use.
        description.setProperty(IContentDescriptionExtended.UNSUPPORTED_CHARSET, encodingMemento.getInvalidEncoding());
        description.setProperty(IContentDescriptionExtended.APPROPRIATE_DEFAULT, encodingMemento.getAppropriateDefault());
    }
    Object detectedCharset = encodingMemento.getDetectedCharsetName();
    Object javaCharset = encodingMemento.getJavaCharsetName();
    // we always include detected, if its different than java
    handleDetectedSpecialCase(description, detectedCharset, javaCharset);
    if (javaCharset != null) {
        Object existingCharset = description.getProperty(IContentDescription.CHARSET);
        if (javaCharset.equals(existingCharset)) {
            handleDetectedSpecialCase(description, detectedCharset, javaCharset);
        } else {
            // we may need to add what we found, but only need to add
            // if different from default.the
            Object defaultCharset = getDetector().getSpecDefaultEncoding();
            if (defaultCharset != null) {
                if (!defaultCharset.equals(javaCharset)) {
                    description.setProperty(IContentDescription.CHARSET, javaCharset);
                }
            } else {
                // assuming if there is no spec default, we always need to
                // add, I'm assuming
                description.setProperty(IContentDescription.CHARSET, javaCharset);
            }
        }
    }
}
Also used : EncodingMemento(org.eclipse.wst.sse.core.internal.encoding.EncodingMemento)

Example 28 with EncodingMemento

use of org.eclipse.wst.sse.core.internal.encoding.EncodingMemento in project webtools.sourceediting by eclipse.

the class JSPResourceEncodingDetector method handleSpecDefault.

private void handleSpecDefault() {
    String encodingName;
    encodingName = getSpecDefaultEncoding();
    if (encodingName != null) {
        // createEncodingMemento(encodingName,
        // EncodingMemento.USED_CONTENT_TYPE_DEFAULT);
        fEncodingMemento = new EncodingMemento();
        fEncodingMemento.setJavaCharsetName(encodingName);
        fEncodingMemento.setAppropriateDefault(encodingName);
    }
}
Also used : EncodingMemento(org.eclipse.wst.sse.core.internal.encoding.EncodingMemento)

Example 29 with EncodingMemento

use of org.eclipse.wst.sse.core.internal.encoding.EncodingMemento in project webtools.sourceediting by eclipse.

the class JSPResourceEncodingDetector 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());
}
Also used : EncodingMemento(org.eclipse.wst.sse.core.internal.encoding.EncodingMemento)

Example 30 with EncodingMemento

use of org.eclipse.wst.sse.core.internal.encoding.EncodingMemento in project webtools.sourceediting by eclipse.

the class CSSResourceEncodingDetector 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;
}
Also used : EncodingMemento(org.eclipse.wst.sse.core.internal.encoding.EncodingMemento)

Aggregations

EncodingMemento (org.eclipse.wst.sse.core.internal.encoding.EncodingMemento)34 InputStream (java.io.InputStream)6 BufferedInputStream (java.io.BufferedInputStream)5 File (java.io.File)5 FileInputStream (java.io.FileInputStream)5 IOException (java.io.IOException)4 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)3 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)3 XMLResourceEncodingDetector (org.eclipse.wst.xml.core.internal.contenttype.XMLResourceEncodingDetector)3 InputStreamReader (java.io.InputStreamReader)2 MalformedInputException (java.nio.charset.MalformedInputException)2 JSPResourceEncodingDetector (org.eclipse.jst.jsp.core.internal.contenttype.JSPResourceEncodingDetector)2 MalformedInputExceptionWithDetail (org.eclipse.wst.sse.core.internal.exceptions.MalformedInputExceptionWithDetail)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Reader (java.io.Reader)1 Charset (java.nio.charset.Charset)1 CharsetDecoder (java.nio.charset.CharsetDecoder)1 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)1