KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > rules > RulesEngine


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2007 Dennis Reil
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21 package com.izforge.izpack.rules;
22
23 import java.util.Hashtable JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import com.izforge.izpack.installer.InstallData;
29 import com.izforge.izpack.util.Debug;
30
31 import net.n3.nanoxml.XMLElement;
32
33 /**
34  * @author Dennis Reil, <Dennis.Reil@reddot.de>
35  * created: 09.11.2006, 13:48:39
36  */

37 public class RulesEngine {
38     protected Map JavaDoc panelconditions;
39     protected Map JavaDoc packconditions;
40     protected Map JavaDoc optionalpackconditions;
41     protected XMLElement conditionsspec;
42     protected static Map JavaDoc conditionsmap = new Hashtable JavaDoc();
43     protected InstallData installdata;
44
45     /**
46      *
47      */

48     public RulesEngine(XMLElement conditionsspecxml, InstallData installdata) {
49         this.conditionsspec = conditionsspecxml;
50         conditionsmap = new Hashtable JavaDoc();
51         this.panelconditions = new Hashtable JavaDoc();
52         this.packconditions = new Hashtable JavaDoc();
53         this.optionalpackconditions = new Hashtable JavaDoc();
54         this.installdata = installdata;
55         this.readConditions();
56     }
57
58     /**
59      * Checks if an attribute for an xmlelement is set.
60      *
61      * @param val value of attribute to check
62      * @param attribute the attribute which is checked
63      * @param element the element
64      * @return true value was set
65      * false no value was set
66      */

67     protected boolean checkAttribute(String JavaDoc val, String JavaDoc attribute, String JavaDoc element) {
68         if ((val != null) && (val.length() > 0)) {
69             return true;
70         } else {
71             Debug.trace("Element " + element + " has to specify an attribute " + attribute);
72             return false;
73         }
74     }
75
76     public static Condition analyzeCondition(XMLElement condition) {
77         String JavaDoc condid = condition.getAttribute("id");
78         String JavaDoc condtype = condition.getAttribute("type");
79         Condition result = null;
80         if (condtype != null) {
81
82             String JavaDoc conditiontype = condtype.toLowerCase();
83             // TODO: externalize package name
84
String JavaDoc conditionclassname = "com.izforge.izpack.rules." + conditiontype.substring(0, 1).toUpperCase() + conditiontype.substring(1, conditiontype.length());
85             conditionclassname += "Condition";
86             ClassLoader JavaDoc loader = ClassLoader.getSystemClassLoader();
87             try {
88                 Class JavaDoc conditionclass = loader.loadClass(conditionclassname);
89                 result = (Condition) conditionclass.newInstance();
90             } catch (ClassNotFoundException JavaDoc e) {
91                 Debug.trace(conditionclassname + " not found.");
92             } catch (InstantiationException JavaDoc e) {
93                 Debug.trace(conditionclassname + " couldn't be instantiated.");
94             } catch (IllegalAccessException JavaDoc e) {
95                 Debug.trace("Illegal access to " + conditionclassname);
96             }
97             result.readFromXML(condition);
98             result.setId(condid);
99         }
100         return result;
101     }
102
103
104     /**
105      * Read the spec for the conditions
106      */

107     protected void readConditions() {
108         if (this.conditionsspec == null){
109             Debug.trace("No specification for conditions found.");
110             return;
111         }
112         try {
113             if (this.conditionsspec.hasChildren()) {
114                 // read in the condition specs
115
Vector JavaDoc childs = this.conditionsspec.getChildrenNamed("condition");
116
117                 for (int i = 0; i < childs.size(); i++) {
118                     XMLElement condition = (XMLElement) childs.get(i);
119                     Condition cond = analyzeCondition(condition);
120                     if (cond != null) {
121                         // this.conditionslist.add(cond);
122
String JavaDoc condid = cond.getId();
123                         cond.setInstalldata(this.installdata);
124                         if ((condid != null) && !("UNKNOWN".equals(condid))) {
125                             conditionsmap.put(condid, cond);
126                         }
127                     }
128                 }
129
130                 Vector JavaDoc panelconditionels = this.conditionsspec.getChildrenNamed("panelcondition");
131                 for (int i = 0; i < panelconditionels.size(); i++) {
132                     XMLElement panelel = (XMLElement) panelconditionels.get(i);
133                     String JavaDoc panelid = panelel.getAttribute("panelid");
134                     String JavaDoc conditionid = panelel.getAttribute("conditionid");
135                     this.panelconditions.put(panelid, conditionid);
136                 }
137
138                 Vector JavaDoc packconditionels = this.conditionsspec.getChildrenNamed("packcondition");
139                 for (int i = 0; i < packconditionels.size(); i++) {
140                     XMLElement panelel = (XMLElement) packconditionels.get(i);
141                     String JavaDoc panelid = panelel.getAttribute("packid");
142                     String JavaDoc conditionid = panelel.getAttribute("conditionid");
143                     this.packconditions.put(panelid, conditionid);
144                     // optional install allowed, if condition is not met?
145
String JavaDoc optional = panelel.getAttribute("optional");
146                     if (optional != null) {
147                         boolean optionalinstall = Boolean.valueOf(optional).booleanValue();
148                         if (optionalinstall) {
149                             // optional installation is allowed
150
this.optionalpackconditions.put(panelid, conditionid);
151                         }
152                     }
153                 }
154             }
155         } catch (Exception JavaDoc e) {
156             e.printStackTrace();
157         }
158     }
159
160
161     public static Condition getCondition(String JavaDoc id) {
162         return (Condition) conditionsmap.get(id);
163     }
164
165     public boolean isConditionTrue(String JavaDoc id, Properties JavaDoc variables) {
166         Condition cond = (Condition) conditionsmap.get(id);
167         if (cond == null) {
168             Debug.trace("Condition (" + id + ") not found.");
169             return true;
170         } else {
171             Debug.trace("Checking condition");
172             return cond.isTrue();
173         }
174     }
175
176     public boolean isConditionTrue(Condition cond, Properties JavaDoc variables) {
177         if (cond == null) {
178             Debug.trace("Condition not found.");
179             return true;
180         } else {
181             Debug.trace("Checking condition");
182             return cond.isTrue();
183         }
184     }
185
186     /**
187      * Can a panel be shown?
188      *
189      * @param panelid - id of the panel, which should be shown
190      * @param variables - the variables
191      * @return true - there is no condition or condition is met
192      * false - there is a condition and the condition was not met
193      */

194     public boolean canShowPanel(String JavaDoc panelid, Properties JavaDoc variables) {
195         Debug.trace("can show panel with id " + panelid + " ?");
196         if (!this.panelconditions.containsKey(panelid)) {
197             Debug.trace("no condition, show panel");
198             return true;
199         }
200         Debug.trace("there is a condition");
201         Condition condition = (Condition) conditionsmap.get(this.panelconditions.get(panelid));
202         if (condition != null) {
203             return condition.isTrue();
204         }
205         return false;
206     }
207
208     /**
209      * Is the installation of a pack possible?
210      *
211      * @param packid
212      * @param variables
213      * @return true - there is no condition or condition is met
214      * false - there is a condition and the condition was not met
215      */

216     public boolean canInstallPack(String JavaDoc packid, Properties JavaDoc variables) {
217         if (packid == null){
218             return true;
219         }
220         Debug.trace("can install pack with id " + packid + "?");
221         if (!this.packconditions.containsKey(packid)) {
222             Debug.trace("no condition, can install pack");
223             return true;
224         }
225         Debug.trace("there is a condition");
226         Condition condition = (Condition) conditionsmap.get(this.packconditions.get(packid));
227         if (condition != null) {
228             return condition.isTrue();
229         }
230         return false;
231     }
232
233     /**
234      * Is an optional installation of a pack possible if the condition is not met?
235      *
236      * @param packid
237      * @param variables
238      * @return
239      */

240     public boolean canInstallPackOptional(String JavaDoc packid, Properties JavaDoc variables) {
241         Debug.trace("can install pack optional with id " + packid + "?");
242         if (!this.optionalpackconditions.containsKey(packid)) {
243             Debug.trace("not in optionalpackconditions.");
244             return false;
245         } else {
246             Debug.trace("optional install possible");
247             return true;
248         }
249     }
250 }
251
Popular Tags