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