📄 staxifactionfactory.java
字号:
/*****************************************************************************//* Software Testing Automation Framework (STAF) *//* (C) Copyright IBM Corp. 2002 *//* *//* This software is licensed under the Common Public License (CPL) V1.0. *//*****************************************************************************/package com.ibm.staf.service.stax;import org.w3c.dom.Node;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.NodeList;import java.util.List;import java.util.ArrayList;public class STAXIfActionFactory implements STAXActionFactory{ private static String fDTDInfo ="\n" +"<!--================= The Conditional Element (if-then-else) ======= -->\n" +"<!--\n" +" Allows you to write an if or a case construct with zero or more\n" +" elseifs and one or no else statements.\n" +"\n" +" The expr attribute value is evaluated via Python and must evaluate\n" +" to a boolean value.\n" +"-->\n" +"<!ELEMENT if ((%task;), elseif*, else?)>\n" +"<!ATTLIST if\n" +" expr CDATA #REQUIRED\n" +">\n" +"<!ELEMENT elseif (%task;)>\n" +"<!ATTLIST elseif\n" +" expr CDATA #REQUIRED\n" +">\n" +"<!ELEMENT else (%task;)>\n"; public String getDTDInfo() { return fDTDInfo; } public String getDTDTaskName() { return "if"; } public STAXAction parseAction(STAX staxService, STAXJob job, org.w3c.dom.Node root) throws STAXException { String ifExpression = new String(); STAXAction ifAction = null; List elseifs = new ArrayList(); STAXAction elseAction = null; NamedNodeMap attrs = root.getAttributes(); for (int i = 0; i < attrs.getLength(); ++i) { Node thisAttr = attrs.item(i); String errorInfo = "\n Element: " + root.getNodeName() + " Attribute: " + thisAttr.getNodeName(); if (thisAttr.getNodeName().equals("expr")) { ifExpression = STAXUtil.parseAndCompileForPython( thisAttr.getNodeValue(), errorInfo); } else { throw new STAXInvalidXMLAttributeException( root.getNodeName() + ": " + thisAttr.getNodeName()); } } NodeList children = root.getChildNodes(); for (int i = 0; i < children.getLength(); ++i) { Node thisChild = children.item(i); if (thisChild.getNodeType() == Node.COMMENT_NODE) { /* Do nothing */ } else if (thisChild.getNodeType() == Node.ELEMENT_NODE) { if (thisChild.getNodeName().equals("elseif")) { // Add STAXElseIf object to elseifs List elseifs.add(handleElseifChild(staxService, job, thisChild)); } else if (thisChild.getNodeName().equals("else")) { elseAction = handleElseChild(staxService, job, thisChild); } else // If Action { if (ifAction != null) { throw new STAXInvalidXMLElementCountException( thisChild.getNodeName()); } STAXActionFactory factory = staxService.getActionFactory(thisChild.getNodeName()); if (factory == null) { throw new STAXInvalidXMLElementException( thisChild.getNodeName()); } ifAction = factory.parseAction(staxService, job, thisChild); } } else { throw new STAXInvalidXMLNodeTypeException( Integer.toString(thisChild.getNodeType())); } } return new STAXIfAction(ifExpression, ifAction, elseifs, elseAction); } private STAXElseIf handleElseifChild(STAX staxService, STAXJob job, Node root) throws STAXException { String elseifExpression = new String(); STAXAction elseifAction = null; NamedNodeMap attrs = root.getAttributes(); for (int i = 0; i < attrs.getLength(); ++i) { Node thisAttr = attrs.item(i); String errorInfo = "\n Element: " + root.getNodeName() + " Attribute: " + thisAttr.getNodeName(); if (thisAttr.getNodeName().equals("expr")) { elseifExpression = STAXUtil.parseAndCompileForPython( thisAttr.getNodeValue(), errorInfo); } else { throw new STAXInvalidXMLAttributeException( root.getNodeName() + ": " + thisAttr.getNodeName()); } } NodeList children = root.getChildNodes(); for (int i = 0; i < children.getLength(); ++i) { Node thisChild = children.item(i); if (thisChild.getNodeType() == Node.COMMENT_NODE) { /* Do nothing */ } else if (thisChild.getNodeType() == Node.ELEMENT_NODE) { if (elseifAction != null) { throw new STAXInvalidXMLElementCountException( thisChild.getNodeName()); } STAXActionFactory factory = staxService.getActionFactory(thisChild.getNodeName()); if (factory == null) { throw new STAXInvalidXMLElementException( thisChild.getNodeName()); } elseifAction = factory.parseAction( staxService, job, thisChild); } else { throw new STAXInvalidXMLNodeTypeException( Integer.toString(thisChild.getNodeType())); } } return new STAXElseIf(elseifExpression, elseifAction); } private STAXAction handleElseChild(STAX staxService, STAXJob job, Node root) throws STAXException { STAXAction elseAction = null; NamedNodeMap attrs = root.getAttributes(); for (int i = 0; i < attrs.getLength(); ++i) { Node thisAttr = attrs.item(i); throw new STAXInvalidXMLAttributeException( root.getNodeName() + ": " + thisAttr.getNodeName()); } NodeList children = root.getChildNodes(); for (int i = 0; i < children.getLength(); ++i) { Node thisChild = children.item(i); if (thisChild.getNodeType() == Node.COMMENT_NODE) { /* Do nothing */ } else if (thisChild.getNodeType() == Node.ELEMENT_NODE) { if (elseAction != null) { throw new STAXInvalidXMLElementCountException( thisChild.getNodeName()); } STAXActionFactory factory = staxService.getActionFactory(thisChild.getNodeName()); if (factory == null) { throw new STAXInvalidXMLElementException( thisChild.getNodeName()); } elseAction = factory.parseAction(staxService, job, thisChild); } else { throw new STAXInvalidXMLNodeTypeException( Integer.toString(thisChild.getNodeType())); } } return elseAction; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -