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.InputStream;
021: import java.io.InputStreamReader;
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.List;
025: import java.util.Map;
026:
027: /**
028: *
029: * @author <a href="mailto:stevearoonie@gmail.com">Steven Williams</a>
030: *
031: * Container for a set of templates (residing in one file).
032: * This class will parse the template file.
033: *
034: */
035: public class DefaultTemplateContainer implements TemplateContainer {
036: private String header;
037:
038: private List columns = new ArrayList();
039:
040: private Map templates = new HashMap();
041:
042: public DefaultTemplateContainer(final String template) {
043: this (DefaultTemplateContainer.class
044: .getResourceAsStream(template));
045: }
046:
047: public DefaultTemplateContainer(final InputStream templateStream) {
048: parseTemplate(templateStream);
049: validateTemplate();
050: }
051:
052: private void validateTemplate() {
053: if (columns.size() == 0) {
054: throw new DecisionTableParseException(
055: "Missing header columns");
056: }
057: if (templates.size() == 0) {
058: throw new DecisionTableParseException("Missing templates");
059: }
060:
061: }
062:
063: private void parseTemplate(final InputStream templateStream) {
064: try {
065: final ColumnFactory cf = new ColumnFactory();
066: final BufferedReader templateReader = new BufferedReader(
067: new InputStreamReader(templateStream));
068: String line = null;
069: StringBuffer header = new StringBuffer();
070: boolean inTemplate = false;
071: boolean inHeader = false;
072: boolean inContents = false;
073: RuleTemplate template = null;
074: StringBuffer contents = new StringBuffer();
075: while ((line = templateReader.readLine()) != null) {
076: if (line.trim().length() > 0) {
077: if (line.startsWith("template header")) {
078: inHeader = true;
079: } else if (line.startsWith("template")) {
080: inTemplate = true;
081: String quotedName = line.substring(8).trim();
082: quotedName = quotedName.substring(1, quotedName
083: .length() - 1);
084: template = new RuleTemplate(quotedName);
085: addTemplate(template);
086:
087: } else if (line.startsWith("package")) {
088: if (inHeader == false) {
089: throw new DecisionTableParseException(
090: "Missing header");
091: }
092: inHeader = false;
093: header.append(line).append("\n");
094: } else if (inHeader) {
095: addColumn(cf.getColumn(line.trim()));
096: } else if (!inTemplate && !inHeader) {
097: header.append(line).append("\n");
098: } else if (!inContents && line.startsWith("rule")) {
099: inContents = true;
100: contents.append(line).append("\n");
101: } else if (line.equals("end template")) {
102: template.setContents(contents.toString());
103: contents.setLength(0);
104: inTemplate = false;
105: inContents = false;
106: } else if (inContents) {
107: contents.append(line).append("\n");
108: } else if (inTemplate) {
109: template.addColumn(line.trim());
110: }
111: }
112:
113: }
114: if (inTemplate) {
115: throw new DecisionTableParseException(
116: "Missing end template");
117: }
118: this .header = header.toString();
119:
120: } catch (IOException e) {
121: throw new RuntimeException(e);
122: } finally {
123: if (templateStream != null)
124: closeStream(templateStream);
125: }
126: }
127:
128: private void addTemplate(RuleTemplate template) {
129: templates.put(template.getName(), template);
130: }
131:
132: /*
133: * (non-Javadoc)
134: *
135: * @see org.drools.decisiontable.parser.TemplateContainer#getTemplates()
136: */
137: public Map getTemplates() {
138: return templates;
139: }
140:
141: private void addColumn(Column c) {
142: columns.add(c);
143: }
144:
145: /*
146: * (non-Javadoc)
147: *
148: * @see org.drools.decisiontable.parser.TemplateContainer#getColumns()
149: */
150: public Column[] getColumns() {
151: return (Column[]) columns.toArray(new Column[columns.size()]);
152: }
153:
154: /*
155: * (non-Javadoc)
156: *
157: * @see org.drools.decisiontable.parser.TemplateContainer#getHeader()
158: */
159: public String getHeader() {
160: return header;
161: }
162:
163: private void closeStream(final InputStream stream) {
164: try {
165: stream.close();
166: } catch (final Exception e) {
167: System.err.print("WARNING: Wasn't able to "
168: + "correctly close stream for decision table. "
169: + e.getMessage());
170: }
171: }
172: }
|