3 import java.util.HashMap;
6 import mir.log.LoggerWrapper;
7 import mir.util.SimpleParser;
9 public class GeneratorLibraryRepository {
10 private Map factories;
11 private LoggerWrapper logger;
13 public GeneratorLibraryRepository() {
14 factories = new HashMap();
15 logger = new LoggerWrapper("TemplateEngine");
18 public void registerLibraryFactory(String aName, Generator.GeneratorLibraryFactory aFactory) {
19 factories.put(aName, aFactory);
22 public Generator.GeneratorLibrary constructLibrary(String aName, String aParameters) throws GeneratorExc {
23 if (!factories.containsKey(aName))
24 throw new GeneratorExc("Unknown library factory: "+aName);
26 return ((Generator.GeneratorLibraryFactory) factories.get(aName)).makeLibrary(aParameters);
29 private final static String SPACE = "[\t\n\r ]*";
30 private final static String IDENTIFIER = "[a-zA-Z_][a-zA-Z0-9_]*";
31 private final static String EQUALS = "=";
32 private final static String LEFT_PARENTHESIS = "[(]";
33 private final static String RIGHT_PARENTHESIS = "[)]";
34 private final static String FACTORY_PARAMETERS = "[^)]*";
35 private final static String SEMICOLON = ";";
37 public Generator.GeneratorLibrary constructCompositeLibrary(String aSpecification) throws GeneratorExc, GeneratorFailure {
40 String factoryParameters;
41 CompositeGeneratorLibrary result = new CompositeGeneratorLibrary();
44 SimpleParser parser = new SimpleParser(aSpecification);
47 while (!parser.isAtEnd()) {
48 identifier = parser.parse(IDENTIFIER, "library key expected");
50 parser.parse(EQUALS, "'=' expected");
52 factory = parser.parse(IDENTIFIER, "factory name expected");
54 parser.parse(LEFT_PARENTHESIS, "'(' expected");
55 factoryParameters = parser.parse(FACTORY_PARAMETERS, "parameters expected");
56 parser.parse(RIGHT_PARENTHESIS, "')' expected");
58 result.addLibrary(identifier, constructLibrary(factory, factoryParameters), first);
62 if (!parser.isAtEnd()) {
63 parser.parse(SEMICOLON, "; expected");
68 e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
69 throw new GeneratorFailure("Failed to construct generator library: " + e.getMessage(), e);