]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConverter.java
32ae1672baa41dc4ff00b588556c3f03ecb6f9a5
[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 ontologyName = schema.getTargetNamespace();\r
106                 if (ontologyName == null) {\r
107                         ontologyName = getSchemaFile().getName();\r
108                         \r
109                         int index = ontologyName.lastIndexOf(".");\r
110                         if (index > 0)\r
111                                 ontologyName = ontologyName.substring(0, index);\r
112                 } \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
118 \r
119                 \r
120                 name = name.replaceAll("\\.", "_");\r
121                 name = name.replaceAll(" ", "_");\r
122                 String className = getPluginName() + "." + name;\r
123                 \r
124                 if (createPGraph) {\r
125                 OntologyGenerator ontologyGenerator = new OntologyGenerator(configuration);\r
126                 ontologyGenerator.createOntology(schema, ontologyName, className, this);\r
127                 }\r
128                 if (createImporter) {\r
129                 ImporterGenerator importerGenerator = new ImporterGenerator(configuration);\r
130                 importerGenerator.createParser(schema, className, this);\r
131                 }\r
132                 if (createExporter) {\r
133                 ExporterGenerator exporterGenerator = new ExporterGenerator(configuration);\r
134                 exporterGenerator.createParser(schema, className, this);\r
135                 }\r
136         }\r
137         \r
138         public File getOntologyFile() {\r
139                 return ontologyFile;\r
140         }\r
141         public File getParserDir() {\r
142                 return parserDir;\r
143         }\r
144         \r
145         public Schema getSchema() {\r
146                 return schema;\r
147         }\r
148         \r
149         public File getSchemaFile() {\r
150                 return schemaFile;\r
151         }\r
152         \r
153         public String getPluginName() {\r
154                 return pluginName;\r
155         }\r
156         \r
157         public String[] getHeader() {\r
158                 return header;\r
159         }\r
160         \r
161         public Configuration getConfiguration() {\r
162                 return configuration;\r
163         }\r
164 \r
165 \r
166 }\r