]> gerrit.simantics Code Review - simantics/interop.git/blob - sax/SchemaConverter.java
Replace log4j with slf4j
[simantics/interop.git] / 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.FileNotFoundException;\r
6 import java.io.IOException;\r
7 import java.io.InputStream;\r
8 import java.util.ArrayList;\r
9 import java.util.Date;\r
10 import java.util.HashMap;\r
11 import java.util.List;\r
12 import java.util.Map;\r
13 \r
14 import javax.xml.bind.JAXBContext;\r
15 import javax.xml.bind.JAXBElement;\r
16 import javax.xml.bind.JAXBException;\r
17 import javax.xml.bind.Unmarshaller;\r
18 \r
19 import org.simantics.utils.datastructures.MapList;\r
20 import org.simantics.xml.sax.configuration.Configuration;\r
21 import org.w3._2001.xmlschema.Annotation;\r
22 import org.w3._2001.xmlschema.Import;\r
23 import org.w3._2001.xmlschema.Include;\r
24 import org.w3._2001.xmlschema.OpenAttrs;\r
25 import org.w3._2001.xmlschema.Schema;\r
26 \r
27 /**\r
28  * This file is developed for XMpLant / Proteus schema conversion is not able to handle all XML Schema definitions.\r
29  * \r
30  * @author mlmarko\r
31  *\r
32  */\r
33 public class SchemaConverter {\r
34         \r
35         File outputPlugin;\r
36         File schemaFile;\r
37         File conversionFile;\r
38         File ontologyFile;\r
39         File parserDir;\r
40         Schema schema;\r
41         Configuration configuration;\r
42         \r
43         String pluginName;\r
44         \r
45         private String[] header;\r
46         \r
47         boolean createPGraph = true;\r
48         boolean createImporter = true;\r
49         boolean createExporter = true;\r
50         \r
51         private List<SchemaConverter> parent = new ArrayList<>();\r
52         private List<SchemaConverter> subConverters = new ArrayList<>();\r
53         private Map<String,SchemaConverter> fileMap;\r
54         private MapList<String,SchemaConverter> schemaNSMap;\r
55         private MapList<String,SchemaConverter> shortNameMap;\r
56         \r
57         String schemaNs;\r
58         String ontologyUri;\r
59         String className;\r
60         String name;\r
61         String shortName;\r
62         \r
63         SchemaConversionBase base;\r
64         \r
65         private ManualSchemaFileImport fileImport;\r
66         \r
67         public SchemaConverter(File schemaFile, File conversionFile, File outputPlugin) throws IOException {\r
68                 this(null,schemaFile,conversionFile,outputPlugin);\r
69         }\r
70         \r
71         public SchemaConverter(SchemaConverter parent,File schemaFile, File conversionFile, File outputPlugin) throws IOException {\r
72                 \r
73                 this.outputPlugin = outputPlugin;\r
74                 this.schemaFile = schemaFile;\r
75                 this.conversionFile = conversionFile;\r
76                 \r
77                 pluginName = outputPlugin.getName();\r
78                 String packageParts[] = pluginName.split("\\.");\r
79                 String outputLoc = outputPlugin.getAbsolutePath();\r
80                 outputLoc += "/src";\r
81                 for (String s : packageParts)\r
82                         outputLoc += "/"+s;\r
83                 String outputGraph = outputPlugin.getAbsolutePath();\r
84                 outputGraph += "/graph";\r
85                 outputGraph += "/" + schemaFile.getName().substring(0, schemaFile.getName().length()-4) +".pgraph";\r
86                 \r
87                 \r
88                 this.ontologyFile = new File(outputGraph);\r
89                 this.parserDir = new File(outputLoc);\r
90                 \r
91                 if (parent != null) {\r
92                         this.parent.add(parent);\r
93                         parent.subConverters.add(this);\r
94                 } else {\r
95                         fileMap = new HashMap<>();\r
96                         schemaNSMap = new MapList<>();\r
97                         shortNameMap = new MapList<>();\r
98                 }\r
99                 getRoot().fileMap.put(schemaFile.getAbsolutePath(), this);\r
100         }\r
101         \r
102         public List<SchemaConverter> getConverter(String schemaNS) {\r
103                 return getRoot().schemaNSMap.getValues(schemaNS);\r
104         }\r
105         \r
106         public void setFileImport(ManualSchemaFileImport fileImport) {\r
107                 this.fileImport = fileImport;\r
108         }\r
109 \r
110         \r
111         public void setCreateExporter(boolean createExporter) {\r
112                 this.createExporter = createExporter;\r
113         }\r
114         \r
115         public void setCreateImporter(boolean createImporter) {\r
116                 this.createImporter = createImporter;\r
117         }\r
118         \r
119         public void setCreatePGraph(boolean createPGraph) {\r
120                 this.createPGraph = createPGraph;\r
121         }\r
122         \r
123         protected SchemaConverter createSubConverter(String location) throws JAXBException, IOException {\r
124                 File directory = schemaFile.getParentFile();\r
125                 File schemaFile = new File(directory.getAbsolutePath()+File.separator+location);\r
126                 if (!schemaFile.exists()) {\r
127                         if (getRoot().fileImport != null) {\r
128                                 schemaFile = getRoot().fileImport.getFileForLocation(location);\r
129                         }\r
130                         if (!schemaFile.exists())\r
131                                 throw new FileNotFoundException(schemaFile.getAbsolutePath());\r
132                 }\r
133                 SchemaConverter subConverter = getRoot().fileMap.get((schemaFile.getAbsolutePath()));\r
134                 if (subConverter == null) {\r
135                         subConverter = new SchemaConverter(this,schemaFile, conversionFile, outputPlugin);\r
136                         subConverter.createPGraph = this.createPGraph;\r
137                         subConverter.createImporter = this.createImporter;\r
138                         subConverter.createExporter = this.createExporter;\r
139                 } else {\r
140                         subConverter.parent.add(this);\r
141                 }\r
142                 return subConverter;\r
143         }\r
144         \r
145         protected SchemaConverter getRoot() {\r
146                 SchemaConverter s = this;\r
147                 while (s.parent.size() > 0)\r
148                         s = s.parent.get(0);\r
149                 return s;\r
150         }\r
151         \r
152         public void convert() throws JAXBException, IOException {\r
153                 \r
154                 init();\r
155                 doConvert();\r
156         }\r
157         \r
158         boolean init = false;\r
159         \r
160         protected void assignShortName() {\r
161                 shortName = name.substring(0, 3).toUpperCase();\r
162                 SchemaConverter root = getRoot();\r
163                 if (!root.shortNameMap.containsKey(shortName)) {\r
164                         root.shortNameMap.add(shortName, this);\r
165                         return;\r
166                 } else {\r
167                         SchemaConverter sc = root.shortNameMap.getValues(shortName).get(0);\r
168                         if (sc.schemaNs.equals(schemaNs)) {\r
169                                 root.shortNameMap.add(shortName, this);\r
170                                 return;\r
171                         }\r
172                 }\r
173                 int i = 1;\r
174                 while (true) {\r
175                         String n = shortName+i;\r
176                         if (!root.shortNameMap.containsKey(n)) {\r
177                                 shortName = n;\r
178                                 root.shortNameMap.add(shortName, this);\r
179                                 return;\r
180                         } else {\r
181                                 SchemaConverter sc = root.shortNameMap.getValues(n).get(0);\r
182                                 if (sc.schemaNs.equals(schemaNs)) {\r
183                                         shortName = n;\r
184                                         root.shortNameMap.add(shortName, this);\r
185                                         return;\r
186                                 }\r
187                         }\r
188                         i++;\r
189                 }\r
190         }\r
191         \r
192         protected void init() throws IOException, JAXBException {\r
193                 if (init)\r
194                         return;\r
195                 init = true;\r
196                 JAXBContext jc = JAXBContext.newInstance("org.w3._2001.xmlschema");\r
197                 Unmarshaller u = jc.createUnmarshaller();\r
198                 //u.setSchema(schema);\r
199         InputStream fileStream = new FileInputStream(schemaFile);\r
200                 schema = (Schema)u.unmarshal(fileStream);\r
201                 \r
202                 if (conversionFile != null) {\r
203                         jc = JAXBContext.newInstance("org.simantics.xml.sax.configuration");\r
204                         u = jc.createUnmarshaller();\r
205                         fileStream = new FileInputStream(conversionFile);\r
206                         configuration = (Configuration)((JAXBElement<?>)u.unmarshal(fileStream)).getValue();\r
207                 } else {\r
208                         configuration = new Configuration();\r
209                 }\r
210                 \r
211                 header = new String[4];\r
212                 header[0] = "Generated with org.simantics.xml.sax XML schema converter";\r
213                 header[1] = "";\r
214                 header[2] = "File " + schemaFile.getAbsolutePath();\r
215                 header[3] = "Date " + new Date().toString();\r
216                 \r
217                 schemaNs = schema.getTargetNamespace();\r
218                 ontologyUri = schemaNs;\r
219                 if (ontologyUri == null) {\r
220                         ontologyUri = getSchemaFile().getName();\r
221                         \r
222                         int index = ontologyUri.lastIndexOf(".");\r
223                         if (index > 0)\r
224                                 ontologyUri = ontologyUri.substring(0, index);\r
225                 } \r
226                 ontologyUri = ontologyUri.replaceAll(" ", "_");\r
227                 String parts[] = ontologyUri.split("/");\r
228                 name = parts[parts.length-1];\r
229                 name = name.replaceAll("\\.", "_");\r
230                 if (!ontologyUri.startsWith("http://"))\r
231                         ontologyUri = "http://" + ontologyUri.replaceAll("/", "_");\r
232                 else\r
233                         ontologyUri = "http://" + ontologyUri.substring("http://".length()).replaceAll("/", "_");\r
234                 \r
235                 String version = schema.getVersion();\r
236                 if (version == null)\r
237                         version = "1.0";\r
238                 ontologyUri +="-"+ version;\r
239 \r
240                 \r
241                 className = getPluginName() + "." + name;\r
242                 assignShortName();\r
243                 if (schemaNs != null)\r
244                         getRoot().schemaNSMap.add(schemaNs, this);\r
245                 \r
246                 \r
247                 for (OpenAttrs attrs : schema.getIncludeOrImportOrRedefine()) {\r
248                         if (attrs instanceof Import) {\r
249                                 Import imp = (Import)attrs;\r
250                                 String location = imp.getSchemaLocation();\r
251                                 SchemaConverter sc = createSubConverter(location);\r
252                                 sc.init();\r
253                         } else if (attrs instanceof Include) {\r
254                                 Include inc = (Include)attrs;\r
255                                 String location = inc.getSchemaLocation();\r
256                                 SchemaConverter sc = createSubConverter(location);\r
257                                 sc.init();\r
258                         } else if (attrs instanceof Annotation) {\r
259                                 \r
260                         } else {\r
261                                 throw new IOException("Cannot handle schema file " + schemaFile.getName() + ", the schema uses redefine elements.");\r
262                         }\r
263                 }\r
264         }\r
265         \r
266         protected void doConvert() throws IOException, JAXBException {\r
267                 if (!ontologyFile.exists()) {\r
268                         ontologyFile.getParentFile().mkdirs();\r
269                         ontologyFile.createNewFile();\r
270                 }\r
271                 if (!parserDir.exists())\r
272                         parserDir.mkdirs();\r
273                 \r
274                 for (SchemaConverter sc : subConverters)\r
275                         sc.doConvert();\r
276                 \r
277                 base = new SchemaConversionBase(this,ontologyUri,className);\r
278                 base.init(schema);\r
279                 \r
280                 if (createPGraph) {\r
281                         OntologyGenerator ontologyGenerator = new OntologyGenerator(this,base);\r
282                         ontologyGenerator.createOntology();\r
283                 }\r
284                 if (createImporter) {\r
285                         ImporterGenerator importerGenerator = new ImporterGenerator(this,base);\r
286                         importerGenerator.createParser();\r
287                 }\r
288                 if (createExporter) {\r
289                         ExporterGenerator exporterGenerator = new ExporterGenerator(this,base);\r
290                         exporterGenerator.createParser();\r
291                 }\r
292                 base.component = null;\r
293         }\r
294         \r
295         public File getOntologyFile() {\r
296                 return ontologyFile;\r
297         }\r
298         public File getParserDir() {\r
299                 return parserDir;\r
300         }\r
301         \r
302         public Schema getSchema() {\r
303                 return schema;\r
304         }\r
305         \r
306         public File getSchemaFile() {\r
307                 return schemaFile;\r
308         }\r
309         \r
310         public String getPluginName() {\r
311                 return pluginName;\r
312         }\r
313         \r
314         public String[] getHeader() {\r
315                 return header;\r
316         }\r
317         \r
318         public Configuration getConfiguration() {\r
319                 return configuration;\r
320         }\r
321 \r
322 \r
323 }\r