initial load
[DTRules.git] / ExcelUtil / src / main / java / excel / util / Excel2XML.java
blobd97127a9a71c81e013e9aea586218105cd06ff72
1 /*
2 * Copyright 2004-2007 MTBJ, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package excel.util;
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileNotFoundException;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.io.PrintStream;
26 import java.nio.channels.FileChannel;
27 import java.util.Iterator;
28 import java.util.List;
30 import com.dtrules.compiler.cup.Compiler;
31 import com.dtrules.compiler.decisiontables.DTCompiler;
32 import com.dtrules.decisiontables.RDecisionTable;
33 import com.dtrules.decisiontables.DTNode.Coordinate;
34 import com.dtrules.entity.IREntity;
35 import com.dtrules.infrastructure.RulesException;
36 import com.dtrules.interpreter.RName;
37 import com.dtrules.mapping.MapGenerator;
38 import com.dtrules.session.EntityFactory;
39 import com.dtrules.session.ICompiler;
40 import com.dtrules.session.ICompilerError;
41 import com.dtrules.session.IRSession;
42 import com.dtrules.session.RSession;
43 import com.dtrules.session.RuleSet;
44 import com.dtrules.session.RulesDirectory;
46 public class Excel2XML {
48 private RuleSet ruleSet;
49 private final String UDTFilename = "Uncompiled_DecisionTables.xml";
50 String path;
51 String rulesDirectoryXML;
52 String ruleset;
53 DTCompiler dtcompiler = null;
55 /**
56 * Returns the ruleSet being used by this compiler
57 * @return
59 public RuleSet getRuleSet() {
60 return ruleSet;
63 /**
64 * We assume that the Rule Set provided gives us all the parameters needed
65 * to open a set of Excel Files and convert them into XML files.
66 * @param propertyfile
68 public Excel2XML(RuleSet ruleSet) {
69 this.ruleSet = ruleSet;
72 /**
73 * Opens the RulesDirectory for the caller.
74 * @param rulesDirectoryXML
75 * @param ruleset
77 public Excel2XML(String path, String rulesDirectoryXML, String ruleset){
78 this.path = path;
79 this.rulesDirectoryXML = rulesDirectoryXML;
80 this.ruleset = ruleset;
81 reset();
84 /**
85 * We reset the Rules Directory after we have modified the XML used to
86 * define the Rules Directory.
88 public void reset (){
89 RulesDirectory rd = new RulesDirectory(path,rulesDirectoryXML);
90 this.ruleSet = rd.getRuleSet(ruleset);
93 /**
94 * Return the Last DecisionTable Compiler used. Null if none exists.
96 * @return Return the Last DecisionTable Compiler used.
98 public DTCompiler getDTCompiler(){
99 return dtcompiler;
102 * Converts the RuleSet
103 * @throws Exception
105 public void convertRuleset() throws Exception {
106 ImportRuleSets dt = new ImportRuleSets();
107 dt.convertEDD (ruleSet.getSystemPath()+"/"+ruleSet.getExcel_edd(), ruleSet.getFilepath()+ruleSet.getEDD_XMLName());
108 dt.convertDecisionTables(ruleSet.getSystemPath()+"/"+ruleSet.getExcel_dtfolder(),ruleSet.getFilepath()+UDTFilename);
109 copyFile(ruleSet.getFilepath()+UDTFilename,ruleSet.getFilepath()+ruleSet.getDT_XMLName());
110 reset();
114 * Copy a file
115 * Throws a runtime exception if anything goes wrong.
117 public static void copyFile(String file1, String file2) {
118 FileChannel inChannel = null;
119 FileChannel outChannel = null;
120 try{
121 inChannel = new FileInputStream(file1).getChannel();
122 outChannel = new FileOutputStream(file2).getChannel();
123 try {
124 // Stupid fix for Windows -- 64Mb - 1024 bytes to avoid a bug.
125 int blksize = (64 * 1024 * 1024)-1024;
126 long size = inChannel.size();
127 long position = 0;
128 while (position < size) {
129 position += inChannel.transferTo(position, blksize, outChannel);
131 inChannel.close();
132 outChannel.close();
133 } catch (IOException e) {
134 throw new RuntimeException(e.getMessage());
136 }catch(FileNotFoundException e) {
137 throw new RuntimeException(e);
142 * Compiles the RuleSet
143 * @param NumErrorsToReport How many errors to print
144 * @param err The output stream which to print the errors
146 public void compile(int NumErrorsToReport, PrintStream err) {
148 try {
149 IRSession session = new RSession(ruleSet);
150 Compiler compiler = new Compiler(session);
151 dtcompiler = new DTCompiler(compiler);
153 InputStream inDTStream = new FileInputStream(ruleSet.getFilepath()+"/"+UDTFilename);
154 OutputStream outDTStream = new FileOutputStream(ruleSet.getFilepath()+"/"+ruleSet.getDT_XMLName());
156 dtcompiler.compile(inDTStream, outDTStream);
158 RulesDirectory rd = new RulesDirectory(path, rulesDirectoryXML);
159 RuleSet rs = rd.getRuleSet(RName.getRName(ruleset));
160 EntityFactory ef = rs.newSession().getEntityFactory();
161 IREntity dt = ef.getDecisiontables();
162 Iterator<RName> idt = ef.getDecisionTableRNameIterator();
164 while(idt.hasNext()){
165 RDecisionTable t = (RDecisionTable) dt.get(idt.next());
166 List<ICompilerError> errs = t.compile();
167 Coordinate err_RowCol = t.validate();
168 if(!t.isCompiled() || err_RowCol!=null){
169 int column = 0;
170 int row = 0;
171 if(err_RowCol!=null){
172 row = err_RowCol.getRow();
173 column = err_RowCol.getCol();
175 dtcompiler.logError(
176 t.getName().stringValue(),
177 t.getFilename(),
178 "validity check",
179 "Decision Table did not compile",
181 "A problem may have been found on row "+row+" and column "+column);
185 dtcompiler.printErrors(err, NumErrorsToReport);
186 err.println("Total Errors Found: "+dtcompiler.getErrors().size());
188 } catch (Exception e) {
189 err.print(e);
194 * Generates a default Mapping File. This file is not complete, but
195 * requires adjustments to match XML tags if they differ from the
196 * Attribute names used in the EDD. Also, the initial Entity Stack and
197 * frequence of Entity types must be examined and updated.
199 * @param mapping Tag for the mapping file. Attributes with this tag in the
200 * EDD will be mapped in the generated mapping
201 * @param filename File name for the mapping file generated
202 * @throws Exception
204 public void generateMap(String mapping, String filename) throws Exception {
205 RuleSet rs = getRuleSet();
206 MapGenerator mgen = new MapGenerator();
207 mgen.generateMapping(
208 mapping,
209 rs.getFilepath()+rs.getEDD_XMLName(),
210 rs.getWorkingdirectory()+"map.xml");