1
|
/**
|
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.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.jaxb;
|
10
|
|
11
|
import java.io.IOException;
|
12
|
import java.io.StringReader;
|
13
|
import java.io.StringWriter;
|
14
|
import java.io.Writer;
|
15
|
|
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;
|
24
|
|
25
|
import org.xml.sax.SAXException;
|
26
|
|
27
|
import eu.etaxonomy.cdm.model.DataSet;
|
28
|
|
29
|
/**
|
30
|
* @author a.babadshanjan
|
31
|
*
|
32
|
*/
|
33
|
public class CdmSchemaGenerator extends SchemaOutputResolver {
|
34
|
|
35
|
private JAXBContext jaxbContext;
|
36
|
StringWriter out = new StringWriter();
|
37
|
|
38
|
public CdmSchemaGenerator() throws SAXException, JAXBException, IOException {
|
39
|
|
40
|
jaxbContext = JAXBContext.newInstance(new Class[] {DataSet.class});
|
41
|
}
|
42
|
|
43
|
/**
|
44
|
* Prints one single generated schema on console.
|
45
|
* @see javax.xml.bind.SchemaOutputResolver#createOutput(java.lang.String, java.lang.String)
|
46
|
*/
|
47
|
@Override
|
48
|
public Result createOutput(String namespaceUri, String filename) throws IOException {
|
49
|
|
50
|
StreamResult res = new StreamResult(System.out);
|
51
|
res.setSystemId(filename);
|
52
|
return res;
|
53
|
}
|
54
|
|
55
|
/**
|
56
|
* Buffers one single generated schema.
|
57
|
* @see javax.xml.bind.SchemaOutputResolver#createOutput(java.lang.String, java.lang.String)
|
58
|
*/
|
59
|
public Schema createSchema() throws SAXException {
|
60
|
|
61
|
Schema schema =
|
62
|
SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema").newSchema(
|
63
|
new StreamSource( new StringReader(out.toString())));
|
64
|
return schema;
|
65
|
}
|
66
|
|
67
|
public void writeSchema(DataSet dataSet, Writer writer) throws JAXBException, IOException, SAXException {
|
68
|
|
69
|
CdmSchemaGenerator out = new CdmSchemaGenerator();
|
70
|
jaxbContext.generateSchema(out);
|
71
|
Schema implicitSchema = out.createSchema();
|
72
|
}
|
73
|
}
|