<commit_before>package xal.tools.apputils;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;
import java.util.Set;
import java.util.prefs.AbstractPreferences;
import java.util.prefs.BackingStoreException;

public class Preferences extends java.util.prefs.AbstractPreferences {
	protected java.util.prefs.Preferences userPrefs;
	protected Properties sysPrefs;
	protected Set<String> usrKeys;
	
	protected Preferences(java.util.prefs.Preferences userPrefs, String name) {
		super(parentPrefs(userPrefs), name);
		this.userPrefs = userPrefs;
	}

	private static Preferences parentPrefs(java.util.prefs.Preferences userPrefs) {
		java.util.prefs.Preferences parentPrefs = userPrefs.parent();
		if (parentPrefs == null) return null;
		return new Preferences(parentPrefs, parentPrefs.name());
	}

	@Override
	protected AbstractPreferences childSpi(String name) {
		java.util.prefs.Preferences child = userPrefs.node(name);
		return new Preferences(child, child.name());
	}

	@Override
	protected String[] childrenNamesSpi() throws BackingStoreException {
		return userPrefs.childrenNames();
	}

	@Override
	protected void flushSpi() throws BackingStoreException {
		userPrefs.flush();
	}
	
	private boolean usrContains(String key) {
		String keys[];
		try {
			keys = userPrefs.keys();
			for (String userKey : keys)
				if (key.equals(userKey)) return true;
		} catch (BackingStoreException e) {
		}
		return false;
	}

	protected String fullName() {
		if ("".equals(name())) return "xal";
		Preferences parent = (Preferences)parent();
		return parent == null || "".equals(parent.name()) ? name() : parent.fullName() + "." + name();
	}
	
	protected Properties getSysPrefs()
	{
		if (sysPrefs == null) {
			File confFile = new File(System.getProperty("OPENXAL_CONF", "/etc/openxal") + "/" + fullName());
			/*System.out.printf("%s\n", confFile.toString());*/
			if (confFile.exists()) {
				sysPrefs = new Properties();
				try {
					sysPrefs.load(new FileReader(confFile));
				} catch (IOException e) {
				}
			}
		}
		return sysPrefs;
	}
	
	@Override
	protected String getSpi(String key) {
		if (usrContains(key) || !getSysPrefs().containsKey(key)) return userPrefs.get(key, null);
		return getSysPrefs().getProperty(key);
	}

	@Override<commit_msg>changed OPENXAL_CONFIG_DIR from java enviroment to system environment variable<commit_after>package xal.tools.apputils;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;
import java.util.Set;
import java.util.prefs.AbstractPreferences;
import java.util.prefs.BackingStoreException;

public class Preferences extends java.util.prefs.AbstractPreferences {
	protected java.util.prefs.Preferences userPrefs;
	protected Properties sysPrefs;
	protected Set<String> usrKeys;
	
	protected Preferences(java.util.prefs.Preferences userPrefs, String name) {
		super(parentPrefs(userPrefs), name);
		this.userPrefs = userPrefs;
	}

	private static Preferences parentPrefs(java.util.prefs.Preferences userPrefs) {
		java.util.prefs.Preferences parentPrefs = userPrefs.parent();
		if (parentPrefs == null) return null;
		return new Preferences(parentPrefs, parentPrefs.name());
	}

	@Override
	protected AbstractPreferences childSpi(String name) {
		java.util.prefs.Preferences child = userPrefs.node(name);
		return new Preferences(child, child.name());
	}

	@Override
	protected String[] childrenNamesSpi() throws BackingStoreException {
		return userPrefs.childrenNames();
	}

	@Override
	protected void flushSpi() throws BackingStoreException {
		userPrefs.flush();
	}
	
	private boolean usrContains(String key) {
		String keys[];
		try {
			keys = userPrefs.keys();
			for (String userKey : keys)
				if (key.equals(userKey)) return true;
		} catch (BackingStoreException e) {
		}
		return false;
	}

	protected String fullName() {
		if ("".equals(name())) return "xal";
		Preferences parent = (Preferences)parent();
		return parent == null || "".equals(parent.name()) ? name() : parent.fullName() + "." + name();
	}
	
	protected Properties getSysPrefs()
	{
		if (sysPrefs == null) {
			String confDir = System.getenv("OPENXAL_CONFIG_DIR");
			if (confDir == null) confDir = "/etc/openxal";
			File confFile = new File(confDir + "/" + fullName());
			//System.out.printf("%s\n", confFile.toString());
			if (confFile.exists()) {
				sysPrefs = new Properties();
				try {
					sysPrefs.load(new FileReader(confFile));
				} catch (IOException e) {
				}
			}
		}
		return sysPrefs;
	}
	
	@Override
	protected String getSpi(String key) {
		if (usrContains(key) || !getSysPrefs().containsKey(key)) return userPrefs.get(key, null);
		return getSysPrefs().getProperty(key);
	}

	@Override
{'fraction_non_alphanumeric': 0.128944618599791, 'fraction_numerical': 0.0, 'mean_word_length': 4.623971797884842, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '242956', 'n_tokens_mistral': 1656, 'n_tokens_neox': 1518, 'n_words': 344}