<commit_before>/** 
 * UIMA common
 * Copyright (C) 2010  <NAME>
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package fr.univnantes.lina.uima.util;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.CASException;
import org.apache.uima.cas.FSIndex;
import org.apache.uima.cas.FSIterator;
import org.apache.uima.cas.Feature;
import org.apache.uima.cas.FeatureStructure;
import org.apache.uima.cas.Type;
import org.apache.uima.cas.text.AnnotationIndex;
import org.apache.uima.examples.SourceDocumentInformation;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.cas.FSArray;
import org.apache.uima.jcas.tcas.Annotation;

import fr.univnantes.lina.util.JavaUtilities;

/**
 * <p>
 * Methods for handling annotations like
 * introspection and creation 
 * </p> 
 * <p>
 * Take in charge the exceptions
 * </p>
 * 
 * @author hernandez
 *
 */
public class AnnotationUtilities   {


	/**
	 * Name of the current class
	 */
	private static String CURRENTCLASSNAME = "AnnotationUtilities";

	/**
	 * Get the class of a given annotation name in order to cast annotations latter
	 * Allow to know the type of the annotation to handle only at the runtime level
	 * @param annotationString
	 * @return Class<Annotation>
	 * 
	 * @throws AnalysisEngineProcessException
	 */
	public static Class<Annotation> getAnnotationClass(String annotationString) throws AnalysisEngineProcessException {
		// Récupère l'annotation courante d'un type connu
		// TokenAnnotation tokenAnnotation = (TokenAnnotation)
		// inputAnnotationIter
		// .next();

		//Génération de la classe  correspondante à la string annotationString
		Class<Annotation> annotationClass = null;
		try {
			annotationClass = (Class<Annotation>) Class
			.forName(annotationString);
		} catch (ClassNotFoundException e) {
			String errmsg = "Error: Class " + annotationString
			+ " not found !";
			throw new AnalysisEngineProcessException(errmsg,
					new Object[] { annotationString },e);	
			//e.printStackTrace();
		}
		// Annotation tokenAnnotation = (Annotation) inputAnnotationIter.next();
		// InputAnnotationClass.cast(tokenAnnotation);

		return annotationClass;

	}


	/**
	 * Get the object of a given class 
	 * Remains to cast the object
	 * Allow to know the type of the annotation to handle only at the runtime level
	 * @param annotationString
	 * @return annotationString
	 * @throws AnalysisEngineProcessException
	 */
	public static Object newJCasObjectClassInstance(Class aObjectClass, JCas aJCas) throws AnalysisEngineProcessException {

		Constructor aClassConstructor = null;
		try {
			aClassConstructor = aObjectClass.getConstructor(new Class [] {JCas.class});
		} catch (SecurityException e) {
			String errmsg = "Error: "+CURRENTCLASSNAME+" getObject SecurityException !";
			throw new AnalysisEngineProcessException(errmsg,
					new Object[] { },e);	
			//e.printStackTrace();
		} catch (NoSuchMethodException e) {
			String errmsg = "Error: "+CURRENTCLASSNAME+" getObject NoSuchMethodException !";
			throw new AnalysisEngineProcessException(errmsg,
					new Object[] { },e);	
			//e.printStackTrace();
		}

		Object aConstructedObject = null;
		try {
			aConstructedObject = aClassConstructor.newInstance(new Object [] {aJCas});
		} catch (IllegalArgumentException e) {
			String errmsg = "Error: "+CURRENTCLASSNAME+" getObject IllegalArgumentException !";
			throw new AnalysisEngineProcessException(errmsg,
					new Object[] { },e);	
			//e.printStackTrace();
		} catch (InstantiationException e) {
			String errmsg = "Error: "+CURRENTCLASSNAME+" getObject InstantiationException !";
			throw new AnalysisEngineProcessException(errmsg,
					new Object[] { },e);	
			//e.printStackTrace();
		} catch (IllegalAccessException e) {
			String errmsg = "Error: "+CURRENTCLASSNAME+" getObject IllegalAccessException !";
			throw new AnalysisEngineProcessException(errmsg,
					new Object[] { },e);	
			//e.printStackTrace();
		} catch (InvocationTargetException e) {
			String errmsg = "Error: "+CURRENTCLASSNAME+" getObject InvocationTargetException !";
			throw new AnalysisEngineProcessException(errmsg,
					new Object[] { },e);	
			//e.printStackTrace();
		}

		return aConstructedObject;

	}
	

	/**
	 * Cast object of a given class 
	 * Remains to cas the object
	 * Allow to know the type of the annotation to handle only at the runtime level
	 * @param annotationString
	 * @return annotationString
	 * @throws AnalysisEngineProcessException
	 */
	public static void castObjectWithAGivenClass(Class aObjectClass, Object aConstructedObject) throws AnalysisEngineProcessException {

		aObjectClass.cast(aConstructedObject);		
	}


	/**
	 * This method create an annotation and sets one feature with a String value.
	 * 
	 * @deprecated
	 * 
	 * @param aJCas
	 *            the CAS over which the process is performed
	 * @param annotationNameToCreate
	 * @param beginFeatureValue
	 * @param endFeatureValue
	 * @param featureNameToSet            
	 * @param valueFeatureValue
	 * @throws AnalysisEngineProcessException 
	 */
	/*
	public static void createAnnotation(JCas aJCas, String annotationNameToCreate,
			int beginFeatureValue, int endFeatureValue, String featureNameToSet, String valueFeatureValue) throws AnalysisEngineProcessException {

		// Crée une annotation générique
		// SequenceMatch sequenceMatch = new SequenceMatch(aJCas);
		// sequenceMatch.setBegin(patternHashMap.get(patternKeyString).getCurrentStartIndexCursor());
		// sequenceMatch.setEnd(tokenAnnotation.getEnd());
		// sequenceMatch.setValue(patternKeyString);
		// sequenceMatch.addToIndexes();

		// Crée une annotation prédéfinie
		// Object[] args = null;


		try {
			Object[] args = null;

			Class<Annotation> TgtClass = (Class<Annotation>) Class
			.forName(annotationNameToCreate);

			// System.out.println("Debug: ----------------------------------------------------------------------"
			// );
			// System.out.println("Debug: TgtClass.getName()	= " +<commit_msg> * AnnotationUtilities: garder deux méthodes createAnnotation une avec featuresHashMap et une seulement avec begin et end <commit_after>/** 
 * UIMA common
 * Copyright (C) 2010  <NAME>
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package fr.univnantes.lina.uima.util;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.CASException;
import org.apache.uima.cas.FSIndex;
import org.apache.uima.cas.FSIterator;
import org.apache.uima.cas.Feature;
import org.apache.uima.cas.FeatureStructure;
import org.apache.uima.cas.Type;
import org.apache.uima.cas.text.AnnotationIndex;
import org.apache.uima.examples.SourceDocumentInformation;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.cas.FSArray;
import org.apache.uima.jcas.tcas.Annotation;

import fr.univnantes.lina.util.JavaUtilities;

/**
 * <p>
 * Methods for handling annotations like
 * introspection and creation 
 * </p> 
 * <p>
 * Take in charge the exceptions
 * </p>
 * 
 * @author hernandez
 *
 */
public class AnnotationUtilities   {


	/**
	 * Name of the current class
	 */
	private static String CURRENTCLASSNAME = "AnnotationUtilities";

	/**
	 * Get the class of a given annotation name in order to cast annotations latter
	 * Allow to know the type of the annotation to handle only at the runtime level
	 * @param annotationString
	 * @return Class<Annotation>
	 * 
	 * @throws AnalysisEngineProcessException
	 */
	public static Class<Annotation> getAnnotationClass(String annotationString) throws AnalysisEngineProcessException {
		// Récupère l'annotation courante d'un type connu
		// TokenAnnotation tokenAnnotation = (TokenAnnotation)
		// inputAnnotationIter
		// .next();

		//Génération de la classe  correspondante à la string annotationString
		Class<Annotation> annotationClass = null;
		try {
			annotationClass = (Class<Annotation>) Class
			.forName(annotationString);
		} catch (ClassNotFoundException e) {
			String errmsg = "Error: Class " + annotationString
			+ " not found !";
			throw new AnalysisEngineProcessException(errmsg,
					new Object[] { annotationString },e);	
			//e.printStackTrace();
		}
		// Annotation tokenAnnotation = (Annotation) inputAnnotationIter.next();
		// InputAnnotationClass.cast(tokenAnnotation);

		return annotationClass;

	}


	/**
	 * Get the object of a given class 
	 * Remains to cast the object
	 * Allow to know the type of the annotation to handle only at the runtime level
	 * @param annotationString
	 * @return annotationString
	 * @throws AnalysisEngineProcessException
	 */
	public static Object newJCasObjectClassInstance(Class aObjectClass, JCas aJCas) throws AnalysisEngineProcessException {

		Constructor aClassConstructor = null;
		try {
			aClassConstructor = aObjectClass.getConstructor(new Class [] {JCas.class});
		} catch (SecurityException e) {
			String errmsg = "Error: "+CURRENTCLASSNAME+" getObject SecurityException !";
			throw new AnalysisEngineProcessException(errmsg,
					new Object[] { },e);	
			//e.printStackTrace();
		} catch (NoSuchMethodException e) {
			String errmsg = "Error: "+CURRENTCLASSNAME+" getObject NoSuchMethodException !";
			throw new AnalysisEngineProcessException(errmsg,
					new Object[] { },e);	
			//e.printStackTrace();
		}

		Object aConstructedObject = null;
		try {
			aConstructedObject = aClassConstructor.newInstance(new Object [] {aJCas});
		} catch (IllegalArgumentException e) {
			String errmsg = "Error: "+CURRENTCLASSNAME+" getObject IllegalArgumentException !";
			throw new AnalysisEngineProcessException(errmsg,
					new Object[] { },e);	
			//e.printStackTrace();
		} catch (InstantiationException e) {
			String errmsg = "Error: "+CURRENTCLASSNAME+" getObject InstantiationException !";
			throw new AnalysisEngineProcessException(errmsg,
					new Object[] { },e);	
			//e.printStackTrace();
		} catch (IllegalAccessException e) {
			String errmsg = "Error: "+CURRENTCLASSNAME+" getObject IllegalAccessException !";
			throw new AnalysisEngineProcessException(errmsg,
					new Object[] { },e);	
			//e.printStackTrace();
		} catch (InvocationTargetException e) {
			String errmsg = "Error: "+CURRENTCLASSNAME+" getObject InvocationTargetException !";
			throw new AnalysisEngineProcessException(errmsg,
					new Object[] { },e);	
			//e.printStackTrace();
		}

		return aConstructedObject;

	}
	

	/**
	 * Cast object of a given class 
	 * Remains to cas the object
	 * Allow to know the type of the annotation to handle only at the runtime level
	 * @param annotationString
	 * @return annotationString
	 * @throws AnalysisEngineProcessException
	 */
	public static void castObjectWithAGivenClass(Class aObjectClass, Object aConstructedObject) throws AnalysisEngineProcessException {

		aObjectClass.cast(aConstructedObject);		
	}


	/*
	 * This method create an annotation and sets one feature with a String value.
	 * 
	 * @deprecated
	 * 
	 * @param aJCas
	 *            the CAS over which the process is performed
	 * @param annotationNameToCreate
	 * @param beginFeatureValue
	 * @param endFeatureValue
	 * @param featureNameToSet            
	 * @param valueFeatureValue
	 * @throws AnalysisEngineProcessException 
	
	public static void createAnnotation(JCas aJCas, String annotationNameToCreate,
			int beginFeatureValue, int endFeatureValue, String featureNameToSet, String valueFeatureValue) throws AnalysisEngineProcessException {

		// Crée une annotation générique
		// SequenceMatch sequenceMatch = new SequenceMatch(aJCas);
		// sequenceMatch.setBegin(patternHashMap.get(patternKeyString).getCurrentStartIndexCursor());
		// sequenceMatch.setEnd(tokenAnnotation.getEnd());
		// sequenceMatch.setValue(patternKeyString);
		// sequenceMatch.addToIndexes();

		// Crée une annotation prédéfinie
		// Object[] args = null;


		try {
			Object[] args = null;

			Class<Annotation> TgtClass = (Class<Annotation>) Class
			.forName(annotationNameToCreate);

			// System.out.println("Debug: ----------------------------------------------------------------------"
			// );
			// System.out.println("Debug: TgtClass.getName()	= " +
{'fraction_non_alphanumeric': 0.10342432451205112, 'fraction_numerical': 0.0011113426408279504, 'mean_word_length': 4.09303148213654, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '6199707', 'n_tokens_mistral': 4295, 'n_tokens_neox': 3830, 'n_words': 1176}