merge of localization branch into HEAD. mh and zap
[mir.git] / source / mir / generator / CompositeGeneratorLibrary.java
1 package mir.generator;
2
3 import java.util.*;
4
5 public class CompositeGeneratorLibrary implements Generator.GeneratorLibrary {
6   private Map generatorLibraries;
7   private Generator.GeneratorLibrary defaultLibrary = null;
8   private static String LIBRARY_QUALIFIER_SEPARATOR = "::";
9
10   public CompositeGeneratorLibrary() {
11     generatorLibraries = new HashMap();
12   }
13
14   public void addLibrary(String aQualifier, Generator.GeneratorLibrary aLibrary, boolean anIsDefault) {
15     if (anIsDefault || defaultLibrary == null) {
16       defaultLibrary = aLibrary;
17     }
18
19     generatorLibraries.put(aQualifier, aLibrary);
20   }
21
22   public Generator makeGenerator(String anIdentifier) throws GeneratorExc, GeneratorFailure {
23     String qualifier;
24     String libraryName;
25     int position;
26     Generator.GeneratorLibrary library;
27
28     position = anIdentifier.indexOf( LIBRARY_QUALIFIER_SEPARATOR );
29     if (position>=0) {
30       libraryName = anIdentifier.substring(0, position);
31       qualifier = anIdentifier.substring(position + LIBRARY_QUALIFIER_SEPARATOR.length());
32
33       library = (Generator.GeneratorLibrary) generatorLibraries.get(libraryName);
34       if (library==null)
35         throw new GeneratorExc("CompositeGeneratorLibrary: library '"+libraryName+"' not found");
36
37       return library.makeGenerator(qualifier);
38     }
39     else {
40       if (defaultLibrary!=null)
41         return defaultLibrary.makeGenerator(anIdentifier);
42       else
43         throw new GeneratorExc("CompositeGeneratorLibrary: no default library speficied");
44     }
45   };
46 }