Jump to: navigation, search

PsychoPathXPathProcessor/UserManual/UserdefinedSchemaTypes

< PsychoPathXPathProcessor‎ | UserManual
Revision as of 22:08, 30 October 2009 by D a carver.yahoo.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This entry will show adopters how they can make the PsychoPath Processor aware of their user defined simpletype constructors.

Sample Code

Warning2.png
Ugly Code
This is some ugly sample code, it is hear for example purposes only.


The code makes use of the new UserDefinedCtrLibrary class. This class takes a Namespace for the schema to be used for the User Defined data types.

    XSNamedMap xstypes = schema.getComponents(XSConstants.TYPE_DEFINITION);  
    if (xstypes.getLength() == 0) {  
       return;  
    }  
 
    dc.add_namespace("myType", "http://www.w3.org/XQueryTest/userDefinedTypes");  
    UserDefinedCtrLibrary udl = new UserDefinedCtrLibrary("http://www.w3.org/XQueryTest/userDefinedTypes");  
 
    for (int i = 0; i < xstypes.getLength(); i++) {  
      XSObject xsobject = xstypes.item(i);  
      if ("http://www.w3.org/XQueryTest/userDefinedTypes".equals(xsobject.getNamespace())) {  
         if (xsobject instanceof XSSimpleTypeDefinition) {  
            if (((XSSimpleTypeDefinition) xsobject).getNumeric()) {  
               if (xsobject.getName().equals("floatBased")) {  
                  XercesFloatUserDefined fudt = new XercesFloatUserDefined(xsobject);  
                  udl.add_type(fudt);  
               } else {  
                  XercesIntegerUserDefined iudt = new XercesIntegerUserDefined(xsobject);  
                  udl.add_type(iudt);  
               }  
            }  else {  
               if (xsobject.getName().equals("QNameBased")) {  
                  XercesQNameUserDefined qudt = new XercesQNameUserDefined(xsobject);  
                  udl.add_type(qudt);  
               } else {  
                  XercesUserDefined udt = new XercesUserDefined(xsobject);  
                 udl.add_type(udt);  
               }  
            }  
         }  
      }  
   }  
 
  dc.add_function_library(udl);

The various Xerces specific classes extend where needed the built in data types. The Xerces specific classes are only in the test suite and aren't generally available. Their source code can be viewed. Adopters will need to add appropriate handling for the various Facets of simpletype, like enumerations, min values, length, etc. This is currently beyond the scope of the PsychoPath processors implementation.

Sample User Defined data type:

public class XercesUserDefined extends CtrType {
 
  private XSObject typeInfo;
  private String value;
 
  public XercesUserDefined(XSObject typeInfo) {
    this.typeInfo = typeInfo;
  }
 
 
  @Override
  public ResultSequence constructor(ResultSequence arg) throws DynamicError {
     ResultSequence rs = ResultSequenceFactory.create_new();
 
     if (arg.empty())
        return rs;
 
     AnyType aat = arg.first();  
 
     rs.add(new XSString(aat.string_value()));
 
     return rs;
  }
 
 
  @Override
  public String string_type() {
    return null;
  }
 
  @Override
  public String string_value() {
    return value;
  }
 
  @Override
  public String type_name() {
    return typeInfo.getName();
  }
 
}

This is the bare bones minimum that is needed by a user defined constructor that leverages Xerces-J for the XML Schema Model. Every User Defined simple data type will need a constructor and it must extend the CtrType class or one of the classes that already extends it (i.e. one of the built in datatypes). There are test suite examples for User Defined functions that extend or restrict QName, Float, and Integer.

Making PsychoPath Aware:

So the final key is making sure that the processor is aware of the namespace and the library to use during processing. This is handled by adding the information to the DynamicContext.

dc.add_namespace("myType", "http://www.w3.org/XQueryTest/userDefinedTypes");
dc.add_function_library(udl);

Remember earlier we created a UserDefinedCtrLibrary class called udl that we then added the appropriate user defined simple types.