001: package org.drools.brms.server.util;
002:
003: import java.util.ArrayList;
004: import java.util.Collections;
005: import java.util.HashMap;
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.Map;
009: import java.util.StringTokenizer;
010:
011: import org.mvel.MVEL;
012:
013: /**
014: * Use mvel to load up map/list of valid items for fields - used by the Guided rule editor.
015: */
016: public class DataEnumLoader {
017:
018: private final List errors;
019: private final Map data;
020:
021: /**
022: * This is the source of the asset, which is an MVEL map (minus the outer "[") of course.
023: */
024: public DataEnumLoader(String mvelSource) {
025: errors = new ArrayList();
026: this .data = loadEnum(mvelSource);
027: }
028:
029: private Map loadEnum(String mvelSource) {
030:
031: if (mvelSource == null || (mvelSource.trim().equals(""))) {
032: return Collections.EMPTY_MAP;
033: }
034: mvelSource = addCommasForNewLines(mvelSource);
035: final Object mvelData;
036: try {
037: mvelSource = "[ " + mvelSource + " ]";
038: mvelData = MVEL.eval(mvelSource, new HashMap());
039: } catch (RuntimeException e) {
040: addError("Unable to load enumeration data.");
041: addError(e.getMessage());
042: addError("Error type: " + e.getClass().getName());
043: return Collections.EMPTY_MAP;
044: }
045: if (!(mvelData instanceof Map)) {
046: addError("The expression is not a map, it is a "
047: + mvelData.getClass().getName());
048: return Collections.EMPTY_MAP;
049: }
050: Map map = (Map) mvelData;
051: Map newMap = new HashMap();
052: for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
053: String key = (String) iter.next();
054: Object list = map.get(key);
055: if (!(list instanceof List)) {
056: if (list == null) {
057: addError("The item with " + key + " is null.");
058: } else {
059: addError("The item with " + key
060: + " is not a list, it is a "
061: + list.getClass().getName());
062: }
063: return Collections.EMPTY_MAP;
064: }
065: List items = (List) list;
066: String[] newItems = new String[items.size()];
067: for (int i = 0; i < items.size(); i++) {
068: Object listItem = items.get(i);
069: if (!(listItem instanceof String)) {
070: newItems[i] = listItem.toString();
071: } else {
072: newItems[i] = (String) listItem;
073: }
074: }
075: newMap.put(key, newItems);
076: }
077: return newMap;
078: }
079:
080: public static String addCommasForNewLines(String mvelSource) {
081: StringTokenizer st = new StringTokenizer(mvelSource, "\r\n");
082: StringBuffer buf = new StringBuffer();
083: while (st.hasMoreTokens()) {
084: String line = st.nextToken().trim();
085: if (st.hasMoreTokens() && line.endsWith(",")) {
086: buf.append(line);
087: } else {
088: buf.append(line);
089: if (st.hasMoreTokens()) {
090: buf.append(",");
091: }
092: }
093: if (st.hasMoreTokens()) {
094: buf.append("\n");
095: }
096: }
097: return buf.toString();
098: }
099:
100: private void addError(String string) {
101: this .errors.add(string);
102: }
103:
104: /**
105: * Return a list of any errors found.
106: */
107: public List getErrors() {
108: return this .errors;
109: }
110:
111: public boolean hasErrors() {
112: return this .errors.size() > 0;
113: }
114:
115: /**
116: * Return the map of Fact.field to List (of Strings).
117: */
118: public Map getData() {
119: return this.data;
120: }
121:
122: }
|