Search in sources :

Example 6 with IEncodedDocument

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;
}
Also used : IEncodedDocument(org.eclipse.wst.sse.core.internal.provisional.document.IEncodedDocument)

Example 7 with IEncodedDocument

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();
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) StructuredDocumentRegionHandler(org.eclipse.wst.sse.core.internal.ltk.parser.StructuredDocumentRegionHandler) DocumentReader(org.eclipse.wst.sse.core.internal.document.DocumentReader) CharacterCodingException(java.nio.charset.CharacterCodingException) StructuredDocumentRegionParser(org.eclipse.wst.sse.core.internal.ltk.parser.StructuredDocumentRegionParser) RegionParser(org.eclipse.wst.sse.core.internal.ltk.parser.RegionParser) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IEncodedDocument(org.eclipse.wst.sse.core.internal.provisional.document.IEncodedDocument) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IModelHandler(org.eclipse.wst.sse.core.internal.ltk.modelhandler.IModelHandler) CharacterCodingException(java.nio.charset.CharacterCodingException) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) IOException(java.io.IOException) StructuredDocumentRegionParser(org.eclipse.wst.sse.core.internal.ltk.parser.StructuredDocumentRegionParser) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) CoreException(org.eclipse.core.runtime.CoreException) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IEncodedDocument(org.eclipse.wst.sse.core.internal.provisional.document.IEncodedDocument) IDocument(org.eclipse.jface.text.IDocument)

Example 8 with IEncodedDocument

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;
}
Also used : CodedReaderCreator(org.eclipse.wst.sse.core.internal.encoding.CodedReaderCreator) IEncodedDocument(org.eclipse.wst.sse.core.internal.provisional.document.IEncodedDocument)

Example 9 with IEncodedDocument

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;
}
Also used : IDocumentExtension3(org.eclipse.jface.text.IDocumentExtension3) IDocumentPartitioner(org.eclipse.jface.text.IDocumentPartitioner) IEncodedDocument(org.eclipse.wst.sse.core.internal.provisional.document.IEncodedDocument)

Example 10 with IEncodedDocument

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);
}
Also used : CSSDocumentLoader(org.eclipse.wst.css.core.internal.encoding.CSSDocumentLoader) ContentTypeEncodingPreferences(org.eclipse.wst.sse.core.internal.encoding.ContentTypeEncodingPreferences) Preferences(org.osgi.service.prefs.Preferences) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) IEncodedDocument(org.eclipse.wst.sse.core.internal.provisional.document.IEncodedDocument)

Aggregations

IEncodedDocument (org.eclipse.wst.sse.core.internal.provisional.document.IEncodedDocument)15 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)6 IEclipsePreferences (org.eclipse.core.runtime.preferences.IEclipsePreferences)4 ContentTypeEncodingPreferences (org.eclipse.wst.sse.core.internal.encoding.ContentTypeEncodingPreferences)4 Preferences (org.osgi.service.prefs.Preferences)4 CoreException (org.eclipse.core.runtime.CoreException)3 CodedReaderCreator (org.eclipse.wst.sse.core.internal.encoding.CodedReaderCreator)3 RegionParser (org.eclipse.wst.sse.core.internal.ltk.parser.RegionParser)2 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)2 IOException (java.io.IOException)1 CharacterCodingException (java.nio.charset.CharacterCodingException)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 Document (org.eclipse.jface.text.Document)1 IDocument (org.eclipse.jface.text.IDocument)1 IDocumentExtension3 (org.eclipse.jface.text.IDocumentExtension3)1 IDocumentPartitioner (org.eclipse.jface.text.IDocumentPartitioner)1 JSPDocumentLoader (org.eclipse.jst.jsp.core.internal.encoding.JSPDocumentLoader)1 CSSDocumentLoader (org.eclipse.wst.css.core.internal.encoding.CSSDocumentLoader)1 HTMLDocumentLoader (org.eclipse.wst.html.core.internal.encoding.HTMLDocumentLoader)1 DocumentReader (org.eclipse.wst.sse.core.internal.document.DocumentReader)1