2 * Copyright (C) 2008 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.io
.jaxb
;
13 import java
.io
.IOException
;
14 import java
.io
.StringReader
;
15 import java
.io
.StringWriter
;
16 import javax
.xml
.bind
.JAXBContext
;
17 import javax
.xml
.bind
.JAXBException
;
18 import javax
.xml
.bind
.SchemaOutputResolver
;
19 import javax
.xml
.transform
.Result
;
20 import javax
.xml
.transform
.stream
.StreamSource
;
21 import javax
.xml
.transform
.stream
.StreamResult
;
22 import javax
.xml
.validation
.Schema
;
23 import javax
.xml
.validation
.SchemaFactory
;
25 import org
.xml
.sax
.SAXException
;
28 * This class is responsible for generating a set of XML schemas for the CDM.
29 * It generates one XML schema per namespace from the JAXB annotations of the
30 * model classes. This class might be used in future to generate the
31 * initial version of the CDM schemas. Once the initially generated schemas
32 * have been manually customized, CdmSchemaGenerator could be used to compare
33 * the customized XML schemas against the generated ones.
35 * @author a.babadshanjan
38 public class CdmSchemaGenerator
extends SchemaOutputResolver
{
40 private JAXBContext jaxbContext
;
41 private StringWriter out
= new StringWriter();
43 public CdmSchemaGenerator() throws SAXException
, JAXBException
, IOException
{
45 jaxbContext
= JAXBContext
.newInstance(new Class
[] {DataSet
.class});
49 * Buffers one schema file per namespace.
50 * Result here is schema1.xsd, ..., schema7.xsd in C:\Temp.
51 * filename param is ignored.
52 * @see javax.xml.bind.SchemaOutputResolver#createOutput(java.lang.String, java.lang.String)
55 public Result
createOutput(String namespaceUri
, String filename
) throws IOException
{
57 String userHome
= System
.getProperty("user.home");
58 StreamResult res
= new StreamResult(new File("C:" + File
.separator
+ "Temp", filename
));
59 //StreamResult res = new StreamResult(new File(filename));
64 * Writes one schema file per namespace.
65 * @see javax.xml.bind.SchemaOutputResolver#createOutput(java.lang.String, java.lang.String)
67 public void writeSchema() throws JAXBException
, IOException
, SAXException
{
69 jaxbContext
.generateSchema(this);
75 * Buffers one single generated schema.
76 * @see javax.xml.bind.SchemaOutputResolver#createOutput(java.lang.String, java.lang.String)
78 public Schema
createSchema() throws SAXException
{
81 SchemaFactory
.newInstance("http://www.w3.org/2001/XMLSchema").newSchema(
82 new StreamSource( new StringReader(out
.toString())));
87 // set implicit schema for validation
88 // Schema implicitSchema = cdmSchemaGenerator.createSchema();