X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=org.simantics.xml.sax%2Fsrc%2Forg%2Fsimantics%2Fxml%2Fsax%2FSchemaConverter.java;h=660b990975f640c66bc093f541fa024ccda7f16f;hb=refs%2Fchanges%2F25%2F3625%2F1;hp=1b2b3722606832c8f55fc62cc7c082e97cc1eab6;hpb=7f59891d2747280b09e77bc833bc70e05eece706;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 1b2b372..660b990 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 @@ -7,9 +7,11 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Date; -import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.regex.Matcher; import javax.xml.bind.JAXBContext; @@ -33,15 +35,15 @@ import org.w3._2001.xmlschema.Schema; */ public class SchemaConverter { - File outputPlugin; - File schemaFile; - File conversionFile; - File ontologyFile; - File parserDir; - Schema schema; - Configuration configuration; + protected File outputPlugin; + protected File schemaFile; + protected File conversionFile; + protected File ontologyFile; + protected File parserDir; + protected Schema schema; + protected Configuration configuration; - String pluginName; + protected String pluginName; private String[] header; @@ -55,13 +57,13 @@ public class SchemaConverter { private MapList schemaNSMap; private MapList shortNameMap; - String schemaNs; - String ontologyUri; - String className; - String name; - String shortName; + protected String schemaNs; + protected String ontologyUri; + protected String className; + protected String name; + protected String shortName; - SchemaConversionBase base; + protected SchemaConversionBase base; private ManualSchemaFileImport fileImport; @@ -70,7 +72,8 @@ public class SchemaConverter { } public SchemaConverter(SchemaConverter parent,File schemaFile, File conversionFile, File outputPlugin) throws IOException { - + if (schemaFile == null || outputPlugin == null) + throw new IllegalArgumentException(); this.outputPlugin = outputPlugin; this.schemaFile = schemaFile; this.conversionFile = conversionFile; @@ -93,7 +96,7 @@ public class SchemaConverter { this.parent.add(parent); parent.subConverters.add(this); } else { - fileMap = new HashMap<>(); + fileMap = new LinkedHashMap<>(); schemaNSMap = new MapList<>(); shortNameMap = new MapList<>(); } @@ -121,7 +124,7 @@ public class SchemaConverter { this.createPGraph = createPGraph; } - protected SchemaConverter createSubConverter(String location) throws JAXBException, IOException { + protected SchemaConverter createSubConverter(String location, String ns) throws JAXBException, IOException { File directory = schemaFile.getParentFile(); File schemaFile = new File(directory.getAbsolutePath()+File.separator+location); if (!schemaFile.exists()) { @@ -133,32 +136,53 @@ public class SchemaConverter { } SchemaConverter subConverter = getRoot().fileMap.get((schemaFile.getAbsolutePath())); if (subConverter == null) { - subConverter = new SchemaConverter(this,schemaFile, conversionFile, outputPlugin); + subConverter = constructSubConverter(this, schemaFile, conversionFile, outputPlugin, ns); subConverter.createPGraph = this.createPGraph; subConverter.createImporter = this.createImporter; subConverter.createExporter = this.createExporter; } else { subConverter.parent.add(this); + subConverters.add(subConverter); } return subConverter; } + protected SchemaConverter constructSubConverter(SchemaConverter parent, File schemaFile, File conversionFile, File outputPlugin, String ns) throws IOException { + return new SchemaConverter(parent,schemaFile, conversionFile, outputPlugin); + } + protected SchemaConverter getRoot() { - SchemaConverter s = this; - if (s.fileMap != null) - return s; - while (s.parent.size() > 0) { - s = s.parent.get(0); - if (s.fileMap != null) - return s; + if (this.fileMap != null) + return this; + Set processed = new HashSet<>(); + return _getRoot(processed); + } + + protected SchemaConverter _getRoot(Set processed) { + if (processed.contains(this)) + return null; + if (this.fileMap != null) + return this; + processed.add(this); + + for (SchemaConverter sc : this.parent) { + if (sc.fileMap != null) + return sc; } - return s; + for (SchemaConverter sc : this.parent) { + SchemaConverter root = sc._getRoot(processed); + if (root != null) + return root; + } + return null; } public void convert() throws JAXBException, IOException { init(); - doConvert(); + for (SchemaConverter sc : getRoot().fileMap.values()) { + sc.doConvert(); + } } boolean init = false; @@ -195,20 +219,25 @@ public class SchemaConverter { } } - protected void init() throws IOException, JAXBException { - if (init) - return; - init = true; + protected Schema createSchema() throws JAXBException, FileNotFoundException { 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); + return (Schema)u.unmarshal(fileStream); + } + + protected void init() throws IOException, JAXBException { + if (init) + return; + init = true; + + schema = createSchema(); if (conversionFile != null) { - jc = JAXBContext.newInstance("org.simantics.xml.sax.configuration"); - u = jc.createUnmarshaller(); - fileStream = new FileInputStream(conversionFile); + JAXBContext jc = JAXBContext.newInstance("org.simantics.xml.sax.configuration"); + Unmarshaller u = jc.createUnmarshaller(); + InputStream fileStream = new FileInputStream(conversionFile); configuration = (Configuration)((JAXBElement)u.unmarshal(fileStream)).getValue(); } else { configuration = new Configuration(); @@ -221,6 +250,7 @@ public class SchemaConverter { header[3] = "Date " + new Date().toString(); schemaNs = schema.getTargetNamespace(); + ontologyUri = schemaNs; if (ontologyUri == null) { ontologyUri = getSchemaFile().getName(); @@ -228,8 +258,19 @@ public class SchemaConverter { int index = ontologyUri.lastIndexOf("."); if (index > 0) ontologyUri = ontologyUri.substring(0, index); - } + schemaNs = ""; + } else { + // Special case for XAML + if (ontologyUri.startsWith("clr-namespace:")) { + ontologyUri = ontologyUri.substring("clr-namespace:".length()); + int i = ontologyUri.indexOf(";assembly"); + if (i > 0) + ontologyUri = ontologyUri.substring(0, i); + } + } ontologyUri = ontologyUri.replaceAll(" ", "_"); + ontologyUri = ontologyUri.replaceAll(":", "_"); + ontologyUri = ontologyUri.replaceAll(";", "_"); String parts[] = ontologyUri.split("/"); for (int i = parts.length-1; i >= 0; i--) { name = parts[i]; @@ -253,22 +294,24 @@ public class SchemaConverter { ontologyUri +="-"+ version; - className = getPluginName() + "." + name; + className = getPluginName() + "." + name + "Ontology"; assignShortName(); if (schemaNs != null) getRoot().schemaNSMap.add(schemaNs, this); + base = new SchemaConversionBase(this,ontologyUri,className); + base.init(schema); for (OpenAttrs attrs : schema.getIncludeOrImportOrRedefine()) { if (attrs instanceof Import) { Import imp = (Import)attrs; String location = imp.getSchemaLocation(); - SchemaConverter sc = createSubConverter(location); + SchemaConverter sc = createSubConverter(location, imp.getNamespace()); sc.init(); } else if (attrs instanceof Include) { Include inc = (Include)attrs; String location = inc.getSchemaLocation(); - SchemaConverter sc = createSubConverter(location); + SchemaConverter sc = createSubConverter(location, null); sc.init(); } else if (attrs instanceof Annotation) { @@ -277,8 +320,13 @@ public class SchemaConverter { } } } + + private boolean converting = false; protected void doConvert() throws IOException, JAXBException { + if (converting) + return; + converting = true; if (!ontologyFile.exists()) { ontologyFile.getParentFile().mkdirs(); ontologyFile.createNewFile(); @@ -286,12 +334,6 @@ public class SchemaConverter { 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(this,base); ontologyGenerator.createOntology(); @@ -334,8 +376,39 @@ public class SchemaConverter { return configuration; } + public List getParent() { + return parent; + } + + public List getSubConverters() { + return subConverters; + } + public boolean isPrimary() { - return getRoot().schemaNSMap.getValues(schemaNs).indexOf(this) == 0; + return true; +// if (getRoot() == this) +// return true; +// List conv = new ArrayList<>(getRoot().fileMap.values()); +// int current = conv.indexOf(this); + + } + + public String getShortName(String namespaceURI) { + List converters = getRoot().getConverter(namespaceURI); + for (SchemaConverter conv : converters) { + if (conv.shortName != null) + return conv.shortName; + } + return null; + } + + public String getOntologyClassName(String namespaceURI) { + List converters = getRoot().getConverter(namespaceURI); + for (SchemaConverter conv : converters) { + if (conv.className != null) + return conv.className; + } + return null; }