if (serviceName == null && portName == null && address == null) {
throw new IllegalStateException("Address in an EPR cannot be null, "
+ " when serviceName or portName is null");
}
try {
W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
writer.setPrefix(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.NS_WSA);
writer.writeStartElement(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.WSA_ERF_NAME,
JAXWSAConstants.NS_WSA);
writer.writeNamespace(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.NS_WSA);
writer.writeStartElement(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.WSA_ADDRESS_NAME,
JAXWSAConstants.NS_WSA);
address = address == null ? "" : address;
writer.writeCharacters(address);
writer.writeEndElement();
if (referenceParameters != null) {
writer
.writeStartElement(JAXWSAConstants.WSA_PREFIX,
JAXWSAConstants.WSA_REFERENCEPARAMETERS_NAME, JAXWSAConstants.NS_WSA);
for (Element ele : referenceParameters) {
StaxUtils.writeElement(ele, writer, true);
}
writer.writeEndElement();
}
writer.writeStartElement(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.WSA_METADATA_NAME,
JAXWSAConstants.NS_WSA);
writer.writeNamespace(JAXWSAConstants.WSAW_PREFIX, JAXWSAConstants.NS_WSAW);
writer.writeNamespace(JAXWSAConstants.WSAM_PREFIX, JAXWSAConstants.NS_WSAM);
if (wsdlDocumentLocation != null) {
writer.writeNamespace(JAXWSAConstants.WSDLI_PFX,
JAXWSAConstants.NS_WSDLI);
writer.writeAttribute(JAXWSAConstants.WSDLI_PFX,
JAXWSAConstants.NS_WSDLI,
JAXWSAConstants.WSDLI_WSDLLOCATION,
wsdlDocumentLocation);
}
if (interfaceName != null) {
writer.writeStartElement(JAXWSAConstants.WSAM_PREFIX,
JAXWSAConstants.WSAM_INTERFACE_NAME,
JAXWSAConstants.NS_WSAM);
String portTypePrefix = interfaceName.getPrefix();
if (portTypePrefix == null || portTypePrefix.equals("")) {
portTypePrefix = "ns1";
}
writer.writeNamespace(portTypePrefix, interfaceName.getNamespaceURI());
writer.writeCharacters(portTypePrefix + ":" + interfaceName.getLocalPart());
writer.writeEndElement();
}
String serviceNamePrefix = null;
if (serviceName != null) {
serviceNamePrefix = (serviceName.getPrefix() == null || serviceName.getPrefix().length() == 0)
? "ns2" : serviceName.getPrefix();
writer.writeStartElement(JAXWSAConstants.WSAM_PREFIX,
JAXWSAConstants.WSAM_SERVICENAME_NAME,
JAXWSAConstants.NS_WSAM);
if (portName != null) {
writer.writeAttribute(JAXWSAConstants.WSAM_ENDPOINT_NAME, portName.getLocalPart());
}
writer.writeNamespace(serviceNamePrefix, serviceName.getNamespaceURI());
writer.writeCharacters(serviceNamePrefix + ":" + serviceName.getLocalPart());
writer.writeEndElement();
}
if (wsdlDocumentLocation != null) {
writer.writeStartElement(WSDLConstants.WSDL_PREFIX, WSDLConstants.QNAME_DEFINITIONS
.getLocalPart(), WSDLConstants.NS_WSDL11);
writer.writeNamespace(WSDLConstants.WSDL_PREFIX, WSDLConstants.NS_WSDL11);
writer.writeStartElement(WSDLConstants.WSDL_PREFIX,
WSDLConstants.QNAME_IMPORT.getLocalPart(),
WSDLConstants.QNAME_IMPORT.getNamespaceURI());
if (serviceName != null) {
writer.writeAttribute(WSDLConstants.ATTR_NAMESPACE, serviceName.getNamespaceURI());
}
writer.writeAttribute(WSDLConstants.ATTR_LOCATION, wsdlDocumentLocation);
writer.writeEndElement();
writer.writeEndElement();
}
if (metadata != null) {
for (Element e : metadata) {
StaxUtils.writeElement(e, writer, true);
}
}
writer.writeEndElement();
if (elements != null) {
for (Element e : elements) {
StaxUtils.writeElement(e, writer, true);
}
}
writer.writeEndElement();
writer.flush();
Unmarshaller unmarshaller = getJAXBContext().createUnmarshaller();
return (W3CEndpointReference)unmarshaller.unmarshal(writer.getDocument());
} catch (Exception e) {
throw new WebServiceException(new Message("ERROR_UNMARSHAL_ENDPOINTREFERENCE", LOG).toString(),
e);
}