Search in sources :

Example 6 with DocumentReader

use of org.eclipse.wst.sse.core.internal.document.DocumentReader in project webtools.sourceediting by eclipse.

the class JSONSyntaxValidator 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(JSONSyntaxValidator.this, message);
    JSONLineTokenizer tokenizer = null;
    try {
        IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForRead(file);
        try {
            if (model == null) {
                tokenizer = new JSONLineTokenizer(new BufferedReader(new InputStreamReader(file.getContents(true), getCharset(file))));
            } else {
                tokenizer = new JSONLineTokenizer(new BufferedReader(new DocumentReader(model.getStructuredDocument())));
            }
            JSONSyntaxValidatorHelper.validate(tokenizer, reporter, this, this);
        } finally {
            if (model != null) {
                model.releaseFromRead();
                model = null;
            }
        }
    } catch (UnsupportedEncodingException e) {
    } catch (CoreException e) {
    } catch (IOException e) {
    }
}
Also used : JSONLineTokenizer(org.eclipse.wst.json.core.internal.parser.JSONLineTokenizer) 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) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) DocumentReader(org.eclipse.wst.sse.core.internal.document.DocumentReader) IOException(java.io.IOException) LocalizedMessage(org.eclipse.wst.validation.internal.operations.LocalizedMessage)

Example 7 with DocumentReader

use of org.eclipse.wst.sse.core.internal.document.DocumentReader in project webtools.sourceediting by eclipse.

the class ModelManagerImpl method saveStructuredDocument.

public void saveStructuredDocument(IStructuredDocument structuredDocument, IFile iFile, EncodingRule encodingRule) throws UnsupportedEncodingException, CoreException, IOException {
    // $NON-NLS-1$
    Assert.isNotNull(iFile, "file parameter can not be null");
    if (FileBufferModelManager.getInstance().isExistingBuffer(structuredDocument)) {
        ITextFileBuffer buffer = FileBufferModelManager.getInstance().getBuffer(structuredDocument);
        if (iFile.getFullPath().equals(buffer.getLocation()) || (iFile.getLocation() != null && iFile.getLocation().equals(buffer.getLocation())) || (iFile.getLocationURI() != null && buffer.getFileStore() != null && iFile.getLocationURI().equals(buffer.getFileStore().toURI()))) {
            buffer.commit(new NullProgressMonitor(), true);
        }
    } else {
        // IModelHandler handler = calculateType(iFile);
        // IDocumentDumper dumper = handler.getDocumentDumper();
        CodedStreamCreator codedStreamCreator = new CodedStreamCreator();
        Reader reader = new DocumentReader(structuredDocument);
        codedStreamCreator.set(iFile, reader);
        codedStreamCreator.setPreviousEncodingMemento(structuredDocument.getEncodingMemento());
        EncodingMemento encodingMemento = codedStreamCreator.getCurrentEncodingMemento();
        // be sure document's is updated, in case exception is thrown in
        // getCodedByteArrayOutputStream
        structuredDocument.setEncodingMemento(encodingMemento);
        // Convert line delimiters after encoding memento is figured out,
        // but
        // before writing to output stream.
        handleConvertLineDelimiters(structuredDocument, iFile, encodingRule, encodingMemento);
        ByteArrayOutputStream codedByteStream = codedStreamCreator.getCodedByteArrayOutputStream(encodingRule);
        InputStream codedStream = new ByteArrayInputStream(codedByteStream.toByteArray());
        if (iFile.exists())
            iFile.setContents(codedStream, true, true, null);
        else
            iFile.create(codedStream, false, null);
        codedByteStream.close();
        codedStream.close();
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) CodedStreamCreator(org.eclipse.wst.sse.core.internal.encoding.CodedStreamCreator) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) EncodingMemento(org.eclipse.wst.sse.core.internal.encoding.EncodingMemento) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) Reader(java.io.Reader) DocumentReader(org.eclipse.wst.sse.core.internal.document.DocumentReader) InputStreamReader(java.io.InputStreamReader) DocumentReader(org.eclipse.wst.sse.core.internal.document.DocumentReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

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