use of org.eclipse.wst.sse.core.internal.provisional.document.IEncodedDocument in project webtools.sourceediting by eclipse.
the class AbstractModelLoader method load.
public void load(InputStream inputStream, IStructuredModel model, EncodingRule encodingRule) throws UnsupportedEncodingException, java.io.IOException {
// note we don't open the stream, so we don't close it
IEncodedDocument structuredDocument = model.getStructuredDocument();
if (inputStream == null) {
structuredDocument = getDocumentLoader().createNewStructuredDocument();
} else {
// assume's model has been initialized already with base location
structuredDocument = getDocumentLoader().createNewStructuredDocument(model.getBaseLocation(), inputStream, encodingRule);
// TODO: model's not designed for this!
// we want to move to this "set" method, but the 'fire' was needed
// as
// a work around for strucutredModel not handling 'set' right, but
// that 'fireNewDocument' method was causing unbalance
// "aboutToChange" and "changed"
// events.
// model.setStructuredDocument((IStructuredDocument)
// structuredDocument);
// ((IStructuredDocument)
// structuredDocument).fireNewDocument(this);
model.getStructuredDocument().set(structuredDocument.get());
}
documentLoaderInstance = null;
}
use of org.eclipse.wst.sse.core.internal.provisional.document.IEncodedDocument 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();
}
use of org.eclipse.wst.sse.core.internal.provisional.document.IEncodedDocument 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;
}
use of org.eclipse.wst.sse.core.internal.provisional.document.IEncodedDocument in project webtools.sourceediting by eclipse.
the class AbstractDocumentLoader method createNewStructuredDocument.
/**
* This method must return a new instance of IEncodedDocument, that has
* been initialized with appropriate parser. For many loaders, the
* (default) parser used is known for any input. For others, the correct
* parser (and its initialization) is normally dependent on the content of
* the file. This no-argument method should assume "empty input" and would
* therefore return the default parser for the default contentType.
*/
public IEncodedDocument createNewStructuredDocument() {
IEncodedDocument structuredDocument = newEncodedDocument();
// Make sure every structuredDocument has an Encoding Memento,
// which is the default one for "empty" structuredDocuments
String charset = ContentTypeEncodingPreferences.useDefaultNameRules(getDocumentEncodingDetector());
String specDefaultCharset = getDocumentEncodingDetector().getSpecDefaultEncoding();
structuredDocument.setEncodingMemento(CodedIO.createEncodingMemento(charset, EncodingMemento.DEFAULTS_ASSUMED_FOR_EMPTY_INPUT, specDefaultCharset));
String lineDelimiter = getPreferredNewLineDelimiter(null);
if (lineDelimiter != null)
structuredDocument.setPreferredLineDelimiter(lineDelimiter);
IDocumentPartitioner defaultPartitioner = getDefaultDocumentPartitioner();
if (structuredDocument instanceof IDocumentExtension3) {
((IDocumentExtension3) structuredDocument).setDocumentPartitioner(IStructuredPartitioning.DEFAULT_STRUCTURED_PARTITIONING, defaultPartitioner);
} else {
structuredDocument.setDocumentPartitioner(defaultPartitioner);
}
defaultPartitioner.connect(structuredDocument);
return structuredDocument;
}
use of org.eclipse.wst.sse.core.internal.provisional.document.IEncodedDocument in project webtools.sourceediting by eclipse.
the class CSSCorePreferencesTest method testDelimiterPreferences.
/**
* Tests line delimiter preferences by making sure document created
* follows line delimiter preference.
*/
public void testDelimiterPreferences() {
// check if content type preferences match
String preferredDelimiter = ContentTypeEncodingPreferences.getPreferredNewLineDelimiter(ContentTypeIdForCSS.ContentTypeID_CSS);
Preferences prefs = ContentBasedPreferenceGateway.getPreferences(ContentTypeIdForCSS.ContentTypeID_CSS);
String gatewayDelimiter = prefs.get(CommonEncodingPreferenceNames.END_OF_LINE_CODE, null);
assertEquals("ContentTypeEncodingPreferences and ContentBasedPreferenceGateway preferences do not match", gatewayDelimiter, preferredDelimiter);
// set a particular line delimiter
prefs.put(CommonEncodingPreferenceNames.END_OF_LINE_CODE, CommonEncodingPreferenceNames.LF);
// create document
CSSDocumentLoader loader = new CSSDocumentLoader();
IEncodedDocument document = loader.createNewStructuredDocument();
String documentDelimiter = document.getPreferredLineDelimiter();
// verify delimiter in document matches preference
assertEquals("Delimiter in document does not match preference", CommonEncodingPreferenceNames.STRING_LF, documentDelimiter);
// return to original preference
prefs.remove(CommonEncodingPreferenceNames.END_OF_LINE_CODE);
}
Aggregations