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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations