📄 staxfunctionactionfactory.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 java.util.HashSet;import org.w3c.dom.Node;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.NodeList;public class STAXFunctionActionFactory implements STAXActionFactory{ private static String fDTDInfo ="\n" +"<!--================= The Function Element ========================= -->\n" +"<!--\n" +" The function element defines a named task which can be called.\n" +" The name and scope attribute values are literals.\n" +" If desired, the function can be described using a function-prolog\n" +" element (or the deprecated function-description element) and/or a\n" +" function-epilog element. Also, if desired, the function element\n" +" can define the arguments that can be passed to the function.\n" +"-->\n" +"<!ELEMENT function ((function-prolog | function-description)?,\n" +" (function-epilog)?,\n" + " (function-no-args | function-single-arg |\n" +" function-list-args | function-map-args)?,\n" +" (%task;))>\n" +"<!ATTLIST function\n" +" name ID #REQUIRED\n" +" requires IDREFS #IMPLIED\n" +" scope (local | global) \"global\"\n" +">\n" +"\n" +"<!ELEMENT function-prolog (#PCDATA)>\n" +"\n" +"<!ELEMENT function-epilog (#PCDATA)>\n" +"\n" +"<!ELEMENT function-description (#PCDATA)>\n" +"\n" +"<!ELEMENT function-no-args EMPTY>\n" +"\n" +"<!ELEMENT function-single-arg (function-required-arg |\n" +" function-optional-arg |\n" +" function-arg-def)>\n" +"\n" +"<!ELEMENT function-list-args ((((function-required-arg+,\n" +" function-optional-arg*) |\n" +" (function-required-arg*,\n" +" function-optional-arg+)),\n" +" (function-other-args)?) |\n" +" function-arg-def+)>\n" +"\n" +"<!ELEMENT function-map-args (((function-required-arg |\n" +" function-optional-arg)+,\n" +" (function-other-args+)?) |\n" +" function-arg-def+)>\n" +"\n" +"<!ELEMENT function-required-arg (#PCDATA)>\n" +"<!ATTLIST function-required-arg\n" +" name CDATA #REQUIRED\n" +">\n" +"\n" +"<!ELEMENT function-optional-arg (#PCDATA)>\n" +"<!ATTLIST function-optional-arg\n" +" name CDATA #REQUIRED\n" +" default CDATA \"None\"\n" +">\n" +"\n" +"<!ELEMENT function-other-args (#PCDATA)>\n" +"<!ATTLIST function-other-args\n" +" name CDATA #REQUIRED\n" +">\n" +"\n" +"<!ELEMENT function-arg-def (function-arg-description?,\n" +" function-arg-private?,\n" +" function-arg-property*)>\n" +"<!ATTLIST function-arg-def\n" +" name CDATA #REQUIRED\n" +" type (required | optional | other) \"required\"\n" +" default CDATA \"None\"\n" +">\n" +"\n" +"<!ELEMENT function-arg-description (#PCDATA)>\n" +"\n" +"<!ELEMENT function-arg-private EMPTY>\n" +"\n" +"<!ELEMENT function-arg-property (function-arg-property-description?,\n" +" function-arg-property-data*)>\n" +"<!ATTLIST function-arg-property\n" +" name CDATA #REQUIRED\n" +" value CDATA #IMPLIED\n" +">\n" +"\n" +"<!ELEMENT function-arg-property-description (#PCDATA)>\n" +"\n" +"<!ELEMENT function-arg-property-data (function-arg-property-data)*>\n" +"<!ATTLIST function-arg-property-data\n" +" type CDATA #REQUIRED\n" +" value CDATA #IMPLIED\n" +">\n"; public String getDTDInfo() { return fDTDInfo; } public String getDTDTaskName() { return null; // The function element is not a task element. } public STAXAction parseAction(STAX staxService, STAXJob job, org.w3c.dom.Node root) throws STAXException { STAXFunctionAction function = new STAXFunctionAction(); String functionName = new String(); String scope = new String(); String requires = new String(); STAXAction functionAction = null; // Used to make sure all argument names are unique for a function HashSet argNameSet = new HashSet(); NamedNodeMap rootAttrs = root.getAttributes(); for (int i = 0; i < rootAttrs.getLength(); ++i) { Node thisAttr = rootAttrs.item(i); if (thisAttr.getNodeName().equals("name")) { function.setName(thisAttr.getNodeValue()); } else if (thisAttr.getNodeName().equals("scope")) { function.setScope(thisAttr.getNodeValue()); } else if (thisAttr.getNodeName().equals("requires")) { function.setRequires(thisAttr.getNodeValue()); } 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("function-description") || thisChild.getNodeName().equals("function-prolog")) { NamedNodeMap attrs = thisChild.getAttributes(); for (int j = 0; j < attrs.getLength(); ++j) { Node thisAttr = attrs.item(j); throw new STAXInvalidXMLAttributeException( thisChild.getNodeName() + ": " + thisAttr.getNodeName()); } function.setProlog(handleChild(thisChild)); } else if (thisChild.getNodeName().equals("function-epilog")) { NamedNodeMap attrs = thisChild.getAttributes(); for (int j = 0; j < attrs.getLength(); ++j) { Node thisAttr = attrs.item(j); throw new STAXInvalidXMLAttributeException( thisChild.getNodeName() + ": " + thisAttr.getNodeName()); } function.setEpilog(handleChild(thisChild)); } else if (thisChild.getNodeName().equals("function-no-args")) { function.setArgDefinition( STAXFunctionAction.FUNCTION_ALLOWS_NO_ARGS); NamedNodeMap attrs = thisChild.getAttributes(); for (int j = 0; j < attrs.getLength(); ++j) { Node thisAttr = attrs.item(j); throw new STAXInvalidXMLAttributeException( thisChild.getNodeName() + ": " + thisAttr.getNodeName()); } } else if (thisChild.getNodeName().equals("function-single-arg")) { function.setArgDefinition( STAXFunctionAction.FUNCTION_DEFINES_ONE_ARG); NamedNodeMap attrs = thisChild.getAttributes(); for (int j = 0; j < attrs.getLength(); ++j) { Node thisAttr = attrs.item(j); throw new STAXInvalidXMLAttributeException( thisChild.getNodeName() + ": " + thisAttr.getNodeName()); } // Iterate child element to get a required/option arg handleArg(thisChild, function, argNameSet); } else if (thisChild.getNodeName().equals("function-list-args")) { function.setArgDefinition( STAXFunctionAction.FUNCTION_DEFINES_LIST_ARGS); NamedNodeMap attrs = thisChild.getAttributes(); for (int j = 0; j < attrs.getLength(); ++j) { Node thisAttr = attrs.item(j); throw new STAXInvalidXMLAttributeException( thisChild.getNodeName() + ": " + thisAttr.getNodeName()); } // Iterate child elements to get required/optional args handleArg(thisChild, function, argNameSet); } else if (thisChild.getNodeName().equals("function-map-args")) { function.setArgDefinition( STAXFunctionAction.FUNCTION_DEFINES_MAP_ARGS); NamedNodeMap attrs = thisChild.getAttributes(); for (int j = 0; j < attrs.getLength(); ++j) { Node thisAttr = attrs.item(j); throw new STAXInvalidXMLAttributeException( thisChild.getNodeName() + ": " + thisAttr.getNodeName()); } // Iterate child elements to get required/optional args handleArg(thisChild, function, argNameSet); } else { if (functionAction != null) { throw new STAXInvalidXMLElementCountException( thisChild.getNodeName()); } STAXActionFactory factory = staxService.getActionFactory(thisChild.getNodeName()); if (factory == null) { throw new STAXInvalidXMLElementException( thisChild.getNodeName()); } functionAction = factory.parseAction(staxService, job, thisChild); function.setAction(functionAction); } } else { throw new STAXInvalidXMLNodeTypeException( Integer.toString(thisChild.getNodeType())); } } return function; } private String handleChild(Node root) throws STAXException { 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); // XXX: Should I be able to have a COMMENT_NODE here? if (thisChild.getNodeType() == Node.COMMENT_NODE) { /* Do nothing */ } else if (thisChild.getNodeType() == Node.CDATA_SECTION_NODE) { return thisChild.getNodeValue(); } else if (thisChild.getNodeType() == Node.TEXT_NODE) { String value = thisChild.getNodeValue().trim(); if (!value.equals("")) { return value; } } else { throw new STAXInvalidXMLNodeTypeException( Integer.toString(thisChild.getNodeType())); } } return new String(); } void handleArg(Node root, STAXFunctionAction function, HashSet argNameSet) throws STAXException { 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("function-required-arg")) { int type = STAXFunctionAction.ARG_REQUIRED;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -