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
.FileInputStream
;
22 import java
.io
.FileNotFoundException
;
23 import java
.io
.IOException
;
24 import java
.io
.InputStream
;
25 import java
.net
.MalformedURLException
;
27 import java
.net
.URLConnection
;
28 import java
.util
.HashMap
;
30 import com
.dtrules
.infrastructure
.RulesException
;
31 import com
.dtrules
.interpreter
.RName
;
32 import com
.dtrules
.xmlparser
.GenericXMLParser
;
33 import com
.dtrules
.xmlparser
.IGenericXMLParser
;
35 @SuppressWarnings({"unchecked"})
36 public class RulesDirectory
{
39 HashMap
<RName
,RuleSet
> rulesets
;
40 String systemPath
=""; // Path to the directory on the File System
41 // where files can be read and written. If
42 // present, it is appended to paths used by
45 public void addRuleSet(RuleSet ruleset
) throws RulesException
{
50 * Returns the ruleset associated with the given name. The conversion
51 * of the String to an RName is done by this routine.
52 * All hash tables in DTRules should use RNames as keys.
54 * Returns true if the RulesDirectory has been successfully loaded.
57 public boolean isLoaded(){return loaded
;}
59 public RuleSet
getRuleSet(String setname
){
60 return getRuleSet(RName
.getRName(setname
));
64 * Returns the ruleset associated with the given name. Note that the
65 * key is an RName. All hash tables in DTRules should use RNames as keys.
70 public RuleSet
getRuleSet(RName setname
){
71 return (RuleSet
) rulesets
.get(setname
);
76 * We attempt to open the streamname as a resource in our jar.
77 * Then failing that, we attempt to open it as a URL.
78 * Then failing that, we attempt to open it as a file.
83 public static InputStream
openstream(Object object
, String streamname
){
84 // First try and open the stream as a resource
85 // InputStream s = System.class.getResourceAsStream(streamname);
87 InputStream s
= object
.getClass().getResourceAsStream(streamname
);
91 // If that fails, try and open it as a URL
93 URL url
= new URL(streamname
);
94 URLConnection urlc
= url
.openConnection();
95 s
= urlc
.getInputStream();
97 } catch (MalformedURLException e
) {
98 } catch (Exception e
){}
100 // If that fails, try and open it as a file.
102 s
= new FileInputStream(streamname
);
104 } catch (FileNotFoundException e
) {}
106 // If all these fail, return a null.
115 * The RulesDirectory manages the various RuleSets and the versions of
116 * RuleSets. We need to do a good bit of work to make all of this
117 * managable. For right now, I am loading the property list from the
118 * path provided this class. It first attempts to use this path as a
119 * jar resource, then an URL, then a file.
121 * The systemPath is assumed to be the name of a directory, either with
122 * or without a ending '/' or '\'.
124 * @param propertyfile
126 public RulesDirectory(String systemPath
, String propertyfile
) {
127 if(systemPath
.endsWith("/")||systemPath
.endsWith("\\")){
128 // If it has an ending slash, chop it off.
129 systemPath
= systemPath
.substring(0,systemPath
.length()-1);
131 if(propertyfile
.startsWith("/")||propertyfile
.startsWith("\\")){
132 // If the propertyfile has a leading slash, chop that off.
133 propertyfile
= propertyfile
.substring(1);
135 this.propertyfile
= propertyfile
;
136 this.systemPath
= systemPath
.trim();
138 InputStream s
= openstream(this,systemPath
+"/"+ propertyfile
);
139 loadRulesDirectory(s
);
142 public RulesDirectory(String systemPath
, InputStream s
) {
143 if(systemPath
.endsWith("/")||systemPath
.endsWith("\\")){
144 systemPath
= systemPath
+"/";
146 this.systemPath
= systemPath
.trim();
147 propertyfile
= s
.toString();
149 loadRulesDirectory(s
);
152 public void loadRulesDirectory(InputStream s
){
153 LoadDirectory parser
= new LoadDirectory(this);
156 throw new RuntimeException("Could not find the file/inputstream :"+propertyfile
);
159 GenericXMLParser
.load(s
,parser
);
160 } catch (Exception e
) {
161 throw new RuntimeException("Error parsing property file/inputstream: "+propertyfile
+"\n"+e
);
166 static class LoadDirectory
implements IGenericXMLParser
{
168 final RulesDirectory rd
;
169 LoadDirectory(RulesDirectory _rd
){
171 rd
.rulesets
= new HashMap
<RName
,RuleSet
>();
173 RuleSet currentset
=null;
175 public void beginTag(String
[] tagstk
, int tagstkptr
, String tag
, HashMap attribs
) throws IOException
, Exception
{
176 if (tag
.equals("RuleSet")){
177 currentset
= new RuleSet(rd
);
178 currentset
.setName((String
) attribs
.get("name"));
179 if(currentset
.name
==null){
180 throw new RuntimeException("Missing name in RuleSet");
182 rd
.rulesets
.put(currentset
.name
, currentset
);
186 public void endTag(String
[] tagstk
, int tagstkptr
, String tag
, String body
, HashMap attribs
) throws Exception
, IOException
{
187 if(tag
.equals("RuleSetResourcePath")){
188 currentset
.setResourcepath(body
.trim());
189 }else if (tag
.equals("RuleSetFilePath")){
190 currentset
.setFilepath(body
.trim());
191 }else if (tag
.equals("WorkingDirectory")){
192 currentset
.setWorkingdirectory(body
.trim());
193 }else if (tag
.equals("Entities")){
194 currentset
.edd_names
.add((String
) attribs
.get("name"));
195 }else if (tag
.equals("Decisiontables")){
196 currentset
.dt_names
.add((String
) attribs
.get("name"));
197 }else if (tag
.equals("Map")){
198 currentset
.map_paths
.add((String
) attribs
.get("name"));
199 }else if (tag
.equals("DTExcelFolder")){
200 currentset
.setExcel_dtfolder(body
.trim());
201 }else if (tag
.equals("EDDExcelFile")){
202 currentset
.setExcel_edd(body
.trim());
205 public boolean error(String v
) throws Exception
{
212 * @return the rulesets
214 public HashMap
getRulesets() {
218 * @return the filepath
220 public String
getFilepath() {
224 * @param filepath the filepath to set
226 public void setFilepath(String filepath
) {
227 this.systemPath
= filepath
;
230 * @return the systemPath
232 public String
getSystemPath() {
236 * @param systemPath the systemPath to set
238 public void setSystemPath(String systemPath
) {
239 this.systemPath
= systemPath
;