]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConverter.java
3b2c1a0c42749b6bddf5babe3525924696bfb844
[simantics/interop.git] / org.simantics.xml.sax / src / org / simantics / xml / sax / SchemaConverter.java
1 package org.simantics.xml.sax;\r
2 \r
3 import java.io.File;\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
8 \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
13 \r
14 import org.simantics.xml.sax.configuration.Configuration;\r
15 import org.w3._2001.xmlschema.Schema;\r
16 \r
17 /**\r
18  * This file is developed for XMpLant / Proteus schema conversion is not able to handle all XML Schema definitions.\r
19  * \r
20  * @author mlmarko\r
21  *\r
22  */\r
23 public class SchemaConverter {\r
24         \r
25         File schemaFile;\r
26         File conversionFile;\r
27         File ontologyFile;\r
28         File parserDir;\r
29         Schema schema;\r
30         Configuration configuration;\r
31         \r
32         String pluginName;\r
33         \r
34         private String[] header;\r
35         \r
36         boolean createPGraph = true;\r
37         boolean createImporter = true;\r
38         boolean createExporter = true;\r
39         \r
40         \r
41         \r
42 \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
49                         outputLoc += "/"+s;\r
50                 String outputGraph = outputPlugin.getAbsolutePath();\r
51                 outputGraph += "/graph";\r
52                 outputGraph += "/" + schemaFile.getName().substring(0, schemaFile.getName().length()-4) +".pgraph";\r
53                 \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
58                 \r
59                 if (!ontologyFile.exists()) {\r
60                         ontologyFile.getParentFile().mkdirs();\r
61                         ontologyFile.createNewFile();\r
62                 }\r
63                 if (!parserDir.exists())\r
64                         parserDir.mkdirs();\r
65                 \r
66                 convert();\r
67                 \r
68         }\r
69         \r
70         public void setCreateExporter(boolean createExporter) {\r
71                 this.createExporter = createExporter;\r
72         }\r
73         \r
74         public void setCreateImporter(boolean createImporter) {\r
75                 this.createImporter = createImporter;\r
76         }\r
77         \r
78         public void setCreatePGraph(boolean createPGraph) {\r
79                 this.createPGraph = createPGraph;\r
80         }\r
81         \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
88                 \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
94                 } else {\r
95                         configuration = new Configuration();\r
96                 }\r
97                 \r
98                 header = new String[4];\r
99                 header[0] = "Generated with org.simantics.xml.sax XML schema converter";\r
100                 header[1] = "";\r
101                 header[2] = "File " + schemaFile.getAbsolutePath();\r
102                 header[3] = "Date " + new Date().toString();\r
103                 \r
104                 \r
105                 String ontologyUri = schema.getTargetNamespace();\r
106                 if (ontologyUri == null) {\r
107                         ontologyUri = getSchemaFile().getName();\r
108                         \r
109                         int index = ontologyUri.lastIndexOf(".");\r
110                         if (index > 0)\r
111                                 ontologyUri = ontologyUri.substring(0, index);\r
112                 } \r
113                 ontologyUri = ontologyUri.replaceAll(" ", "_");\r
114                 String parts[] = ontologyUri.split("/");\r
115                 String name = parts[parts.length-1];\r
116                 name = name.replaceAll("\\.", "_");\r
117                 if (!ontologyUri.startsWith("http://"))\r
118                         ontologyUri = "http://" + ontologyUri.replaceAll("/", "_");\r
119                 else\r
120                         ontologyUri = "http://" + ontologyUri.substring("http://".length()).replaceAll("/", "_");\r
121                 \r
122                 String version = schema.getVersion();\r
123                 if (version == null)\r
124                         version = "1.0";\r
125                 ontologyUri +="-"+ version;\r
126 \r
127                 \r
128                 String className = getPluginName() + "." + name;\r
129                 \r
130                 if (createPGraph) {\r
131                 OntologyGenerator ontologyGenerator = new OntologyGenerator(configuration);\r
132                 ontologyGenerator.createOntology(schema, ontologyUri, className, this);\r
133                 }\r
134                 if (createImporter) {\r
135                 ImporterGenerator importerGenerator = new ImporterGenerator(configuration);\r
136                 importerGenerator.createParser(schema, ontologyUri, className, this);\r
137                 }\r
138                 if (createExporter) {\r
139                 ExporterGenerator exporterGenerator = new ExporterGenerator(configuration);\r
140                 exporterGenerator.createParser(schema, ontologyUri,className, this);\r
141                 }\r
142         }\r
143         \r
144         public File getOntologyFile() {\r
145                 return ontologyFile;\r
146         }\r
147         public File getParserDir() {\r
148                 return parserDir;\r
149         }\r
150         \r
151         public Schema getSchema() {\r
152                 return schema;\r
153         }\r
154         \r
155         public File getSchemaFile() {\r
156                 return schemaFile;\r
157         }\r
158         \r
159         public String getPluginName() {\r
160                 return pluginName;\r
161         }\r
162         \r
163         public String[] getHeader() {\r
164                 return header;\r
165         }\r
166         \r
167         public Configuration getConfiguration() {\r
168                 return configuration;\r
169         }\r
170 \r
171 \r
172 }\r