1 package org.simantics.xml.sax;
\r
4 import java.io.FileInputStream;
\r
5 import java.io.IOException;
\r
6 import java.io.InputStream;
\r
7 import java.util.Date;
\r
9 import javax.xml.bind.JAXBContext;
\r
10 import javax.xml.bind.JAXBElement;
\r
11 import javax.xml.bind.JAXBException;
\r
12 import javax.xml.bind.Unmarshaller;
\r
14 import org.simantics.xml.sax.configuration.Configuration;
\r
15 import org.w3._2001.xmlschema.Schema;
\r
18 * This file is developed for XMpLant / Proteus schema conversion is not able to handle all XML Schema definitions.
\r
23 public class SchemaConverter {
\r
26 File conversionFile;
\r
30 Configuration configuration;
\r
34 private String[] header;
\r
37 public void convertSchema(File schemaFile, File conversionFile, File outputPlugin) throws JAXBException, IOException {
\r
38 pluginName = outputPlugin.getName();
\r
39 String packageParts[] = pluginName.split("\\.");
\r
40 String outputLoc = outputPlugin.getAbsolutePath();
\r
41 outputLoc += "/src";
\r
42 for (String s : packageParts)
\r
44 String outputGraph = outputPlugin.getAbsolutePath();
\r
45 outputGraph += "/graph";
\r
46 outputGraph += "/" + schemaFile.getName().substring(0, schemaFile.getName().length()-4) +".pgraph";
\r
48 this.schemaFile = schemaFile;
\r
49 this.conversionFile = conversionFile;
\r
50 this.ontologyFile = new File(outputGraph);
\r
51 this.parserDir = new File(outputLoc);
\r
53 if (!ontologyFile.exists()) {
\r
54 ontologyFile.getParentFile().mkdirs();
\r
55 ontologyFile.createNewFile();
\r
57 if (!parserDir.exists())
\r
64 private void convert() throws JAXBException, IOException {
\r
65 JAXBContext jc = JAXBContext.newInstance("org.w3._2001.xmlschema");
\r
66 Unmarshaller u = jc.createUnmarshaller();
\r
67 //u.setSchema(schema);
\r
68 InputStream fileStream = new FileInputStream(schemaFile);
\r
69 schema = (Schema)u.unmarshal(fileStream);
\r
71 if (conversionFile != null) {
\r
72 jc = JAXBContext.newInstance("org.simantics.xml.sax.configuration");
\r
73 u = jc.createUnmarshaller();
\r
74 fileStream = new FileInputStream(conversionFile);
\r
75 configuration = (Configuration)((JAXBElement<?>)u.unmarshal(fileStream)).getValue();
\r
77 configuration = new Configuration();
\r
80 header = new String[4];
\r
81 header[0] = "Generated with org.simantics.xml.sax XML schema converter";
\r
83 header[2] = "File " + schemaFile.getAbsolutePath();
\r
84 header[3] = "Date " + new Date().toString();
\r
87 OntologyGenerator ontologyGenerator = new OntologyGenerator(configuration);
\r
88 ontologyGenerator.createOntology(schema, this);
\r
89 ImporterGenerator importerGenerator = new ImporterGenerator(configuration);
\r
90 importerGenerator.createParser(schema, ontologyGenerator.getClassName(), this);
\r
93 public File getOntologyFile() {
\r
94 return ontologyFile;
\r
96 public File getParserDir() {
\r
100 public Schema getSchema() {
\r
104 public File getSchemaFile() {
\r
108 public String getPluginName() {
\r
112 public String[] getHeader() {
\r
116 public Configuration getConfiguration() {
\r
117 return configuration;
\r