producer XML configuration support: Producers can be set up using an XML input
[mir.git] / source / mir / generator / CompositeGeneratorLibrary.java
diff --git a/source/mir/generator/CompositeGeneratorLibrary.java b/source/mir/generator/CompositeGeneratorLibrary.java
new file mode 100755 (executable)
index 0000000..2c1a949
--- /dev/null
@@ -0,0 +1,46 @@
+package mir.generator;
+
+import java.util.*;
+
+public class CompositeGeneratorLibrary implements Generator.GeneratorLibrary {
+  private Map generatorLibraries;
+  private Generator.GeneratorLibrary defaultLibrary = null;
+  private static String LIBRARY_QUALIFIER_SEPARATOR = "::";
+
+  public CompositeGeneratorLibrary() {
+    generatorLibraries = new HashMap();
+  }
+
+  public void addLibrary(String aQualifier, Generator.GeneratorLibrary aLibrary, boolean anIsDefault) {
+    if (anIsDefault || defaultLibrary == null) {
+      defaultLibrary = aLibrary;
+    }
+
+    generatorLibraries.put(aQualifier, aLibrary);
+  }
+
+  public Generator makeGenerator(String anIdentifier) throws GeneratorExc, GeneratorFailure {
+    String qualifier;
+    String libraryName;
+    int position;
+    Generator.GeneratorLibrary library;
+
+    position = anIdentifier.indexOf( LIBRARY_QUALIFIER_SEPARATOR );
+    if (position>=0) {
+      libraryName = anIdentifier.substring(0, position);
+      qualifier = anIdentifier.substring(position + LIBRARY_QUALIFIER_SEPARATOR.length());
+
+      library = (Generator.GeneratorLibrary) generatorLibraries.get(libraryName);
+      if (library==null)
+        throw new GeneratorExc("CompositeGeneratorLibrary: library '"+libraryName+"' not found");
+
+      return library.makeGenerator(qualifier);
+    }
+    else {
+      if (defaultLibrary!=null)
+        return defaultLibrary.makeGenerator(anIdentifier);
+      else
+        throw new GeneratorExc("CompositeGeneratorLibrary: no default library speficied");
+    }
+  };
+}
\ No newline at end of file