001: package org.drools.decisiontable.parser;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.LinkedList;
020: import java.util.List;
021: import java.util.StringTokenizer;
022:
023: import org.drools.decisiontable.model.Global;
024: import org.drools.decisiontable.model.Import;
025:
026: /**
027: * @author <a href="mailto:michael.neale@gmail.com"> Michael Neale </a>
028: *
029: * Parking lot for utility methods that don't belong anywhere else.
030: */
031: public class RuleSheetParserUtil {
032:
033: private RuleSheetParserUtil() {
034: // strictly util
035: }
036:
037: public static String getRuleName(final String ruleRow) {
038: final int left = ruleRow
039: .indexOf(DefaultRuleSheetListener.RULE_TABLE_TAG);
040:
041: if (ruleRow.indexOf('(') > -1 || ruleRow.indexOf(')') > -1) {
042: invalidRuleTableDef(ruleRow);
043: }
044: return ruleRow
045: .substring(
046: left
047: + DefaultRuleSheetListener.RULE_TABLE_TAG
048: .length()).trim();
049: }
050:
051: private static void invalidRuleTableDef(final String ruleRow) {
052: throw new IllegalArgumentException(
053: "Invalid rule table header cell. Should be in the format of 'RuleTable YourRuleName'. "
054: + "It was: \n [" + ruleRow + "] \n");
055: }
056:
057: /**
058: *
059: * @param importCell
060: * The cell text for all the classes to import.
061: * @return A list of Import classes, which can be added to the ruleset.
062: */
063: public static List getImportList(final String importCell) {
064: final List importList = new LinkedList();
065: if (importCell == null) {
066: return importList;
067: }
068: final StringTokenizer tokens = new StringTokenizer(importCell,
069: ",");
070: while (tokens.hasMoreTokens()) {
071: final Import imp = new Import();
072: imp.setClassName(tokens.nextToken().trim());
073: importList.add(imp);
074: }
075: return importList;
076: }
077:
078: /**
079: * 08 - 18 - 2005
080: * Ricardo Rojas
081: * @param variableCell
082: * The cell text for all the application data variables to set.
083: * @return A list of Variable classes, which can be added to the ruleset.
084: */
085: public static List getVariableList(final String variableCell) {
086: final List variableList = new LinkedList();
087: if (variableCell == null) {
088: return variableList;
089: }
090: final StringTokenizer tokens = new StringTokenizer(
091: variableCell, ",");
092: while (tokens.hasMoreTokens()) {
093: final String token = tokens.nextToken();
094: final Global vars = new Global();
095: final StringTokenizer paramTokens = new StringTokenizer(
096: token, " ");
097: vars.setClassName(paramTokens.nextToken());
098: if (!paramTokens.hasMoreTokens()) {
099: throw new DecisionTableParseException(
100: "The format for global variables is incorrect. "
101: + "It should be: [Class name, Class otherName]. But it was: ["
102: + variableCell + "]");
103: }
104: vars.setIdentifier(paramTokens.nextToken());
105: variableList.add(vars);
106: }
107: return variableList;
108: }
109:
110: /**
111: * @return true is the String could possibly mean true. False otherwise !
112: */
113: public static boolean isStringMeaningTrue(String property) {
114: if (property == null) {
115: return false;
116: } else {
117: property = property.trim();
118: if (property.equalsIgnoreCase("true")) {
119: return true;
120: } else if (property.startsWith("Y")) {
121: return true;
122: } else if (property.startsWith("y")) {
123: return true;
124: } else if (property.equalsIgnoreCase("on")) {
125: return true;
126: } else {
127: return false;
128: }
129: }
130: }
131:
132: }
|