001: package org.drools.decisiontable;
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.InputStream;
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.drools.decisiontable.parser.DecisionTableParser;
026: import org.drools.decisiontable.parser.ExternalSheetListener;
027: import org.drools.decisiontable.parser.DefaultTemplateContainer;
028: import org.drools.decisiontable.parser.TemplateContainer;
029: import org.drools.decisiontable.parser.xls.ExcelParser;
030:
031: public class ExternalSpreadsheetCompiler {
032:
033: public String compile(final String xls, final String template,
034: int startRow, int startCol) {
035: return compile(xls, template, InputType.XLS, startRow, startCol);
036:
037: }
038:
039: public String compile(final String xls, final String template,
040: InputType type, int startRow, int startCol) {
041: final InputStream xlsStream = this .getClass()
042: .getResourceAsStream(xls);
043: final InputStream templateStream = this .getClass()
044: .getResourceAsStream(template);
045: return compile(xlsStream, templateStream, type, startRow,
046: startCol);
047:
048: }
049:
050: public String compile(final String xls, final String worksheetName,
051: final String template, int startRow, int startCol) {
052: final InputStream xlsStream = this .getClass()
053: .getResourceAsStream(xls);
054: final InputStream templateStream = this .getClass()
055: .getResourceAsStream(template);
056: return compile(xlsStream, worksheetName, templateStream,
057: startRow, startCol);
058:
059: }
060:
061: public String compile(final InputStream xlsStream,
062: final InputStream templateStream, InputType type,
063: int startRow, int startCol) {
064: TemplateContainer tc = new DefaultTemplateContainer(
065: templateStream);
066: closeStream(templateStream);
067: return compile(xlsStream, type, new ExternalSheetListener(
068: startRow, startCol, tc));
069: }
070:
071: public String compile(final InputStream xlsStream,
072: final String worksheetName,
073: final InputStream templateStream, int startRow, int startCol) {
074: TemplateContainer tc = new DefaultTemplateContainer(
075: templateStream);
076: closeStream(templateStream);
077: return compile(xlsStream, worksheetName,
078: new ExternalSheetListener(startRow, startCol, tc));
079: }
080:
081: public void compile(final String xls, InputType type,
082: final List listeners) {
083: final InputStream xlsStream = this .getClass()
084: .getResourceAsStream(xls);
085: compile(xlsStream, type, listeners);
086: }
087:
088: public void compile(final String xls, final Map listeners) {
089: final InputStream xlsStream = this .getClass()
090: .getResourceAsStream(xls);
091: compile(xlsStream, listeners);
092: }
093:
094: public void compile(final InputStream xlsStream, InputType type,
095: final List listeners) {
096: final DecisionTableParser parser = type.createParser(listeners);
097: parser.parseFile(xlsStream);
098: closeStream(xlsStream);
099: }
100:
101: public void compile(final InputStream xlsStream, final Map listeners) {
102: final DecisionTableParser parser = new ExcelParser(listeners);
103: parser.parseFile(xlsStream);
104: closeStream(xlsStream);
105: }
106:
107: /**
108: * Generates DRL from the input stream containing the spreadsheet.
109: *
110: * @param xlsStream
111: * The stream to the spreadsheet. Uses the first worksheet found
112: * for the decision tables, ignores others.
113: * @param type
114: * The type of the file - InputType.CSV or InputType.XLS
115: * @param listener
116: * @return DRL xml, ready for use in drools.
117: * @throws IOException
118: */
119: public String compile(final InputStream xlsStream,
120: final InputType type, final ExternalSheetListener listener) {
121: List listeners = new ArrayList();
122: listeners.add(listener);
123: compile(xlsStream, type, listeners);
124: return listener.renderDRL();
125: }
126:
127: public String compile(final InputStream xlsStream,
128: final String worksheetName,
129: final ExternalSheetListener listener) {
130: Map listeners = new HashMap();
131: List l = new ArrayList();
132: l.add(listener);
133: listeners.put(worksheetName, l);
134: compile(xlsStream, listeners);
135: return listener.renderDRL();
136: }
137:
138: private void closeStream(final InputStream stream) {
139: try {
140: stream.close();
141: } catch (final Exception e) {
142: System.err.print("WARNING: Wasn't able to "
143: + "correctly close stream for decision table. "
144: + e.getMessage());
145: }
146: }
147:
148: }
|