<commit_before>////////////////////////////////////////////////////////////////////////////////
//
//  ADOBE SYSTEMS INCORPORATED
//  Copyright 2006-2007 Adobe Systems Incorporated
//  All Rights Reserved.
//
//  NOTICE: Adobe permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////

package mx.rpc.wsdl
{

import mx.logging.ILogger;
import mx.logging.Log;
import mx.resources.IResourceManager;
import mx.resources.ResourceManager;
import mx.rpc.Fault;
import mx.rpc.soap.SOAPConstants;
import mx.rpc.xml.QualifiedResourceManager;
import mx.rpc.xml.Schema;
import mx.rpc.xml.SchemaConstants;
import mx.rpc.xml.SchemaManager;
import mx.rpc.xml.SchemaTypeRegistry;

[ResourceBundle("rpc")]

[ExcludeClass]

/**
 * Manages a WSDL top-level <code>definitions</code> element. WSDL definitions
 * may contain imports to other WSDL definitions. Only SOAP bindings are
 * supported.
 * 
 * @private
 */
public class WSDL
{
    //--------------------------------------------------------------------------
    //
    // Constructor
    // 
    //--------------------------------------------------------------------------

    /**
     * Constructs a WSDL from XML. Services and their operations are
     * parsed into object representations on the first call to
     * <code>getService()</code>, <code>getPort()</code> or
     * <code>getOperation()</code>.
     *
     * @param xml An XML document starting from the top-level WSDL 
     * <code>defintions</code> element.
     *  
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    public function WSDL(xml:XML, topLevelManager:SchemaManager=null)
    {
        super();
        
        // Fix for SDK-18926
        // If this wsdl was imported, the top-level wsdl should pass it's own schemaManager
        // which we should reuse.
        if (topLevelManager != null)
        {
            _schemaManager = topLevelManager;
        }
        _xml = xml;
        _log = Log.getLogger("mx.rpc.wsdl.WSDL");
        processNamespaces();
        processSchemas();
    }

    //--------------------------------------------------------------------------
    //
    // Variables
    // 
    //--------------------------------------------------------------------------

    /**
     *  @private
     */
    private var resourceManager:IResourceManager =
									ResourceManager.getInstance();

    //--------------------------------------------------------------------------
    //
    // Properties
    // 
    //--------------------------------------------------------------------------

    public function get schemaManager():SchemaManager
    {
        if (_schemaManager == null)
        {
            _schemaManager = new SchemaManager();
            _schemaManager.schemaConstants = schemaConstants;
        }

        return _schemaManager;
    }

    public function get schemaConstants():SchemaConstants
    {
        if (_schemaConstants == null)
            _schemaConstants = SchemaConstants.getConstants(xml);

        return _schemaConstants;
    }

    public function get soapConstants():SOAPConstants
    {
        if (_soapConstants == null)
            _soapConstants = SOAPConstants.getConstants(xml);
        
        return _soapConstants;
    }

    public function get targetNamespace():Namespace
    {
        return _targetNamespace;
    }

    public function get wsdlConstants():WSDLConstants
    {
        if (_wsdlConstants == null)
            _wsdlConstants = WSDLConstants.getConstants(xml);

        return _wsdlConstants;
    }

    /**
     * The raw XML representing the WSDL starting from the top-level
     * definitions element.
     *  
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    public function get xml():XML
    {
        return _xml;
    }

    //--------------------------------------------------------------------------
    //
    // Methods
    // 
    //--------------------------------------------------------------------------

    /*
        FIXME:
        1. Validate that the targetNamespace matches the one defined on the
           import XML
        2. Also, check that the import being added does not cause a cyclic
           relationship.
    *  
    *  @langversion 3.0
    *  @playerversion Flash 9
    *  @playerversion AIR 1.1
    *  @productversion Flex 3
    */
    public function addImport(targetNamespace:Namespace, wsdl:WSDL):void
    {
        if (importsManager == null)
            importsManager = new QualifiedResourceManager();

        importsManager.addResource(targetNamespace, wsdl);
    }

    public function addSchema(schema:Schema):void
    {
        schemaManager.addSchema(schema);
    }

    public function getOperation(operationName:String, serviceName:String = null, portName:String = null):WSDLOperation
    {
        var port:WSDLPort = getPort(serviceName, portName);
        var binding:WSDLBinding = port.binding;
        var portType:WSDLPortType = binding.portType;
        var operation:WSDLOperation = portType.getOperation(operationName);
        return operation;
    }

    /**
     * Locate a port for a service by name. If a serviceName is provided, a
     * service is first located, otherwise the first service is used by default.
     * Once a service has been selected, the search then considers the port.
     * If a portName is not provided, the first port is used by default.
     *  
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    public function getPort(serviceName:String = null, portName:String = null):WSDLPort
    {
        var port:WSDLPort;

        var service:WSDLService = getService(serviceName, portName);
        if (service != null)
        {
            if (portName == null)
                port = service.defaultPort;
            else
                port = service.getPort(portName);
        }

        if (port == null)
        {
			var message:String = resourceManager.getString(
				"rpc", "noServiceAndPort", [ serviceName, portName ]);
            throw new Fault("Client.NoSuchPort", message);
        }

        return port;
    }

    /**
     * Search for WSDL service and port by name. If a serviceName is not
     * provided the first service found will be selected by default. 
     *  
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    public function getService(serviceName:String = null, portName:String = null):WSDLService
    {
        var service:WSDLService;
        var port:WSDLPort;

        if (serviceMap != null && serviceName != null)
        {    
            service = serviceMap[serviceName];
            if (service != null)
            {
                if (portName != null)
                    port = service.getPort(portName);

                if (port == null)
                {
                    var tempService:WSDLService = parseService(serviceName, portName);
                    for each (var p:WSDLPort in tempService.ports)
                    {
                        service.addPort(p);
                    }
                }
            }
        }

        // We didn't find a service, so we parse to find one...
        if (service == null)
        {
            service = parseService(serviceName, portName);

			var message:String;
			var detail:String;

            if (service != null)
            {
                if (serviceMap == null)
                    serviceMap = {};

                serviceMap[service.name] = service;
            }
            else if (serviceName != null)
            {
				message = resourceManager.getString(
					"rpc", "noSuchServiceInWSDL", [ serviceName ]);
                throw new Fault("Client.NoSuchService", message);
            }
            else
            {
				message = resourceManager.getString(
					"rpc", "noServiceElement");
				detail = resourceManager.getString(
					"rpc", "noServiceElement.details", [ "" ]);
                throw new Fault("Server.NoServicesInWSDL", message, detail);
            }
        }

        return service;
    }

    public function getTypes(targetNamespace:Namespace):XML
    {
        var types:XML;

        // First, check current WSDL for types element
        var typesXMLList:XMLList = xml.elements(wsdlConstants.typesQName);
        if (typesXMLList.length() > 0)
        {
            // There should only be one types element.
            types = typesXMLList[0];
        }
        else if (importsManager != null)
        {
            // Check WSDL imports for types element
            var imports:Array = importsManager.getResourcesForNamespace(targetNamespace);
            for each (var childWSDL:WSDL in imports)
            {
                types = childWSDL.getTypes(targetNamespace);
                if (types != null)
                    break;
            }
        }

        return types;
    }

    /**
     * Search for a requested service in the definitions XML, including all
     * WSDL imports. This is the usual entry point to start parsing the WSDL as
     * only one service and one of its port will be used at any time.
     * <p>
     * If the service name is not specified, the first service is used by
     * default. If a service is located it is parsed and then the search
     * continues for the requested port, which is in turn parsed, and so forth.
     * </p>
     *  
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    private function parseService(serviceName:String = null, portName:String = null):WSDLService
    {
        var service:WSDLService;
        var serviceXML:XML;

        // First, look for the service in this WSDL
        var serviceXMLList:XMLList = xml.elements(wsdlConstants.serviceQName);
        for each (var x:XML in serviceXMLList)
        {
            if (serviceName == null)
            {
                // Select the first service if a service name wasn't specified
                serviceXML = x;
                serviceName = x.@name.toString();
                break;
            }
            else if (x.@name == serviceName)
            {
                serviceXML = x;
                break;
            }
        }

        if (serviceXML != null)
        {
            // We have the service, now look for the port
            service = new WSDLService(serviceName);

            var port:WSDLPort = parsePort(service, serviceXML, portName);

            if (port != null)
            {
                service.addPort(port);
            }
        }
        else if (importsManager != null)
        {
            // Otherwise, look for the service in our WSDL imports (assuming
            // the port must be in the same import as the service).
            var imports:Array = importsManager.getResources();
            for each (var childWSDL:WSDL in imports)
            {
                service = childWSDL.parseService(serviceName, portName);
                if (service != null)
                    break;
            }
        }

        return service;
    }

    /**
     * Search for a requested port in the given service XML. If a port
     * name was not specified, the first port is used by default.
     *  
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    private function parsePort(service:WSDLService, serviceXML:XML, portName:String = null):WSDLPort
    {
        var port:WSDLPort;
        var portXML:XML;

        var portXMLList:XMLList = serviceXML.elements(wsdlConstants.portQName);
        for each (var x:XML in portXMLList)
        {
            if (portName == null)
            {
                portXML = x;
                portName = x.@name.toString();
                break;
            }
            else if (x.@name == portName)
            {
                portXML = x;
                break;
            }
        }

        if (portXML != null)
        {
            // We have the port, now look for the location and binding
            port = new WSDLPort(portName, service);

            var soapAddressXMLList:XMLList;
            soapAddressXMLList = portXML.elements(wsdlConstants.soapAddressQName);
            if (soapAddressXMLList.length() > 0)
            {
                port.endpointURI = soapAddressXMLList[0].@location.toString();
            }

            var prefixedBindingName:String = portXML.@binding.toString();
            var bindingQName:QName = schemaManager.getQNameForPrefixedName(prefixedBindingName, portXML);

            var binding:WSDLBinding = parseBinding(bindingQName);

            if (binding != null)
            {
                port.binding = binding;
            }
            else
            {
				var message:String = resourceManager.getString(
					"rpc", "unrecognizedBindingName",
					[ bindingQName.localName, bindingQName.uri ]);
                throw new Fault("WSDL.UnrecognizedBindingName", message);
            }
        }

        return port;
    }

    /**
     * Search for a binding by QName. If a URI is not specified, the
     * targetNamespace is assumed. FIXME: We may need to consider whether
     * formElementDefault is set to qualified or not.
     *  
     *  @langversion 3.0<commit_msg>Transferred from the Adobe Open Source Subversion Repository<commit_after>////////////////////////////////////////////////////////////////////////////////
//
//  ADOBE SYSTEMS INCORPORATED
//  Copyright 2006-2007 Adobe Systems Incorporated
//  All Rights Reserved.
//
//  NOTICE: Adobe permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////

package mx.rpc.wsdl
{

import mx.logging.ILogger;
import mx.logging.Log;
import mx.resources.IResourceManager;
import mx.resources.ResourceManager;
import mx.rpc.Fault;
import mx.rpc.soap.SOAPConstants;
import mx.rpc.xml.QualifiedResourceManager;
import mx.rpc.xml.Schema;
import mx.rpc.xml.SchemaConstants;
import mx.rpc.xml.SchemaManager;
import mx.rpc.xml.SchemaTypeRegistry;

[ResourceBundle("rpc")]

[ExcludeClass]

/**
 * Manages a WSDL top-level <code>definitions</code> element. WSDL definitions
 * may contain imports to other WSDL definitions. Only SOAP bindings are
 * supported.
 * 
 * @private
 */
public class WSDL
{
    //--------------------------------------------------------------------------
    //
    // Constructor
    // 
    //--------------------------------------------------------------------------

    /**
     * Constructs a WSDL from XML. Services and their operations are
     * parsed into object representations on the first call to
     * <code>getService()</code>, <code>getPort()</code> or
     * <code>getOperation()</code>.
     *
     * @param xml An XML document starting from the top-level WSDL 
     * <code>defintions</code> element.
     *  
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    public function WSDL(xml:XML, topLevelManager:SchemaManager=null)
    {
        super();
        
        // Fix for SDK-18926
        // If this wsdl was imported, the top-level wsdl should pass it's own schemaManager
        // which we should reuse.
        if (topLevelManager != null)
        {
            _schemaManager = topLevelManager;
        }
        _xml = xml;
        _log = Log.getLogger("mx.rpc.wsdl.WSDL");
        processNamespaces();
        processSchemas();
    }

    //--------------------------------------------------------------------------
    //
    // Variables
    // 
    //--------------------------------------------------------------------------

    /**
     *  @private
     */
    private var resourceManager:IResourceManager =
                                    ResourceManager.getInstance();

    //--------------------------------------------------------------------------
    //
    // Properties
    // 
    //--------------------------------------------------------------------------

    public function get schemaManager():SchemaManager
    {
        if (_schemaManager == null)
        {
            _schemaManager = new SchemaManager();
            _schemaManager.schemaConstants = schemaConstants;
        }

        return _schemaManager;
    }

    public function get schemaConstants():SchemaConstants
    {
        if (_schemaConstants == null)
            _schemaConstants = SchemaConstants.getConstants(xml);

        return _schemaConstants;
    }

    public function get soapConstants():SOAPConstants
    {
        if (_soapConstants == null)
            _soapConstants = SOAPConstants.getConstants(xml);
        
        return _soapConstants;
    }

    public function get targetNamespace():Namespace
    {
        return _targetNamespace;
    }

    public function get wsdlConstants():WSDLConstants
    {
        if (_wsdlConstants == null)
            _wsdlConstants = WSDLConstants.getConstants(xml);

        return _wsdlConstants;
    }

    /**
     * The raw XML representing the WSDL starting from the top-level
     * definitions element.
     *  
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    public function get xml():XML
    {
        return _xml;
    }

    //--------------------------------------------------------------------------
    //
    // Methods
    // 
    //--------------------------------------------------------------------------

    /*
        FIXME:
        1. Validate that the targetNamespace matches the one defined on the
           import XML
        2. Also, check that the import being added does not cause a cyclic
           relationship.
    */
    public function addImport(targetNamespace:Namespace, wsdl:WSDL):void
    {
        if (importsManager == null)
            importsManager = new QualifiedResourceManager();

        importsManager.addResource(targetNamespace, wsdl);
    }

    public function addSchema(schema:Schema):void
    {
        schemaManager.addSchema(schema);
    }

    public function getOperation(operationName:String, serviceName:String = null, portName:String = null):WSDLOperation
    {
        var port:WSDLPort = getPort(serviceName, portName);
        var binding:WSDLBinding = port.binding;
        var portType:WSDLPortType = binding.portType;
        var operation:WSDLOperation = portType.getOperation(operationName);
        return operation;
    }

    /**
     * Locate a port for a service by name. If a serviceName is provided, a
     * service is first located, otherwise the first service is used by default.
     * Once a service has been selected, the search then considers the port.
     * If a portName is not provided, the first port is used by default.
     *  
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    public function getPort(serviceName:String = null, portName:String = null):WSDLPort
    {
        var port:WSDLPort;

        var service:WSDLService = getService(serviceName, portName);
        if (service != null)
        {
            if (portName == null)
                port = service.defaultPort;
            else
                port = service.getPort(portName);
        }

        if (port == null)
        {
            var message:String = resourceManager.getString(
                "rpc", "noServiceAndPort", [ serviceName, portName ]);
            throw new Fault("Client.NoSuchPort", message);
        }

        return port;
    }

    /**
     * Search for WSDL service and port by name. If a serviceName is not
     * provided the first service found will be selected by default. 
     *  
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    public function getService(serviceName:String = null, portName:String = null):WSDLService
    {
        var service:WSDLService;
        var port:WSDLPort;

        if (serviceMap != null && serviceName != null)
        {    
            service = serviceMap[serviceName];
            if (service != null)
            {
                if (portName != null)
                    port = service.getPort(portName);

                if (port == null)
                {
                    var tempService:WSDLService = parseService(serviceName, portName);
                    for each (var p:WSDLPort in tempService.ports)
                    {
                        service.addPort(p);
                    }
                }
            }
        }

        // We didn't find a service, so we parse to find one...
        if (service == null)
        {
            service = parseService(serviceName, portName);

            var message:String;
            var detail:String;

            if (service != null)
            {
                if (serviceMap == null)
                    serviceMap = {};

                serviceMap[service.name] = service;
            }
            else if (serviceName != null)
            {
                message = resourceManager.getString(
                    "rpc", "noSuchServiceInWSDL", [ serviceName ]);
                throw new Fault("Client.NoSuchService", message);
            }
            else
            {
                message = resourceManager.getString(
                    "rpc", "noServiceElement");
                detail = resourceManager.getString(
                    "rpc", "noServiceElement.details", [ "" ]);
                throw new Fault("Server.NoServicesInWSDL", message, detail);
            }
        }

        return service;
    }

    public function getTypes(targetNamespace:Namespace):XML
    {
        var types:XML;

        // First, check current WSDL for types element
        var typesXMLList:XMLList = xml.elements(wsdlConstants.typesQName);
        if (typesXMLList.length() > 0)
        {
            // There should only be one types element.
            types = typesXMLList[0];
        }
        else if (importsManager != null)
        {
            // Check WSDL imports for types element
            var imports:Array = importsManager.getResourcesForNamespace(targetNamespace);
            for each (var childWSDL:WSDL in imports)
            {
                types = childWSDL.getTypes(targetNamespace);
                if (types != null)
                    break;
            }
        }

        return types;
    }

    /**
     * Search for a requested service in the definitions XML, including all
     * WSDL imports. This is the usual entry point to start parsing the WSDL as
     * only one service and one of its port will be used at any time.
     * <p>
     * If the service name is not specified, the first service is used by
     * default. If a service is located it is parsed and then the search
     * continues for the requested port, which is in turn parsed, and so forth.
     * </p>
     *  
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    private function parseService(serviceName:String = null, portName:String = null):WSDLService
    {
        var service:WSDLService;
        var serviceXML:XML;

        // First, look for the service in this WSDL
        var serviceXMLList:XMLList = xml.elements(wsdlConstants.serviceQName);
        for each (var x:XML in serviceXMLList)
        {
            if (serviceName == null)
            {
                // Select the first service if a service name wasn't specified
                serviceXML = x;
                serviceName = x.@name.toString();
                break;
            }
            else if (x.@name == serviceName)
            {
                serviceXML = x;
                break;
            }
        }

        if (serviceXML != null)
        {
            // We have the service, now look for the port
            service = new WSDLService(serviceName);

            var port:WSDLPort = parsePort(service, serviceXML, portName);

            if (port != null)
            {
                service.addPort(port);
            }
        }
        else if (importsManager != null)
        {
            // Otherwise, look for the service in our WSDL imports (assuming
            // the port must be in the same import as the service).
            var imports:Array = importsManager.getResources();
            for each (var childWSDL:WSDL in imports)
            {
                service = childWSDL.parseService(serviceName, portName);
                if (service != null)
                    break;
            }
        }

        return service;
    }

    /**
     * Search for a requested port in the given service XML. If a port
     * name was not specified, the first port is used by default.
     *  
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    private function parsePort(service:WSDLService, serviceXML:XML, portName:String = null):WSDLPort
    {
        var port:WSDLPort;
        var portXML:XML;

        var portXMLList:XMLList = serviceXML.elements(wsdlConstants.portQName);
        for each (var x:XML in portXMLList)
        {
            if (portName == null)
            {
                portXML = x;
                portName = x.@name.toString();
                break;
            }
            else if (x.@name == portName)
            {
                portXML = x;
                break;
            }
        }

        if (portXML != null)
        {
            // We have the port, now look for the location and binding
            port = new WSDLPort(portName, service);

            var soapAddressXMLList:XMLList;
            soapAddressXMLList = portXML.elements(wsdlConstants.soapAddressQName);
            if (soapAddressXMLList.length() > 0)
            {
                port.endpointURI = soapAddressXMLList[0].@location.toString();
            }

            var prefixedBindingName:String = portXML.@binding.toString();
            var bindingQName:QName = schemaManager.getQNameForPrefixedName(prefixedBindingName, portXML);

            var binding:WSDLBinding = parseBinding(bindingQName);

            if (binding != null)
            {
                port.binding = binding;
            }
            else
            {
                var message:String = resourceManager.getString(
                    "rpc", "unrecognizedBindingName",
                    [ bindingQName.localName, bindingQName.uri ]);
                throw new Fault("WSDL.UnrecognizedBindingName", message);
            }
        }

        return port;
    }

    /**
     * Search for a binding by QName. If a URI is not specified, the
     * targetNamespace is assumed. FIXME: We may need to consider whether
     * formElementDefault is set to qualified or not.
     *  
     *  @langversion 3.0
{'fraction_non_alphanumeric': 0.13664890899802962, 'fraction_numerical': 0.004378603225571043, 'mean_word_length': 1.502693817916172, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 32}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '126706', 'n_tokens_mistral': 7268, 'n_tokens_neox': 6810, 'n_words': 1971}