001: package org.drools.brms.server.util;
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.io.BufferedReader;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.InputStreamReader;
023: import java.util.ArrayList;
024: import java.util.List;
025: import java.util.StringTokenizer;
026:
027: import org.drools.compiler.DrlParser;
028: import org.drools.compiler.DroolsParserException;
029: import org.drools.lang.descr.FunctionDescr;
030: import org.drools.lang.descr.RuleDescr;
031:
032: /**
033: * This class imports legacy DRL into a structure suitable for storing more
034: * normalised in the repository.
035: *
036: * @author Michael Neale
037: */
038: public class ClassicDRLImporter {
039:
040: private String source;
041:
042: private String packageName;
043:
044: private List<Rule> rules = new ArrayList<Rule>();
045:
046: private StringBuffer header;
047:
048: private boolean usesDSL;
049:
050: public ClassicDRLImporter(InputStream in) throws IOException,
051: DroolsParserException {
052: String line = "";
053: StringBuffer drl = new StringBuffer();
054: BufferedReader reader = new BufferedReader(
055: new InputStreamReader(in));
056: while ((line = reader.readLine()) != null) {
057: drl.append("\n" + line);
058: }
059: this .source = drl.toString();
060:
061: parse();
062: }
063:
064: private void parse() throws DroolsParserException {
065: StringTokenizer lines = new StringTokenizer(source, "\r\n");
066:
067: header = new StringBuffer();
068:
069: while (lines.hasMoreTokens()) {
070: String line = lines.nextToken().trim();
071: if (line.startsWith("package")) {
072: packageName = getPackage(line);
073: } else if (line.startsWith("rule")) {
074: String ruleName = getRuleName(line);
075: StringBuffer currentRule = new StringBuffer();
076: laConsumeToEnd(lines, currentRule);
077: addRule(ruleName, currentRule);
078:
079: } else if (line.startsWith("expander")) {
080: usesDSL = true;
081: } else {
082:
083: header.append(line);
084: header.append("\n");
085: }
086: }
087: }
088:
089: private void laConsumeToEnd(StringTokenizer lines,
090: StringBuffer currentRule) {
091: String line;
092: while (true && lines.hasMoreTokens()) {
093: line = lines.nextToken();
094: if (line.trim().startsWith("end")) {
095: break;
096: }
097: currentRule.append(line);
098: currentRule.append("\n");
099: }
100: }
101:
102: private void addRule(String ruleName, StringBuffer currentRule) {
103: this .rules.add(new Rule(ruleName, currentRule.toString()));
104: }
105:
106: private String getRuleName(String line)
107: throws DroolsParserException {
108: DrlParser parser = new DrlParser();
109: RuleDescr rule = (RuleDescr) parser.parse(line).getRules().get(
110: 0);
111: return rule.getName();
112: }
113:
114: private String getPackage(String line) throws DroolsParserException {
115: DrlParser parser = new DrlParser();
116: return parser.parse(line).getName();
117:
118: }
119:
120: public List<Rule> getRules() {
121: return this .rules;
122: }
123:
124: public String getPackageName() {
125: return this .packageName;
126: }
127:
128: public String getPackageHeader() {
129: return this .header.toString();
130: }
131:
132: public boolean isDSLEnabled() {
133: return this .usesDSL;
134: }
135:
136: /**
137: * Holds a rule to import. The content does not include the "end".
138: *
139: * @author Michael Neale
140: */
141: public static class Rule {
142:
143: public Rule(String name, String content) {
144: this .name = name;
145: this .content = content;
146: }
147:
148: public String name;
149: public String content;
150: }
151:
152: }
|