use of org.eclipse.wst.sse.core.internal.document.DocumentReader in project webtools.sourceediting by eclipse.
the class ModelManagerImpl method _doCommonCreateModel.
private void _doCommonCreateModel(InputStream inputStream, String id, IModelHandler handler, URIResolver resolver, ReadEditType rwType, String encoding, String lineDelimiter, SharedObject sharedObject) throws IOException {
boolean doRemove = true;
try {
synchronized (sharedObject) {
IStructuredModel model = null;
try {
model = _commonCreateModel(id, handler, resolver);
IModelLoader loader = handler.getModelLoader();
if (inputStream == null) {
// $NON-NLS-1$ //$NON-NLS-2$
Logger.log(Logger.WARNING, "model was requested for id " + id + " without a content InputStream");
}
loader.load(id, Utilities.getMarkSupportedStream(inputStream), model, encoding, lineDelimiter);
} catch (ResourceInUse e) {
// impossible, since we've already found
handleProgramError(e);
}
if (model != null) {
/**
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=264228
*
* Ensure that the content type identifier field of the model
* is properly set. This is normally handled by the
* FileBufferModelManager when working with files as it knows
* the content type in advance; here is where we handle it for
* streams.
*/
if (model instanceof AbstractStructuredModel) {
DocumentReader reader = new DocumentReader(model.getStructuredDocument());
IContentDescription description = Platform.getContentTypeManager().getDescriptionFor(reader, id, new QualifiedName[0]);
reader.close();
if (description != null && description.getContentType() != null) {
((AbstractStructuredModel) model).setContentTypeIdentifier(description.getContentType().getId());
}
}
sharedObject.theSharedModel = model;
_initCount(sharedObject, rwType);
doRemove = false;
}
}
} finally {
if (doRemove) {
SYNC.acquire();
// remove it if we didn't get one back
fManagedObjects.remove(id);
SYNC.release();
}
sharedObject.setLoaded();
}
}
use of org.eclipse.wst.sse.core.internal.document.DocumentReader in project webtools.sourceediting by eclipse.
the class ModelManagerImpl method saveModel.
/**
* @deprecated - this method is less efficient than IFile form, since it
* requires an extra "copy" of byte array, and should be avoid
* in favor of the IFile form.
*/
public void saveModel(String id, OutputStream outputStream, EncodingRule encodingRule) throws UnsupportedEncodingException, CoreException, IOException {
// $NON-NLS-1$
Assert.isNotNull(id, "id parameter can not be null");
SYNC.acquire();
// let's see if we already have it in our cache
SharedObject sharedObject = (SharedObject) fManagedObjects.get(id);
if (sharedObject == null) {
SYNC.release();
// $NON-NLS-1$ = "Program Error: ModelManagerImpl::saveModel. Model should be in the cache"
throw new IllegalStateException(SSECoreMessages.Program_Error__ModelManage_EXC_);
} else {
SYNC.release();
sharedObject.waitForLoadAttempt();
synchronized (sharedObject) {
CodedStreamCreator codedStreamCreator = new CodedStreamCreator();
codedStreamCreator.set(sharedObject.theSharedModel.getId(), new DocumentReader(sharedObject.theSharedModel.getStructuredDocument()));
codedStreamCreator.setPreviousEncodingMemento(sharedObject.theSharedModel.getStructuredDocument().getEncodingMemento());
ByteArrayOutputStream byteArrayOutputStream = codedStreamCreator.getCodedByteArrayOutputStream(encodingRule);
byte[] outputBytes = byteArrayOutputStream.toByteArray();
outputStream.write(outputBytes);
// $NON-NLS-1$
trace("saving model", id);
sharedObject.theSharedModel.setDirtyState(false);
sharedObject.theSharedModel.setNewState(false);
}
}
}
use of org.eclipse.wst.sse.core.internal.document.DocumentReader in project webtools.sourceediting by eclipse.
the class XMLSourceParser method reset.
/**
* Resets the input.
*/
public void reset(java.io.Reader reader, int position) {
primReset();
fOffset = position;
getTokenizer().reset(reader, position);
if (reader instanceof DocumentReader) {
IDocument doc = ((DocumentReader) reader).getDocument();
if (doc instanceof CharSequence) {
fCharSequenceSource = (CharSequence) doc;
} else {
// old fashioned IDocument
fDocumentInput = ((DocumentReader) reader).getDocument();
}
} else if (reader instanceof CharSequenceReader) {
fCharSequenceSource = ((CharSequenceReader) reader).getOriginalSource();
}
}
use of org.eclipse.wst.sse.core.internal.document.DocumentReader in project webtools.sourceediting by eclipse.
the class StreamingMarkupValidator method validateFile.
/**
* Validates the given file. It will stream the contents of the file without creating a model for the file; it will only
* use existing
* @param file the file to validate
* @param reporter the reporter
*/
private void validateFile(IFile file, IReporter reporter) {
Message message = new LocalizedMessage(IMessage.LOW_SEVERITY, file.getFullPath().toString().substring(1));
reporter.displaySubtask(StreamingMarkupValidator.this, message);
XMLLineTokenizer tokenizer = null;
try {
tagStack = new Stack();
model = StructuredModelManager.getModelManager().getExistingModelForRead(file);
try {
if (model == null) {
tokenizer = new XMLLineTokenizer(new BufferedReader(new InputStreamReader(file.getContents(true), getCharset(file))));
} else {
tokenizer = new XMLLineTokenizer(new BufferedReader(new DocumentReader(model.getStructuredDocument())));
}
checkTokens(tokenizer, reporter);
} finally {
if (model != null) {
model.releaseFromRead();
model = null;
}
}
} catch (UnsupportedEncodingException e) {
} catch (CoreException e) {
} catch (IOException e) {
}
}
use of org.eclipse.wst.sse.core.internal.document.DocumentReader in project webtools.sourceediting by eclipse.
the class StructuredFileTaskScanner method findTasks.
private void findTasks(IFile file, final TaskTag[] taskTags, final IProgressMonitor monitor) {
try {
IModelHandler handler = ModelHandlerRegistry.getInstance().getHandlerFor(file);
// records if the optimized streamish parse was possible
boolean didStreamParse = false;
final IEncodedDocument defaultDocument = handler.getDocumentLoader().createNewStructuredDocument();
if (defaultDocument instanceof IStructuredDocument) {
RegionParser parser = ((IStructuredDocument) defaultDocument).getParser();
if (parser instanceof StructuredDocumentRegionParser) {
didStreamParse = true;
String charset = detectCharset(file);
StructuredDocumentRegionParser documentParser = (StructuredDocumentRegionParser) parser;
final IDocument textDocument = new Document();
setDocumentContent(textDocument, file.getContents(true), charset);
monitor.beginTask("", textDocument.getLength());
documentParser.reset(new DocumentReader(textDocument));
documentParser.addStructuredDocumentRegionHandler(new StructuredDocumentRegionHandler() {
public void nodeParsed(IStructuredDocumentRegion documentRegion) {
ITextRegionList regions = documentRegion.getRegions();
for (int j = 0; j < regions.size(); j++) {
ITextRegion comment = regions.get(j);
findTasks(textDocument, taskTags, documentRegion, comment);
}
// disconnect the document regions
if (documentRegion.getPrevious() != null) {
documentRegion.getPrevious().setPrevious(null);
documentRegion.getPrevious().setNext(null);
}
if (monitor.isCanceled()) {
// $NON-NLS-1$
textDocument.set("");
}
monitor.worked(documentRegion.getLength());
}
public void resetNodes() {
}
});
documentParser.getDocumentRegions();
}
}
if (!didStreamParse) {
// Use a StructuredDocument
IEncodedDocument document = handler.getDocumentLoader().createNewStructuredDocument(file);
monitor.beginTask("", document.getLength());
if (document instanceof IStructuredDocument) {
IStructuredDocumentRegion documentRegion = ((IStructuredDocument) document).getFirstStructuredDocumentRegion();
while (documentRegion != null) {
ITextRegionList regions = documentRegion.getRegions();
for (int j = 0; j < regions.size(); j++) {
ITextRegion comment = regions.get(j);
findTasks(document, taskTags, documentRegion, comment);
}
monitor.worked(documentRegion.getLength());
documentRegion = documentRegion.getNext();
}
}
}
} catch (CoreException e) {
// $NON-NLS-1$
Logger.logException("Exception with " + file.getFullPath().toString(), e);
} catch (CharacterCodingException e) {
// $NON-NLS-1$
Logger.log(Logger.INFO, "StructuredFileTaskScanner encountered CharacterCodingException reading " + file.getFullPath());
} catch (Exception e) {
// $NON-NLS-1$
Logger.logException("Exception with " + file.getFullPath().toString(), e);
}
monitor.done();
}
Aggregations