Advanced Axis2 Databinding Framework Features

The aim of this section is provide an insight into the newly added advanced features of the Axis2 Databinding (ADB) Framework.

Content

xsi:type Support

This is implemented by adding a extension mapping class. The code that calls the extension mapper is generated inside the Factory.parse method of the beans and gets activated when the xsi:type attribute is present. The following code fragment shows what the generated type mapper looks like :

            public static java.lang.Object getTypeObject(java.lang.String namespaceURI,
                                java.lang.String typeName,
                                javax.xml.stream.XMLStreamReader reader) throws java.lang.Exception{
              
                  if (
                  "http://soapinterop.org/types".equals(namespaceURI) &&
                  "SOAPStruct".equals(typeName)){
                            return  com.test.SOAPStruct.Factory.parse(reader);
                  }
              throw new java.lang.RuntimeException("Unsupported type " + namespaceURI + " " + typeName);
            }

Inside every Factory.parse method, the extension mapper gets called when a xsi:type attribute is encountered and that type is not the type that is currently being parsed.

The following code fragment shows how the ADB deserialize method calls the mapper class:

             if (reader.getAttributeValue("http://www.w3.org/2001/XMLSchema-instance","type")!=null){
                  java.lang.String fullTypeName = reader.getAttributeValue("http://www.w3.org/2001/XMLSchema-instance",
                        "type");
                  if (fullTypeName!=null){
                    java.lang.String nsPrefix = fullTypeName.substring(0,fullTypeName.indexOf(":"));
                    nsPrefix = nsPrefix==null?"":nsPrefix;

                    java.lang.String type = fullTypeName.substring(fullTypeName.indexOf(":")+1);
                    if (!"SOAPStruct".equals(type)){
                        //find namespace for the prefix
                        java.lang.String nsUri = reader.getNamespaceContext().getNamespaceURI(nsPrefix);
                        return (SOAPStruct)org.soapinterop.types.ExtensionMapper.getTypeObject(
                             nsUri,type,reader);
                      }

                  }
              }

This makes xsi:type based parsing possible and results in proper xsi:type based serializations at runtime.

By default, the mapping package is derived from the targetnamespace of the first schema that is encountered. The package name can also be explicitly set by a CompilerOption:

   
        CompilerOptions compilerOptions = new CompilerOptions();
        compilerOptions.setWriteOutput(true);
        compilerOptions.setMapperClassPackage("com.test");
        compilerOptions.setOutputLocation(new File("src"));
        try {
            SchemaCompiler schemaCompiler = new SchemaCompiler(compilerOptions);
            XmlSchemaCollection xmlSchemaCollection = new XmlSchemaCollection();
            XmlSchema xmlSchema =xmlSchemaCollection.read(new FileReader("schema/sample.xsd"),null);
            schemaCompiler.compile(xmlSchema);
        } catch (Exception e) {
            e.printStackTrace();
        }

Helper mode

Helper mode is a fairly new feature. In the helper mode, the beans are plain Java beans and all the deserialization/serialization code is moved to a helper class. For example, the simple schema mentioned in the ADB-howto document will yield four classes instead of the two previously generated:

  1. MyElement.java
  2. MyElementHelper.java
  3. SOAPStruct.java
  4. SOAPStructHelper.java

The helpers basically contain all the serialization code that otherwise would go into the ADBBeans. Hence the beans in the helper mode are much more simplified. Also note that the helper mode is available only if you are in unpacked mode. The code generator by default does not expand the classes.

Helper mode can be switched on by using the setHelperMode method in CompilerOptions:

compilerOptions.setHelperMode(true);

Additional ADB Topics