<commit_before>package org.strategoxt.lang;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.spoofax.interpreter.adapter.aterm.ATermConverter;
import org.spoofax.interpreter.terms.IStrategoTerm;
import org.spoofax.interpreter.terms.ITermFactory;
import org.spoofax.terms.LazyTerm;

import aterm.ATerm;
import aterm.ATermFactory;

/**
 * @author <NAME> <lennart add lclnet.nl>
 */
public class ImportTerm extends LazyTerm {
	
	private final ITermFactory factory;
	
	private final String path, name;
	
	private final Class<?> container;
	
	private ATermFactory atermFactory;
	
	private ATerm aterm;
	
	private ZipFile lastZipFile;
	
	public ImportTerm(ITermFactory factory, Class<?> container, String path, String name) {
		assert path.endsWith("/");
		this.factory = factory;
		this.container = container;
		this.path = path;
		this.name = name;
	}
	
	public ATerm getATerm(ATermFactory factory) {
		if (aterm == null) {
			aterm = initATerm(factory);
			atermFactory = factory;
		} else if (factory != atermFactory) {
			try {
				aterm = factory.importTerm(aterm);
			} catch (RuntimeException e) {
				 // factory.importTerm(aterm) may not be implemented, try again...
				aterm = initATerm(factory);
			}
			atermFactory = factory;
		}
		return aterm;
	}
	
	@Override
	protected IStrategoTerm init() {
		if (aterm != null)
			return new ATermConverter(aterm.getFactory(), factory, true).convert(aterm);
		
		InputStream stream = openStream();
		try {
			IStrategoTerm result = ((org.spoofax.interpreter.terms.io.IFileTermFactory) factory).parseFromStream(stream);
			return result;
		} catch (java.io.IOException e) {
			throw new StrategoException(container.getSimpleName()
					+ ": Could not read imported term file " + name, e);
		} catch (RuntimeException e) {
			throw new StrategoException(container.getSimpleName()
					+ ": Could not read imported term file " + name, e);
		} finally {
			try {
				if (stream != null)
					stream.close();
				if (lastZipFile != null)
					lastZipFile.close();
				lastZipFile = null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private ATerm initATerm(ATermFactory factory) {
		if (getWrapped(true) != null)
			return new ATermConverter(factory, this.factory, false).convert(getWrapped());
		InputStream stream = null;
		try {
			stream = openStream();
			return factory.readFromFile(stream);
		} catch (java.io.IOException e) {
			throw new StrategoException(container.getSimpleName()
					+ ": Could not read imported term file " + name, e);
		} catch (RuntimeException e) {<commit_msg>- jsglr now uses org.spoofax.terms terms with token/sort attachments - various changes to the term library<commit_after>package org.strategoxt.lang;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.spoofax.interpreter.adapter.aterm.ATermConverter;
import org.spoofax.interpreter.terms.IStrategoTerm;
import org.spoofax.interpreter.terms.ITermFactory;
import org.spoofax.terms.LazyTerm;
import org.spoofax.terms.io.TermReader;

import aterm.ATerm;
import aterm.ATermFactory;

/**
 * @author <NAME> <<EMAIL> add lclnet.nl>
 */
public class ImportTerm extends LazyTerm {
	
	private final ITermFactory factory;
	
	private final String path, name;
	
	private final Class<?> container;
	
	private ATermFactory atermFactory;
	
	private ATerm aterm;
	
	private ZipFile lastZipFile;
	
	public ImportTerm(ITermFactory factory, Class<?> container, String path, String name) {
		assert path.endsWith("/");
		this.factory = factory;
		this.container = container;
		this.path = path;
		this.name = name;
	}
	
	public ATerm getATerm(ATermFactory factory) {
		if (aterm == null) {
			aterm = initATerm(factory);
			atermFactory = factory;
		} else if (factory != atermFactory) {
			try {
				aterm = factory.importTerm(aterm);
			} catch (RuntimeException e) {
				 // factory.importTerm(aterm) may not be implemented, try again...
				aterm = initATerm(factory);
			}
			atermFactory = factory;
		}
		return aterm;
	}
	
	@Override
	protected IStrategoTerm init() {
		if (aterm != null)
			return new ATermConverter(aterm.getFactory(), factory, true).convert(aterm);
		
		InputStream stream = openStream();
		try {
			IStrategoTerm result = new TermReader(factory).parseFromStream(stream);
			return result;
		} catch (java.io.IOException e) {
			throw new StrategoException(container.getSimpleName()
					+ ": Could not read imported term file " + name, e);
		} catch (RuntimeException e) {
			throw new StrategoException(container.getSimpleName()
					+ ": Could not read imported term file " + name, e);
		} finally {
			try {
				if (stream != null)
					stream.close();
				if (lastZipFile != null)
					lastZipFile.close();
				lastZipFile = null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private ATerm initATerm(ATermFactory factory) {
		if (getWrapped(true) != null)
			return new ATermConverter(factory, this.factory, false).convert(getWrapped());
		InputStream stream = null;
		try {
			stream = openStream();
			return factory.readFromFile(stream);
		} catch (java.io.IOException e) {
			throw new StrategoException(container.getSimpleName()
					+ ": Could not read imported term file " + name, e);
		} catch (RuntimeException e) {
{'fraction_non_alphanumeric': 0.11408167002380516, 'fraction_numerical': 0.0, 'mean_word_length': 3.903052064631957, 'pattern_counts': {'":': 6, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2644077', 'n_tokens_mistral': 1987, 'n_tokens_neox': 1711, 'n_words': 444}