X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.xml.sax%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2FSchemaConverter.java;h=ed11c14cd7ad762f9cb4ffc81b49db92ad1387ef;hb=f11cbe76b3f4be142c9f84ef9a7b6bc9dcc8ff23;hp=3b2c1a0c42749b6bddf5babe3525924696bfb844;hpb=404be73748777cdd2d09b2f29308ae6f4a3d730c;p=simantics%2Finterop.git diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConverter.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConverter.java index 3b2c1a0..ed11c14 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConverter.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConverter.java @@ -2,16 +2,26 @@ package org.simantics.xml.sax; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; +import org.simantics.utils.datastructures.MapList; import org.simantics.xml.sax.configuration.Configuration; +import org.w3._2001.xmlschema.Annotation; +import org.w3._2001.xmlschema.Import; +import org.w3._2001.xmlschema.Include; +import org.w3._2001.xmlschema.OpenAttrs; import org.w3._2001.xmlschema.Schema; /** @@ -22,6 +32,7 @@ import org.w3._2001.xmlschema.Schema; */ public class SchemaConverter { + File outputPlugin; File schemaFile; File conversionFile; File ontologyFile; @@ -37,10 +48,32 @@ public class SchemaConverter { boolean createImporter = true; boolean createExporter = true; + private List parent = new ArrayList<>(); + private List subConverters = new ArrayList<>(); + private Map fileMap; + private MapList schemaNSMap; + private MapList shortNameMap; + String schemaNs; + String ontologyUri; + String className; + String name; + String shortName; - - public void convertSchema(File schemaFile, File conversionFile, File outputPlugin) throws JAXBException, IOException { + SchemaConversionBase base; + + private ManualSchemaFileImport fileImport; + + public SchemaConverter(File schemaFile, File conversionFile, File outputPlugin) throws IOException { + this(null,schemaFile,conversionFile,outputPlugin); + } + + public SchemaConverter(SchemaConverter parent,File schemaFile, File conversionFile, File outputPlugin) throws IOException { + + this.outputPlugin = outputPlugin; + this.schemaFile = schemaFile; + this.conversionFile = conversionFile; + pluginName = outputPlugin.getName(); String packageParts[] = pluginName.split("\\."); String outputLoc = outputPlugin.getAbsolutePath(); @@ -51,21 +84,29 @@ public class SchemaConverter { 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 (parent != null) { + this.parent.add(parent); + parent.subConverters.add(this); + } else { + fileMap = new HashMap<>(); + schemaNSMap = new MapList<>(); + shortNameMap = new MapList<>(); } - if (!parserDir.exists()) - parserDir.mkdirs(); - - convert(); - + getRoot().fileMap.put(schemaFile.getAbsolutePath(), this); + } + + public List getConverter(String schemaNS) { + return getRoot().schemaNSMap.getValues(schemaNS); + } + + public void setFileImport(ManualSchemaFileImport fileImport) { + this.fileImport = fileImport; } + public void setCreateExporter(boolean createExporter) { this.createExporter = createExporter; @@ -79,7 +120,79 @@ public class SchemaConverter { this.createPGraph = createPGraph; } - private void convert() throws JAXBException, IOException { + protected SchemaConverter createSubConverter(String location) throws JAXBException, IOException { + File directory = schemaFile.getParentFile(); + File schemaFile = new File(directory.getAbsolutePath()+File.separator+location); + if (!schemaFile.exists()) { + if (getRoot().fileImport != null) { + schemaFile = getRoot().fileImport.getFileForLocation(location); + } + if (!schemaFile.exists()) + throw new FileNotFoundException(schemaFile.getAbsolutePath()); + } + SchemaConverter subConverter = getRoot().fileMap.get((schemaFile.getAbsolutePath())); + if (subConverter == null) { + subConverter = new SchemaConverter(this,schemaFile, conversionFile, outputPlugin); + subConverter.createPGraph = this.createPGraph; + subConverter.createImporter = this.createImporter; + subConverter.createExporter = this.createExporter; + } else { + subConverter.parent.add(this); + } + return subConverter; + } + + protected SchemaConverter getRoot() { + SchemaConverter s = this; + while (s.parent.size() > 0) + s = s.parent.get(0); + return s; + } + + public void convert() throws JAXBException, IOException { + + init(); + doConvert(); + } + + boolean init = false; + + protected void assignShortName() { + shortName = name.substring(0, 3).toUpperCase(); + SchemaConverter root = getRoot(); + if (!root.shortNameMap.containsKey(shortName)) { + root.shortNameMap.add(shortName, this); + return; + } else { + SchemaConverter sc = root.shortNameMap.getValues(shortName).get(0); + if (sc.schemaNs.equals(schemaNs)) { + root.shortNameMap.add(shortName, this); + return; + } + } + int i = 1; + while (true) { + String n = shortName+i; + if (!root.shortNameMap.containsKey(n)) { + shortName = n; + root.shortNameMap.add(shortName, this); + return; + } else { + SchemaConverter sc = root.shortNameMap.getValues(n).get(0); + if (sc.schemaNs.equals(schemaNs)) { + shortName = n; + root.shortNameMap.add(shortName, this); + return; + } + } + i++; + } + } + + protected void init() throws IOException, JAXBException { + if (init) + return; + init = true; JAXBContext jc = JAXBContext.newInstance("org.w3._2001.xmlschema"); Unmarshaller u = jc.createUnmarshaller(); //u.setSchema(schema); @@ -101,8 +214,8 @@ public class SchemaConverter { header[2] = "File " + schemaFile.getAbsolutePath(); header[3] = "Date " + new Date().toString(); - - String ontologyUri = schema.getTargetNamespace(); + schemaNs = schema.getTargetNamespace(); + ontologyUri = schemaNs; if (ontologyUri == null) { ontologyUri = getSchemaFile().getName(); @@ -112,7 +225,7 @@ public class SchemaConverter { } ontologyUri = ontologyUri.replaceAll(" ", "_"); String parts[] = ontologyUri.split("/"); - String name = parts[parts.length-1]; + name = parts[parts.length-1]; name = name.replaceAll("\\.", "_"); if (!ontologyUri.startsWith("http://")) ontologyUri = "http://" + ontologyUri.replaceAll("/", "_"); @@ -125,20 +238,58 @@ public class SchemaConverter { ontologyUri +="-"+ version; - String className = getPluginName() + "." + name; + className = getPluginName() + "." + name; + assignShortName(); + if (schemaNs != null) + getRoot().schemaNSMap.add(schemaNs, this); + + + for (OpenAttrs attrs : schema.getIncludeOrImportOrRedefine()) { + if (attrs instanceof Import) { + Import imp = (Import)attrs; + String location = imp.getSchemaLocation(); + SchemaConverter sc = createSubConverter(location); + sc.init(); + } else if (attrs instanceof Include) { + Include inc = (Include)attrs; + String location = inc.getSchemaLocation(); + SchemaConverter sc = createSubConverter(location); + sc.init(); + } else if (attrs instanceof Annotation) { + + } else { + throw new IOException("Cannot handle schema file " + schemaFile.getName() + ", the schema uses redefine elements."); + } + } + } + + protected void doConvert() throws IOException, JAXBException { + if (!ontologyFile.exists()) { + ontologyFile.getParentFile().mkdirs(); + ontologyFile.createNewFile(); + } + if (!parserDir.exists()) + parserDir.mkdirs(); + + for (SchemaConverter sc : subConverters) + sc.doConvert(); + + base = new SchemaConversionBase(this,ontologyUri,className); + base.init(schema); if (createPGraph) { - OntologyGenerator ontologyGenerator = new OntologyGenerator(configuration); - ontologyGenerator.createOntology(schema, ontologyUri, className, this); + OntologyGenerator ontologyGenerator = new OntologyGenerator(this,base); + ontologyGenerator.createOntology(); } if (createImporter) { - ImporterGenerator importerGenerator = new ImporterGenerator(configuration); - importerGenerator.createParser(schema, ontologyUri, className, this); + ImporterGenerator importerGenerator = new ImporterGenerator(this,base); + importerGenerator.createParser(); } if (createExporter) { - ExporterGenerator exporterGenerator = new ExporterGenerator(configuration); - exporterGenerator.createParser(schema, ontologyUri,className, this); + ExporterGenerator exporterGenerator = new ExporterGenerator(this,base); + exporterGenerator.createParser(); } + base.component = null; } public File getOntologyFile() {