/** * Reads an Xml node representation of a rule-set element. * @param emptyElt the empty xml node. */ public static RuleSet readXml(XmlNode ruleSetElt) { RuleSet rs = null; if (ruleSetElt == null) { return(rs); } Log log = Log.getOnly(); if (ruleSetElt.HasChildNodes) { // it contains rules string id = XmlFiler.getStringAttr((XmlElement)ruleSetElt, "id", NoValue); if (id.Equals(NoValue)) { log.writeElt("fail"); log.writeAttr("node", ruleSetElt.Name); log.writeAttr("expected", "id"); log.writeAttr("was", "not found"); log.endElt(); return(rs); } string desc = XmlFiler.getStringAttr((XmlElement)ruleSetElt, "desc", NoValue); rs = new RuleSet(id, desc); XmlAttributeCollection attrs = ruleSetElt.Attributes; for (int a = 0; a < attrs.Count; a++) { // read all attributes XmlNode attr = attrs.Item(a); string nn = attr.Name; if (!nn.Equals("id") && !nn.Equals("desc")) { rs.setParameter(attr.Name, attr.Value); } } XmlNodeList rules = ruleSetElt.ChildNodes; for (int r = 0; r < rules.Count; r++) { XmlNode ruleN = rules.Item(r); if (ruleN.NodeType == XmlNodeType.Element) { Rule rule = Rule.readXml((XmlElement)ruleN); rs.addRule(rule); } } } else // this is a truly empty node { log.writeElt("fail"); log.writeAttr("node", ruleSetElt.Name); log.writeAttr("expected", "rules"); log.writeAttr("was", "empty"); log.endElt(); } return(rs); }
/** * Parses the XML representation of a rule and creates a Rule * object based on it. * @param ruleElt the XML <rule> node and subnodes. * @return the rule that was read or null. */ public static Rule readXml(XmlElement ruleElt) { Rule rule = null; Log log = Log.getOnly(); if (ruleElt.Name.Equals("rule")) { // get <when> XmlNodeList nodes = ruleElt.ChildNodes; if (nodes == null || 0 == nodes.Count) { return(rule); } String id = XmlFiler.getStringAttr(ruleElt, "id", NoId); String desc = XmlFiler.getStringAttr(ruleElt, "desc", NoDesc); String fireA = XmlFiler.getStringAttr(ruleElt, "fire", NoDesc); bool fire = fireA.Equals("always"); ArrayList when = null; ArrayList actions = null; for (int n = 0; n < nodes.Count; n++) { // one when and many action elements XmlNode node = nodes.Item(n); if (node.Name.Equals("when") && when == null) { // process the one when element when = EmptyElement.readEmptyChildren(node); } else if (node.Name.Equals("when")) { // too many when elements log.writeElt("fail"); log.writeAttr("rule", id); log.writeAttr("when", "multiple"); log.endElt(); when = null; } else { // an action element if (node.NodeType == XmlNodeType.Element) { EmptyElement empty = EmptyElement.readXml(node); if (actions == null) { actions = new ArrayList(); } actions.Add(empty); } } } rule = new Rule(id, desc, fire, when, actions); } return(rule); }
/// <summary> /// Determines the result of a Simian action. /// When actions can't be performed the problem is logged /// and false is returned. /// </summary> /// <param name="actionRef">An action in a rule.</param> /// <returns>true if the action was initiated successfully.</returns> public bool doAction(EmptyElement actionRef) { if (actionRef.getName().Equals("launch")) { // launch the specified application bool usedModel = false; string path = actionRef.getValue("path"); if (path == null) { path = m_Config.getExePath(); usedModel = true; } string name = actionRef.getValue("name"); if (name == null) { name = m_Config.getExeName(); } string args = actionRef.getValue("args"); if (args == null && usedModel) { args = m_Config.getExeArgs(); } string work = actionRef.getValue("work"); if (work == null && usedModel) { work = m_Config.getWorkDir(); } return(LaunchApp(path, name, args, work)); } if (actionRef.getName().Equals("mark")) { // mark the node as indicated string id = actionRef.getValue("id"); // a VarNode if (id == null) { return(false); } string As = actionRef.getValue("as"); // How to mark the node // As == null is valid for removing the mark. return(m_views.Mark(id, As)); } if (actionRef.getName().Equals("free")) { // free the VarNode named string id = actionRef.getValue("id"); // the VarNode to free if (id == null) { return(false); } m_varNodes.add(id, null); } if (actionRef.getName().Equals("choose")) { // choose the control via the method and name it via id string control = actionRef.getValue("control"); // Type of the control to choose string id = actionRef.getValue("id"); // a VarNode string exclude = actionRef.getValue("exclude"); // How to choose string method = actionRef.getValue("method"); // How to choose if (!Utilities.isGoodStr(control)) { return(false); } if (!Utilities.isGoodStr(id)) { return(false); } if (!Utilities.isGoodStr(method)) { return(false); } MarkedNode mn = null; if (control.Equals("view")) { mn = m_views.Choose(method, exclude); } if (mn == null) { m_log.writeEltTime("fail"); m_log.writeAttr(control, mn.node.Name); m_log.writeAttr("was", "not known"); m_log.endElt(); return(false); } else { m_varNodes.add(id, mn); m_log.writeEltTime("selected"); m_log.writeAttr(control, mn.node.Name); m_log.writeAttr("as", id); m_log.endElt(); } } if (actionRef.getName().Equals("nav")) { // find a model path to the referenced node string to = actionRef.getValue("to"); // a VarNode string via = actionRef.getValue("via"); // a VarNode name, not set yet if (!Utilities.isGoodStr(to)) { return(false); } MarkedNode mn = m_varNodes.get(to); if (mn == null) { return(false); } // What kind of node is this? string role = XmlFiler.getStringAttr((XmlElement)mn.node, "role", "*not Found*"); if (role.Equals("*not Found*")) { return(false); } MarkedNode viaN = null; if (role.Equals("view")) { // get the menu node via mn and name it "via" string xPath = "menubar//" + mn.node.Name + "[@role='menu']"; XmlPath mPath = m_GuiModel.selectToXmlPath(null, xPath); if (mPath == null) { return(false); // really bad! } XmlNode menuNode = mPath.ModelNode; if (menuNode == null) { return(false); // really bad again! } viaN = new MarkedNode(menuNode, null); m_varNodes.add(via, viaN); } if (viaN == null) { return(false); // nothing more to do at the moment } } if (actionRef.getName().Equals("click")) { // click the specified control string id = actionRef.getValue("id"); // a VarNode string appPath = actionRef.getValue("on"); // an appPath string guiPath = actionRef.getValue("at"); // a giuPath string side = actionRef.getValue("side"); // "left" or "right" bool leftSide = true; if (side != null && side.Equals("right")) { leftSide = false; } // Id provides a context ah that must be used to find the rest of the path! // can't just use the appPath from it. What if it's a dialog? XmlNode context = null; MarkedNode mn = null; if (Utilities.isGoodStr(id)) { mn = m_varNodes.get(id); // bail out if id not defined yet. if (mn == null || mn.node == null) { return(false); } context = mn.node; } return(click(context, appPath, guiPath, leftSide)); } if (actionRef.getName().Equals("close")) { // close the specified application if (m_proc != null) { closeWindow(m_proc); } } return(true); }