001: package org.drools.compiler;
002:
003: import java.util.ArrayList;
004: import java.util.HashMap;
005: import java.util.Iterator;
006: import java.util.List;
007: import java.util.Map;
008:
009: /**
010: * A Registry of DialectConfigurations. It is also responsible for issueing actions to all registered
011: * dialects.
012: * This Class api is subject to change.
013: *
014: */
015: public class DialectRegistry {
016: private Map map;
017:
018: public DialectRegistry() {
019: this .map = new HashMap();
020: }
021:
022: /**
023: * Add a DialectConfiguration to the registry
024: * @param name
025: * @param dialect
026: */
027: public void addDialectConfiguration(final String name,
028: final DialectConfiguration dialect) {
029: this .map.put(name, dialect);
030: }
031:
032: /**
033: * Get a DialectConfiguration for a named dialect
034: * @param name
035: * @return
036: */
037: public DialectConfiguration getDialectConfiguration(
038: final String name) {
039: return (DialectConfiguration) this .map.get(name);
040: }
041:
042: /**
043: * Initialise all registered dialects for the given PackageBuilder.
044: * @param builder
045: */
046: public void initAll(PackageBuilder builder) {
047: for (Iterator it = this .map.values().iterator(); it.hasNext();) {
048: DialectConfiguration dialect = (DialectConfiguration) it
049: .next();
050: dialect.getDialect().init(builder);
051: }
052: }
053:
054: /**
055: * Instruct all registered dialects to compile what ever they have attempted to build.
056: *
057: */
058: public void compileAll() {
059: for (Iterator it = this .map.values().iterator(); it.hasNext();) {
060: DialectConfiguration dialect = (DialectConfiguration) it
061: .next();
062: dialect.getDialect().compileAll();
063: }
064: }
065:
066: /**
067: * Return an Iterator of DialectConfigurations
068: * @return
069: */
070: public Iterator iterator() {
071: return this .map.values().iterator();
072: }
073:
074: /**
075: * Add all registered Dialect results to the provided List.
076: * @param list
077: * @return
078: */
079: public List addResults(List list) {
080: if (list == null) {
081: list = new ArrayList();
082: }
083: for (Iterator it = this .map.values().iterator(); it.hasNext();) {
084: DialectConfiguration dialect = (DialectConfiguration) it
085: .next();
086: List results = dialect.getDialect().getResults();
087: if (results != null) {
088: list.addAll(results);
089: }
090: }
091: return list;
092: }
093:
094: /**
095: * Iterates all registered dialects, informing them of an import added to the PackageBuilder
096: * @param importEntry
097: */
098: public void addImport(String importEntry) {
099: for (Iterator it = this .map.values().iterator(); it.hasNext();) {
100: DialectConfiguration dialect = (DialectConfiguration) it
101: .next();
102: dialect.getDialect().addImport(importEntry);
103: }
104: }
105:
106: /**
107: * Iterates all registered dialects, informing them of a static imports added to the PackageBuilder
108: * @param staticImportEntry
109: */
110: public void addStaticImport(String staticImportEntry) {
111: for (Iterator it = this .map.values().iterator(); it.hasNext();) {
112: DialectConfiguration dialect = (DialectConfiguration) it
113: .next();
114: dialect.getDialect().addStaticImport(staticImportEntry);
115: }
116: }
117:
118: }
|