2 * This class is responsible for generating a set of XML schemas for the CDM.
3 * It generates one XML schema per namespace from the JAXB annotations of the
4 * model classes. This class might be used in future to generate the
5 * initial version of the CDM schemas. Once the initially generated schemas
6 * have been manually customized, CdmSchemaGenerator could be used to compare
7 * the customized XML schemas against the generated ones.
9 package eu
.etaxonomy
.cdm
.jaxb
;
12 import java
.io
.IOException
;
13 import java
.io
.StringReader
;
14 import java
.io
.StringWriter
;
15 import javax
.xml
.bind
.JAXBContext
;
16 import javax
.xml
.bind
.JAXBException
;
17 import javax
.xml
.bind
.SchemaOutputResolver
;
18 import javax
.xml
.transform
.Result
;
19 import javax
.xml
.transform
.stream
.StreamSource
;
20 import javax
.xml
.transform
.stream
.StreamResult
;
21 import javax
.xml
.validation
.Schema
;
22 import javax
.xml
.validation
.SchemaFactory
;
24 import org
.xml
.sax
.SAXException
;
26 import eu
.etaxonomy
.cdm
.model
.DataSet
;
29 * @author a.babadshanjan
32 public class CdmSchemaGenerator
extends SchemaOutputResolver
{
34 private JAXBContext jaxbContext
;
35 private StringWriter out
= new StringWriter();
37 public CdmSchemaGenerator() throws SAXException
, JAXBException
, IOException
{
39 jaxbContext
= JAXBContext
.newInstance(new Class
[] {DataSet
.class});
43 * Buffers one schema file per namespace.
44 * Result here is schema1.xsd, ..., schema7.xsd in C:\Temp.
45 * filename param is ignored.
46 * @see javax.xml.bind.SchemaOutputResolver#createOutput(java.lang.String, java.lang.String)
49 public Result
createOutput(String namespaceUri
, String filename
) throws IOException
{
51 String userHome
= System
.getProperty("user.home");
52 StreamResult res
= new StreamResult(new File("C:" + File
.separator
+ "Temp", filename
));
53 //StreamResult res = new StreamResult(new File(filename));
58 * Writes one schema file per namespace.
59 * @see javax.xml.bind.SchemaOutputResolver#createOutput(java.lang.String, java.lang.String)
61 public void writeSchema() throws JAXBException
, IOException
, SAXException
{
63 jaxbContext
.generateSchema(this);
69 * Buffers one single generated schema.
70 * @see javax.xml.bind.SchemaOutputResolver#createOutput(java.lang.String, java.lang.String)
72 public Schema
createSchema() throws SAXException
{
75 SchemaFactory
.newInstance("http://www.w3.org/2001/XMLSchema").newSchema(
76 new StreamSource( new StringReader(out
.toString())));
81 // set implicit schema for validation
82 // Schema implicitSchema = cdmSchemaGenerator.createSchema();