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
36 boolean createPGraph = true;
\r
37 boolean createImporter = true;
\r
38 boolean createExporter = true;
\r
43 public void convertSchema(File schemaFile, File conversionFile, File outputPlugin) throws JAXBException, IOException {
\r
44 pluginName = outputPlugin.getName();
\r
45 String packageParts[] = pluginName.split("\\.");
\r
46 String outputLoc = outputPlugin.getAbsolutePath();
\r
47 outputLoc += "/src";
\r
48 for (String s : packageParts)
\r
50 String outputGraph = outputPlugin.getAbsolutePath();
\r
51 outputGraph += "/graph";
\r
52 outputGraph += "/" + schemaFile.getName().substring(0, schemaFile.getName().length()-4) +".pgraph";
\r
54 this.schemaFile = schemaFile;
\r
55 this.conversionFile = conversionFile;
\r
56 this.ontologyFile = new File(outputGraph);
\r
57 this.parserDir = new File(outputLoc);
\r
59 if (!ontologyFile.exists()) {
\r
60 ontologyFile.getParentFile().mkdirs();
\r
61 ontologyFile.createNewFile();
\r
63 if (!parserDir.exists())
\r
70 public void setCreateExporter(boolean createExporter) {
\r
71 this.createExporter = createExporter;
\r
74 public void setCreateImporter(boolean createImporter) {
\r
75 this.createImporter = createImporter;
\r
78 public void setCreatePGraph(boolean createPGraph) {
\r
79 this.createPGraph = createPGraph;
\r
82 private void convert() throws JAXBException, IOException {
\r
83 JAXBContext jc = JAXBContext.newInstance("org.w3._2001.xmlschema");
\r
84 Unmarshaller u = jc.createUnmarshaller();
\r
85 //u.setSchema(schema);
\r
86 InputStream fileStream = new FileInputStream(schemaFile);
\r
87 schema = (Schema)u.unmarshal(fileStream);
\r
89 if (conversionFile != null) {
\r
90 jc = JAXBContext.newInstance("org.simantics.xml.sax.configuration");
\r
91 u = jc.createUnmarshaller();
\r
92 fileStream = new FileInputStream(conversionFile);
\r
93 configuration = (Configuration)((JAXBElement<?>)u.unmarshal(fileStream)).getValue();
\r
95 configuration = new Configuration();
\r
98 header = new String[4];
\r
99 header[0] = "Generated with org.simantics.xml.sax XML schema converter";
\r
101 header[2] = "File " + schemaFile.getAbsolutePath();
\r
102 header[3] = "Date " + new Date().toString();
\r
105 String ontologyName = schema.getTargetNamespace();
\r
106 if (ontologyName == null) {
\r
107 ontologyName = getSchemaFile().getName();
\r
109 int index = ontologyName.lastIndexOf(".");
\r
111 ontologyName = ontologyName.substring(0, index);
\r
113 if (!ontologyName.startsWith("http"))
\r
114 ontologyName = "http://" + ontologyName;
\r
115 String parts[] = ontologyName.split("/");
\r
116 String name = parts[parts.length-1];
\r
117 //String ontRoot = name.substring(0, Math.min(3, name.length())).toUpperCase();
\r
120 name = name.replaceAll("\\.", "_");
\r
121 name = name.replaceAll(" ", "_");
\r
122 String className = getPluginName() + "." + name;
\r
124 if (createPGraph) {
\r
125 OntologyGenerator ontologyGenerator = new OntologyGenerator(configuration);
\r
126 ontologyGenerator.createOntology(schema, ontologyName, className, this);
\r
128 if (createImporter) {
\r
129 ImporterGenerator importerGenerator = new ImporterGenerator(configuration);
\r
130 importerGenerator.createParser(schema, className, this);
\r
132 if (createExporter) {
\r
133 ExporterGenerator exporterGenerator = new ExporterGenerator(configuration);
\r
134 exporterGenerator.createParser(schema, className, this);
\r
138 public File getOntologyFile() {
\r
139 return ontologyFile;
\r
141 public File getParserDir() {
\r
145 public Schema getSchema() {
\r
149 public File getSchemaFile() {
\r
153 public String getPluginName() {
\r
157 public String[] getHeader() {
\r
161 public Configuration getConfiguration() {
\r
162 return configuration;
\r