/*
 * Copyright (c) 2004-2007, Dennis M. Sosnoski. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
 * following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following
 * disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
 * following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of
 * JiBX nor the names of its contributors may be used to endorse or promote products derived from this software without
 * specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.jibx.ws.wsdl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Top-level component of WSDL definition.
 * 
 * @author Dennis M. Sosnoski
 */
public class Definitions
{
    /** Transport specification for SOAP over HTTP. */
    public static final String HTTP_TRANSPORT = "http://schemas.xmlsoap.org/soap/http";
    
    /** Supported style value. */
    public static final String STYLE_DOCUMENT = "document";
    
    /** Prefix for WSDL target namespace. */
    private final String m_wsdlPrefix;
    
    /** Target namespace for WSDL. */
    private final String m_wsdlNamespace;
    
    /** Name for port type. */
    private final String m_portTypeName;
    
    /** Name for binding. */
    private final String m_bindingName;
    
    /** Name for service. */
    private final String m_serviceName;
    
    /** Name for port. */
    private final String m_portName;
    
    /** Schema definition holders. */
    private final ArrayList m_schemas;
    
    /** Map from namespace URIs to prefixes. */
    private final Map m_uriPrefixMap;
    
    /** Message definitions. */
    private final ArrayList m_messages;
    
    /** Operation definitions. */
    private final ArrayList m_operations;
    
    /** Documentation for the portType. */
    private List m_portTypeDocumentation;
    
    /** Service location URL. */
    private String m_serviceLocation;
    
    /**
     * Standard constructor.
     * 
     * @param tname port type name
     * @param bname binding name
     * @param sname service name
     * @param pname port name
     * @param wpfx prefix for WSDL target namespace
     * @param wuri WSDL target namespace
     * @param spfx prefix for schema target namespace
     * @param suri schema target namespace
     */
    public Definitions(String tname, String bname, String sname, String pname, String wpfx, String wuri, String spfx,
        String suri) {
        m_portTypeName = tname;
        m_bindingName = bname;
        m_serviceName = sname;
        m_portName = pname;
        m_wsdlPrefix = wpfx;
        m_wsdlNamespace = wuri;
        m_schemas = new ArrayList();
        m_uriPrefixMap = new HashMap();
        m_messages = new ArrayList();
        m_operations = new ArrayList();
        m_uriPrefixMap.put(wuri, wpfx);
        if (suri != null && !wuri.equals(suri)) {
            m_uriPrefixMap.put(suri, spfx);
        }
    }
    
    /**
     * Set service location.
     * 
     * @param sloc service location URL string
     */
    public void setServiceLocation(String sloc) {
        m_serviceLocation = sloc;
    }
    
    /**
     * Add message definition.
     * 
     * @param msg message definition
     */
    public void addMessage(Message msg) {
        m_messages.add(msg);
    }
    
    /**
     * Add operation definition.
     * 
     * @param op operation definition
     */
    public void addOperation(Operation op) {
        m_operations.add(op);
    }
    
    /**
     * Get port type name.
     * 
     * @return port type name
     */
    public String getPortTypeName() {
        return m_portTypeName;
    }
    
    /**
     * Get binding name.
     * 
     * @return binding name
     */
    public String getBindingName() {
        return m_bindingName;
    }
    
    /**
     * Get service name.
     * 
     * @return service name
     */
    public String getServiceName() {
        return m_serviceName;
    }
    
    /**
     * Get port name.
     * 
     * @return port name
     */
    public String getPortName() {
        return m_portTypeName;
    }
    
    /**
     * Get WSDL target namespace prefix.
     * 
     * @return target namespace prefix
     */
    public String getWsdlPrefix() {
        return m_wsdlPrefix;
    }
    
    /**
     * Get WSDL target namespace URI.
     * 
     * @return target namespace
     */
    public String getWsdlNamespace() {
        return m_wsdlNamespace;
    }
    
    /**
     * Get schema definition holders.
     * 
     * @return schemas
     */
    public ArrayList getSchemas() {
        return m_schemas;
    }
    
    /**
     * Get service location.
     * 
     * @return service location URL string
     */
    public String getServiceLocation() {
        return m_serviceLocation;
    }
    
    /**
     * Get portType documentation.
     * 
     * @return list of nodes
     */
    public List getPortTypeDocumentation() {
        return m_portTypeDocumentation;
    }
    
    /**
     * Set portType documentation.
     * 
     * @param nodes list of nodes
     */
    public void setPortTypeDocumentation(List nodes) {
        m_portTypeDocumentation = nodes;
    }
    
    /**
     * Get messages.
     * 
     * @return list of messages
     */
    public ArrayList getMessages() {
        return m_messages;
    }
    
    /**
     * Get operations.
     * 
     * @return list of operations
     */
    public ArrayList getOperations() {
        return m_operations;
    }
    
    /**
     * Get the prefix for a namespace URI. The first time this is called for a particular namespace URI a prefix is
     * assigned and returned.
     * 
     * @param uri
     * @return prefix
     */
    public String getPrefix(String uri) {
        if (uri == null) {
            return "";
        } else {
            String prefix = (String)m_uriPrefixMap.get(uri);
            if (prefix == null) {
                prefix = "ns" + (m_uriPrefixMap.size() - 1);
                m_uriPrefixMap.put(uri, prefix);
            }
            return prefix;
        }
    }
    
    /**
     * Get the complete set of namespace URIs used by this definition.
     * 
     * @return set of URIs
     */
    public Set getNamespaces() {
        return m_uriPrefixMap.keySet();
    }
}