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 java
.io
.Writer
;
17 import javax
.xml
.bind
.JAXBContext
;
18 import javax
.xml
.bind
.JAXBException
;
19 import javax
.xml
.bind
.SchemaOutputResolver
;
20 import javax
.xml
.transform
.Result
;
21 import javax
.xml
.transform
.stream
.StreamSource
;
22 import javax
.xml
.transform
.stream
.StreamResult
;
23 import javax
.xml
.validation
.Schema
;
24 import javax
.xml
.validation
.SchemaFactory
;
26 import org
.xml
.sax
.SAXException
;
28 import eu
.etaxonomy
.cdm
.model
.DataSet
;
31 * @author a.babadshanjan
34 public class CdmSchemaGenerator
extends SchemaOutputResolver
{
36 private JAXBContext jaxbContext
;
37 private StringWriter out
= new StringWriter();
39 public CdmSchemaGenerator() throws SAXException
, JAXBException
, IOException
{
41 jaxbContext
= JAXBContext
.newInstance(new Class
[] {DataSet
.class});
45 * Buffers one schema file per namespace.
46 * Result here is schema1.xsd, ..., schema7.xsd in C:\Temp.
47 * filename param is ignored.
48 * @see javax.xml.bind.SchemaOutputResolver#createOutput(java.lang.String, java.lang.String)
51 public Result
createOutput(String namespaceUri
, String filename
) throws IOException
{
53 String userHome
= System
.getProperty("user.home");
54 StreamResult res
= new StreamResult(new File("C:" + File
.separator
+ "Temp", filename
));
55 //StreamResult res = new StreamResult(new File(filename));
60 * Writes one schema file per namespace.
61 * @see javax.xml.bind.SchemaOutputResolver#createOutput(java.lang.String, java.lang.String)
63 public void writeSchema() throws JAXBException
, IOException
, SAXException
{
65 jaxbContext
.generateSchema(this);
71 * Buffers one single generated schema.
72 * @see javax.xml.bind.SchemaOutputResolver#createOutput(java.lang.String, java.lang.String)
74 public Schema
createSchema() throws SAXException
{
77 SchemaFactory
.newInstance("http://www.w3.org/2001/XMLSchema").newSchema(
78 new StreamSource( new StringReader(out
.toString())));
83 // set implicit schema for validation
84 // Schema implicitSchema = cdmSchemaGenerator.createSchema();