package c.t.s.xfire;
import java.io.PrintWriter;
import java.util.Set;
import javax.wsdl.Definition;
import javax.wsdl.WSDLException;
import javax.wsdl.extensions.ExtensibilityElement;
import javax.wsdl.extensions.ExtensionRegistry;
import javax.wsdl.extensions.ExtensionSerializer;
import javax.xml.namespace.QName;
import com.ibm.wsdl.util.xml.DOMUtils;
final class Utils {
/** The URL of the partnerLinkType XML schema */
private static final String _PARTNER_LINK_TYPE_XSD_URI = "http://schemas.xmlsoap.org/ws/2003/05/partner-link/";
/** The prefix to use for the partnerLinkType namespace */
private static final String _PARTNER_LINK_TYPE_XSD_PREFIX = "plnk";
/** The name of the partnerLinkType XML element */
private static final String _PARTNER_LINK_TYPE_ELEMENT_NAME = "partnerLinkType";
/** The name of the role XML element */
private static final String _ROLE_ELEMENT_NAME = "role";
/** The name of the portType XML element */
private static final String _PORT_TYPE_ELEMENT_NAME = "portType";
/** The name attribute (used in partnerLinkType, role and portType XML elements) */
private static final String _NAME_ATTRIBUTE = "name";
/** A QName object for the partnerLinkType element */
private static final QName _PLT_ELEMENT_QNAME = new QName(_PARTNER_LINK_TYPE_XSD_URI, _PARTNER_LINK_TYPE_ELEMENT_NAME);
/**
* Private constructor; this class should not be instantiated
*
*/
private Utils() {
}
/**
* Add a partnerLinkType information to the given
* WSDL {@link Definition} using the information contained in
* the given {@link PartnerLinkType}.
*
* This method uses the
* WSDL4J
* Extension Architecture to define {@link PartnerLinkTypeElement} as a WSDL
* extension and uses a {@link PartnerLinkTypeElementSerializer} as a serializer
* for this extension.
*
* @param definition the WSDL definition to which the partnerLinkType
* element will be added
* @param partnerLinkType the partner link type information to add to the WSDL definition.
* @throws WSDLException if the underlying WSDL4J implementation decides to throw an exception
* (when it might do this is not stated in the Javadoc at v1.5.2)
*
*/
public static void addPartnerLinkType(Definition wsdlDefinition, PartnerLinkType partnerLinkType) throws WSDLException {
// add the PLTNamespace to the WSDL definition
wsdlDefinition.addNamespace(_PARTNER_LINK_TYPE_XSD_PREFIX, _PARTNER_LINK_TYPE_XSD_URI);
// Register the class type and serialiser with the extensions registry
// so that the WSDL writer knows what to call to write these elements
ExtensionRegistry extReg = wsdlDefinition.getExtensionRegistry();
extReg.registerSerializer(Definition.class, _PLT_ELEMENT_QNAME, new PartnerLinkTypeElementSerializer());
extReg.mapExtensionTypes(Definition.class, _PLT_ELEMENT_QNAME, PartnerLinkTypeElement.class);
// create the PLTElement
PartnerLinkTypeElement pltElement = (PartnerLinkTypeElement)extReg.createExtension(Definition.class, _PLT_ELEMENT_QNAME);
// set the wrapped object on the PLTElement
pltElement.setPartnerLinkType(partnerLinkType);
// add the PLTElement to the WSDL definition
wsdlDefinition.addExtensibilityElement(pltElement);
}
/**
* A class used to represent partnerLinkType XML element by the
* WSDL4J Extensions Architecture. This class wraps a
* {@link PartnerLinkType} object.
*/
public static class PartnerLinkTypeElement implements ExtensibilityElement {
// A representation of the fully-qualified name of the partnerLinkType tag */
protected QName _elementType;
// According to WSDL4J, "whether the semantics of this element are required" */
protected Boolean _required = Boolean.FALSE;
// The wrapped PartnerLinkType object
private PartnerLinkType _partnerLinkType;
/**
* Constructor.
*
* Note that because the constructor isn't called directly,
* a setter is used to insert the wrapped object.
*
* @param partnerLinkType the wrapped object
*/
public PartnerLinkTypeElement() {
}
/**
* Setter for the wrapped object
*
* @param partnerLinkType
*/
public void setPartnerLinkType(PartnerLinkType partnerLinkType) {
_partnerLinkType = partnerLinkType;
}
public QName getElementType() {
return _elementType;
}
public Boolean getRequired() {
return _required;
}
/**
* Getter for the name of the partnerLinkType
* element.
*
* @return the name of the partnerLinkType element
*/
public String getName() {
return _partnerLinkType.getName();
}
/**
* Getter for the roles defined on the partnerLinkType
* object.
*
* @return the roles defined on the partnerLinkType
* object
*/
public Set getRoles() {
// no need for a defensive copy here as the
// wrapped object does this
return _partnerLinkType.getRoles();
}
public void setElementType(QName elementType) {
this._elementType = elementType;
}
public void setRequired(Boolean required) {
this._required = required;
}
}
/**
* A class used to serialise a partnerLinkType XML element by the
* WSDL4J Extensions Architecture.
*/
private static class PartnerLinkTypeElementSerializer implements ExtensionSerializer {
/**
* Writes an XML representation of a partnerLinkType element to
* the given PrintWriter.
*/
public void marshall(Class parentType, QName elementType, ExtensibilityElement element, PrintWriter writer, Definition wsdlDefinition, ExtensionRegistry extReg) throws WSDLException {
PartnerLinkTypeElement pltElement = (PartnerLinkTypeElement)element;
// DOMUtils.getQualifiedValue ensures correct namespace prefix is used
String pltTagName = DOMUtils.getQualifiedValue(_PARTNER_LINK_TYPE_XSD_URI, _PARTNER_LINK_TYPE_ELEMENT_NAME, wsdlDefinition);
String roleTagName = DOMUtils.getQualifiedValue(_PARTNER_LINK_TYPE_XSD_URI, _ROLE_ELEMENT_NAME, wsdlDefinition);
String portTypeTagName = DOMUtils.getQualifiedValue(_PARTNER_LINK_TYPE_XSD_URI, _PORT_TYPE_ELEMENT_NAME, wsdlDefinition);
// TODO there has to be a better way of doing
// the indentation and the element delimiters
writer.print(" <" + pltTagName);
DOMUtils.printAttribute(_NAME_ATTRIBUTE, pltElement.getName(), writer);
writer.println(">");
for (PartnerLinkType.Role role : pltElement.getRoles()) {
writer.print(" <" + roleTagName);
DOMUtils.printAttribute(_NAME_ATTRIBUTE, role.getName(), writer);
writer.println(">");
writer.print(" <" + portTypeTagName);
DOMUtils.printQualifiedAttribute(_NAME_ATTRIBUTE, role.getPortTypeName(), wsdlDefinition, writer);
writer.println("/>");
writer.println(" " + roleTagName + ">");
}
writer.println(" " + pltTagName + ">");
}
}
}