(no commit message)
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / jaxb / CdmSchemaGenerator.java
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.File;
12 import java.io.IOException;
13 import java.io.StringReader;
14 import java.io.StringWriter;
15 import java.io.Writer;
16
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;
25
26 import org.xml.sax.SAXException;
27
28 import eu.etaxonomy.cdm.model.DataSet;
29
30 /**
31 * @author a.babadshanjan
32 *
33 */
34 public class CdmSchemaGenerator extends SchemaOutputResolver {
35
36 private JAXBContext jaxbContext;
37 private StringWriter out = new StringWriter();
38
39 public CdmSchemaGenerator() throws SAXException, JAXBException, IOException {
40
41 jaxbContext = JAXBContext.newInstance(new Class[] {DataSet.class});
42 }
43
44 /**
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)
49 */
50 @Override
51 public Result createOutput(String namespaceUri, String filename) throws IOException {
52
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));
56 return res;
57 }
58
59 /**
60 * Writes one schema file per namespace.
61 * @see javax.xml.bind.SchemaOutputResolver#createOutput(java.lang.String, java.lang.String)
62 */
63 public void writeSchema() throws JAXBException, IOException, SAXException {
64
65 jaxbContext.generateSchema(this);
66 }
67
68 /**
69 * Not used
70 *
71 * Buffers one single generated schema.
72 * @see javax.xml.bind.SchemaOutputResolver#createOutput(java.lang.String, java.lang.String)
73 */
74 public Schema createSchema() throws SAXException {
75
76 Schema schema =
77 SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema").newSchema(
78 new StreamSource( new StringReader(out.toString())));
79 return schema;
80
81 }
82
83 // set implicit schema for validation
84 // Schema implicitSchema = cdmSchemaGenerator.createSchema();
85
86 }