Added support for a new EDD format, as well as support for the EDD being defined...
[DTRules.git] / DTRules / src / main / java / com / dtrules / session / EDDLoader.java
blob6f17e135151940c5e8a20410ad14e0a622f4ec1f
1 /*
2 * $Id$
3 *
4 * Copyright 2004-2007 MTBJ, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
19 package com.dtrules.session;
21 import java.io.IOException;
22 import java.util.HashMap;
24 import com.dtrules.entity.REntity;
25 import com.dtrules.infrastructure.RulesException;
26 import com.dtrules.interpreter.IRObject;
27 import com.dtrules.interpreter.RName;
28 import com.dtrules.xmlparser.IGenericXMLParser;
30 @SuppressWarnings({"unchecked"})
31 public class EDDLoader implements IGenericXMLParser {
33 final EntityFactory ef;
34 final String filename;
35 boolean succeeded = true;
36 String errorMsgs = "";
37 int version = 1;
39 EDDLoader(String _filename, EntityFactory _ef){
40 ef = _ef;
41 filename = _filename;
44 /**
45 * If this string has a non-zero length, the EDD did not load
46 * properly. The caller is responsible for checking this. Otherwise
47 * the loader can only report a single error.
49 * @return
53 public String getErrorMsgs() {
54 return errorMsgs;
60 public void beginTag(String[] tagstk, int tagstkptr, String tag,
61 HashMap attribs) throws IOException, Exception {
63 if(tag.equals("entity_data_dictionary") ){
65 try{
66 version = Integer.parseInt((String) attribs.get("version"));
67 }catch(NullPointerException e){} // Ignore any errors
68 catch(Exception e){}
70 }else if(version == 2){
71 beginTag2(tagstk,tagstkptr,tag,attribs);
75 public void endTag(String[] tagstk,
76 int tagstkptr,
77 String tag,
78 String body,
79 HashMap attribs) throws Exception, IOException {
80 //
82 if(version==2){
84 endTag2(tagstk,tagstkptr,tag,body,attribs);
86 }else if(tag.equals("entity")){
88 String entityname = (String) attribs.get("entityname");
89 String attribute = (String) attribs.get("attribute");
90 String type = (String) attribs.get("type");
91 String subtype = (String) attribs.get("subtype");
92 String access = (String) attribs.get("access");
93 String defaultv = (String) attribs.get("default");
95 boolean writeable = true; // We need to convert access to a boolean
96 boolean readable = true; // Make an assumption of r/w
97 int itype = -1; // We need to convert the type to an int.
98 IRObject defaultO = null; // We need to convert the default into a Rules Engine Object.
100 //First deal with the access thing... Compute the boolean we need.
101 if(access==null){ // Check to see if we need to handle TIERS EDD files.
102 access = (String) attribs.get("cdd_i_c_flag");
103 if(access!=null){
104 writeable = access.toLowerCase().indexOf("i")<0;
106 }else{ // Nope? Then handle the more rational DTRules EDD files
107 writeable = access.toLowerCase().indexOf("w")>=0;
108 readable = access.toLowerCase().indexOf("r")>=0;
109 if(!writeable && !readable){
110 errorMsgs +="\nThe attribute "+attribute+" has to be either readable or writable\r\n";
111 succeeded=false;
115 // Now the type. An easy thing.
116 try {
117 itype = RSession.typeStr2Int(type,entityname,attribute);
118 } catch (RulesException e1) {
119 errorMsgs+= e1.getMessage()+"\n";
120 succeeded = false;
123 // Now deal with the default specified.
124 if (defaultv==null){ // Do we need to handle TIERS EDD files?
125 defaultv = (String) attribs.get("cdd_default_value");
128 defaultO = EntityFactory.computeDefaultValue(ef, ef.ruleset, defaultv, itype) ;
130 RName entityRName = RName.getRName(entityname.trim(),false);
131 RName attributeRName = RName.getRName(attribute.trim(),false);
132 REntity entity = ef.findcreateRefEntity(false,entityRName);
133 int intType = -1;
134 try {
135 intType = RSession.typeStr2Int(type,entityname,attribute);
136 } catch (RulesException e) {
137 errorMsgs += "Bad Type: '"+type+"' encountered on entity: '"+entityname+"' attribute: '"+attribute+"' \n";
138 succeeded = false;
140 String errstr = entity.addAttribute(attributeRName,
141 defaultv,
142 defaultO,
143 writeable,
144 readable,
145 intType,
146 subtype);
147 if(errstr!=null){
148 succeeded = false;
149 errorMsgs += errstr;
154 public boolean error(String v) throws Exception {
155 return true;
159 /** Support for the New EDD format **/
161 String entityname;
162 String entitycomment;
163 String entityaccess;
164 public void beginTag2(String[] tagstk, int tagstkptr, String tag,
165 HashMap attribs) throws IOException, Exception {
166 if(tag.equals("entity")){
167 entityname = (String) attribs.get("name");
168 entitycomment = (String) attribs.get("comment");
169 entityaccess = (String) attribs.get("access");
173 public void endTag2(String[] tagstk,
174 int tagstkptr,
175 String tag,
176 String body,
177 HashMap attribs) throws Exception, IOException {
178 if(!tag.equals("field")) return;
181 String default_value = (String) attribs.get("default_value");
182 String attrib_name = (String) attribs.get("name");
183 String attrib_comment = (String) attribs.get("comment");
184 String access = (String) attribs.get("access");
185 String subtype = (String) attribs.get("subtype");
186 String type = (String) attribs.get("type");
188 boolean writeable = access.toLowerCase().indexOf("w")>=0;
189 boolean readable = access.toLowerCase().indexOf("r")>=0;
190 if(!writeable && !readable){
191 errorMsgs +="\nThe attribute "+attrib_name+" has to be either readable or writable\r\n";
192 succeeded=false;
195 int itype=-1;
197 // Now the type. An easy thing.
198 try {
199 itype = RSession.typeStr2Int(type,entityname,attrib_name);
200 } catch (RulesException e1) {
201 errorMsgs+= e1.getMessage()+"\n";
202 succeeded = false;
205 IRObject defaultO = EntityFactory.computeDefaultValue(ef, ef.ruleset, default_value, itype) ;
207 RName entityRName = RName.getRName(entityname.trim(),false);
208 RName attributeRName = RName.getRName(attrib_name.trim(),false);
209 REntity entity = ef.findcreateRefEntity(false,entityRName);
210 int intType = -1;
211 try {
212 intType = RSession.typeStr2Int(type,entityname,attrib_name);
213 } catch (RulesException e) {
214 errorMsgs += "Bad Type: '"+type+"' encountered on entity: '"+entityname+"' attribute: '"+attrib_name+"' \n";
215 succeeded = false;
217 String errstr = entity.addAttribute(attributeRName,
218 default_value,
219 defaultO,
220 writeable,
221 readable,
222 intType,
223 subtype);
224 if(errstr!=null){
225 succeeded = false;
226 errorMsgs += errstr;