]> gerrit.simantics Code Review - simantics/interop.git/commitdiff
Create version neutral wrapper class for converted XML schema ontology classes
authorluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 14 Sep 2016 14:44:37 +0000 (14:44 +0000)
committerMarko Luukkainen <marko.luukkainen@vtt.fi>
Thu, 2 Feb 2017 12:01:34 +0000 (14:01 +0200)
fixes #6687

git-svn-id: https://www.simantics.org/svn/simantics/interoperability/trunk@33194 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/AbstractImporter.java
org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/wizard/SchemaCombinationPage.java [new file with mode: 0644]
org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/wizard/SchemaConversionWizard.java
org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyCombinator.java [new file with mode: 0644]

index af5c2c3da0fc43c1da6ea4f42e53129aa0fd43c3..9e0d57dac8a0f6acb8aafbc3e99d8f07262fccf5 100644 (file)
@@ -2,8 +2,6 @@ package org.simantics.xml.sax.base;
 \r
 import java.io.File;\r
 \r
-import org.eclipse.core.runtime.IStatus;\r
-import org.eclipse.core.runtime.Status;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.Session;\r
 import org.simantics.db.WriteGraph;\r
diff --git a/org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/wizard/SchemaCombinationPage.java b/org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/wizard/SchemaCombinationPage.java
new file mode 100644 (file)
index 0000000..1539f50
--- /dev/null
@@ -0,0 +1,158 @@
+package org.simantics.xml.sax.ui.wizard;\r
+\r
+import java.io.File;\r
+import java.io.FilenameFilter;\r
+import java.io.IOException;\r
+import java.util.Arrays;\r
+\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.viewers.CheckboxTableViewer;\r
+import org.eclipse.jface.viewers.IStructuredContentProvider;\r
+import org.eclipse.jface.viewers.ITableLabelProvider;\r
+import org.eclipse.jface.viewers.LabelProvider;\r
+import org.eclipse.jface.viewers.Viewer;\r
+import org.eclipse.jface.wizard.WizardPage;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.SelectionAdapter;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.graphics.Image;\r
+import org.eclipse.swt.layout.GridLayout;\r
+import org.eclipse.swt.widgets.Button;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Label;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.simantics.xml.sax.OntologyCombinator;\r
+\r
+public class SchemaCombinationPage extends WizardPage {\r
+       private Composite composite;\r
+       \r
+       File outputPlugin;\r
+       File srcDir;\r
+       \r
+       CheckboxTableViewer fileSelector;\r
+       Text outputText;\r
+       \r
+       public SchemaCombinationPage() {\r
+               super("XML Schema conversion","Combining schema versions", null);\r
+               setPageComplete(false);\r
+       }\r
+       \r
+       @Override\r
+       public void setVisible(boolean visible) {\r
+               super.setVisible(visible);\r
+               if (visible) {\r
+                       if (outputPlugin == null) {\r
+                               Label label = new Label(composite, SWT.NONE);\r
+                               label.setText("Output plug-in has not been set, cannot create combination resources");\r
+                               return;\r
+                       }\r
+                       String name = outputPlugin.getName();\r
+                       name = name.replaceAll("\\.", "/");\r
+                       srcDir = new File(outputPlugin.getAbsolutePath() +"/src/" + name);\r
+                       fileSelector.setInput(srcDir);\r
+               }\r
+       }\r
+       \r
+       \r
+       @Override\r
+       public void createControl(Composite parent) {\r
+               composite = new Composite(parent, SWT.NONE);\r
+               composite.setLayout(new GridLayout(1,true));\r
+               setControl(composite);  \r
+\r
+               Label label = new Label(composite, SWT.NONE);\r
+               label.setText("Ontology resource files");\r
+               fileSelector = CheckboxTableViewer.newCheckList(composite, SWT.BORDER);\r
+               fileSelector.setContentProvider(new FileContentProvider());\r
+               fileSelector.setLabelProvider(new FileLabelProvider());\r
+\r
+               label = new Label(composite, SWT.NONE);\r
+               label.setText("Output file");\r
+               outputText = new Text(composite, SWT.BORDER|SWT.SINGLE);\r
+               Button button = new Button(composite, SWT.PUSH);\r
+               button.setText("Combine");\r
+               button.addSelectionListener(new SelectionAdapter() {\r
+                       @Override\r
+                       public void widgetSelected(SelectionEvent e) {\r
+                               doCombination();\r
+                       }\r
+               });\r
+               \r
+               GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).applyTo(fileSelector.getControl());\r
+               GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(outputText);\r
+               GridDataFactory.fillDefaults().align(SWT.RIGHT, SWT.CENTER).applyTo(button);\r
+       }\r
+       \r
+       public void setOutputPlugin(File outputPlugin) {\r
+               this.outputPlugin = outputPlugin;\r
+       }\r
+       \r
+       private void doCombination() {\r
+               String outputName = outputText.getText();\r
+               if (outputName == null || outputName.length() < 2)\r
+                       return;\r
+               if (!outputName.endsWith(".java"))\r
+                       outputName+=".java";\r
+               \r
+               File outputFile = new File(srcDir.getAbsolutePath()+"/"+outputName);\r
+               \r
+               Object sel[] = fileSelector.getCheckedElements();\r
+               File inputFiles[] = new File[sel.length];\r
+               for (int i = 0; i < sel.length; i++) {\r
+                       inputFiles[i] = (File)sel[i];\r
+               }\r
+               \r
+               try {\r
+                       OntologyCombinator.combine(inputFiles, outputFile);\r
+                       setPageComplete(true);\r
+               } catch (IOException e) {\r
+                       setErrorMessage(e.getMessage());\r
+                       e.printStackTrace();\r
+               }\r
+               \r
+       }\r
+       \r
+       private static class FileContentProvider implements IStructuredContentProvider {\r
+               @Override\r
+               public Object[] getElements(Object inputElement) {\r
+                       File directory = (File)inputElement;\r
+                       if (!directory.isDirectory())\r
+                               return new Object[0];\r
+                       File[] files = directory.listFiles(new FilenameFilter() {\r
+                               \r
+                               @Override\r
+                               public boolean accept(File dir, String name) {\r
+                                       return name.endsWith("java");\r
+                               }\r
+                       });\r
+                       return files;\r
+               }\r
+               \r
+               @Override\r
+               public void dispose() {\r
+                       \r
+               }\r
+               \r
+               @Override\r
+               public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {\r
+                       \r
+               }\r
+       }\r
+       \r
+       private static class FileLabelProvider extends LabelProvider implements ITableLabelProvider {\r
+               @Override\r
+               public Image getColumnImage(Object element, int columnIndex) {\r
+                       return null;\r
+               }\r
+               \r
+               @Override\r
+               public String getColumnText(Object element, int columnIndex) {\r
+                       File file = (File)element;\r
+                       if (columnIndex == 0)\r
+                               return file.getName();\r
+                       else\r
+                               return null;\r
+               }\r
+       }\r
+\r
+}\r
index fae0a576deffcbae1383198979e1612a30e36433..7512c8adc375cdf78ef1b8ce65051f9ac59319ea 100644 (file)
@@ -10,11 +10,9 @@ import org.simantics.xml.sax.SchemaConverter;
 \r
 public class SchemaConversionWizard extends Wizard implements IImportWizard {\r
 \r
-//     FileSelectionPage fileselectionPage;\r
-//     FileSelectionPage configurationSelectionPage;\r
-//     DirectorySelectionPage pluginSelectionPage;\r
-       InputSelectionPage inputSelectionPage ;\r
+       InputSelectionPage inputSelectionPage;\r
        ImportProcessPage importProcessPage;\r
+       SchemaCombinationPage schemaCombinationPage;\r
        \r
        public SchemaConversionWizard() {\r
                setWindowTitle("XML Schema conversion");\r
@@ -22,7 +20,6 @@ public class SchemaConversionWizard extends Wizard implements IImportWizard {
 \r
        @Override\r
        public void init(IWorkbench workbench, IStructuredSelection selection) {\r
-               // TODO Auto-generated method stub\r
 \r
        }\r
 \r
@@ -36,21 +33,14 @@ public class SchemaConversionWizard extends Wizard implements IImportWizard {
        \r
        @Override\r
        public void addPages() {\r
-//             fileselectionPage = new FileSelectionPage();\r
-//             fileselectionPage.setFilterExtensions(new String[]{"*.xsd"});\r
-//             fileselectionPage.setFilterNames(new String[]{"XML schema files"});\r
-//             pluginSelectionPage = new DirectorySelectionPage();\r
-//             addPage(fileselectionPage);\r
-//             addPage(pluginSelectionPage);\r
                addPage(inputSelectionPage = new InputSelectionPage());\r
                addPage(importProcessPage = new ImportProcessPage());\r
+               addPage(schemaCombinationPage = new SchemaCombinationPage());\r
        }\r
        \r
        public void doConversion() throws Exception {\r
                SchemaConverter converter = new SchemaConverter();\r
-//             File inputFile = new File(fileselectionPage.getFilename());\r
-//             File outputPlugin = new File(pluginSelectionPage.getFilename());\r
-               \r
+\r
                File inputFile = new File(inputSelectionPage.getSchemaFilename());\r
                File configurationFile = null;\r
                if (inputSelectionPage.getConfigurationFilename() != null)\r
@@ -58,6 +48,8 @@ public class SchemaConversionWizard extends Wizard implements IImportWizard {
                File outputPlugin = new File(inputSelectionPage.getPluginFilename());\r
                \r
                converter.convertSchema(inputFile,configurationFile,outputPlugin);\r
+               \r
+               schemaCombinationPage.setOutputPlugin(outputPlugin);\r
        }\r
 \r
 }\r
diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyCombinator.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyCombinator.java
new file mode 100644 (file)
index 0000000..0d08416
--- /dev/null
@@ -0,0 +1,181 @@
+package org.simantics.xml.sax;\r
+\r
+import java.io.BufferedReader;\r
+import java.io.BufferedWriter;\r
+import java.io.File;\r
+import java.io.FileReader;\r
+import java.io.FileWriter;\r
+import java.io.IOException;\r
+import java.io.PrintWriter;\r
+import java.util.ArrayList;\r
+import java.util.Collections;\r
+import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.Set;\r
+\r
+public class OntologyCombinator {\r
+       \r
+       \r
+       public static void combine(File[] inputFiles, File outputFile) throws IOException{\r
+               Set<String> resources = new HashSet<>();\r
+               List<String> ontologyUris = new ArrayList<>();\r
+               List<String> classNames = new ArrayList<>();\r
+               String packageLine = null;\r
+               \r
+               \r
+               String packageLineStart = "package";\r
+               String resourceLineStart = "public final Resource ";\r
+               String uriLineStart = "public static final String ";\r
+               \r
+               \r
+               \r
+               for (File inputFile : inputFiles) {\r
+                       BufferedReader reader = new BufferedReader(new FileReader(inputFile));\r
+                       try {\r
+                               String line;\r
+                               String uri = null;\r
+                               String filename = inputFile.getName();\r
+                               String classname = filename.substring(0,filename.length()-5);\r
+                               classNames.add(classname);\r
+                               while ((line = reader.readLine()) != null) {\r
+                                       line = line.trim();\r
+                                       if (line.startsWith(resourceLineStart)) {\r
+                                               String res = line.substring(resourceLineStart.length(), line.length()-1);\r
+                                               resources.add(res);\r
+                                       } else if (line.startsWith(uriLineStart)) {\r
+                                               String euri = line.substring(line.indexOf('"')+1, line.length()-2);\r
+                                               if (uri == null) {\r
+                                                       uri = euri;\r
+                                               } else {\r
+                                                       int max = Math.min(euri.length(), uri.length());\r
+                                                       for (int i = 0; i < max; i++) {\r
+                                                               if (euri.charAt(i) != uri.charAt(i)) {\r
+                                                                       uri = uri.substring(0,i);\r
+                                                                       break;\r
+                                                               }\r
+                                                       }\r
+                                               }\r
+                                       } else if (line.startsWith(packageLineStart)) {\r
+                                               packageLine = line;\r
+                                       }\r
+                               }\r
+                               if (uri != null && uri.length() > 6) {\r
+                                       if (uri.endsWith("/")) {\r
+                                               uri = uri.substring(0, uri.length()-1);\r
+                                       }\r
+                                       ontologyUris.add(uri);\r
+                               }\r
+                               else\r
+                                       throw new IOException("Could not resolve ontology uri for file " + inputFile.getName());\r
+                       } finally {\r
+                               reader.close();\r
+                       }\r
+               }\r
+               List<String> sortedResourcses = new ArrayList<>();\r
+               sortedResourcses.addAll(resources);\r
+               Collections.sort(sortedResourcses);\r
+               \r
+               PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)));\r
+               String className = outputFile.getName();\r
+               int dp = className.indexOf(".");\r
+               if (dp > 0)\r
+                       className = className.substring(0,dp);\r
+               \r
+               try {\r
+                       writer.println(packageLine);\r
+                       writer.println();\r
+                       writer.println("import java.lang.reflect.Field;");\r
+                       writer.println("import java.util.Collection;");\r
+                       writer.println("import java.util.HashMap;");\r
+                       writer.println("import java.util.Map;");\r
+                       writer.println("import org.simantics.Simantics;");\r
+                       writer.println("import org.simantics.db.ReadGraph;");\r
+                       writer.println("import org.simantics.db.Resource;");\r
+                       writer.println("import org.simantics.db.exception.DatabaseException;");\r
+                       writer.println("import org.simantics.db.request.Read;");\r
+                       writer.println("import org.simantics.layer0.Layer0;");\r
+                       writer.println();\r
+                       writer.println();\r
+                       writer.println("public class " + className + " {");\r
+                       writer.println("  private static boolean init = false;");\r
+                       writer.println("  private static Map<Resource, Object> resourceObjects = new HashMap<>();");\r
+                       writer.println("  private static Map<Resource, "+className+"> loaderObjects = new HashMap<>();");\r
+                       writer.println();\r
+                       writer.println("  private Resource ontologyResource;");\r
+                       writer.println("  private Object resourceObject;");\r
+                       writer.println();\r
+                       writer.println("  static void init(ReadGraph graph) throws DatabaseException{");\r
+                       writer.println("     if (init)");\r
+                       writer.println("       return;");\r
+                       writer.println("     ");\r
+                       for (int i = 0; i < inputFiles.length; i++) {\r
+                       writer.println("     add(graph.getResource(\"" +ontologyUris.get(i) +"\"), " + classNames.get(i) +".getInstance(graph));");\r
+                       }\r
+                       writer.println("     ");\r
+                       writer.println("     init = true;");\r
+                       writer.println("  }");\r
+                       writer.println("  ");\r
+                       writer.println("  public static void add(Resource resource, Object object) {");\r
+                       writer.println("     if (resource == null || object == null)");\r
+                       writer.println("        throw new IllegalArgumentException();");\r
+                       writer.println("     resourceObjects.put(resource, object);");\r
+                       writer.println("     loaderObjects.put(resource, new "+className+"(resource, object));");\r
+                       writer.println("  }");\r
+                       writer.println("  ");\r
+                       writer.println("  public static "+className+" getOntology(final Resource res) throws DatabaseException {");\r
+                       writer.println("    return Simantics.getSession().syncRequest(new Read<"+className+">() {");\r
+                       writer.println("        @Override");\r
+                       writer.println("        public "+className+" perform(ReadGraph graph) throws DatabaseException {");\r
+                       writer.println("          return getOntology(graph, res);");\r
+                       writer.println("        }");\r
+                       writer.println("    });");\r
+                       writer.println("  }");\r
+                       writer.println("  ");\r
+                       writer.println("  public static "+className+" getOntology(ReadGraph graph, Resource res) throws DatabaseException{");\r
+                       writer.println("     init(graph);");\r
+                       writer.println("     Layer0 L0 = Layer0.getInstance(graph);");\r
+                       writer.println("     Collection<Resource> types = graph.getTypes(res);");\r
+                       writer.println("     for (Resource type : types) {");\r
+                       writer.println("        Resource part = graph.getPossibleObject(type, L0.PartOf);");\r
+                       writer.println("        while (part != null) {");\r
+                       writer.println("          if (loaderObjects.containsKey(part))");\r
+                       writer.println("             return loaderObjects.get(part);");\r
+                       writer.println("          part = graph.getPossibleObject(part, L0.PartOf);");\r
+                       writer.println("        }");\r
+                       writer.println("     }");\r
+                       writer.println("     return null;");\r
+                       writer.println("  }");\r
+                       writer.println("  ");\r
+                       writer.println("  private "+className+"(Resource ontologyRes, Object resourceObject) {");\r
+                       writer.println("     this.ontologyResource = ontologyRes;");\r
+                       writer.println("     this.resourceObject = resourceObject;");\r
+                       writer.println("     init();");\r
+                       writer.println("  }");\r
+                       writer.println("  ");\r
+                       writer.println("  private Resource get(String name)  {");\r
+                       writer.println("    try {");\r
+                       writer.println("      Field field = resourceObject.getClass().getDeclaredField(name);");\r
+                       writer.println("      return (Resource)field.get(resourceObject);");\r
+                       writer.println("    } catch (Exception e){");\r
+                       writer.println("      return null;");\r
+                       writer.println("    }");\r
+                       writer.println("  }");\r
+                       writer.println("  ");\r
+                       for (String s : sortedResourcses) {\r
+                   writer.println("  public Resource "+s+";");\r
+                       }\r
+                       writer.println("  ");\r
+                       writer.println("  private void init() {");\r
+                       for (String s : sortedResourcses) {\r
+                       writer.println("        "+s+" = get(\""+s+"\");");\r
+                       }\r
+                       writer.println("  }");\r
+                       writer.println("}");\r
+                       \r
+                       \r
+               }       finally {\r
+                       writer.close();\r
+               }\r
+               \r
+       }\r
+}\r