4 * Copyright 2004-2007 MTBJ, Inc.
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
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.
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
= "";
39 EDDLoader(String _filename
, EntityFactory _ef
){
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.
53 public String
getErrorMsgs() {
60 public void beginTag(String
[] tagstk
, int tagstkptr
, String tag
,
61 HashMap attribs
) throws IOException
, Exception
{
63 if(tag
.equals("entity_data_dictionary") ){
66 version
= Integer
.parseInt((String
) attribs
.get("version"));
67 }catch(NullPointerException e
){} // Ignore any errors
70 }else if(version
== 2){
71 beginTag2(tagstk
,tagstkptr
,tag
,attribs
);
75 public void endTag(String
[] tagstk
,
79 HashMap attribs
) throws Exception
, IOException
{
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");
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";
115 // Now the type. An easy thing.
117 itype
= RSession
.typeStr2Int(type
,entityname
,attribute
);
118 } catch (RulesException e1
) {
119 errorMsgs
+= e1
.getMessage()+"\n";
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
);
135 intType
= RSession
.typeStr2Int(type
,entityname
,attribute
);
136 } catch (RulesException e
) {
137 errorMsgs
+= "Bad Type: '"+type
+"' encountered on entity: '"+entityname
+"' attribute: '"+attribute
+"' \n";
140 String errstr
= entity
.addAttribute(attributeRName
,
154 public boolean error(String v
) throws Exception
{
159 /** Support for the New EDD format **/
162 String entitycomment
;
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
,
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";
197 // Now the type. An easy thing.
199 itype
= RSession
.typeStr2Int(type
,entityname
,attrib_name
);
200 } catch (RulesException e1
) {
201 errorMsgs
+= e1
.getMessage()+"\n";
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
);
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";
217 String errstr
= entity
.addAttribute(attributeRName
,