⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 staxparser.java

📁 Software Testing Automation Framework (STAF)的开发代码
💻 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.xml.sax.ErrorHandler;import org.xml.sax.EntityResolver;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import org.xml.sax.SAXNotRecognizedException;import org.xml.sax.SAXNotSupportedException;import org.w3c.dom.Node;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.NodeList;import org.w3c.dom.Element;import java.io.*;public class STAXParser implements ErrorHandler, EntityResolver{    public STAXParser(STAX staxService) throws SAXNotRecognizedException,                                               SAXNotSupportedException    {      fSTAX = staxService;      fParser.setErrorHandler(this);      fParser.setFeature(          "http://apache.org/xml/features/dom/include-ignorable-whitespace",          false);            fParser.setFeature("http://xml.org/sax/features/validation", true);              // Use a custom EntityResolver to intercept the external DTD entity      //  and replace the DTD with a STAX DTD held in a memory buffer.      fParser.setEntityResolver(this);    }    public STAXJob parse(InputSource xmlSource) throws STAXException    {        STAXJob job = null;	  try	  {	    	    fParser.parse(xmlSource);            job = new STAXJob(fSTAX);                        Node root = fParser.getDocument().getDocumentElement();            root.normalize();            NamedNodeMap attrs = root.getAttributes();                        for (int i = 0; i < attrs.getLength(); ++i)            {                Node thisAttr = attrs.item(i);            }            NodeList children = root.getChildNodes();                        for (int i = 0; i < children.getLength(); ++i)            {                Node thisChild = children.item(i);                if ((thisChild.getNodeType() == Node.ELEMENT_NODE) &&                    (thisChild.getNodeName().equals("defaultcall")))                {                    handleDefaultFunction(job, thisChild);                }                else if (thisChild.getNodeType() == Node.COMMENT_NODE)                {                    /* Do nothing */                }                else if (thisChild.getNodeType() == Node.ELEMENT_NODE)                {                    STAXActionFactory factory =                         fSTAX.getActionFactory(thisChild.getNodeName());                    if (factory == null)                    {                        throw new STAXInvalidXMLElementException(                                  thisChild.getNodeName());                    }                    STAXAction action = factory.parseAction(fSTAX, job,                                                            thisChild);                    if (thisChild.getNodeName().equals("function"))                    {                        job.addFunction((STAXFunctionAction)action);                    }                    else if (thisChild.getNodeName().equals("signalhandler"))                    {                        job.addDefaultAction(action);                    }                    else if (thisChild.getNodeName().equals("script"))                    {                        job.addDefaultAction(action);                    }                    /*else if (thisChild.getNodeName().equals("import"))                    {                        job.addDefaultAction(action);                    }*/                    else                    {                        throw new STAXInvalidXMLElementException(                                  thisChild.getNodeName());                    }                }                else                {                    throw new STAXInvalidXMLNodeTypeException(                              Integer.toString(thisChild.getNodeType()));                }            }	}	catch (java.io.IOException e)	{            throw new STAXException("IOError: " + e.getMessage());	}	catch (SAXException e)	{            throw new STAXXMLParseException(e.getMessage());	}        return job;    }    private void handleDefaultFunction(STAXJob job,                                       Node root) throws STAXException    {        NamedNodeMap attrs = root.getAttributes();        for (int i = 0; i < attrs.getLength(); ++i)        {            Node thisAttr = attrs.item(i);            if (thisAttr.getNodeName().equals("function"))            {                job.setStartFunction(thisAttr.getNodeValue());                // Set function arguments to null by default                job.setStartFuncArgs(null);            }            else            {                throw new STAXInvalidXMLAttributeException(                          root.getNodeName() + ": " + thisAttr.getNodeName());            }        }        NodeList children = root.getChildNodes();        String errorInfo = "\n  Element: " + root.getNodeName();        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.TEXT_NODE)            {                job.setStartFuncArgs(STAXUtil.parseAndCompileForPython(                    thisChild.getNodeValue(), errorInfo));            }            else            {                throw new STAXInvalidXMLNodeTypeException(                          Integer.toString(thisChild.getNodeType()));            }        }    }    // ErrorHandler interface methods    public void warning(SAXParseException e)    {        // Ignore warnings    }    public void error(SAXParseException e) throws SAXException    {        throw new SAXException("\nLine " + e.getLineNumber() +                               ": " + e.getMessage());    }    public void fatalError(SAXParseException e) throws SAXException    {        throw new SAXException("\nLine " + e.getLineNumber() +                               ": " + e.getMessage());    }    // EntityResolver interface methods     public InputSource resolveEntity(String publicId, String systemId)                                  throws java.io.FileNotFoundException    {        // Intercept the external DTD entity in the XML document        // and replace it with a common DTD in memory.        StringReader reader = new StringReader(fSTAX.getDTD());        return new InputSource(reader);     }     STAX fSTAX;    org.apache.xerces.parsers.DOMParser fParser =	new org.apache.xerces.parsers.DOMParser();}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -