<commit_before>/*
 * Copyright (c) 2011 Google Inc. All rights reserved. This program and the accompanying materials are made available
 * under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package com.google.eclipse.protobuf.ui.editor.model;

import static com.google.eclipse.protobuf.ui.ProtobufUiModule.PLUGIN_ID;
import static com.google.eclipse.protobuf.ui.util.Resources.URI_SCHEME_FOR_FILES;
import static java.util.Collections.singletonMap;
import static org.eclipse.core.runtime.IStatus.ERROR;
import static org.eclipse.emf.common.util.URI.createURI;
import static org.eclipse.emf.ecore.resource.ContentHandler.UNSPECIFIED_CONTENT_TYPE;
import static org.eclipse.xtext.EcoreUtil2.resolveLazyCrossReferences;
import static org.eclipse.xtext.resource.XtextResource.OPTION_ENCODING;
import static org.eclipse.xtext.util.CancelIndicator.NullImpl;
import static org.eclipse.xtext.validation.CheckMode.FAST_ONLY;

import java.io.*;
import java.net.URI;

import org.eclipse.core.filesystem.*;
import org.eclipse.core.runtime.*;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.ui.IURIEditorInput;
import org.eclipse.ui.ide.FileStoreEditorInput;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.model.*;
import org.eclipse.xtext.ui.editor.quickfix.IssueResolutionProvider;
import org.eclipse.xtext.ui.editor.validation.*;
import org.eclipse.xtext.ui.resource.IResourceSetProvider;
import org.eclipse.xtext.util.StringInputStream;
import org.eclipse.xtext.validation.IResourceValidator;

import com.google.eclipse.protobuf.ui.util.*;
import com.google.inject.Inject;

/**
 * @author <EMAIL> (<NAME>)
 */
public class ProtobufDocumentProvider extends XtextDocumentProvider {

  private static final String ENCODING = "UTF-8";

  @Inject private Closeables closeables;
  @Inject private IssueResolutionProvider issueResolutionProvider;
  @Inject private IResourceSetProvider resourceSetProvider;
  @Inject private IResourceValidator resourceValidator;
  @Inject private Resources resources;

  @Override protected ElementInfo createElementInfo(Object element) throws CoreException {
    if (element instanceof FileStoreEditorInput) return createElementInfo((FileStoreEditorInput) element);
    return super.createElementInfo(element);
  }

  private ElementInfo createElementInfo(FileStoreEditorInput input) throws CoreException {
    IDocument document = null;
    IStatus status = null;
    try {
      document = createDocument(input);
    } catch (CoreException e) {
      handleCoreException(e, "ProtobufDocumentProvider.createElementInfo");
      document = createEmptyDocument();
      status = e.getStatus();
    }
    IFileStore store = EFS.getLocalFileSystem().getStore(fileFrom(input).toURI());
    IFileInfo fileInfo = store.fetchInfo();
    IAnnotationModel model = createAnnotationModel(input);
    // new FileSynchronizer(input).install();
    FileInfo info = new FileInfo(document, model, null);
    info.fModificationStamp = fileInfo.getLastModified();
    info.fStatus = status;
    info.fEncoding = ENCODING;
    cacheEncodingState(input);
    XtextDocument xtextDocument = (XtextDocument) document;
    AnnotationIssueProcessor annotationIssueProcessor = new AnnotationIssueProcessor(xtextDocument, model,
        issueResolutionProvider);
    ValidationJob job = new ValidationJob(resourceValidator, xtextDocument, annotationIssueProcessor, FAST_ONLY);
    xtextDocument.setValidationJob(job);
    return info;
  }

  /** {@inheritDoc} */
  @Override protected IDocument createDocument(Object element) throws CoreException {
    if (element instanceof FileStoreEditorInput) return createDocument((FileStoreEditorInput) element);
    return super.createDocument(element);
  }

  private IDocument createDocument(FileStoreEditorInput input) throws CoreException {
    XtextDocument document = createEmptyDocument();
    File file = fileFrom(input);
    try {
      String contents = contentsOf(file);<commit_msg>Fixed: [ Issue 59 ] NPE when validating proto files outside of workspace https://code.google.com/p/protobuf-dt/issues/detail?id=59<commit_after>/*
 * Copyright (c) 2011 Google Inc.
 *
 * All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse
 * Public License v1.0 which accompanies this distribution, and is available at
 *
 * http://www.eclipse.org/legal/epl-v10.html
 */
package com.google.eclipse.protobuf.ui.editor.model;

import static com.google.eclipse.protobuf.ui.ProtobufUiModule.PLUGIN_ID;
import static com.google.eclipse.protobuf.ui.util.Resources.URI_SCHEME_FOR_FILES;
import static java.util.Collections.singletonMap;
import static org.eclipse.core.runtime.IStatus.ERROR;
import static org.eclipse.emf.common.util.URI.createURI;
import static org.eclipse.emf.ecore.resource.ContentHandler.UNSPECIFIED_CONTENT_TYPE;
import static org.eclipse.xtext.EcoreUtil2.resolveLazyCrossReferences;
import static org.eclipse.xtext.resource.XtextResource.OPTION_ENCODING;
import static org.eclipse.xtext.util.CancelIndicator.NullImpl;

import java.io.*;
import java.net.URI;

import org.eclipse.core.filesystem.*;
import org.eclipse.core.runtime.*;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.ui.IURIEditorInput;
import org.eclipse.ui.ide.FileStoreEditorInput;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.model.*;
import org.eclipse.xtext.ui.resource.IResourceSetProvider;
import org.eclipse.xtext.util.StringInputStream;

import com.google.eclipse.protobuf.ui.util.*;
import com.google.inject.Inject;

/**
 * @author <EMAIL> (<NAME>)
 */
public class ProtobufDocumentProvider extends XtextDocumentProvider {

  private static final String ENCODING = "UTF-8";

  @Inject private Closeables closeables;
  @Inject private IResourceSetProvider resourceSetProvider;
  @Inject private Resources resources;

  @Override protected ElementInfo createElementInfo(Object element) throws CoreException {
    if (element instanceof FileStoreEditorInput) return createElementInfo((FileStoreEditorInput) element);
    return super.createElementInfo(element);
  }

  private ElementInfo createElementInfo(FileStoreEditorInput input) throws CoreException {
    IDocument document = null;
    IStatus status = null;
    try {
      document = createDocument(input);
    } catch (CoreException e) {
      handleCoreException(e, "ProtobufDocumentProvider.createElementInfo");
      document = createEmptyDocument();
      status = e.getStatus();
    }
    IFileStore store = EFS.getLocalFileSystem().getStore(fileFrom(input).toURI());
    IFileInfo fileInfo = store.fetchInfo();
    IAnnotationModel model = createAnnotationModel(input);
    // new FileSynchronizer(input).install();
    FileInfo info = new FileInfo(document, model, null);
    info.fModificationStamp = fileInfo.getLastModified();
    info.fStatus = status;
    info.fEncoding = ENCODING;
    cacheEncodingState(input);
    return info;
  }

  @Override protected IDocument createDocument(Object element) throws CoreException {
    if (element instanceof FileStoreEditorInput) return createDocument((FileStoreEditorInput) element);
    return super.createDocument(element);
  }

  private IDocument createDocument(FileStoreEditorInput input) throws CoreException {
    XtextDocument document = createEmptyDocument();
    File file = fileFrom(input);
    try {
      String contents = contentsOf(file);
{'fraction_non_alphanumeric': 0.09445876288659794, 'fraction_numerical': 0.003092783505154639, 'mean_word_length': 5.268982229402262, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6208430', 'n_tokens_mistral': 2172, 'n_tokens_neox': 2128, 'n_words': 484}