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

📄 wsdlreader.java

📁 一个完整的XACML工程,学习XACML技术的好例子!
💻 JAVA
字号:
/*
 * WSDLReader.java - 12/09/2006
 */

package issrg.editor2.WSDLandTAP;

import issrg.editor2.configurations.TAPFileParserErrorHandler;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * This class helps loading a WSDL File to a DOM. 
 * It also helps obtain the targets/actions.
 *
 * @author Christian Azzopardi
 */
public class WSDLReader {
    
    Document WSDLdom;
    
    private static final String WSDL_NS = "http://schemas.xmlsoap.org/wsdl/";
    private static final String XMLSCHEMA_NS = "http://www.w3.org/2001/XMLSchema";
    private static final String WSDL_SOAP_NS = "http://schemas.xmlsoap.org/wsdl/soap/";
    
    /** Creates a new instance of WSDLReader */
    public WSDLReader(String wsdlPath) {
        this.WSDLdom = loadDocument(wsdlPath);
    }
    
    public Document loadDocument(String pathName) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        Document document = null;
        try {
            FileInputStream pathStream = new FileInputStream(pathName);
            DocumentBuilder builder = factory.newDocumentBuilder();
            builder.setErrorHandler(new TAPFileParserErrorHandler());
            document = builder.parse(pathStream);
        } catch (FileNotFoundException fnfe) { } catch (SAXException sxe) { } catch (ParserConfigurationException pce) { } catch (IOException ioe) { } catch (SecurityException se) { }
        
        return document;
    }
    
    public Element getWSDLTarget() {
        Element service = (Element)WSDLdom.getElementsByTagNameNS(WSDL_NS, "service").item(0);
        if (service == null) return null;
        Element target = (Element)service.getElementsByTagNameNS(WSDL_NS, "port").item(0);
        return target;
    }
    
    public Element getWSDLTargetAddress() {
        if (getWSDLTarget() == null) return null;
        return (Element)getWSDLTarget().getElementsByTagNameNS(WSDL_SOAP_NS, "address").item(0);
    }
    
    public Vector getActions() {
        NodeList operation = WSDLdom.getElementsByTagNameNS(WSDL_NS, "operation");
        Node portType = WSDLdom.getElementsByTagNameNS(WSDL_NS, "portType").item(0);

        Vector ht = new Vector();
        for (int j=0; j<operation.getLength(); j++) {
            Element e = (Element)operation.item(j);
            if (e.getParentNode() == portType) {
                Vector action = new Vector();
                action.add(e.getAttribute("name"));
                action.add((Element)e.getElementsByTagNameNS(WSDL_NS, "input").item(0));
                ht.add(action);
            }
        }
        return ht;
    }
    
    public Vector getParameters(String action, Element input) {
        String types = "";
        String names = "";
        Vector argVector = new Vector();
        Vector result = new Vector();
        NodeList messages = WSDLdom.getElementsByTagNameNS(WSDL_NS, "message");
        for (int i=0; i<messages.getLength(); i++) {
            Element e = (Element)messages.item(i);
            if (input.getAttribute("name") == null || input.getAttribute("name").equals("")) {
                String inputMessage = removeColon(input.getAttribute("message"));
                if (e.getAttribute("name").intern().equals(inputMessage)) {
                    NodeList parts = e.getElementsByTagNameNS(WSDL_NS, "part");
                    for (int j=0; j<parts.getLength(); j++) {
                        Element el = (Element)parts.item(j);
                        if (el.getAttribute("element") != null || !el.getAttribute("element").equals("")) {
                            NodeList elements = WSDLdom.getElementsByTagNameNS(XMLSCHEMA_NS, "element");
                            for (int k=0; k<elements.getLength(); k++) {
                                Element elem = (Element)elements.item(k);
                                String elemName = removeColon(el.getAttribute("element"));
                                if (elem.getAttribute("name").intern().equals(elemName)) {
                                    if (elem.getAttribute("type").equals("")) {
                                        if (elem.getElementsByTagNameNS(XMLSCHEMA_NS, "complexType").getLength() == 1) {
                                            argVector.add("1");
                                            argVector.add("complexType");
                                            result.add(argVector);
                                            argVector = new Vector();
                                        }
                                    } else { // else if (elem.getAttribute("type") != null || !elem.getAttribute("type").equals("")) {
                                        argVector.add("1");
                                        argVector.add(elem.getAttribute("type"));
                                        result.add(argVector);
                                        argVector = new Vector();
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                if (e.getAttribute("name").intern().equals(input.getAttribute("name"))) {
                    NodeList parts = e.getElementsByTagNameNS(WSDL_NS, "part");
                    for (int j=0; j<parts.getLength(); j++) {
                        Element el = (Element)parts.item(j);
                        if (el.getAttribute("element") == null ||
                            el.getAttribute("element").equals("")) {
                            argVector.add(el.getAttribute("name"));
                            argVector.add(removeColon(el.getAttribute("type")));
                            result.add(argVector);
                            argVector = new Vector();
                        } 
                    }
                    //
                }
            }
        }
        return result;
    }
    
    public String removeColon(String str) {
        int index = str.indexOf(":");
        if (index > -1) str = str.substring(index+1);
        return str;
    }
}

⌨️ 快捷键说明

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