[izpack-changes] r1639 - in izpack-src/trunk/src/lib/com/izforge/izpack: . rules

noreply at berlios.de noreply at berlios.de
Mon Nov 13 09:17:13 CET 2006


Author: dreil
Date: 2006-11-13 09:17:10 +0100 (Mon, 13 Nov 2006)
New Revision: 1639

Added:
   izpack-src/trunk/src/lib/com/izforge/izpack/rules/
   izpack-src/trunk/src/lib/com/izforge/izpack/rules/AndCondition.java
   izpack-src/trunk/src/lib/com/izforge/izpack/rules/Condition.java
   izpack-src/trunk/src/lib/com/izforge/izpack/rules/JavaCondition.java
   izpack-src/trunk/src/lib/com/izforge/izpack/rules/NotCondition.java
   izpack-src/trunk/src/lib/com/izforge/izpack/rules/OrCondition.java
   izpack-src/trunk/src/lib/com/izforge/izpack/rules/PackselectionCondition.java
   izpack-src/trunk/src/lib/com/izforge/izpack/rules/RefCondition.java
   izpack-src/trunk/src/lib/com/izforge/izpack/rules/RulesEngine.java
   izpack-src/trunk/src/lib/com/izforge/izpack/rules/VariableCondition.java
   izpack-src/trunk/src/lib/com/izforge/izpack/rules/XOrCondition.java
Log:
Added implementation of conditions and rulesengine


Added: izpack-src/trunk/src/lib/com/izforge/izpack/rules/AndCondition.java
===================================================================
--- izpack-src/trunk/src/lib/com/izforge/izpack/rules/AndCondition.java	2006-11-13 08:16:19 UTC (rev 1638)
+++ izpack-src/trunk/src/lib/com/izforge/izpack/rules/AndCondition.java	2006-11-13 08:17:10 UTC (rev 1639)
@@ -0,0 +1,55 @@
+package com.izforge.izpack.rules;
+
+import java.util.Properties;
+
+import net.n3.nanoxml.XMLElement;
+import com.izforge.izpack.util.Debug;
+
+/**
+ * Defines a condition where both operands have to be true
+ *
+ * @author Dennis Reil, <Dennis.Reil at reddot.de>
+ */
+public class AndCondition extends Condition {
+    protected Condition leftoperand;
+    protected Condition rightoperand;
+
+    /**
+     *
+     */
+    public AndCondition() {
+        super();        
+    }
+
+    /**
+     *
+     */
+    public AndCondition(Condition operand1, Condition operand2) {
+        this.leftoperand = operand1;
+        this.rightoperand = operand2;
+    }
+
+    /* (non-Javadoc)
+    * @see de.reddot.installer.util.Condition#isTrue()
+    */
+    public boolean isTrue(Properties variables) {
+        return leftoperand.isTrue(variables) && rightoperand.isTrue(variables);
+    }
+
+    /* (non-Javadoc)
+    * @see de.reddot.installer.rules.Condition#readFromXML(net.n3.nanoxml.XMLElement)
+    */
+    public void readFromXML(XMLElement xmlcondition) {
+        try {
+            if (xmlcondition.getChildrenCount() != 2) {
+                Debug.log("and-condition needs two conditions as operands");
+                return;
+            }
+            this.leftoperand = RulesEngine.analyzeCondition(xmlcondition.getChildAtIndex(0));
+            this.rightoperand = RulesEngine.analyzeCondition(xmlcondition.getChildAtIndex(1));
+        }
+        catch (Exception e) {
+            Debug.log("missing element in and-condition");
+        }
+    }
+}

Added: izpack-src/trunk/src/lib/com/izforge/izpack/rules/Condition.java
===================================================================
--- izpack-src/trunk/src/lib/com/izforge/izpack/rules/Condition.java	2006-11-13 08:16:19 UTC (rev 1638)
+++ izpack-src/trunk/src/lib/com/izforge/izpack/rules/Condition.java	2006-11-13 08:17:10 UTC (rev 1639)
@@ -0,0 +1,58 @@
+package com.izforge.izpack.rules;
+
+import java.util.List;
+import java.util.Properties;
+
+import net.n3.nanoxml.XMLElement;
+
+/**
+ * Abstract base class for all conditions
+ *
+ * @author Dennis Reil, <Dennis.Reil at reddot.de>
+ */
+public abstract class Condition {
+
+    protected String id;
+
+    public Condition() {
+        this.id = "UNKNOWN";
+    }
+
+    /**
+     * checks if this condition is met.
+     *
+     * @return true if condition is fulfilled
+     *         false if condition is not fulfilled
+     */
+    public abstract boolean isTrue(Properties variables);
+
+    /**
+     * checks if this condition is met.
+     *
+     * @param variables
+     * @param selectedpacks
+     * @return true if condition is fulfilled
+     *         false if condition is not fulfilled
+     */
+    public boolean isTrue(Properties variables, List selectedpacks) {
+        // default implementation is to ignore the selected packs
+        return this.isTrue(variables);
+    }
+
+    /**
+     * @return the id
+     */
+    public String getId() {
+        return this.id;
+    }
+
+
+    /**
+     * @param id the id to set
+     */
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    public abstract void readFromXML(XMLElement xmlcondition);
+}

Added: izpack-src/trunk/src/lib/com/izforge/izpack/rules/JavaCondition.java
===================================================================
--- izpack-src/trunk/src/lib/com/izforge/izpack/rules/JavaCondition.java	2006-11-13 08:16:19 UTC (rev 1638)
+++ izpack-src/trunk/src/lib/com/izforge/izpack/rules/JavaCondition.java	2006-11-13 08:17:10 UTC (rev 1639)
@@ -0,0 +1,118 @@
+package com.izforge.izpack.rules;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.Properties;
+
+import net.n3.nanoxml.XMLElement;
+import com.izforge.izpack.util.Debug;
+
+/**
+ * A condition based on the value of a static java field or static java method.
+ *
+ * @author Dennis Reil, <Dennis.Reil at reddot.de>
+ */
+public class JavaCondition extends Condition {
+    protected String classname;
+    protected String methodname;
+    protected String fieldname;
+    protected boolean complete;
+    protected String returnvalue;
+    protected String returnvaluetype;
+
+
+    protected Class usedclass;
+    protected Field usedfield;
+    protected Method usedmethod;
+
+    public JavaCondition() {
+
+    }
+
+    public boolean isTrue(Properties variables) {
+        if (!this.complete) {
+            return false;
+        } else {
+            if (this.usedclass == null) {
+                ClassLoader loader = ClassLoader.getSystemClassLoader();
+                try {
+                    this.usedclass = loader.loadClass(this.classname);
+                } catch (ClassNotFoundException e) {
+                    Debug.log("Can't find class " + this.classname);
+                    return false;
+                }
+            }
+            if ((this.usedfield == null) && (this.fieldname != null)) {
+                try {
+                    this.usedfield = this.usedclass.getField(this.fieldname);
+                } catch (SecurityException e) {
+                    Debug.log("No permission to access specified field: " + this.fieldname);
+                    return false;
+                } catch (NoSuchFieldException e) {
+                    Debug.log("No such field: " + this.fieldname);
+                    return false;
+                }
+            }
+            if ((this.usedmethod == null) && (this.methodname != null)) {
+                Debug.log("not implemented yet.");
+                return false;
+            }
+
+            if (this.usedfield != null) {
+                // access field
+                if ("boolean".equals(this.returnvaluetype)) {
+                    try {
+                        boolean returnval = this.usedfield.getBoolean(null);
+                        boolean expectedreturnval = Boolean.valueOf(this.returnvalue).booleanValue();
+                        return returnval == expectedreturnval;
+                    } catch (IllegalArgumentException e) {
+                        Debug.log("IllegalArgumentexeption " + this.fieldname);
+                    } catch (IllegalAccessException e) {
+                        Debug.log("IllegalAccessException " + this.fieldname);
+                    }
+                } else {
+                    Debug.log("not implemented yet.");
+                    return false;
+                }
+            }
+            return false;
+        }
+    }
+
+    public void readFromXML(XMLElement xmlcondition) {
+        if (xmlcondition.getChildrenCount() != 2) {
+            Debug.log("Condition of type java needs (java,returnvalue)");
+            return;
+        }
+        XMLElement javael = xmlcondition.getFirstChildNamed("java");
+        XMLElement classel = javael.getFirstChildNamed("class");
+        if (classel != null) {
+            this.classname = classel.getContent();
+        } else {
+            Debug.log("Java-Element needs (class,method?,field?)");
+            return;
+        }
+        XMLElement methodel = javael.getFirstChildNamed("method");
+        if (methodel != null) {
+            this.methodname = methodel.getContent();
+        }
+        XMLElement fieldel = javael.getFirstChildNamed("field");
+        if (fieldel != null) {
+            this.fieldname = fieldel.getContent();
+        }
+        if ((this.methodname == null) && (this.fieldname == null)) {
+            Debug.log("java element needs (class, method?,field?)");
+            return;
+        }
+        XMLElement returnvalel = xmlcondition.getFirstChildNamed("returnvalue");
+        if (returnvalel != null) {
+            this.returnvalue = returnvalel.getContent();
+            this.returnvaluetype = returnvalel.getAttribute("type");
+        } else {
+            Debug.log("no returnvalue-element specified.");
+            return;
+        }
+        this.complete = true;
+    }
+
+}

Added: izpack-src/trunk/src/lib/com/izforge/izpack/rules/NotCondition.java
===================================================================
--- izpack-src/trunk/src/lib/com/izforge/izpack/rules/NotCondition.java	2006-11-13 08:16:19 UTC (rev 1638)
+++ izpack-src/trunk/src/lib/com/izforge/izpack/rules/NotCondition.java	2006-11-13 08:17:10 UTC (rev 1639)
@@ -0,0 +1,53 @@
+package com.izforge.izpack.rules;
+
+import java.util.Properties;
+
+import net.n3.nanoxml.XMLElement;
+import com.izforge.izpack.util.Debug;
+
+
+/**
+ * @author Dennis Reil, <Dennis.Reil at reddot.de>
+ */
+public class NotCondition extends Condition {
+
+    protected Condition operand;
+
+    /**
+     *
+     */
+    public NotCondition() {
+        super();
+        // TODO Auto-generated constructor stub
+    }
+
+    /**
+     *
+     */
+    public NotCondition(Condition operand) {
+        this.operand = operand;
+    }
+
+    /* (non-Javadoc)
+    * @see de.reddot.installer.util.Condition#isTrue()
+    */
+    public boolean isTrue(Properties variables) {
+        return !operand.isTrue(variables);
+    }
+
+    /* (non-Javadoc)
+    * @see de.reddot.installer.rules.Condition#readFromXML(net.n3.nanoxml.XMLElement)
+    */
+    public void readFromXML(XMLElement xmlcondition) {
+        try {
+            if (xmlcondition.getChildrenCount() != 1) {
+                Debug.log("not-condition needs one condition as operand");
+                return;
+            }
+            this.operand = RulesEngine.analyzeCondition(xmlcondition.getChildAtIndex(0));
+        }
+        catch (Exception e) {
+            Debug.log("missing element in not-condition");
+        }
+    }
+}

Added: izpack-src/trunk/src/lib/com/izforge/izpack/rules/OrCondition.java
===================================================================
--- izpack-src/trunk/src/lib/com/izforge/izpack/rules/OrCondition.java	2006-11-13 08:16:19 UTC (rev 1638)
+++ izpack-src/trunk/src/lib/com/izforge/izpack/rules/OrCondition.java	2006-11-13 08:17:10 UTC (rev 1639)
@@ -0,0 +1,59 @@
+package com.izforge.izpack.rules;
+
+import java.util.Properties;
+
+import net.n3.nanoxml.XMLElement;
+import com.izforge.izpack.util.Debug;
+
+
+/**
+ * @author Dennis Reil, <Dennis.Reil at reddot.de>
+ * @version $Id: OrCondition.java,v 1.1 2006/09/29 14:40:38 dennis Exp $
+ */
+public class OrCondition extends Condition {
+    public static final String RDE_VCS_REVISION = "$Revision: 1.1 $";
+    public static final String RDE_VCS_NAME = "$Name:  $";
+
+    protected Condition leftoperand;
+    protected Condition rightoperand;
+
+    /**
+     *
+     */
+    public OrCondition() {
+        super();
+        // TODO Auto-generated constructor stub
+    }
+
+    /**
+     *
+     */
+    public OrCondition(Condition operand1, Condition operand2) {
+        this.leftoperand = operand1;
+        this.rightoperand = operand2;
+    }
+
+    /* (non-Javadoc)
+    * @see de.reddot.installer.util.Condition#isTrue()
+    */
+    public boolean isTrue(Properties variables) {
+        return this.leftoperand.isTrue(variables) || this.rightoperand.isTrue(variables);
+    }
+
+    /* (non-Javadoc)
+    * @see de.reddot.installer.rules.Condition#readFromXML(net.n3.nanoxml.XMLElement)
+    */
+    public void readFromXML(XMLElement xmlcondition) {
+        try {
+            if (xmlcondition.getChildrenCount() != 2) {
+                Debug.log("or-condition needs two conditions as operands");
+                return;
+            }
+            this.leftoperand = RulesEngine.analyzeCondition(xmlcondition.getChildAtIndex(0));
+            this.rightoperand = RulesEngine.analyzeCondition(xmlcondition.getChildAtIndex(1));
+        }
+        catch (Exception e) {
+            Debug.log("missing element in or-condition");
+        }
+    }
+}

Added: izpack-src/trunk/src/lib/com/izforge/izpack/rules/PackselectionCondition.java
===================================================================
--- izpack-src/trunk/src/lib/com/izforge/izpack/rules/PackselectionCondition.java	2006-11-13 08:16:19 UTC (rev 1638)
+++ izpack-src/trunk/src/lib/com/izforge/izpack/rules/PackselectionCondition.java	2006-11-13 08:17:10 UTC (rev 1639)
@@ -0,0 +1,61 @@
+package com.izforge.izpack.rules;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Properties;
+
+import com.izforge.izpack.Pack;
+import com.izforge.izpack.util.Debug;
+
+import net.n3.nanoxml.XMLElement;
+
+/**
+ * @author Dennis Reil, <Dennis.Reil at reddot.de>
+ * @version $Id: PackselectionCondition.java,v 1.1 2006/11/03 13:03:26 dennis Exp $
+ */
+public class PackselectionCondition extends Condition {
+
+    protected String packid;
+
+    /**
+     *
+     */
+    public PackselectionCondition() {
+        // TODO Auto-generated constructor stub
+    }
+
+    /* (non-Javadoc)
+    * @see de.reddot.installer.rules.Condition#isTrue(java.util.Properties)
+    */
+    public boolean isTrue(Properties variables) {
+        // no information about selected packs given, so return false
+        return false;
+    }
+
+    /* (non-Javadoc)
+    * @see de.reddot.installer.rules.Condition#readFromXML(net.n3.nanoxml.XMLElement)
+    */
+    public void readFromXML(XMLElement xmlcondition) {
+        try {
+            this.packid = xmlcondition.getFirstChildNamed("packid").getContent();
+        }
+        catch (Exception e) {
+            Debug.log("missing element in <condition type=\"variable\"/>");
+        }
+    }
+
+    public boolean isTrue(Properties variables, List selectedpacks) {
+        if (selectedpacks != null) {
+            for (Iterator iter = selectedpacks.iterator(); iter.hasNext();) {
+                Pack p = (Pack) iter.next();
+                if (packid.equals(p.id)) {
+                    // pack is selected
+                    return true;
+                }
+            }
+        }
+        // pack is not selected
+        return false;
+    }
+
+}

Added: izpack-src/trunk/src/lib/com/izforge/izpack/rules/RefCondition.java
===================================================================
--- izpack-src/trunk/src/lib/com/izforge/izpack/rules/RefCondition.java	2006-11-13 08:16:19 UTC (rev 1638)
+++ izpack-src/trunk/src/lib/com/izforge/izpack/rules/RefCondition.java	2006-11-13 08:17:10 UTC (rev 1639)
@@ -0,0 +1,32 @@
+package com.izforge.izpack.rules;
+
+import java.util.Properties;
+
+import net.n3.nanoxml.XMLElement;
+
+/**
+ * References an already defined condition
+ *
+ * @author Dennis Reil, <Dennis.Reil at reddot.de>
+ */
+public class RefCondition extends Condition {
+    Condition referencedcondition;
+
+    public RefCondition() {
+        this.referencedcondition = null;
+    }
+
+    public boolean isTrue(Properties variables) {
+        if (referencedcondition == null) {
+            return false;
+        } else {
+            return referencedcondition.isTrue(variables);
+        }
+    }
+
+    public void readFromXML(XMLElement xmlcondition) {
+        String refid = xmlcondition.getAttribute("refid");
+        this.referencedcondition = RulesEngine.getCondition(refid);
+    }
+
+}

Added: izpack-src/trunk/src/lib/com/izforge/izpack/rules/RulesEngine.java
===================================================================
--- izpack-src/trunk/src/lib/com/izforge/izpack/rules/RulesEngine.java	2006-11-13 08:16:19 UTC (rev 1638)
+++ izpack-src/trunk/src/lib/com/izforge/izpack/rules/RulesEngine.java	2006-11-13 08:17:10 UTC (rev 1639)
@@ -0,0 +1,222 @@
+package com.izforge.izpack.rules;
+
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Vector;
+
+import com.izforge.izpack.installer.InstallData;
+import com.izforge.izpack.util.Debug;
+
+import net.n3.nanoxml.XMLElement;
+
+/**
+ * @author Dennis Reil, <Dennis.Reil at reddot.de>
+ *         created: 09.11.2006, 13:48:39
+ */
+public class RulesEngine {
+    protected Map panelconditions;
+    protected Map packconditions;
+    protected Map optionalpackconditions;
+    protected XMLElement conditionsspec;
+    protected static Map conditionsmap = new Hashtable();
+    protected InstallData installdata;
+
+    /**
+     *
+     */
+    public RulesEngine(XMLElement conditionsspecxml, InstallData installdata) {
+        this.conditionsspec = conditionsspecxml;
+        conditionsmap = new Hashtable();
+        this.panelconditions = new Hashtable();
+        this.packconditions = new Hashtable();
+        this.optionalpackconditions = new Hashtable();
+        this.installdata = installdata;
+        this.readConditions();
+    }
+
+    /**
+     * Checks if an attribute for an xmlelement is set.
+     *
+     * @param val       value of attribute to check
+     * @param attribute the attribute which is checked
+     * @param element   the element
+     * @return true value was set
+     *         false no value was set
+     */
+    protected boolean checkAttribute(String val, String attribute, String element) {
+        if ((val != null) && (val.length() > 0)) {
+            return true;
+        } else {
+            Debug.log("Element " + element + " has to specify an attribute " + attribute);
+            return false;
+        }
+    }
+
+    public static Condition analyzeCondition(XMLElement condition) {
+        String condid = condition.getAttribute("id");
+        String condtype = condition.getAttribute("type");
+        Condition result = null;
+        if (condtype != null) {
+
+            String conditiontype = condtype.toLowerCase();
+            // TODO: externalize package name
+            String conditionclassname = "com.izforge.izpack.rules." + conditiontype.substring(0, 1).toUpperCase() + conditiontype.substring(1, conditiontype.length());
+            conditionclassname += "Condition";
+            ClassLoader loader = ClassLoader.getSystemClassLoader();
+            try {
+                Class conditionclass = loader.loadClass(conditionclassname);
+                result = (Condition) conditionclass.newInstance();
+            } catch (ClassNotFoundException e) {
+                Debug.log(conditionclassname + " not found.");
+            } catch (InstantiationException e) {
+                Debug.log(conditionclassname + " couldn't be instantiated.");
+            } catch (IllegalAccessException e) {
+                Debug.log("Illegal access to " + conditionclassname);
+            }
+            result.readFromXML(condition);
+            result.setId(condid);
+        }
+        return result;
+    }
+
+
+    /**
+     * Read the spec for the conditions
+     */
+    protected void readConditions() {
+        try {
+            if (this.conditionsspec.hasChildren()) {
+                // read in the condition specs
+                Vector childs = this.conditionsspec.getChildrenNamed("condition");
+
+                for (int i = 0; i < childs.size(); i++) {
+                    XMLElement condition = (XMLElement) childs.get(i);
+                    Condition cond = analyzeCondition(condition);
+                    if (cond != null) {
+                        // this.conditionslist.add(cond);
+                        String condid = cond.getId();
+                        if ((condid != null) && !("UNKNOWN".equals(condid))) {
+                            conditionsmap.put(condid, cond);
+                        }
+                    }
+                }
+
+                Vector panelconditionels = this.conditionsspec.getChildrenNamed("panelcondition");
+                for (int i = 0; i < panelconditionels.size(); i++) {
+                    XMLElement panelel = (XMLElement) panelconditionels.get(i);
+                    String panelid = panelel.getAttribute("panelid");
+                    String conditionid = panelel.getAttribute("conditionid");
+                    this.panelconditions.put(panelid, conditionid);
+                }
+
+                Vector packconditionels = this.conditionsspec.getChildrenNamed("packcondition");
+                for (int i = 0; i < packconditionels.size(); i++) {
+                    XMLElement panelel = (XMLElement) packconditionels.get(i);
+                    String panelid = panelel.getAttribute("packid");
+                    String conditionid = panelel.getAttribute("conditionid");
+                    this.packconditions.put(panelid, conditionid);
+                    // optional install allowed, if condition is not met?
+                    String optional = panelel.getAttribute("optional");
+                    if (optional != null) {
+                        boolean optionalinstall = Boolean.valueOf(optional).booleanValue();
+                        if (optionalinstall) {
+                            // optional installation is allowed
+                            this.optionalpackconditions.put(panelid, conditionid);
+                        }
+                    }
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+
+    public static Condition getCondition(String id) {
+        return (Condition) conditionsmap.get(id);
+    }
+
+    public boolean isConditionTrue(String id, Properties variables) {
+        Condition cond = (Condition) conditionsmap.get(id);
+        if (cond == null) {
+            Debug.log("Condition (" + id + ") not found.");
+            return true;
+        } else {
+            Debug.log("Checking condition");
+            return cond.isTrue(variables, this.installdata.selectedPacks);
+        }
+    }
+
+    public boolean isConditionTrue(Condition cond, Properties variables) {
+        if (cond == null) {
+            Debug.log("Condition not found.");
+            return true;
+        } else {
+            Debug.log("Checking condition");
+            return cond.isTrue(variables, this.installdata.selectedPacks);
+        }
+    }
+
+    /**
+     * Can a panel be shown?
+     *
+     * @param panelid   - id of the panel, which should be shown
+     * @param variables - the variables
+     * @return true - there is no condition or condition is met
+     *         false - there is a condition and the condition was not met
+     */
+    public boolean canShowPanel(String panelid, Properties variables) {
+        Debug.log("can show panel with id " + panelid + " ?");
+        if (!this.panelconditions.containsKey(panelid)) {
+            Debug.log("no condition, show panel");
+            return true;
+        }
+        Debug.log("there is a condition");
+        Condition condition = (Condition) conditionsmap.get(this.panelconditions.get(panelid));
+        if (condition != null) {
+            return condition.isTrue(variables, this.installdata.selectedPacks);
+        }
+        return false;
+    }
+
+    /**
+     * Is the installation of a pack possible?
+     *
+     * @param packid
+     * @param variables
+     * @return true - there is no condition or condition is met
+     *         false - there is a condition and the condition was not met
+     */
+    public boolean canInstallPack(String packid, Properties variables) {
+        Debug.log("can install pack with id " + packid + "?");
+        if (!this.packconditions.containsKey(packid)) {
+            Debug.log("no condition, can install pack");
+            return true;
+        }
+        Debug.log("there is a condition");
+        Condition condition = (Condition) conditionsmap.get(this.packconditions.get(packid));
+        if (condition != null) {
+            return condition.isTrue(variables, this.installdata.selectedPacks);
+        }
+        return false;
+    }
+
+    /**
+     * Is an optional installation of a pack possible if the condition is not met?
+     *
+     * @param packid
+     * @param variables
+     * @return
+     */
+    public boolean canInstallPackOptional(String packid, Properties variables) {
+        Debug.log("can install pack optional with id " + packid + "?");
+        if (!this.optionalpackconditions.containsKey(packid)) {
+            Debug.log("not in optionalpackconditions.");
+            return false;
+        } else {
+            Debug.log("optional install possible");
+            return true;
+        }
+    }
+}

Added: izpack-src/trunk/src/lib/com/izforge/izpack/rules/VariableCondition.java
===================================================================
--- izpack-src/trunk/src/lib/com/izforge/izpack/rules/VariableCondition.java	2006-11-13 08:16:19 UTC (rev 1638)
+++ izpack-src/trunk/src/lib/com/izforge/izpack/rules/VariableCondition.java	2006-11-13 08:17:10 UTC (rev 1639)
@@ -0,0 +1,73 @@
+package com.izforge.izpack.rules;
+
+import java.util.HashMap;
+import java.util.Properties;
+
+import net.n3.nanoxml.XMLElement;
+import com.izforge.izpack.util.Debug;
+
+/**
+ * @author Dennis Reil, <Dennis.Reil at reddot.de>
+ */
+public class VariableCondition extends Condition {
+    protected String variablename;
+    protected String value;
+
+    public VariableCondition(String variablename, String value, HashMap packstoremove) {
+        super();
+        this.variablename = variablename;
+        this.value = value;
+    }
+
+    public VariableCondition(String variablename, String value) {
+        super();
+        this.variablename = variablename;
+        this.value = value;
+    }
+
+    public VariableCondition() {
+        super();
+    }
+
+    public String getValue() {
+        return value;
+    }
+
+    public void setValue(String value) {
+        this.value = value;
+    }
+
+    public String getVariablename() {
+        return variablename;
+    }
+
+    public void setVariablename(String variablename) {
+        this.variablename = variablename;
+    }
+
+    /* (non-Javadoc)
+    * @see de.reddot.installer.util.Condition#isTrue()
+    */
+    public boolean isTrue(Properties variables) {
+        String val = variables.getProperty(variablename);
+        if (val == null) {
+            return false;
+        } else {
+            return val.equals(value);
+        }
+    }
+
+    /* (non-Javadoc)
+    * @see de.reddot.installer.rules.Condition#readFromXML(net.n3.nanoxml.XMLElement)
+    */
+    public void readFromXML(XMLElement xmlcondition) {
+        try {
+            this.variablename = xmlcondition.getFirstChildNamed("name").getContent();
+            this.value = xmlcondition.getFirstChildNamed("value").getContent();
+        }
+        catch (Exception e) {
+            Debug.log("missing element in <condition type=\"variable\"/>");
+        }
+
+    }
+}
\ No newline at end of file

Added: izpack-src/trunk/src/lib/com/izforge/izpack/rules/XOrCondition.java
===================================================================
--- izpack-src/trunk/src/lib/com/izforge/izpack/rules/XOrCondition.java	2006-11-13 08:16:19 UTC (rev 1638)
+++ izpack-src/trunk/src/lib/com/izforge/izpack/rules/XOrCondition.java	2006-11-13 08:17:10 UTC (rev 1639)
@@ -0,0 +1,60 @@
+package com.izforge.izpack.rules;
+
+import java.util.Properties;
+
+import net.n3.nanoxml.XMLElement;
+import com.izforge.izpack.util.Debug;
+
+
+/**
+ * @author Dennis Reil, <Dennis.Reil at reddot.de>
+ * @version $Id: XOrCondition.java,v 1.1 2006/09/29 14:40:38 dennis Exp $
+ */
+public class XOrCondition extends OrCondition {
+    /**
+     *
+     */
+    public XOrCondition() {
+        super();
+        // TODO Auto-generated constructor stub
+    }
+
+    /**
+     * @param operand1
+     * @param operand2
+     */
+    public XOrCondition(Condition operand1, Condition operand2) {
+        super(operand1, operand2);
+    }
+
+    /* (non-Javadoc)
+    * @see de.reddot.installer.util.OrCondition#isTrue()
+    */
+    public boolean isTrue(Properties variables) {
+        boolean op1true = leftoperand.isTrue(variables);
+        boolean op2true = rightoperand.isTrue(variables);
+
+        if (op1true && op2true) {
+            // in case where both are true
+            return false;
+        }
+        return op1true || op2true;
+    }
+
+    /* (non-Javadoc)
+    * @see de.reddot.installer.rules.Condition#readFromXML(net.n3.nanoxml.XMLElement)
+    */
+    public void readFromXML(XMLElement xmlcondition) {
+        try {
+            if (xmlcondition.getChildrenCount() != 2) {
+                Debug.log("xor-condition needs two conditions as operands");
+                return;
+            }
+            this.leftoperand = RulesEngine.analyzeCondition(xmlcondition.getChildAtIndex(0));
+            this.rightoperand = RulesEngine.analyzeCondition(xmlcondition.getChildAtIndex(1));
+        }
+        catch (Exception e) {
+            Debug.log("missing element in xor-condition");
+        }
+    }
+}




More information about the izpack-changes mailing list