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

org.gramar.plugin.PluginSource Maven / Gradle / Ivy

package org.gramar.plugin;

import java.util.HashMap;

import org.gramar.IFileStore;
import org.gramar.IGramar;
import org.gramar.IPluginSource;
import org.gramar.ITemplatingExtension;
import org.gramar.exception.InvalidGramarException;
import org.gramar.exception.NoSuchFileStoreException;
import org.gramar.exception.NoSuchGramarException;


public abstract class PluginSource implements IPluginSource {

	protected boolean autoRefresh = true;
	private HashMap cache = null;
	private HashMap knownPatterns = new HashMap();
	
	public PluginSource() {

	}
	
	public void gather() {
		if ((cache==null) || autoRefresh) {
			HashMap map = new HashMap();
			gather(map);
			cache = map;
		}
	}

	public void gather(HashMap map) {
		for (String id: knownPatterns.keySet()) {
			map.put(id, knownPatterns.get(id));
		}
	}

	@Override
	public String[] list() {
		gather();
		String result[] = new String[cache.size()];
		cache.keySet().toArray(result);
		return result;
	}

	@Override
	public IGramar[] gramars() {
		gather();
		IGramar result[] = new IGramar[cache.size()];
		cache.values().toArray(result);
		return result;
	}

	@Override
	public IGramar getGramar(String gramarId) throws NoSuchGramarException, InvalidGramarException {
		gather();
		IGramar pattern = cache.get(gramarId);
		if (pattern == null) {
			throw new NoSuchGramarException();
		}
		return pattern;
	}

	@Override
	public void addKnownGramar(String id) throws NoSuchGramarException, InvalidGramarException {
		knownPatterns.put(id, getGramar(id));
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy