SaajUtils { public static Name toName(QName qName, SOAPElement resolveElement) throws SOAPException { String qNamePrefix = qName.getPrefix(); SOAPEnvelope envelope = getEnvelope(resolveElement); if (StringUtils.hasLength(qName.getNamespaceURI()) && StringUtils.hasLength(qNamePrefix)) { return envelope.createName(qName.getLocalPart(), qNamePrefix, qName.getNamespaceURI()); } else if (StringUtils.hasLength(qName.getNamespaceURI())) { Iterator<?> prefixes; if (getSaajVersion(resolveElement) == SAAJ_11) { prefixes = resolveElement.getNamespacePrefixes(); } else { prefixes = resolveElement.getVisibleNamespacePrefixes(); } while (prefixes.hasNext()) { String prefix = (String) prefixes.next(); if (qName.getNamespaceURI().equals(resolveElement.getNamespaceURI(prefix))) { return envelope.createName(qName.getLocalPart(), prefix, qName.getNamespaceURI()); } } return envelope.createName(qName.getLocalPart(), "", qName.getNamespaceURI()); } else { return envelope.createName(qName.getLocalPart()); } }  static int getSaajVersion(); static int getSaajVersion(SOAPMessage soapMessage); static int getSaajVersion(SOAPElement soapElement); static String getSaajVersionString(); static Name toName(QName qName, SOAPElement resolveElement); static QName toQName(Name name); static SOAPMessage loadMessage(Resource resource, MessageFactory messageFactory); static SOAPEnvelope getEnvelope(SOAPElement element); static SOAPElement getFirstBodyElement(SOAPBody body); static final int SAAJ_11; static final int SAAJ_12; static final int SAAJ_13; }
@Test public void testToName() throws Exception { SOAPMessage message = messageFactory.createMessage(); QName qName = new QName("localPart"); SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); Name name = SaajUtils.toName(qName, envelope); Assert.assertNotNull("Invalid name", name); Assert.assertEquals("Invalid local part", qName.getLocalPart(), name.getLocalName()); Assert.assertFalse("Invalid prefix", StringUtils.hasLength(name.getPrefix())); Assert.assertFalse("Invalid namespace", StringUtils.hasLength(name.getURI())); }