Using annotations to define schema mappings
adrian.brock Oct 5, 2006 7:42 AMSince:
1) obviously we can't define schema annotations on the javaee schemas
2) writing handlers (objectmodelfactory, schemabindinginitialzer) by hand
is laborious and error prone
3) none of this information on the mapping is exposed to tools
I've created an alternate way of mapping schemas to object models.
This involves the use of annotations.
In fact, it goes a bit beyond that. In many cases, the annotations
are not required since it will make a "good guess" at the mapping
using conventions and looking at the object model's class tree.
Here's an example showing what I've got working so far
which caters for about 80-90% of use cases:
/* * JBoss, Home of Professional Open Source * Copyright 2006, JBoss Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.test.xml.builder.support; import java.util.Collection; import org.jboss.xb.binding.annotations.Schema; import org.jboss.xb.binding.annotations.SchemaAttribute; import org.jboss.xb.binding.annotations.SchemaProperty; import org.jboss.xb.binding.annotations.SchemaType; import org.jboss.xb.binding.annotations.SchemaValue; /** * Example. Example of using annotations to map the schema JBossXBBuilder.build(schemaBinding, Example.class); This will look at Example.class and the classes it references, e.g. AnotherExample.class It will also look at parameterized classes, e.g. if there was a property of type Collection<AnotherExample> it will also look at AnotherExample. * @author <a href="adrian@jboss.com">Adrian Brock</a> * @version $Revision: 1.1 $ */ // This is only processed on the root type // passed to the builder @Schema(ignoreUnresolvedFieldOrClass=true) // The xsd name // <xsd:complex-type name="theTypeNameInTheXSD"/> @SchemaType(name="theTypeNameInTheXSD") // Alternatively an element and can take an namespace // No namespace means it will search for the name // in any namespace in the schema // @SchemaElement(name="foo", namespace="urn:jboss.com:bar") // The type/example is optional // currently it will search for an element or type // with name "example". (lowercase first letter) public class Example { private String someAttribute; private Collection<String> someCollection; private AnotherExample someProperty; private String someValue; public String getSomeAttribute() { return someAttribute; } // maps the xml attribute name <x id="blah"/> @SchemaAttribute(name="id") public void setSomeAttribute(String someAttribute) { this.someAttribute = someAttribute; } public Collection<String> getSomeCollection() { return someCollection; } // maps the xml child name for a collection // <parent> // <child/> // <child/> // </parent> @SchemaProperty(name="child") public void setSomeCollection(Collection<String> someCollection) { this.someCollection = someCollection; } public AnotherExample getSomeProperty() { return someProperty; } // maps the xml child name for a non-collection // <parent> // <child/> // </parent> @SchemaProperty(name="child") public void setSomeProperty(AnotherExample someProperty) { this.someProperty = someProperty; } public String getSomeValue() { return someValue; } // A tag to say this takes the character data // <x>this</x> @SchemaValue public void setSomeValue(String someValue) { this.someValue = someValue; } }
This work is currently uncommitted to SVN.