1 21 package com.izforge.izpack.rules; 22 23 import java.util.Hashtable ; 24 import java.util.Map ; 25 import java.util.Properties ; 26 import java.util.Vector ; 27 28 import com.izforge.izpack.installer.InstallData; 29 import com.izforge.izpack.util.Debug; 30 31 import net.n3.nanoxml.XMLElement; 32 33 37 public class RulesEngine { 38 protected Map panelconditions; 39 protected Map packconditions; 40 protected Map optionalpackconditions; 41 protected XMLElement conditionsspec; 42 protected static Map conditionsmap = new Hashtable (); 43 protected InstallData installdata; 44 45 48 public RulesEngine(XMLElement conditionsspecxml, InstallData installdata) { 49 this.conditionsspec = conditionsspecxml; 50 conditionsmap = new Hashtable (); 51 this.panelconditions = new Hashtable (); 52 this.packconditions = new Hashtable (); 53 this.optionalpackconditions = new Hashtable (); 54 this.installdata = installdata; 55 this.readConditions(); 56 } 57 58 67 protected boolean checkAttribute(String val, String attribute, String 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 condid = condition.getAttribute("id"); 78 String condtype = condition.getAttribute("type"); 79 Condition result = null; 80 if (condtype != null) { 81 82 String conditiontype = condtype.toLowerCase(); 83 String conditionclassname = "com.izforge.izpack.rules." + conditiontype.substring(0, 1).toUpperCase() + conditiontype.substring(1, conditiontype.length()); 85 conditionclassname += "Condition"; 86 ClassLoader loader = ClassLoader.getSystemClassLoader(); 87 try { 88 Class conditionclass = loader.loadClass(conditionclassname); 89 result = (Condition) conditionclass.newInstance(); 90 } catch (ClassNotFoundException e) { 91 Debug.trace(conditionclassname + " not found."); 92 } catch (InstantiationException e) { 93 Debug.trace(conditionclassname + " couldn't be instantiated."); 94 } catch (IllegalAccessException 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 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 Vector 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 String 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 panelconditionels = this.conditionsspec.getChildrenNamed("panelcondition"); 131 for (int i = 0; i < panelconditionels.size(); i++) { 132 XMLElement panelel = (XMLElement) panelconditionels.get(i); 133 String panelid = panelel.getAttribute("panelid"); 134 String conditionid = panelel.getAttribute("conditionid"); 135 this.panelconditions.put(panelid, conditionid); 136 } 137 138 Vector packconditionels = this.conditionsspec.getChildrenNamed("packcondition"); 139 for (int i = 0; i < packconditionels.size(); i++) { 140 XMLElement panelel = (XMLElement) packconditionels.get(i); 141 String panelid = panelel.getAttribute("packid"); 142 String conditionid = panelel.getAttribute("conditionid"); 143 this.packconditions.put(panelid, conditionid); 144 String optional = panelel.getAttribute("optional"); 146 if (optional != null) { 147 boolean optionalinstall = Boolean.valueOf(optional).booleanValue(); 148 if (optionalinstall) { 149 this.optionalpackconditions.put(panelid, conditionid); 151 } 152 } 153 } 154 } 155 } catch (Exception e) { 156 e.printStackTrace(); 157 } 158 } 159 160 161 public static Condition getCondition(String id) { 162 return (Condition) conditionsmap.get(id); 163 } 164 165 public boolean isConditionTrue(String id, Properties 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 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 194 public boolean canShowPanel(String panelid, Properties 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 216 public boolean canInstallPack(String packid, Properties 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 240 public boolean canInstallPackOptional(String packid, Properties 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 |