Annotating for schemagen

JAXB schemagen automatically creates XML Schemas from JavaBeans. Scala can create JavaBeans, and now that it has the ability to attach JDK 1.5 annotations, it can be used with schemagen.

package com.soletta.lb;
 
import javax.xml.bind.annotation._;
import java.util.Collection;
import scala.reflect.BeanProperty;
 
/** <p>This class demonstrates the use of JAXB annotations on Scala
classes, so the generated class can be interpreted by the schemagen
tool of JAXB 2. The schema generated by this class looks like this:</p>
<pre>
&lt;complexType name="FileInfo">
  &lt;complexContent>
    &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
      &lt;sequence>
        &lt;element ref="{http://www.soletta.com/schema/lb}fileInfo" maxOccurs="unbounded"/>
        &lt;element name="directory" type="{http://www.w3.org/2001/XMLSchema}boolean"/>
        &lt;element name="lastModified" type="{http://www.w3.org/2001/XMLSchema}long"/>
        &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
        &lt;element name="size" type="{http://www.w3.org/2001/XMLSchema}long"/>
      &lt;/sequence>
    &lt;/restriction>
  &lt;/complexContent>
&lt;/complexType></pre>
*/
  
// Assign an XML Schema type (and namespace to the FileInfo) class
@XmlType{val name="FileInfo", val namespace="http://www.soletta.com/schema/lb"}
// Indicate that it can be a root element, and associate a namespace
@XmlRootElement{val namespace="http://www.soletta.com/schema/lb"}
// Note that we can still use BeanProperty for vars declared in a constructor
class FileInfo(@BeanProperty var name: String) {
    // We use BeanProperty to let scalac generate the getters
    // and setters for us, whenever we can just take JAXB's 
    // default behavior. 
    @BeanProperty
    var lastModified = System.currentTimeMillis()
    @BeanProperty
    var size = 0L
    @BeanProperty
    var directory = false
    
    // We can't use the default behavior here, so we need to write
    // our own getter and setter. We can then set the JAXB annotation
    // on the getter, and schemagen will be happy.
    var children: Collection = null
 
    // JAXB needs to see a no-argument constructor, so we provide one.
    def this() = this(null)
 
    // Because JAXB doesn't see the generic type information it normally gets
    // from javac, we need to provide a little more information to it. 
    // Note the use of the backquote to allow use of the annotation parameter
    // named "type".
    @XmlElementRef{val name="fileInfo", 
                   val namespace="http://www.soletta.com/schema/lb", 
                   val `type`=classOf[FileInfo]}
    def getChildren = children;
    
}
 
code/jaxb-schemagen.txt · Last modified: 2007/09/08 06:21 by 141.156.191.131
 
Recent changes RSS feed Valid XHTML 1.0 Driven by DokuWiki