minor
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / jaxb / CdmSchemaGenerator.java
1 /**
2 * Copyright (C) 2008 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.cdm.io.jaxb;
11
12 import java.io.File;
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;
24
25 import org.xml.sax.SAXException;
26
27 /**
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.
34 *
35 * @author a.babadshanjan
36 *
37 */
38 public class CdmSchemaGenerator extends SchemaOutputResolver {
39
40 private JAXBContext jaxbContext;
41 private StringWriter out = new StringWriter();
42
43 public CdmSchemaGenerator() throws SAXException, JAXBException, IOException {
44
45 jaxbContext = JAXBContext.newInstance(new Class[] {DataSet.class});
46 }
47
48 /**
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)
53 */
54 @Override
55 public Result createOutput(String namespaceUri, String filename) throws IOException {
56
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));
60 return res;
61 }
62
63 /**
64 * Writes one schema file per namespace.
65 * @see javax.xml.bind.SchemaOutputResolver#createOutput(java.lang.String, java.lang.String)
66 */
67 public void writeSchema() throws JAXBException, IOException, SAXException {
68
69 jaxbContext.generateSchema(this);
70 }
71
72 /**
73 * Not used
74 *
75 * Buffers one single generated schema.
76 * @see javax.xml.bind.SchemaOutputResolver#createOutput(java.lang.String, java.lang.String)
77 */
78 public Schema createSchema() throws SAXException {
79
80 Schema schema =
81 SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema").newSchema(
82 new StreamSource( new StringReader(out.toString())));
83 return schema;
84
85 }
86
87 // set implicit schema for validation
88 // Schema implicitSchema = cdmSchemaGenerator.createSchema();
89
90 }