Author: scheu Date: Tue Mar 30 19:09:01 2010 New Revision: 929230 URL: http://svn.apache.org/viewvc?rev=929230&view=rev Log: AXIS2-4669 Contributor: Min Zheng & Rich Scheuerle Preserve the attribute type information during the conversion from OM->SAAJ and SAAJ->OM Min has contributed a testcase to verify the code
Modified: axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/util/SAAJConverter.java axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/util/impl/SAAJConverterImpl.java axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/util/impl/XMLStreamReaderFromDOM.java axis/axis2/java/core/trunk/modules/jaxws/test/org/apache/axis2/jaxws/message/SAAJConverterTests.java Modified: axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/util/SAAJConverter.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/util/SAAJConverter.java?rev=929230&r1=929229&r2=929230&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/util/SAAJConverter.java (original) +++ axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/util/SAAJConverter.java Tue Mar 30 19:09:01 2010 @@ -31,6 +31,9 @@ import javax.xml.ws.WebServiceException; /** SAAJConverter Provides Conversion between SAAJ and OM Constructed via the SAAJConverterFactory */ public interface SAAJConverter { + + public final static String OM_ATTRIBUTE_KEY = "ATTRIBUTE_TYPE_KEY"; + /** * Convert OM SOAPEnvleope to SAAJ SOAPEnvelope * Modified: axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/util/impl/SAAJConverterImpl.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/util/impl/SAAJConverterImpl.java?rev=929230&r1=929229&r2=929230&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/util/impl/SAAJConverterImpl.java (original) +++ axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/util/impl/SAAJConverterImpl.java Tue Mar 30 19:09:01 2010 @@ -38,6 +38,7 @@ import org.apache.axis2.jaxws.utility.SA import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.dom.Node; +import org.w3c.dom.Attr; import javax.xml.namespace.QName; import javax.xml.soap.Detail; @@ -79,6 +80,10 @@ public class SAAJConverterImpl implement */ public SOAPEnvelope toSAAJ(org.apache.axiom.soap.SOAPEnvelope omEnvelope) throws WebServiceException { + if (log.isDebugEnabled()) { + log.debug("Converting OM SOAPEnvelope to SAAJ SOAPEnvelope"); + } + SOAPEnvelope soapEnvelope = null; try { // Build the default envelope @@ -167,6 +172,10 @@ public class SAAJConverterImpl implement private org.apache.axiom.soap.SOAPEnvelope toOM(String xml) throws WebServiceException { + if (log.isDebugEnabled()) { + log.debug("Converting SAAJ SOAPEnvelope String to an OM SOAPEnvelope"); + } + XMLStreamReader reader; try { reader = StAXUtils.createXMLStreamReader(new ByteArrayInputStream(xml.getBytes())); @@ -529,7 +538,10 @@ public class SAAJConverterImpl implement protected void addAttributes(NameCreator nc, SOAPElement element, XMLStreamReader reader) throws SOAPException { - + if (log.isDebugEnabled()) { + log.debug("addAttributes: Entry"); + } + // Add the attributes from the reader int size = reader.getAttributeCount(); for (int i = 0; i < size; i++) { @@ -538,7 +550,34 @@ public class SAAJConverterImpl implement String value = reader.getAttributeValue(i); Name name = nc.createName(qName.getLocalPart(), prefix, qName.getNamespaceURI()); element.addAttribute(name, value); + + try { + if (log.isDebugEnabled()) { + log.debug("Setting attrType"); + } + String namespace = qName.getNamespaceURI(); + Attr attr = null; // This is an org.w3c.dom.Attr + if (namespace == null || namespace.length() == 0) { + attr = element.getAttributeNode(qName.getLocalPart()); + } else { + attr = element.getAttributeNodeNS(namespace, qName.getLocalPart()); + } + if (attr != null) { + String attrType = reader.getAttributeType(i); + attr.setUserData(SAAJConverter.OM_ATTRIBUTE_KEY, attrType, null); + if (log.isDebugEnabled()) { + log.debug("Storing attrType in UserData: " + attrType); + } + } + } catch (Exception e) { + if (log.isDebugEnabled()) { + log.debug("An error occured while processing attrType: " + e.getMessage()); + } + } } + if (log.isDebugEnabled()) { + log.debug("addAttributes: Exit"); + } } private void _unexpectedEvent(String event) throws WebServiceException { Modified: axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/util/impl/XMLStreamReaderFromDOM.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/util/impl/XMLStreamReaderFromDOM.java?rev=929230&r1=929229&r2=929230&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/util/impl/XMLStreamReaderFromDOM.java (original) +++ axis/axis2/java/core/trunk/modules/jaxws/src/org/apache/axis2/jaxws/message/util/impl/XMLStreamReaderFromDOM.java Tue Mar 30 19:09:01 2010 @@ -21,6 +21,9 @@ package org.apache.axis2.jaxws.message.util.impl; import org.apache.axis2.jaxws.i18n.Messages; +import org.apache.axis2.jaxws.message.util.SAAJConverter; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.w3c.dom.Attr; import org.w3c.dom.CDATASection; import org.w3c.dom.CharacterData; @@ -33,6 +36,7 @@ import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.ProcessingInstruction; import org.w3c.dom.Text; +import org.w3c.dom.TypeInfo; import javax.xml.namespace.NamespaceContext; import javax.xml.namespace.QName; @@ -51,6 +55,7 @@ import java.util.StringTokenizer; * @see org.apache.axis2.jaxws.util.SOAPElementReader */ public class XMLStreamReaderFromDOM implements XMLStreamReader { + private static final Log log = LogFactory.getLog(XMLStreamReaderFromDOM.class); private Node cursor; private Stack<Node> nextCursorStack = new Stack<Node>(); @@ -243,6 +248,9 @@ public class XMLStreamReaderFromDOM impl attr.getName().startsWith("xmlns:")) { // this is a namespace declaration } else { + if (log.isDebugEnabled()) { + log.debug("Attr string: " + attr.toString()); + } attrs.add(attr); } } @@ -306,8 +314,27 @@ public class XMLStreamReaderFromDOM impl * @see javax.xml.stream.XMLStreamReader#getAttributeType(int) */ public String getAttributeType(int index) { + String attrType = null; Attr attr = (Attr)getAttributes().get(index); - return attr.getSchemaTypeInfo().getTypeName(); + TypeInfo typeInfo = attr.getSchemaTypeInfo(); + if (typeInfo != null) { + attrType = typeInfo.getTypeName(); + } + + if (attrType == null) { + try { + attrType = (String) attr.getUserData(SAAJConverter.OM_ATTRIBUTE_KEY); + if (log.isDebugEnabled()) { + log.debug("Retrieving attrType from UserData: " + attrType); + } + } catch (Exception e) { + if (log.isDebugEnabled()) { + log.debug("An error occured while getting attrType: " + e.getMessage()); + } + } + } + + return attrType; } /* (non-Javadoc) Modified: axis/axis2/java/core/trunk/modules/jaxws/test/org/apache/axis2/jaxws/message/SAAJConverterTests.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/jaxws/test/org/apache/axis2/jaxws/message/SAAJConverterTests.java?rev=929230&r1=929229&r2=929230&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/jaxws/test/org/apache/axis2/jaxws/message/SAAJConverterTests.java (original) +++ axis/axis2/java/core/trunk/modules/jaxws/test/org/apache/axis2/jaxws/message/SAAJConverterTests.java Tue Mar 30 19:09:01 2010 @@ -28,6 +28,10 @@ import org.apache.axiom.soap.impl.builde import org.apache.axis2.jaxws.message.factory.SAAJConverterFactory; import org.apache.axis2.jaxws.message.util.SAAJConverter; import org.apache.axis2.jaxws.registry.FactoryRegistry; +import org.w3c.dom.Attr; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.TypeInfo; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPBody; @@ -39,6 +43,8 @@ import javax.xml.soap.SOAPMessage; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamReader; import java.io.StringReader; +import java.util.ArrayList; +import java.util.List; /** * SAAJConverterTests @@ -62,6 +68,18 @@ public class SAAJConverterTests extends sampleText + "</soapenv:Body></soapenv:Envelope>"; + private static final String sampleText1 = + "<pre:a xmlns:pre=\"urn://sample\">" + + "<b id=\"100\">Hello</b>" + + "<c>World</c>" + + "</pre:a>"; + + private static final String sampleEnvelope1 = + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">" + + "<soapenv:Header /><soapenv:Body>" + + sampleText1 + + "</soapenv:Body></soapenv:Envelope>"; + private static XMLInputFactory inputFactory = XMLInputFactory.newInstance(); public SAAJConverterTests() { @@ -176,4 +194,60 @@ public class SAAJConverterTests extends // Step 3: Do the conversion converter.toSAAJ(ome, se, sf); } + + /** + * @testStrategy Tests conversions between OM and SAAJ SOAPEnvelopes + * and verify attribute type is correctly stored + */ + public void test4() throws Exception { + // Bootstrap: Create an OM SOAPEnvelope from the sample text + StringReader sr = new StringReader(sampleEnvelope1); + XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr); + StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(inflow, null); + org.apache.axiom.soap.SOAPEnvelope omEnvelope = builder.getSOAPEnvelope(); + + // Step 1: Get the SAAJConverter object from the Factory + SAAJConverterFactory f = (SAAJConverterFactory) + FactoryRegistry.getFactory(SAAJConverterFactory.class); + SAAJConverter converter = f.getSAAJConverter(); + + // Step 2: Convert the OM SOAPEnvelope to an SAAJ SOAPEnvelope + SOAPEnvelope saajEnvelope = converter.toSAAJ(omEnvelope); + + // Step 3: Verify attribute type is stored after conversion + Element testElement = (Element) saajEnvelope.getBody().getFirstChild().getFirstChild(); + assertTrue("b".equals(testElement.getLocalName())); + + List attrs = new ArrayList(); + NamedNodeMap map = testElement.getAttributes(); + + if (map != null) { + for (int i = 0; i < map.getLength(); i++) { + Attr attr = (Attr)map.item(i); + if (attr.getName().equals("xmlns") || attr.getName().startsWith("xmlns:")) { + // this is a namespace declaration + } else { + attrs.add(attr); + } + } + } + + Attr attr = (Attr)attrs.get(0); + TypeInfo typeInfo = null; + String attrType = null; + + try { + typeInfo = attr.getSchemaTypeInfo(); + if (typeInfo != null) { + attrType = typeInfo.getTypeName(); + } + } catch (Throwable t) { + ; + } + + if (attrType == null) { + attrType = (String) attr.getUserData(SAAJConverter.OM_ATTRIBUTE_KEY); + } + assert("CDATA".equals(attrType)); + } }