package org.simantics.xml.sax; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Date; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import org.simantics.xml.sax.configuration.Configuration; import org.w3._2001.xmlschema.Schema; /** * This file is developed for XMpLant / Proteus schema conversion is not able to handle all XML Schema definitions. * * @author mlmarko * */ public class SchemaConverter { File schemaFile; File conversionFile; File ontologyFile; File parserDir; Schema schema; Configuration configuration; String pluginName; private String[] header; public void convertSchema(File schemaFile, File conversionFile, File outputPlugin) throws JAXBException, IOException { pluginName = outputPlugin.getName(); String packageParts[] = pluginName.split("\\."); String outputLoc = outputPlugin.getAbsolutePath(); outputLoc += "/src"; for (String s : packageParts) outputLoc += "/"+s; String outputGraph = outputPlugin.getAbsolutePath(); outputGraph += "/graph"; outputGraph += "/" + schemaFile.getName().substring(0, schemaFile.getName().length()-4) +".pgraph"; this.schemaFile = schemaFile; this.conversionFile = conversionFile; this.ontologyFile = new File(outputGraph); this.parserDir = new File(outputLoc); if (!ontologyFile.exists()) { ontologyFile.getParentFile().mkdirs(); ontologyFile.createNewFile(); } if (!parserDir.exists()) parserDir.mkdirs(); convert(); } private void convert() throws JAXBException, IOException { JAXBContext jc = JAXBContext.newInstance("org.w3._2001.xmlschema"); Unmarshaller u = jc.createUnmarshaller(); //u.setSchema(schema); InputStream fileStream = new FileInputStream(schemaFile); schema = (Schema)u.unmarshal(fileStream); if (conversionFile != null) { jc = JAXBContext.newInstance("org.simantics.xml.sax.configuration"); u = jc.createUnmarshaller(); fileStream = new FileInputStream(conversionFile); configuration = (Configuration)((JAXBElement)u.unmarshal(fileStream)).getValue(); } else { configuration = new Configuration(); } header = new String[4]; header[0] = "Generated with org.simantics.xml.sax XML schema converter"; header[1] = ""; header[2] = "File " + schemaFile.getAbsolutePath(); header[3] = "Date " + new Date().toString(); OntologyGenerator ontologyGenerator = new OntologyGenerator(configuration); ontologyGenerator.createOntology(schema, this); ImporterGenerator importerGenerator = new ImporterGenerator(configuration); importerGenerator.createParser(schema, ontologyGenerator.getClassName(), this); } public File getOntologyFile() { return ontologyFile; } public File getParserDir() { return parserDir; } public Schema getSchema() { return schema; } public File getSchemaFile() { return schemaFile; } public String getPluginName() { return pluginName; } public String[] getHeader() { return header; } public Configuration getConfiguration() { return configuration; } }