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: import java.io.BufferedReader;
019: import java.io.IOException;
020: import java.io.StringReader;
021: import java.util.ArrayList;
022: import java.util.List;
023: import java.util.regex.Matcher;
024: import java.util.regex.Pattern;
025:
026: import org.drools.util.StringUtils;
027:
028: /**
029: * @author <a href="mailto:stevearoonie@gmail.com">Steven Williams</a>
030: *
031: * A rule template made up of a name, the decision table columns required,
032: * the decision table columns that must be empty and the contents of the
033: * rule.
034: */
035: public class RuleTemplate {
036: private String name;
037: private String contents;
038: private List columns;
039: private List notColumns;
040:
041: public RuleTemplate(final String n) {
042: name = n;
043: columns = new ArrayList();
044: notColumns = new ArrayList();
045: }
046:
047: public String getName() {
048: return name;
049: }
050:
051: public List getColumns() {
052: return columns;
053: }
054:
055: public String[] getNotColumns() {
056: return (String[]) notColumns.toArray(new String[notColumns
057: .size()]);
058: }
059:
060: public String getContents() {
061: return contents;
062: }
063:
064: public void addColumn(String column) {
065: if (column.startsWith("!")) {
066: this .notColumns.add(column.substring(1));
067: } else {
068: this .columns.add(column);
069: }
070: }
071:
072: public void setContents(String contents) {
073: this .contents = replaceOptionals(contents);
074: }
075:
076: /**
077: * @see java.lang.Object#toString()
078: */
079: public String toString() {
080: return "RuleTemplate[name," + this .name + "notColumns,"
081: + this .notColumns + "contents," + this .columns
082: + "columns";
083: }
084:
085: /*
086: * Replace the optional columns in the rule contents with an if statement.
087: * if (column is empty) do not show the line.
088: */
089: private String replaceOptionals(String contents) {
090: try {
091: final Pattern pattern = Pattern.compile("@\\{(.[^}]*)\\}");
092: final List columns = new ArrayList(getColumns());
093: columns.add("row.rowNumber");
094: final BufferedReader reader = new BufferedReader(
095: new StringReader(contents));
096: String line = null;
097: final StringBuffer newLine = new StringBuffer();
098: while ((line = reader.readLine()) != null) {
099: final Matcher matcher = pattern.matcher(line);
100: int optCols = 0;
101: while (matcher.find()) {
102: final String c = matcher.group(1);
103: if (!columns.contains(c)) {
104: newLine.append("@if{").append(matcher.group(1))
105: .append(" != null} ");
106: optCols++;
107: }
108: }
109: newLine.append(line);
110: newLine.append(StringUtils.repeat(" @end{}", optCols));
111: newLine.append("\n");
112: }
113: // System.out.println("newLine: " + newLine);
114: return newLine.toString();
115:
116: } catch (IOException e) {
117: throw new RuntimeException(e);
118: }
119: }
120:
121: }
|