Search in sources :

Example 1 with DocumentReader

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();
    }
}
Also used : IModelLoader(org.eclipse.wst.sse.core.internal.provisional.IModelLoader) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) DocumentReader(org.eclipse.wst.sse.core.internal.document.DocumentReader) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) ResourceInUse(org.eclipse.wst.sse.core.internal.provisional.exceptions.ResourceInUse)

Example 2 with DocumentReader

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);
        }
    }
}
Also used : CodedStreamCreator(org.eclipse.wst.sse.core.internal.encoding.CodedStreamCreator) DocumentReader(org.eclipse.wst.sse.core.internal.document.DocumentReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 3 with DocumentReader

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();
    }
}
Also used : CharSequenceReader(org.eclipse.wst.sse.core.internal.text.CharSequenceReader) DocumentReader(org.eclipse.wst.sse.core.internal.document.DocumentReader) IDocument(org.eclipse.jface.text.IDocument)

Example 4 with DocumentReader

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) {
    }
}
Also used : IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) LocalizedMessage(org.eclipse.wst.validation.internal.operations.LocalizedMessage) Message(org.eclipse.wst.validation.internal.core.Message) InputStreamReader(java.io.InputStreamReader) CoreException(org.eclipse.core.runtime.CoreException) BufferedReader(java.io.BufferedReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) XMLLineTokenizer(org.eclipse.wst.xml.core.internal.parser.XMLLineTokenizer) DocumentReader(org.eclipse.wst.sse.core.internal.document.DocumentReader) IOException(java.io.IOException) LocalizedMessage(org.eclipse.wst.validation.internal.operations.LocalizedMessage) Stack(java.util.Stack)

Example 5 with DocumentReader

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();
}
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)

Aggregations

DocumentReader (org.eclipse.wst.sse.core.internal.document.DocumentReader)7 IOException (java.io.IOException)3 InputStreamReader (java.io.InputStreamReader)3 CoreException (org.eclipse.core.runtime.CoreException)3 BufferedReader (java.io.BufferedReader)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 IDocument (org.eclipse.jface.text.IDocument)2 CodedStreamCreator (org.eclipse.wst.sse.core.internal.encoding.CodedStreamCreator)2 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)2 Message (org.eclipse.wst.validation.internal.core.Message)2 LocalizedMessage (org.eclipse.wst.validation.internal.operations.LocalizedMessage)2 IMessage (org.eclipse.wst.validation.internal.provisional.core.IMessage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 CharacterCodingException (java.nio.charset.CharacterCodingException)1 Stack (java.util.Stack)1 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1