All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.siwenyan.common.MyEnvironment Maven / Gradle / Ivy

There is a newer version: 1.25
Show newest version
package com.github.siwenyan.common;

import com.github.siwenyan.dish_parser.Constants;

import java.io.*;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class MyEnvironment {

	private MapStringToObject env = new MapStringToObject();

	private MyEnvironment upperEnvironment = null;

	private String path = null;

	public Object get(String key) {
		Object object = env.get(key);
		if (object == null) {
			object = getUpper(key);
		}
		return object;
	}

	private Object getUpper(String key) {
		if (upperEnvironment != null) {
			return this.upperEnvironment.get(key);
		} else {
			return null;
		}
	}

	public Object getTheOnlyAlike(String keyRegex) {
		Set set = env.getKeySet(keyRegex);
		if (null == set || 1 != set.size()) {
			return getUpperTheOnlyAlike(keyRegex);
		}
		return env.get(set.iterator().next());
	}

	private Object getUpperTheOnlyAlike(String keyRegex) {
		if (upperEnvironment != null) {
			return this.upperEnvironment.getTheOnlyAlike(keyRegex);
		} else {
			return null;
		}
	}

	public void put(String key, Object value) {
		env.put(key, value);
	}

	public String getString(String key) {
		Object object = get(key);
		return object == null ? null : object.toString();
	}

	public String getTheOnlyAlikeString(String keyRegex) {
		Object object = getTheOnlyAlike(keyRegex);
		return object == null ? null : object.toString();
	}

	private Set getTreeSet() {
		TreeSet treeSet = new TreeSet();
		treeSet.addAll(env.keySet());

		return treeSet;
	}

	@Override
	public String toString() {
		return this.toString(".*");
	}

	public String toString(String regex) {
		String str = "";
		for (String key : getTreeSet()) {
			if (key.matches(regex)) {
				str += Sys.LBR + "    ";
				str += StringTools.padRight("\"" + key + "\"", 60, ' ');
				str += "= ";
				str += "\"" + env.get(key).toString() + "\"";
			}
		}
		return "{ set " + str + Sys.LBR + "}";

	}

	public boolean contains(String key) {
		boolean contains = env.containsKey(key);
		if (!contains && this.upperEnvironment != null) {
			contains = this.upperEnvironment.contains(key);
		}

		return contains;
	}

	// public String replaceAllVars(String text, String prefix, String suffix,
	// ReplaceMode mode) {
	// if (text == null) {
	// return null;
	// }
	// if (prefix == null) {
	// prefix = "";
	// }
	// if (suffix == null) {
	// suffix = "";
	// }
	//
	// String regex = "(" + prefix + "([^" + prefix + "^" + suffix + "]+)" +
	// suffix + ")";
	// Pattern pattern = Pattern.compile(regex);
	// Matcher matcher = pattern.matcher(text);
	// StringBuffer stringBuffer = new StringBuffer();
	//
	// while (matcher.find()) {
	// String key = matcher.group(2);
	// String replacement = this.getString(key);
	// if (null == replacement) {
	// replacement = this.getTheOnlyAlikeString(key);
	// }
	// if (null == replacement) {
	// matcher.appendReplacement(stringBuffer, matcher.group(1));
	// } else {
	// replacement = MyEscaper.escapeOne(replacement, "\\$");
	// replacement = MyEscaper.escapeOne(replacement, "\\\\");
	// matcher.appendReplacement(stringBuffer, replacement);
	// if (mode.equals(ReplaceMode.FIRST)) {
	// break;
	// }
	// }
	// }
	// matcher.appendTail(stringBuffer);
	//
	// String raw = null;
	// if (mode.equals(ReplaceMode.DEEP)) {
	// String oldText = stringBuffer.toString();
	// String newText = replaceAllVars(oldText, prefix, suffix,
	// ReplaceMode.ROUND);
	// while (!newText.equals(oldText)) {
	// oldText = newText;
	// newText = replaceAllVars(oldText, prefix, suffix, ReplaceMode.ROUND);
	// }
	// raw = newText;
	// } else {
	// raw = stringBuffer.toString();
	// }
	//
	// raw = MyEscaper.unescapeOne(raw, "\\$");
	// raw = MyEscaper.unescapeOne(raw, "\\\\");
	//
	// return raw;
	// }

	public String replaceVariables(String text) {
		String text0 = text;
		String text1 = this.replaceVariables1(text0, Constants.SYMBOL_VAR_DELIMITER);
		while (!text1.equals(text0)) {
			text0 = text1;
			text1 = this.replaceVariables1(text0, Constants.SYMBOL_VAR_DELIMITER);
		}

		return text1;
	}

	private String replaceVariables1(String s, String delimiter) {
		if (null == delimiter) {
			delimiter = Constants.SYMBOL_VAR_DELIMITER;
		}

		int p0 = s.indexOf(delimiter);
		int p1 = s.indexOf(delimiter, p0 + 1);
		while (p0 >= 0 && p1 > p0) {
			String vn = s.substring(p0 + 1, p1);
			if (vn.isEmpty()) {
				s = s.replaceFirst(Constants.SYMBOL_VAR_DELIMITER, "%");
			} else {
				String vv = this.contains(vn) ? this.getString(vn) : "%" + vn + Constants.SYMBOL_VAR_DELIMITER;
				s = s.replace(delimiter + vn + delimiter, vv);
			}

			p0 = s.indexOf(delimiter);
			p1 = s.indexOf(delimiter, p0 + 1);
		}

		return s.replaceAll("%", Constants.SYMBOL_VAR_DELIMITER);
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getPath() {
		return this.path;
	}

	public void setUpperPath(String path) {
		if (this.upperEnvironment != null) {
			this.upperEnvironment.setPath(path);
		}
	}

	public void write() throws FileNotFoundException, IOException {
		write(new FileOutputStream(getPath()));
	}

	private void write(FileOutputStream fileOutputStream) throws IOException {
		try {
			XStreamUtil.write(env, fileOutputStream);
		} catch (UnsupportedEncodingException e) {
			throw new IOException(e);
		}
	}

	public void read() throws FileNotFoundException {
		read(new FileInputStream(getPath()));
	}

	private void read(FileInputStream fileInputStream) {
		@SuppressWarnings("unchecked")
		Map newEnv = (Map) XStreamUtil.read(fileInputStream);

		env.putAll(newEnv);
	}

	public void setUpperEnvironment(MyEnvironment upperEnvironment) {
		this.upperEnvironment = upperEnvironment;
	}

	public MyEnvironment getUpperEnvironment() {
		return this.upperEnvironment;
	}

	public void clear() {
		this.env.clear();
	}

	public void clear(String regex) {
		Set tbr = new HashSet();
		for (String key : this.env.keySet()) {
			if (key.matches(regex)) {
				tbr.add(key);
			}
		}
		for (String key : tbr) {
			this.env.remove(key);
		}
	}

	public void putAll(MyEnvironment src) {
		this.env.putAll(src.env);
	}

	public Set names() {
		return this.env.keySet();
	}

	public String toJson() {
		return EasyJson.prettyJson(this.env);
	}

}