2222import org .apache .axis2 .AxisFault ;
2323import org .apache .ws .commons .schema .utils .XmlSchemaRef ;
2424
25+ import org .apache .commons .logging .Log ;
26+ import org .apache .commons .logging .LogFactory ;
27+
2528import org .apache .ws .commons .schema .XmlSchema ;
29+ import org .apache .ws .commons .schema .XmlSchemaAttribute ;
30+ import org .apache .ws .commons .schema .XmlSchemaAttributeOrGroupRef ;
2631import org .apache .ws .commons .schema .XmlSchemaComplexType ;
2732import org .apache .ws .commons .schema .XmlSchemaElement ;
2833import org .apache .ws .commons .schema .XmlSchemaParticle ;
3237import org .apache .ws .commons .schema .XmlSchemaType ;
3338
3439import javax .xml .namespace .QName ;
40+ import java .util .ArrayList ;
3541import java .util .LinkedList ;
3642import java .util .List ;
3743import java .util .Queue ;
3844
3945public class XmlNodeGenerator {
4046
47+ private static final Log log = LogFactory .getLog (XmlNodeGenerator .class );
48+
4149 List <XmlSchema > xmlSchemaList ;
4250
4351 QName elementQname ;
4452
4553 private XmlNode mainXmlNode ;
4654
4755 Queue <JsonObject > queue = new LinkedList <JsonObject >();
56+ Queue <JsonObject > attribute_queue = new LinkedList <JsonObject >();
4857
4958 public XmlNodeGenerator (List <XmlSchema > xmlSchemaList , QName elementQname ) {
5059 this .xmlSchemaList = xmlSchemaList ;
@@ -84,6 +93,7 @@ private void processSchemaList() throws AxisFault {
8493 }
8594
8695 private void processElement (XmlSchemaElement element , XmlNode parentNode , XmlSchema schema ) throws AxisFault {
96+ log .debug ("XmlNodeGenerator.processElement() found parentNode node name: " + parentNode .getName () + " , isAttribute: " + parentNode .isAttribute () + " , element name: " + element .getName ());
8797 String targetNamespace = schema .getTargetNamespace ();
8898 XmlNode xmlNode ;
8999 QName schemaTypeName = element .getSchemaTypeName ();
@@ -147,6 +157,48 @@ private void processSchemaType(XmlSchemaType xmlSchemaType , XmlNode parentNode
147157 }
148158 }
149159 }
160+ /*
161+ TODO: attribute support Proof of Concept (POC) by adding currency attribute to:
162+
163+ samples/quickstartadb/resources/META-INF/StockQuoteService.wsdl:
164+
165+ <xs:element name="getPrice">
166+ <xs:complexType>
167+ <xs:sequence>
168+ <xs:element name="symbol" nillable="true" type="xs:string"/>
169+ </xs:sequence>
170+ <xs:attribute name="currency" type="xs:string" use="required"/>
171+ </xs:complexType>
172+ </xs:element>
173+
174+ resulting in this SOAP Envelope:
175+
176+ <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header/><soapenv:Body><ns1:getPrice xmlns:ns1="http://quickstart.samples/xsd" ns1:currency="USD"><ns1:symbol>ABC</ns1:symbol></ns1:getPrice></soapenv:Body></soapenv:Envelope>
177+
178+ Below, add complexType.getAttributes() code to support this JSON:
179+
180+ { "getPrice" : {"symbol": "IBM","currency":USD}}
181+
182+ Possibly using @ as @currency to flag a variable name as as attribute.
183+
184+ One thing to note is XmlNode has isAttribute() but was never used
185+ */
186+ if (complexType .getAttributes () != null && complexType .getAttributes ().size () > 0 ) {
187+ log .debug ("XmlNodeGenerator.processSchemaType() found attribute size from complexType: " + complexType .getAttributes ().size ());
188+ List <XmlSchemaAttributeOrGroupRef > list = complexType .getAttributes ();
189+ for (XmlSchemaAttributeOrGroupRef ref : list ) {
190+ XmlSchemaAttribute xsa = (XmlSchemaAttribute )ref ;
191+ String name = xsa .getName ();
192+ QName schemaTypeName = xsa .getSchemaTypeName ();
193+ if (schema != null && schema .getTargetNamespace () != null && schemaTypeName != null && schemaTypeName .getLocalPart () != null ) {
194+ log .debug ("XmlNodeGenerator.processSchemaType() found attribute name from complexType: " + name + " , adding it to parentNode" );
195+ XmlNode xmlNode = new XmlNode (name , schema .getTargetNamespace (), true , false , schemaTypeName .getLocalPart ());
196+ parentNode .addChildToList (xmlNode );
197+ } else {
198+ log .debug ("XmlNodeGenerator.processSchemaType() found attribute name from complexType: " + name + " , however could not resolve namespace and localPart" );
199+ }
200+ }
201+ }
150202 }else if (xmlSchemaType instanceof XmlSchemaSimpleType ) {
151203 // nothing to do with simpleType
152204 }
@@ -163,6 +215,11 @@ private XmlSchema getXmlSchema(QName qName) {
163215 }
164216
165217 private void generateQueue (XmlNode node ) {
218+ log .debug ("XmlNodeGenerator.generateQueue() found node name: " + node .getName () + " , isAttribute: " + node .isAttribute ());
219+ if (node .isAttribute ()) {
220+ attribute_queue .add (new JsonObject (node .getName (), JSONType .OBJECT , node .getValueType () , node .getNamespaceUri ()));
221+ return ;
222+ }
166223 if (node .isArray ()) {
167224 if (node .getChildrenList ().size () > 0 ) {
168225 queue .add (new JsonObject (node .getName (), JSONType .NESTED_ARRAY , node .getValueType () , node .getNamespaceUri ()));
@@ -186,7 +243,6 @@ private void processXmlNodeChildren(List<XmlNode> childrenNodes) {
186243 }
187244 }
188245
189-
190246 public XmlNode getMainXmlNode () throws AxisFault {
191247 if (mainXmlNode == null ) {
192248 try {
@@ -203,4 +259,9 @@ public Queue<JsonObject> getQueue(XmlNode node) {
203259 return queue ;
204260 }
205261
262+ // need to invoke getQueue() before getAttributeQueue()
263+ public Queue <JsonObject > getAttributeQueue () {
264+ return attribute_queue ;
265+ }
266+
206267}
0 commit comments