Search in sources :

Example 1 with CodedReaderCreator

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

the class AbstractDocumentLoader method createNewStructuredDocument.

public IEncodedDocument createNewStructuredDocument(String filename, InputStream inputStream, EncodingRule encodingRule) throws UnsupportedEncodingException, IOException {
    if (filename == null && inputStream == null) {
        // $NON-NLS-1$
        throw new IllegalArgumentException("can not have both null filename and inputstream");
    }
    IEncodedDocument structuredDocument = createNewStructuredDocument();
    CodedReaderCreator codedReaderCreator = getCodedReaderCreator();
    try {
        codedReaderCreator.set(filename, inputStream);
        codedReaderCreator.setEncodingRule(encodingRule);
        fEncodingMemento = codedReaderCreator.getEncodingMemento();
        fFullPreparedReader = codedReaderCreator.getCodedReader();
        structuredDocument.setEncodingMemento(fEncodingMemento);
        setDocumentContentsFromReader(structuredDocument, fFullPreparedReader);
    } catch (CoreException e) {
        // impossible in this context
        throw new Error(e);
    } finally {
        if (fFullPreparedReader != null) {
            fFullPreparedReader.close();
        }
    }
    return structuredDocument;
}
Also used : CodedReaderCreator(org.eclipse.wst.sse.core.internal.encoding.CodedReaderCreator) CoreException(org.eclipse.core.runtime.CoreException) IEncodedDocument(org.eclipse.wst.sse.core.internal.provisional.document.IEncodedDocument)

Example 2 with CodedReaderCreator

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

the class JSPDocumentLoader method createNewStructuredDocument.

public IEncodedDocument createNewStructuredDocument(String filename, InputStream inputStream) throws UnsupportedEncodingException, IOException {
    if (filename == null && inputStream == null) {
        // $NON-NLS-1$
        throw new IllegalArgumentException("can not have both null filename and inputstream");
    }
    IEncodedDocument structuredDocument = createNewStructuredDocument();
    CodedReaderCreator codedReaderCreator = new CodedReaderCreator();
    try {
        codedReaderCreator.set(filename, inputStream);
        fFullPreparedReader = codedReaderCreator.getCodedReader();
        fEncodingMemento = codedReaderCreator.getEncodingMemento();
        structuredDocument.setEncodingMemento(fEncodingMemento);
        // the fact that file is null means this method/code path is no
        // good for JSP fragments
        EmbeddedTypeHandler embeddedType = getEmbeddedType((IFile) null);
        fFullPreparedReader.reset();
        if (embeddedType != null)
            embeddedType.initializeParser(((IStructuredDocument) structuredDocument).getParser());
        setDocumentContentsFromReader(structuredDocument, fFullPreparedReader);
    } catch (CoreException e) {
        // impossible in this context
        throw new Error(e);
    } finally {
        if (fFullPreparedReader != null) {
            fFullPreparedReader.close();
        }
    }
    return structuredDocument;
}
Also used : EmbeddedTypeHandler(org.eclipse.wst.sse.core.internal.ltk.modelhandler.EmbeddedTypeHandler) CodedReaderCreator(org.eclipse.wst.sse.core.internal.encoding.CodedReaderCreator) CoreException(org.eclipse.core.runtime.CoreException) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IEncodedDocument(org.eclipse.wst.sse.core.internal.provisional.document.IEncodedDocument)

Example 3 with CodedReaderCreator

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

the class StorageModelProvider method createDocument.

protected IDocument createDocument(Object element) {
    if (debugOperations) {
        if (element instanceof IStorageEditorInput)
            try {
                // $NON-NLS-1$
                System.out.println("StorageModelProvider: createDocument for " + ((IStorageEditorInput) element).getStorage().getFullPath());
            } catch (CoreException e) {
                // $NON-NLS-1$ //$NON-NLS-2$
                System.out.println("StorageModelProvider: createDocument for " + element + "(exception caught)");
            }
        else {
            // $NON-NLS-1$
            System.out.println("StorageModelProvider: createDocument for " + element);
        }
    }
    // The following is largely copied from FileModelProvider
    IDocument document = null;
    if (element instanceof IEditorInput) {
        // create a new IDocument for the element; should always reflect
        // the contents of the resource
        ModelInfo info = getModelInfoFor((IEditorInput) element);
        if (info == null) {
            // $NON-NLS-1$
            throw new IllegalArgumentException("no corresponding model info found");
        }
        IStructuredModel model = info.fStructuredModel;
        if (model != null) {
            if (!fReuseModelDocument && element instanceof IStorageEditorInput) {
                Reader reader = null;
                IStructuredDocument innerdocument = null;
                try {
                    // update document from input's contents
                    CodedReaderCreator codedReaderCreator = new CodedReaderCreator(calculateID((IStorageEditorInput) element), Utilities.getMarkSupportedStream(((IStorageEditorInput) element).getStorage().getContents()));
                    reader = codedReaderCreator.getCodedReader();
                    innerdocument = model.getStructuredDocument();
                    int originalLengthToReplace = innerdocument.getLength();
                    /*
						 * TODO_future: we could implement with sequential
						 * rewrite, if we don't pickup automatically from
						 * FileBuffer support, so not so much has to be pulled
						 * into memory (as an extra big string), but we need
						 * to carry that API through so that StructuredModel
						 * is not notified until done.
						 */
                    // innerdocument.startSequentialRewrite(true);
                    // innerdocument.replaceText(this, 0,
                    // innerdocument.getLength(), "");
                    StringBuffer stringBuffer = new StringBuffer();
                    int bufferSize = 2048;
                    char[] buffer = new char[bufferSize];
                    int nRead = 0;
                    boolean eof = false;
                    while (!eof) {
                        nRead = reader.read(buffer, 0, bufferSize);
                        if (nRead == -1) {
                            eof = true;
                        } else {
                            stringBuffer.append(buffer, 0, nRead);
                        // innerdocument.replaceText(this,
                        // innerdocument.getLength(), 0, new
                        // String(buffer, 0, nRead));
                        }
                    }
                    // ignore read-only settings if reverting whole
                    // document
                    innerdocument.replaceText(this, 0, originalLengthToReplace, stringBuffer.toString(), true);
                    model.setDirtyState(false);
                } catch (CoreException e) {
                    Logger.logException(e);
                } catch (IOException e) {
                    Logger.logException(e);
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException e1) {
                            // would be highly unusual
                            Logger.logException(e1);
                        }
                    }
                // if (innerdocument != null) {
                // innerdocument.stopSequentialRewrite();
                // }
                }
            }
            document = model.getStructuredDocument();
        }
    }
    return document;
}
Also used : IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) CodedReaderCreator(org.eclipse.wst.sse.core.internal.encoding.CodedReaderCreator) Reader(java.io.Reader) IOException(java.io.IOException) CoreException(org.eclipse.core.runtime.CoreException) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IDocument(org.eclipse.jface.text.IDocument) IEditorInput(org.eclipse.ui.IEditorInput)

Example 4 with CodedReaderCreator

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

the class TestCodedReader method doCoreTest.

protected Reader doCoreTest(String expectedJavaCharset, String expectedDetectedCharset, IFile file) throws CoreException, IOException {
    Reader reader;
    // create these first, to test exception being thrown correctly
    CodedReaderCreator codedReaderCreator = new CodedReaderCreator();
    codedReaderCreator.set(file);
    reader = codedReaderCreator.getCodedReader();
    String javaCharsetName = file.getCharset();
    IContentDescription description = file.getContentDescription();
    javaCharsetName = massageCharset(javaCharsetName);
    // codedReaderCreator.getEncodingMemento().getJavaCharsetName();
    if (expectedJavaCharset.equals("expectPlatformCharset")) {
        String platformDefault = NonContentBasedEncodingRules.useDefaultNameRules(null);
        assertTrue(javaCharsetName.equals(platformDefault));
    } else {
        boolean asExpected = javaCharsetName.equals(expectedJavaCharset);
        assertTrue(javaCharsetName + " did not equal the expected " + expectedJavaCharset + " (this is a VM dependent test)", asExpected);
    }
    String javaCharsetNameProperty = (String) description.getProperty(IContentDescription.CHARSET);
    String detectedCharsetNameProperty = getDetectedCharsetName(description);
    detectedCharsetNameProperty = massageCharset(detectedCharsetNameProperty);
    // }
    if (!expectedJavaCharset.equals("expectPlatformCharset")) {
        boolean expecedResult = expectedJavaCharset.equals(javaCharsetNameProperty);
        assertTrue("java based charset name was not as expected", expecedResult);
    } else {
        String expectedDefault = NonContentBasedEncodingRules.useDefaultNameRules(null);
        boolean ExpectedResult = expectedDefault.equals(javaCharsetNameProperty);
        assertTrue("java based charset name not as expected when platform default expected", ExpectedResult);
    }
    if (expectedDetectedCharset != null) {
        boolean expectedResult = expectedDetectedCharset.equals(detectedCharsetNameProperty);
        assertTrue("detected charset name was not as expected", expectedResult);
    }
    // test if can read/write file
    StringBuffer buffer = readInputStream(reader);
    if (DEBUG_TEST_DETAIL) {
        System.out.print(buffer);
    }
    return reader;
}
Also used : CodedReaderCreator(org.eclipse.wst.sse.core.internal.encoding.CodedReaderCreator) Reader(java.io.Reader) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 5 with CodedReaderCreator

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

the class AbstractDocumentLoader method createNewStructuredDocument.

/**
 * This abstract version should handle most cases, but won't if
 * contentType is sensitive to encoding, and/or embedded types
 */
public IEncodedDocument createNewStructuredDocument(IFile iFile) throws IOException, CoreException {
    IEncodedDocument structuredDocument = createNewStructuredDocument();
    String lineDelimiter = getPreferredNewLineDelimiter(iFile);
    if (lineDelimiter != null)
        structuredDocument.setPreferredLineDelimiter(lineDelimiter);
    try {
        CodedReaderCreator creator = getCodedReaderCreator();
        creator.set(iFile);
        fEncodingMemento = creator.getEncodingMemento();
        structuredDocument.setEncodingMemento(fEncodingMemento);
        fFullPreparedReader = getCodedReaderCreator().getCodedReader();
        setDocumentContentsFromReader(structuredDocument, fFullPreparedReader);
    } finally {
        if (fFullPreparedReader != null) {
            fFullPreparedReader.close();
        }
    }
    return structuredDocument;
}
Also used : CodedReaderCreator(org.eclipse.wst.sse.core.internal.encoding.CodedReaderCreator) IEncodedDocument(org.eclipse.wst.sse.core.internal.provisional.document.IEncodedDocument)

Aggregations

CodedReaderCreator (org.eclipse.wst.sse.core.internal.encoding.CodedReaderCreator)6 CoreException (org.eclipse.core.runtime.CoreException)3 IEncodedDocument (org.eclipse.wst.sse.core.internal.provisional.document.IEncodedDocument)3 Reader (java.io.Reader)2 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 URL (java.net.URL)1 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)1 MalformedInputException (java.nio.charset.MalformedInputException)1 Iterator (java.util.Iterator)1 List (java.util.List)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 IContentDescription (org.eclipse.core.runtime.content.IContentDescription)1 IDocument (org.eclipse.jface.text.IDocument)1 IEditorInput (org.eclipse.ui.IEditorInput)1 IStorageEditorInput (org.eclipse.ui.IStorageEditorInput)1 UnsupportedCharsetExceptionWithDetail (org.eclipse.wst.sse.core.internal.exceptions.UnsupportedCharsetExceptionWithDetail)1