Search in sources :

Example 6 with CodedStreamCreator

use of org.eclipse.wst.sse.core.internal.encoding.CodedStreamCreator 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)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 CodedStreamCreator (org.eclipse.wst.sse.core.internal.encoding.CodedStreamCreator)6 DocumentReader (org.eclipse.wst.sse.core.internal.document.DocumentReader)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 EncodingMemento (org.eclipse.wst.sse.core.internal.encoding.EncodingMemento)1