3 import java.util.HashMap;
6 import mir.util.SimpleParser;
8 public class GeneratorLibraryRepository {
11 public GeneratorLibraryRepository() {
12 factories = new HashMap();
15 public void registerLibraryFactory(String aName, Generator.GeneratorLibraryFactory aFactory) {
16 factories.put(aName, aFactory);
19 public Generator.GeneratorLibrary constructLibrary(String aName, String aParameters) throws GeneratorExc {
20 if (!factories.containsKey(aName))
21 throw new GeneratorExc("Unknown library factory: "+aName);
23 return ((Generator.GeneratorLibraryFactory) factories.get(aName)).makeLibrary(aParameters);
26 private final static String SPACE = "[\t\n\r ]*";
27 private final static String IDENTIFIER = "[a-zA-Z_][a-zA-Z0-9_]*";
28 private final static String EQUALS = "=";
29 private final static String LEFT_PARENTHESIS = "[(]";
30 private final static String RIGHT_PARENTHESIS = "[)]";
31 private final static String FACTORY_PARAMETERS = "[^)]*";
32 private final static String SEMICOLON = ";";
34 public Generator.GeneratorLibrary constructCompositeLibrary(String aSpecification) throws GeneratorExc, GeneratorFailure {
35 //main=freemarker(path=/var/www/test); test=freemarker(path=/var/www/test2)
38 String factoryParameters;
39 CompositeGeneratorLibrary result = new CompositeGeneratorLibrary();
42 SimpleParser parser = new SimpleParser(aSpecification);
45 while (!parser.isAtEnd()) {
46 identifier = parser.parse(IDENTIFIER, "library key expected");
48 parser.parse(EQUALS, "'=' expected");
50 factory = parser.parse(IDENTIFIER, "factory name expected");
52 parser.parse(LEFT_PARENTHESIS, "'(' expected");
53 factoryParameters = parser.parse(FACTORY_PARAMETERS, "parameters expected");
54 parser.parse(RIGHT_PARENTHESIS, "')' expected");
56 result.addLibrary(identifier, constructLibrary(factory, factoryParameters), first);
60 if (!parser.isAtEnd()) {
61 parser.parse(SEMICOLON, "; expected");
66 e.printStackTrace(System.out);
67 throw new GeneratorFailure("Failed to construct generator library: " + e.getMessage(), e);