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; boolean createPGraph = true; boolean createImporter = true; boolean createExporter = true; 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(); } public void setCreateExporter(boolean createExporter) { this.createExporter = createExporter; } public void setCreateImporter(boolean createImporter) { this.createImporter = createImporter; } public void setCreatePGraph(boolean createPGraph) { this.createPGraph = createPGraph; } 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(); String ontologyName = schema.getTargetNamespace(); if (ontologyName == null) { ontologyName = getSchemaFile().getName(); int index = ontologyName.lastIndexOf("."); if (index > 0) ontologyName = ontologyName.substring(0, index); } if (!ontologyName.startsWith("http")) ontologyName = "http://" + ontologyName; String parts[] = ontologyName.split("/"); String name = parts[parts.length-1]; //String ontRoot = name.substring(0, Math.min(3, name.length())).toUpperCase(); name = name.replaceAll("\\.", "_"); name = name.replaceAll(" ", "_"); String className = getPluginName() + "." + name; if (createPGraph) { OntologyGenerator ontologyGenerator = new OntologyGenerator(configuration); ontologyGenerator.createOntology(schema, ontologyName, className, this); } if (createImporter) { ImporterGenerator importerGenerator = new ImporterGenerator(configuration); importerGenerator.createParser(schema, className, this); } if (createExporter) { ExporterGenerator exporterGenerator = new ExporterGenerator(configuration); exporterGenerator.createParser(schema, className, 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; } }