<commit_before>/************************************************************************
 * Copyright © 2007-2017 - General Electric Company, All Rights Reserved
 * 
 * Project: SADL
 * 
 * Description: The Semantic Application Design Language (SADL) is a
 * language for building semantic models and expressing rules that
 * capture additional domain knowledge. The SADL-IDE (integrated
 * development environment) is a set of Eclipse plug-ins that
 * support the editing and testing of semantic models using the
 * SADL language.
 * 
 * This software is distributed "AS-IS" without ANY WARRANTIES
 * and licensed under the Eclipse Public License - v 1.0
 * which is available at http://www.eclipse.org/org/documents/epl-v10.php
 * 
 ***********************************************************************/
package com.ge.research.sadl.markers

import com.ge.research.sadl.model.DeclarationExtensions
import com.ge.research.sadl.sADL.SadlResource
import com.google.inject.ImplementedBy
import com.google.inject.Inject
import com.google.inject.Singleton
import java.nio.file.Path
import org.eclipse.emf.common.EMFPlugin
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.xtend.lib.annotations.Data
import org.eclipse.xtext.nodemodel.util.NodeModelUtils
import org.slf4j.LoggerFactory

import static extension org.eclipse.emf.ecore.util.EcoreUtil.getAllContents

/**
 * Representation of a service that provides the location of a SADL marker in
 * an arbitrary resource.
 * 
 * @author akos.kitta
 */
@ImplementedBy(SadlMarkerLocationProvider.Default)
interface SadlMarkerLocationProvider {

	@Data
	static class Location {

		/**
		 * Shared, unknown location.
		 */
		public static val UNKNOWN = new Location(0, 1, 1);

		/**
		 * An integer value indicating where a text marker starts.
		 * This attribute is zero-relative and inclusive.
		 */
		int start;

		/**
		 * An integer value indicating where a text marker ends. 
		 * This attribute is zero-relative and exclusive.
		 */
		int end;

		/**
		 * An integer value indicating the line number for a text marker. 
		 * This attribute is 1-relative.
		 */
		int lineNumber;

	}

	/**
	 * Returns with the location of the marker argument.
	 */
	def Location getLocation(ResourceSet resourceSet, SadlMarker marker, Path projectLocation);

	@Singleton
	static class Default implements SadlMarkerLocationProvider {

		static val LOGGER = LoggerFactory.getLogger(Default);

		@Inject
		extension DeclarationExtensions;

		@Override
		override getLocation(ResourceSet resourceSet, SadlMarker marker, Path projectLocation) {
			try {
				val uri = if (EMFPlugin.IS_ECLIPSE_RUNNING) {
					URI.createPlatformResourceURI('''«projectLocation.fileName»/«marker.filePath»''', true);
				} else {
					URI.createFileURI(projectLocation.resolve(marker.filePath).toFile.absolutePath);
				}
				val resource = resourceSet.getResource(uri, true);
				if (resource === null) {
					return Location.UNKNOWN;
				}

				val eObject = getEObjectForMarker(resource, marker);
				if (eObject === null) {
					return Location.UNKNOWN;
				}

				val node = NodeModelUtils.getNode(eObject);
				if (node === null) {
					return Location.UNKNOWN;
				}

				return new Location(node.offset, node.offset + node.length, node.startLine);
			} catch (Exception e) {
				LOGGER.error('''Error while trying to get the location of marker: «marker».''', e);
				return Location.UNKNOWN;
			}
		}

		protected def getEObjectForMarker(Resource resource, SadlMarker marker) {
			return resource.getAllContents(true).filter(SadlResource).filter[concreteName == marker.astNodeName].head;
		}

	}

}<commit_msg>changes incourse of this morning's tcon<commit_after>/************************************************************************
 * Copyright © 2007-2017 - General Electric Company, All Rights Reserved
 * 
 * Project: SADL
 * 
 * Description: The Semantic Application Design Language (SADL) is a
 * language for building semantic models and expressing rules that
 * capture additional domain knowledge. The SADL-IDE (integrated
 * development environment) is a set of Eclipse plug-ins that
 * support the editing and testing of semantic models using the
 * SADL language.
 * 
 * This software is distributed "AS-IS" without ANY WARRANTIES
 * and licensed under the Eclipse Public License - v 1.0
 * which is available at http://www.eclipse.org/org/documents/epl-v10.php
 * 
 ***********************************************************************/
package com.ge.research.sadl.markers

import com.ge.research.sadl.model.DeclarationExtensions
import com.ge.research.sadl.sADL.SadlResource
import com.google.inject.ImplementedBy
import com.google.inject.Inject
import com.google.inject.Singleton
import java.nio.file.Path
import org.eclipse.emf.common.EMFPlugin
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.xtend.lib.annotations.Data
import org.eclipse.xtext.nodemodel.util.NodeModelUtils
import org.slf4j.LoggerFactory

import static extension org.eclipse.emf.ecore.util.EcoreUtil.getAllContents
import org.eclipse.emf.ecore.EObject

/**
 * Representation of a service that provides the location of a SADL marker in
 * an arbitrary resource.
 * 
 * @author akos.kitta
 */
@ImplementedBy(SadlMarkerLocationProvider.Default)
interface SadlMarkerLocationProvider {

	@Data
	static class Location {

		/**
		 * Shared, unknown location.
		 */
		public static val UNKNOWN = new Location(0, 1, 1);

		/**
		 * An integer value indicating where a text marker starts.
		 * This attribute is zero-relative and inclusive.
		 */
		int start;

		/**
		 * An integer value indicating where a text marker ends. 
		 * This attribute is zero-relative and exclusive.
		 */
		int end;

		/**
		 * An integer value indicating the line number for a text marker. 
		 * This attribute is 1-relative.
		 */
		int lineNumber;

	}

	/**
	 * Returns with the location of the marker argument.
	 */
	def Location getLocation(ResourceSet resourceSet, SadlMarker marker, Path projectLocation);

	@Singleton
	static class Default implements SadlMarkerLocationProvider {

		static val LOGGER = LoggerFactory.getLogger(Default);

		@Inject
		extension DeclarationExtensions;

		@Override
		override getLocation(ResourceSet resourceSet, SadlMarker marker, Path projectLocation) {
			try {
				//TODO this is where a model URI would need to be used to get the Xtext Resource
				val uri = if (EMFPlugin.IS_ECLIPSE_RUNNING) {
					URI.createPlatformResourceURI('''«projectLocation.fileName»/«marker.filePath»''', true);
				} else {
					URI.createFileURI(projectLocation.resolve(marker.filePath).toFile.absolutePath);
				}
				val resource = resourceSet.getResource(uri, true);
				if (resource === null) {
					return Location.UNKNOWN;
				}

				val eObject = getEObjectForMarker(resource, marker);
				if (eObject === null) {
					return Location.UNKNOWN;
				}

				val node = NodeModelUtils.getNode(eObject);
				if (node === null) {
					return Location.UNKNOWN;
				}

				return new Location(node.offset, node.offset + node.length, node.startLine);
			} catch (Exception e) {
				LOGGER.error('''Error while trying to get the location of marker: «marker».''', e);
				return Location.UNKNOWN;
			}
		}

		protected def EObject getEObjectForMarker(Resource resource, SadlMarker marker) {
			return resource.getAllContents(true).filter(SadlResource).filter[concreteName == marker.astNodeName].head;
		}

	}

}
{'fraction_non_alphanumeric': 0.12769028871391075, 'fraction_numerical': 0.004461942257217848, 'mean_word_length': 4.047019867549669, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '4675904', 'n_tokens_mistral': 2423, 'n_tokens_neox': 2097, 'n_words': 645}