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