1 Reply Latest reply on Jul 28, 2005 5:26 AM by aloubyansky

    ObjectModelFactory, schemas and type conversion

    starksm64

      So I was looking at adding the ignoreUnresolvedFieldOrClass setting to the jbxb:schemaBindings as an element, the addition to the jndi-binding-service_1_0.xsd being:

      <xs:schema version="1.0beta"
       attributeFormDefault="unqualified"
       elementFormDefault="qualified"
       xmlns:jbxb="http://www.jboss.org/xml/ns/jbxb"
       xmlns:xs="http://www.w3.org/2001/XMLSchema">
       <xs:annotation>
       <xs:documentation>The JBossXB schema customization
       elements</xs:documentation>
       </xs:annotation>
       <xs:element name="schemaBindings">
       <xs:annotation>
       <xs:documentation>This element defines default bindings for the
       schema</xs:documentation>
       </xs:annotation>
       <xs:complexType>
       <!-- The child elements can be selected in any order. -->
       <xs:choice maxOccurs="unbounded" minOccurs="1">
       <xs:element maxOccurs="1" minOccurs="0" ref="jbxb:package"/>
      
       <xs:element name="ignoreUnresolvedFieldOrClass" type="xs:boolean" default="true"
       maxOccurs="1" minOccurs="0">
       <xs:annotation>
       <xs:documentation>The ignoreUnresolvedFieldOrClass specifies the behavior the parser chooses
       when a field is not found in the parent class for a child value, or when an element does not have
       any class assignement. If false, an exception will be thrown when either situation is encountered.
       If true, the missing mapping is ignored and the parse continues.
       </xs:documentation>
       </xs:annotation>
       </xs:element>
      
       </xs:choice>
       </xs:complexType>
       </xs:element>
      


      My first hack were the following changes to the XsdAnnotation$JbxbObjectModelFactory which was using a BooleanEditor as the older for the immutable boolean:
       public Object newChild(Object parent,
       UnmarshallingContext ctx,
       String namespaceURI,
       String localName,
       Attributes attrs)
       {
       Object child = null;
       // schemaBindings/ignoreUnresolvedFieldOrClass
       if( "ignoreUnresolvedFieldOrClass".equals(localName) )
       {
       // Just a place holder to get to the element text value
       child = new BooleanEditor();
       }
      ...
      
       public void addChild(Object parent,
       Object child,
       UnmarshallingContext ctx,
       String namespaceURI,
       String localName)
       {
      
       // schemaBindings/ignoreUnresolvedFieldOrClass
       if( "ignoreUnresolvedFieldOrClass".equals(localName) )
       {
       BooleanEditor editor = (BooleanEditor) child;
       Boolean flag = (Boolean) editor.getValue();
       SchemaMetaData schema = (SchemaMetaData) parent;
       schema.setIgnoreUnresolvedFieldOrClass(flag.booleanValue());
       }
      
      ...
      
       public void setValue(Object o, UnmarshallingContext ctx, String namespaceURI, String localName, String value)
       {
       if( o instanceof PropertyEditor )
       {
       PropertyEditor pe = (PropertyEditor) o;
       pe.setAsText(value);
       }
       else
       {
       log.warn("setValue: " + localName + "=" + value);
       }
       }
      


      This can be handled much easier by just ignoring the ignoreUnresolvedFieldOrClass element and just converting its text content:

       public void setValue(Object o, UnmarshallingContext ctx, String namespaceURI, String localName, String value)
       {
       // schemaBindings/ignoreUnresolvedFieldOrClass
       if( "ignoreUnresolvedFieldOrClass".equals(localName) )
       {
       SchemaMetaData schema = (SchemaMetaData) o;
       Boolean flag = Boolean.valueOf(value);
       schema.setIgnoreUnresolvedFieldOrClass(flag.booleanValue());
       }
       else
       {
       log.warn("setValue: " + localName + "=" + value);
       }
       }
      


      So it seems that there is something of a chicken and egg problem here. We are writing a sax based content handler to provide schema based xml/java mapping, but the jbxb elements themselves have a schema that a parser could use to perform basic conversions.

      The base problem is that schemas still aren't well integrated into the javax.xml.* apis. Browsering through the jdk api, I see this javax.xml.validation package that at least knows about schemas, but appears to only really support validation of a document against the schema as shown by this fragment from the package docs:

       // parse an XML document into a DOM tree
       DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
       Document document = parser.parse(new File("instance.xml"));
      
       // create a SchemaFactory capable of understanding WXS schemas
       SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
      
       // load a WXS schema, represented by a Schema instance
       Source schemaFile = new StreamSource(new File("mySchema.xsd"));
       Schema schema = factory.newSchema(schemaFile);
      
       // create a Validator instance, which can be used to validate an instance document
       Validator validator = schema.newValidator();
      
       // validate the DOM tree
       try {
       validator.validate(new DOMSource(document));
       } catch (SAXException e) {
       // instance document is invalid!
       }
      


      What is missing is an expansion of the sax parser apis to provide basic type conversion. There are some hooks in the javax.xml.validation like javax.xml.validation.TypeInfoProvider which provides org.w3c.dom.TypeInfo objects for the current attributes and elements.

      So two questions:

      1. Have we looked as using the javax.xml.validation to simplify/standardize the jbossxb impl?

      2. Is it possible/worth it to have a GenericObjectModelFactory base that accepts a schema that automates or simplifies the amount of type conversion needed in an implementation like JaxbObjectModelFactory.


        • 1. Re: ObjectModelFactory, schemas and type conversion
          aloubyansky

           

          "scott.stark@jboss.org" wrote:
          1. Have we looked as using the javax.xml.validation to simplify/standardize the jbossxb impl?


          No, not yet.

          "scott.stark@jboss.org" wrote:
          2. Is it possible/worth it to have a GenericObjectModelFactory base that accepts a schema that automates or simplifies the amount of type conversion needed in an implementation like JaxbObjectModelFactory.


          The codebase and this part specifically requires a lot of clean-up work. This will be addressed by using a higher level API.