]> gerrit.simantics Code Review - simantics/interop.git/commitdiff
XML data based schema and ontology generation 26/2026/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Fri, 24 Aug 2018 09:45:10 +0000 (12:45 +0300)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Fri, 24 Aug 2018 09:45:10 +0000 (12:45 +0300)
gitlab #3

Change-Id: Ibc1821ede1bafb1aa9b68b32aff2f57faffa5a1f

14 files changed:
org.simantics.xml.sax.ui/plugin.xml
org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/datawizard/DataConversionWizard.java [new file with mode: 0644]
org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/datawizard/ImportProcessPage.java [new file with mode: 0644]
org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/datawizard/InputSelectionPage.java [new file with mode: 0644]
org.simantics.xml.sax/META-INF/MANIFEST.MF
org.simantics.xml.sax/src/org/simantics/xml/data/DataSchemaConverter.java [new file with mode: 0644]
org.simantics.xml.sax/src/org/simantics/xml/data/XmlDataConverter.java [new file with mode: 0644]
org.simantics.xml.sax/src/org/simantics/xml/sax/ExporterGenerator.java
org.simantics.xml.sax/src/org/simantics/xml/sax/ImporterGenerator.java
org.simantics.xml.sax/src/org/simantics/xml/sax/JavaGenerator.java
org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java
org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionBase.java
org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConverter.java
org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaObject.java

index f88644b13538391bd5729e998d3e8deed2220783..dcd3c4c8b5ec9602c6bd600aae03b5a5c183a4a2 100644 (file)
@@ -1,16 +1,26 @@
-<?xml version="1.0" encoding="UTF-8"?>\r
-<?eclipse version="3.4"?>\r
-<plugin>\r
-   <extension\r
-         point="org.eclipse.ui.importWizards">\r
-      <wizard\r
-            class="org.simantics.xml.sax.ui.wizard.SchemaConversionWizard"\r
-            id="org.simantics.xml.sax.ui.schemaconversionwizard"\r
-            name="XML Schema To Ontology">\r
-         <description>\r
-            Converts XML Schemas to Simantics ontologies and generates SAX Parser based importer for Schema compliant XML files\r
-         </description>\r
-      </wizard>\r
-   </extension>\r
-\r
-</plugin>\r
+<?xml version="1.0" encoding="UTF-8"?>
+<?eclipse version="3.4"?>
+<plugin>
+   <extension
+         point="org.eclipse.ui.importWizards">
+      <wizard
+            class="org.simantics.xml.sax.ui.wizard.SchemaConversionWizard"
+            id="org.simantics.xml.sax.ui.schemaconversionwizard"
+            name="XML Schema To Ontology">
+         <description>
+            Converts XML Schemas to Simantics ontologies and generates SAX Parser based importer for Schema compliant XML files
+         </description>
+      </wizard>
+      <wizard
+            class="org.simantics.xml.sax.ui.datawizard.DataConversionWizard"
+            id="org.simantics.xml.sax.ui.dataconversionwizard"
+            name="XML Data To Ontology">
+         <description>
+            Converts XML data files  to Simantics ontologies and generates SAX Parser based importer for similar XML files.
+
+This alternative approach to XML schema conversion, which is the preferred way to create conversions.
+         </description>
+      </wizard>
+   </extension>
+
+</plugin>
diff --git a/org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/datawizard/DataConversionWizard.java b/org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/datawizard/DataConversionWizard.java
new file mode 100644 (file)
index 0000000..ada9602
--- /dev/null
@@ -0,0 +1,56 @@
+package org.simantics.xml.sax.ui.datawizard;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.wizard.Wizard;
+import org.eclipse.ui.IImportWizard;
+import org.eclipse.ui.IWorkbench;
+import org.simantics.xml.data.XmlDataConverter;
+
+public class DataConversionWizard extends Wizard implements IImportWizard {
+
+       InputSelectionPage inputSelectionPage;
+       ImportProcessPage importProcessPage;
+       
+       public DataConversionWizard() {
+               setWindowTitle("XML Data conversion");
+       }
+
+       @Override
+       public void init(IWorkbench workbench, IStructuredSelection selection) {
+
+       }
+
+       @Override
+       public boolean performFinish() {
+               if (importProcessPage.isImporting())
+                       return false;
+               return true;
+       }
+       
+       
+       @Override
+       public void addPages() {
+               addPage(inputSelectionPage = new InputSelectionPage());
+               addPage(importProcessPage = new ImportProcessPage());
+       }
+       
+       public void doConversion() throws Exception {
+               
+               List<File> inputFiles = new ArrayList<>();
+               for (String name : inputSelectionPage.getDataFilenames())
+                       inputFiles.add(new File(name));
+               File configurationFile = null;
+               if (inputSelectionPage.getConfigurationFilename() != null)
+                       configurationFile = new File(inputSelectionPage.getConfigurationFilename());
+               File outputPlugin = new File(inputSelectionPage.getPluginFilename());
+
+               XmlDataConverter converter = new XmlDataConverter(inputFiles,configurationFile,outputPlugin);
+               
+               converter.convert();
+       }
+
+}
diff --git a/org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/datawizard/ImportProcessPage.java b/org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/datawizard/ImportProcessPage.java
new file mode 100644 (file)
index 0000000..c42f682
--- /dev/null
@@ -0,0 +1,111 @@
+package org.simantics.xml.sax.ui.datawizard;
+
+import java.lang.reflect.InvocationTargetException;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+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.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Label;
+import org.simantics.utils.ui.ErrorLogger;
+
+
+public class ImportProcessPage extends WizardPage {
+       private Composite composite;
+       
+       private Button button;
+       private Label label;
+       
+       private boolean doneImport = false;
+       private boolean importing = false;
+       
+       public ImportProcessPage() {
+               super("XML Data conversion","Conversion Summary", null);
+               setPageComplete(false);
+               
+       }
+       
+       public boolean isDoneImport() {
+               return doneImport;
+       }
+       
+       @Override
+       public void createControl(Composite parent) {
+               composite = new Composite(parent, SWT.NONE);
+               composite.setLayout(new GridLayout(1,true));
+               
+               Label fileLabel = new Label(composite, SWT.NONE);
+               fileLabel.setText("File: ");
+                               
+               label = new Label(composite, SWT.NONE);
+               label.setText("Import has not been started.");
+               GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BOTTOM).grab(true, false).applyTo(label);
+               button = new Button(composite, SWT.PUSH);
+               button.setText("Import");
+               GridDataFactory.fillDefaults().align(SWT.RIGHT, SWT.CENTER).applyTo(button);
+               button.addSelectionListener(new SelectionAdapter() {
+                       @Override
+                       public void widgetSelected(SelectionEvent e) {
+                               doImport();
+                               
+                       }
+               });
+               setControl(composite);  
+       }
+       
+       private Exception exception;
+
+       private void doImport() {
+               button.setEnabled(false);
+               label.setText("Import started.");
+               importing = true;
+               try {
+                       getWizard().getContainer().run(true, true, new IRunnableWithProgress() {
+                               
+                               @Override
+                               public void run(final IProgressMonitor monitor) throws InvocationTargetException,
+                                               InterruptedException {
+                                       try {
+                                               
+                                               monitor.beginTask("XML Data conversion", IProgressMonitor.UNKNOWN);
+                                               final DataConversionWizard wizard = (DataConversionWizard)getWizard();
+                                               wizard.doConversion();
+
+                                               
+                                       } catch (final Exception err) {
+                                               exception = err;
+                                       }
+                                       monitor.done();
+                               }
+                       });
+               } catch (InvocationTargetException err) {
+                       exception = err;
+               } catch (InterruptedException err) {
+                       exception = err;
+               }
+               if (exception != null) {
+                       setErrorMessage("Conversion failed: " + exception.getMessage());
+                       ErrorLogger.defaultLogError("XML data conversion failed.",exception);
+                       label.setText("Schema conversion failed.");
+               } else {
+                       label.setText("Schema conversion done.");
+               }
+               doneImport = true;
+               importing = false;
+               setPreviousPage(null);
+               setPageComplete(true);
+               getContainer().updateButtons();
+               
+       }
+       
+       public boolean isImporting() {
+               return importing;
+       }
+
+}
diff --git a/org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/datawizard/InputSelectionPage.java b/org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/datawizard/InputSelectionPage.java
new file mode 100644 (file)
index 0000000..0af558c
--- /dev/null
@@ -0,0 +1,73 @@
+package org.simantics.xml.sax.ui.datawizard;
+
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.jface.wizard.WizardPage;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.simantics.utils.ui.widgets.DirectorySelectionWidget;
+import org.simantics.utils.ui.widgets.FileOrDirectorySelectionWidget;
+import org.simantics.utils.ui.widgets.FileSelectionListener;
+import org.simantics.utils.ui.widgets.FileSelectionWidget;
+
+public class InputSelectionPage extends WizardPage implements FileSelectionListener{
+       
+       public InputSelectionPage() {
+               super("InputPage","Select input files and output plug-in",null);
+       }
+
+       private FileSelectionWidget schemaSelection;
+       private FileSelectionWidget configurationSelection;
+       private DirectorySelectionWidget pluginSelection;
+       
+       
+       @Override
+       public void createControl(Composite parent) {
+               Composite composite = new Composite(parent, SWT.NONE);
+               composite.setLayout(new GridLayout(1,false));
+               GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).applyTo(composite);
+               
+               schemaSelection = new FileSelectionWidget(composite, "XML data files", SWT.OPEN|SWT.MULTI);
+               configurationSelection = new FileSelectionWidget(composite, "Configuration file (Optional)", SWT.OPEN);
+               pluginSelection = new DirectorySelectionWidget(composite, "Output plug-in", SWT.SAVE);
+               
+               schemaSelection.setFilterExtensions(new String[]{"*.*"});
+               schemaSelection.setFilterNames(new String[]{"XML data files"});
+               
+               configurationSelection.setFilterExtensions(new String[]{"*.xml"});
+               configurationSelection.setFilterNames(new String[]{"XML files"});
+               
+               schemaSelection.addListener(this);
+               pluginSelection.addListener(this);
+               
+               
+               
+               setControl(composite);
+               setPageComplete(false);
+       }
+       
+       @Override
+       public void fileSelected(FileOrDirectorySelectionWidget source, String[] filename) {
+               setPageComplete(schemaSelection.getFilename() != null && pluginSelection.getFilename() != null);
+               
+       }
+
+       public String[] getDataFilenames() {
+               if (schemaSelection.getFilename() == null ||schemaSelection.getFilename().length == 0)
+                       return null;
+               return schemaSelection.getFilename();
+       }
+       
+       public String getConfigurationFilename() {
+               if (configurationSelection.getFilename() == null || configurationSelection.getFilename().length == 0)
+                       return null;
+               return configurationSelection.getFilename()[0];
+       }
+       
+       public String getPluginFilename() {
+               if (pluginSelection.getFilename() == null ||pluginSelection.getFilename().length == 0)
+                       return null;
+               return pluginSelection.getFilename()[0];
+       }
+
+}
index d8e63571ce3f398863931a215c9841cffce9aa4f..62fbab98952b18d846d85985b8179950c26788e3 100644 (file)
@@ -5,5 +5,6 @@ Bundle-SymbolicName: org.simantics.xml.sax
 Bundle-Version: 1.0.0.qualifier
 Bundle-Vendor: VTT
 Bundle-RequiredExecutionEnvironment: JavaSE-1.7
-Export-Package: org.simantics.xml.sax
+Export-Package: org.simantics.xml.data,
+ org.simantics.xml.sax
 Require-Bundle: org.simantics.utils.datastructures;bundle-version="1.1.0"
diff --git a/org.simantics.xml.sax/src/org/simantics/xml/data/DataSchemaConverter.java b/org.simantics.xml.sax/src/org/simantics/xml/data/DataSchemaConverter.java
new file mode 100644 (file)
index 0000000..e94f93e
--- /dev/null
@@ -0,0 +1,54 @@
+package org.simantics.xml.data;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.Map;
+
+import javax.xml.bind.JAXBException;
+
+import org.simantics.xml.sax.SchemaConverter;
+import org.w3._2001.xmlschema.Schema;
+
+public class DataSchemaConverter extends SchemaConverter {
+
+       
+       private Map<String,Schema> schemaMap;
+       private Map<Schema,File> fileMap;
+       
+       public DataSchemaConverter(Schema schema, File schemaFile, File conversionFile, File outputPlugin) throws IOException {
+               super(schemaFile, conversionFile, outputPlugin);
+               this.schema = schema;
+       }
+       
+       @Override
+       protected Schema createSchema() throws JAXBException, FileNotFoundException {
+               return this.schema;
+       }
+       
+       public void setSchemaMap(Map<String, Schema> schemaMap) {
+               this.schemaMap = schemaMap;
+       }
+       
+       public void setFileMap(Map<Schema, File> fileMap) {
+               this.fileMap = fileMap;
+       }
+       
+       
+       @Override
+       protected SchemaConverter constructSubConverter(SchemaConverter parent, File schemaFile, File conversionFile,
+                       File outputPlugin, String ns) throws IOException {
+               
+               Schema schema = ((DataSchemaConverter)getRoot()).schemaMap.get(ns);
+               File file = ((DataSchemaConverter)getRoot()).fileMap.get(schema);
+               return new DataSchemaConverter(schema, this, file, conversionFile, outputPlugin);
+       }
+
+       public DataSchemaConverter(Schema schema, SchemaConverter parent, File schemaFile, File conversionFile, File outputPlugin)
+                       throws IOException {
+               super(parent, schemaFile, conversionFile, outputPlugin);
+               this.schema = schema;
+       }
+
+       
+}
diff --git a/org.simantics.xml.sax/src/org/simantics/xml/data/XmlDataConverter.java b/org.simantics.xml.sax/src/org/simantics/xml/data/XmlDataConverter.java
new file mode 100644 (file)
index 0000000..b4e7c21
--- /dev/null
@@ -0,0 +1,442 @@
+package org.simantics.xml.data;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.Date;
+import java.util.Deque;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+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;
+import javax.xml.bind.JAXBElement;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.Attribute;
+import javax.xml.stream.events.Characters;
+import javax.xml.stream.events.EndElement;
+import javax.xml.stream.events.StartElement;
+import javax.xml.stream.events.XMLEvent;
+
+import org.simantics.xml.sax.SchemaConversionBase;
+import org.w3._2001.xmlschema.Annotated;
+import org.w3._2001.xmlschema.ComplexType;
+import org.w3._2001.xmlschema.Element;
+import org.w3._2001.xmlschema.ExplicitGroup;
+import org.w3._2001.xmlschema.Import;
+import org.w3._2001.xmlschema.LocalComplexType;
+import org.w3._2001.xmlschema.LocalElement;
+import org.w3._2001.xmlschema.OpenAttrs;
+import org.w3._2001.xmlschema.Schema;
+import org.w3._2001.xmlschema.TopLevelElement;
+
+/**
+ * This class generates XML-file parsers based on bunch of XML data files. It is recommended to use schema based parser (org.simantics.xml.sax.SchemaConverter) if possible.
+ * Parser generated by this class is not reliable...
+ * 
+ * @author luukkainen
+ *
+ */
+public class XmlDataConverter {
+       
+       File outputPlugin;
+       File conversionFile;
+       List<File> inputFiles;
+       
+       String pluginName;
+       
+       private String[] header;
+       
+       public XmlDataConverter(List<File> inputFiles, File conversionFile, File outputPlugin) {
+               if (inputFiles.size() == 0)
+                       throw new IllegalArgumentException("At least one input file must be given.");
+               this.outputPlugin = outputPlugin;
+               this.conversionFile = conversionFile;
+               this.inputFiles = inputFiles;
+               
+               pluginName = outputPlugin.getName();
+               
+       }
+       
+       public void convert() throws IOException, XMLStreamException, JAXBException {
+               
+               init();
+               doConvert();
+               
+               Map<Schema, File> fileMap = new HashMap<>();
+               JAXBContext jc = JAXBContext.newInstance("org.w3._2001.xmlschema");
+               Marshaller m = jc.createMarshaller();
+               m.setProperty("jaxb.formatted.output", true);
+               Set<String> filenames = new HashSet<>(); 
+               for (Schema s : schemaMap.values()) {
+                       String name = s.getTargetNamespace();
+                       // Special case for XAML
+                       if (name.startsWith("clr-namespace:")) {
+                               name = name.substring("clr-namespace:".length());
+                               int i = name.indexOf(";assembly");
+                               if (i > 0)
+                                       name = name.substring(0, i);
+                       }
+                       name = name.replaceAll("\\.", "_");
+                       name = name.replaceAll("/", "_");
+                       name = name.replaceAll(":", "_");
+                       name = name.replaceAll(";", "_");
+                       if (filenames.contains(name)) {
+                               int i = 2;
+                               while (filenames.contains(name+i)) {
+                                       i++;
+                               }
+                               name = name+i;
+                       }
+                       filenames.add(name);
+                       File file = new File(outputPlugin.getAbsolutePath() + File.separator + name +".xsd");
+                       fileMap.put(s, file);
+               }
+               for (Schema s : schemaMap.values()) {
+                       for (OpenAttrs openAttrs : s.getIncludeOrImportOrRedefine()) {
+                               if (openAttrs instanceof Import) {
+                                       Import import1 = (Import)openAttrs;
+                                       Schema dep = schemaMap.get(import1.getNamespace());
+                                       import1.setSchemaLocation(fileMap.get(dep).getName());
+                               }
+                       }
+               }
+               for (Schema s : schemaMap.values()) {
+                       File file = fileMap.get(s);
+                       m.marshal(s, file);
+               }
+               Schema rootSchema = schemaMap.values().iterator().next();
+               DataSchemaConverter schemaConverter = new DataSchemaConverter(rootSchema,fileMap.get(rootSchema),conversionFile,outputPlugin);
+               schemaConverter.setFileMap(fileMap);
+               schemaConverter.setSchemaMap(schemaMap);
+               schemaConverter.convert();
+               
+               
+               header = null;
+               schemaMap = null;
+               elementMap = null;
+       }
+       
+       protected void init()  throws IOException {
+               
+               header = new String[4];
+               header[0] = "Generated with org.simantics.xml.sax XML data file converter";
+               header[1] = "";
+               header[2] = "File " + inputFiles.get(0).getAbsolutePath().replaceAll(Matcher.quoteReplacement("\\"), "/") + " , total file count: " + (inputFiles.size()) + "";
+               header[3] = "Date " + new Date().toString();
+               
+               schemaMap = new HashMap<>();
+               elementMap = new HashMap<>();
+               attributeMap = new HashMap<>();
+               elementNsMap = new HashMap<>();
+       }
+       
+       Map<String, Schema> schemaMap = new LinkedHashMap<>();
+       Map<Schema,Map<String,Element>> elementMap = new HashMap<>();
+       Map<Element,String> elementNsMap = new HashMap<>();
+       Map<Schema,Map<String,org.w3._2001.xmlschema.Attribute>> attributeMap = new HashMap<>();
+       
+       protected void doConvert() throws IOException, XMLStreamException, JAXBException {
+               XMLInputFactory input = XMLInputFactory.newInstance();
+               Deque<Element> elementStack = new ArrayDeque<>();
+               
+               for (File inputFile : inputFiles) {
+                       XMLEventReader reader = input.createXMLEventReader(new FileInputStream(inputFile));
+                       while (reader.hasNext()) {
+                               XMLEvent event = reader.nextEvent();
+                               if (event.isStartElement()) {
+                                       StartElement parseElement = event.asStartElement();
+//                                     System.out.println("Start " + parseElement.getName());
+                                       Element schemaElement = null;
+                                       String currentNS = parseElement.getName().getNamespaceURI();
+                                       Schema s = schemaMap.get(currentNS);
+                                       String elementName = parseElement.getName().getLocalPart();
+                                       if ("GroupComponent".equals(elementName))
+                                               System.out.println();
+                                       if (s == null) {
+                                               s = getOrCreateSchema(parseElement);
+                                       } else {
+                                               schemaElement = elementMap.get(s).get(elementName);
+                                       }
+                                       Element parentElement = elementStack.peek();
+                                       
+                                       boolean newElement = false;
+                                       boolean sameNameSpace = true;
+                                       
+                                       if (parentElement != null) {
+                                               //QName parentType = parentElement.getType();
+                                               String parentNs = elementNsMap.get(parentElement);
+                                               sameNameSpace =currentNS.equals(parentNs);
+                                               if (!sameNameSpace) {
+                                                       Schema ps = getOrCreateSchema(parentNs);
+                                                       addSchemaDependency(ps, s);
+                                               }
+                                                       
+                                       }
+                                       if (schemaElement == null) {
+                                               LocalElement localElement = null;
+                                               //QName type = null;
+                                               if (elementStack.isEmpty()) {
+                                                       schemaElement = new TopLevelElement();
+                                                       s.getSimpleTypeOrComplexTypeOrGroup().add(schemaElement);
+                                               } else {
+                                                       
+                                                       
+//                                                     if (sameNameSpace) {
+//                                                             localElement = new LocalElement();
+//                                                             schemaElement = localElement;
+//                                                             //type = new QName(elementName);
+//                                                             
+//                                                     } else {
+                                                               schemaElement = new TopLevelElement();
+                                                               s.getSimpleTypeOrComplexTypeOrGroup().add(schemaElement);
+                                                               //type = new QName(SchemaConversionBase.SCHEMA_NS,"element");
+                                                               localElement = new LocalElement();
+                                                               localElement.setRef(new QName(parseElement.getName().getNamespaceURI(), elementName));
+                                                                       
+//                                                     }
+                                               }
+                                               schemaElement.setName(elementName);
+                                               elementNsMap.put(schemaElement, currentNS);
+//                                             if (sameNameSpace) {
+//                                                     schemaElement.setType(new QName(parseElement.getName().getNamespaceURI(),elementName));
+//                                             } else {
+//                                                     schemaElement.setType(new QName(parseElement.getName().getNamespaceURI(), elementName));
+//                                             }
+                                               if (!elementStack.isEmpty()) {
+                                                       ComplexType complexType = parentElement.getComplexType(); 
+                                                       ExplicitGroup choice = complexType.getChoice();
+                                                       if (choice == null) {
+                                                               choice = new ExplicitGroup();
+                                                               complexType.setChoice(choice);
+                                                               choice.setMaxOccurs("unbounded");
+                                                       }
+                                                       addElement(choice, new QName(SchemaConversionBase.SCHEMA_NS,"element"), localElement);
+                                               }
+                                               
+                                               
+                                               elementMap.get(s).put(elementName, schemaElement);
+                                               newElement = true;
+                                       }
+                                       elementStack.push(schemaElement);
+                                       
+                                       Iterator<Attribute> attributeIterator = parseElement.getAttributes();
+                                       
+//                                     while (attributeIterator.hasNext()) {
+//                                             Attribute attribute = attributeIterator.next();
+//                                             System.out.println("Attribute " + attribute.getName() + " " + attribute.getValue());
+//                                     }
+                                       if (newElement) {
+                                               LocalComplexType complexType = new LocalComplexType();
+                                               schemaElement.setComplexType(complexType);
+                                               attributeIterator = parseElement.getAttributes();
+                                               while (attributeIterator.hasNext()) {
+                                                       Attribute attribute = attributeIterator.next();
+                                                       addAttribute(attribute, complexType, currentNS);
+                                               }
+                                               
+                                       } else {
+                                               LocalComplexType complexType = schemaElement.getComplexType();
+                                               attributeIterator = parseElement.getAttributes();
+                                               Map<String,org.w3._2001.xmlschema.Attribute> currentAttributes = new HashMap<>();
+                                               Iterator<Annotated> currentAttributeIterator = complexType.getAttributeOrAttributeGroup().iterator();
+                                               while (currentAttributeIterator.hasNext()) {
+                                                       Annotated annotated = currentAttributeIterator.next();
+                                                       if (annotated instanceof org.w3._2001.xmlschema.Attribute) {
+                                                               org.w3._2001.xmlschema.Attribute schemaAttribute = (org.w3._2001.xmlschema.Attribute)annotated;
+                                                               String n = schemaAttribute.getName();
+                                                               if (n != null)
+                                                                       currentAttributes.put(n, schemaAttribute);
+                                                       }
+                                               }
+                                               while (attributeIterator.hasNext()) {
+                                                       Attribute attribute = attributeIterator.next();
+                                                       org.w3._2001.xmlschema.Attribute schemaAttribute = currentAttributes.get(attribute.getName().getLocalPart());
+                                                       if (schemaAttribute == null) {
+                                                               addAttribute(attribute, complexType, currentNS);
+                                                       } else {
+                                                               QName newType = getType(attribute.getValue());
+                                                               updateAttributeType(schemaAttribute, newType);
+                                                       }
+                                                       
+                                               }
+                                       }
+                                       
+                               } else if (event.isEndElement()) {
+                                       EndElement element = event.asEndElement();
+//                                     System.out.println("End " + element.getName());
+                                       elementStack.pop();
+                               } else if (event.isAttribute()) {
+                                       
+                               } else if (event.isStartDocument()) {
+                               
+                               } else if (event.isEndDocument()) {
+                                       
+                               } else if (event.isEntityReference()) {
+                               
+                               } else if (event.isCharacters()) {
+                                       Characters characters = event.asCharacters();
+//                                     if (!characters.isWhiteSpace())
+//                                             System.out.println(characters.getData());
+                           } else if (event.isNamespace()) {
+                               
+                           }
+                       }
+               }
+               
+       }
+       
+       private void updateAttributeType(org.w3._2001.xmlschema.Attribute schemaAttribute, QName newType) {
+               
+               QName currentType = schemaAttribute.getType();
+               if (!newType.getLocalPart().equals(currentType.getLocalPart())) {
+                               
+                       
+                       if (currentType.getLocalPart().equals("integer") && newType.getLocalPart().equals("double")) {
+                               // change integer to double
+                               schemaAttribute.setType(newType);
+                       } else if (currentType.getLocalPart().equals("double") && newType.getLocalPart().equals("integer")) {
+                               // nothing to do, integer can be parsed as double
+                       } else if (!currentType.getLocalPart().equals("string")){
+                               schemaAttribute.setType(new QName(SchemaConversionBase.SCHEMA_NS, "string"));
+                       }
+               }
+       }
+       
+       private void addElement(ExplicitGroup choice, QName type, LocalElement localElement) {
+               for (Object o  : choice.getParticle()) {
+                       JAXBElement<LocalElement> el = (JAXBElement<LocalElement>)o;
+                       if (el.getName().equals(type)) {
+                               QName ref = el.getValue().getRef();
+                               QName ref2 = localElement.getRef();
+                               if (ref != null) {
+                                       if (ref.equals(ref2))
+                                               return;
+                               } else if (el.getValue().getType().equals(localElement.getType()))
+                                               return; 
+                       }
+                               
+               }
+               choice.getParticle().add(new JAXBElement<LocalElement>(type, LocalElement.class, null, localElement));
+       }
+       
+       private void addSchemaDependency(Schema parentSchema, Schema schema) {
+               for (OpenAttrs openAttrs : parentSchema.getIncludeOrImportOrRedefine()) {
+                       if (openAttrs instanceof Import) {
+                               Import import1 = (Import)openAttrs;
+                               if (import1.getNamespace().equals(schema.getTargetNamespace()))
+                                       return;
+                       }
+               }
+               Import import1 = new Import();
+               import1.setNamespace(schema.getTargetNamespace());
+               parentSchema.getIncludeOrImportOrRedefine().add(import1);
+       }
+       
+       private void addAttribute(Attribute attribute, ComplexType complexType, String currentNS) {
+               if (attribute.getName().getLocalPart().equals("GridOptions.GridVisibility"))
+                       System.out.println();
+               if (attribute.getName().getNamespaceURI().length() == 0 || attribute.getName().getNamespaceURI().equals(currentNS)) {
+                       org.w3._2001.xmlschema.Attribute schemaAttribute = new org.w3._2001.xmlschema.Attribute();
+                       schemaAttribute.setName(attribute.getName().getLocalPart());
+                       schemaAttribute.setType(getType(attribute.getValue()));
+                       addAttribute(complexType, schemaAttribute);
+               } else {
+                       {
+                               Schema schema = getOrCreateSchema(currentNS);
+                               Schema attrSchema = getOrCreateSchema(attribute.getName().getNamespaceURI());
+                               
+                               org.w3._2001.xmlschema.Attribute schemaAttribute = attributeMap.get(attrSchema).get(attribute.getName().getLocalPart());
+                               if (schemaAttribute == null) {
+                                       schemaAttribute = new org.w3._2001.xmlschema.TopLevelAttribute();
+                                       schemaAttribute.setName(attribute.getName().getLocalPart());
+                                       schemaAttribute.setType(getType(attribute.getValue()));
+                                       attrSchema.getSimpleTypeOrComplexTypeOrGroup().add(schemaAttribute);
+                                       attributeMap.get(attrSchema).put(attribute.getName().getLocalPart(), schemaAttribute);
+                               }
+                               addSchemaDependency(schema, attrSchema);
+                               
+                       }
+                       {
+                               org.w3._2001.xmlschema.Attribute schemaAttribute = new org.w3._2001.xmlschema.Attribute();
+                               schemaAttribute.setRef(new QName(attribute.getName().getNamespaceURI(),attribute.getName().getLocalPart()));
+                               addAttribute(complexType, schemaAttribute);
+                       }
+                       
+               }
+       }
+       
+       private void addAttribute(ComplexType complexType, org.w3._2001.xmlschema.Attribute schemaAttribute) {
+               if (schemaAttribute.getName() != null) {
+                       for (Annotated annotated : complexType.getAttributeOrAttributeGroup()) {
+                               if (annotated instanceof org.w3._2001.xmlschema.Attribute) {
+                                       org.w3._2001.xmlschema.Attribute attr = (org.w3._2001.xmlschema.Attribute)annotated;
+                                       if (schemaAttribute.getName().equals(attr.getName())) {
+                                               updateAttributeType(attr, schemaAttribute.getType());
+                                       }
+                               }
+                       }
+               } else {
+                       for (Annotated annotated : complexType.getAttributeOrAttributeGroup()) {
+                               if (annotated instanceof org.w3._2001.xmlschema.Attribute) {
+                                       org.w3._2001.xmlschema.Attribute attr = (org.w3._2001.xmlschema.Attribute)annotated;
+                                       if (attr.getName() != null)
+                                               continue;
+                                       if (schemaAttribute.getRef().equals(attr.getRef())) {
+                                               return;
+                                       }
+                               }
+                       }
+               }
+               complexType.getAttributeOrAttributeGroup().add(schemaAttribute);
+       }
+       
+       
+       private QName getType(String value) {
+               try {
+                       Integer.parseInt(value);
+                       return new QName(SchemaConversionBase.SCHEMA_NS, "integer");
+               } catch (NumberFormatException e) {
+                       
+               }
+               
+               try {
+                       Double.parseDouble(value);
+                       return new QName(SchemaConversionBase.SCHEMA_NS, "double");
+               } catch (NumberFormatException e) {
+                       
+               }
+               if ("True".equals(value) || "False".equals(value))
+                       return new QName(SchemaConversionBase.SCHEMA_NS, "boolean");
+               return new QName(SchemaConversionBase.SCHEMA_NS, "string");
+               
+       }
+       
+       private Schema getOrCreateSchema(StartElement parseElement) {
+               return getOrCreateSchema(parseElement.getName().getNamespaceURI());
+       }
+       
+       private Schema getOrCreateSchema(String ns) {
+               Schema s = schemaMap.get(ns);
+               if (s == null) {
+                       s = new Schema();
+                       s.setTargetNamespace(ns);
+                       schemaMap.put(ns, s);
+                       elementMap.put(s, new HashMap<String,Element>());
+                       attributeMap.put(s, new HashMap<String, org.w3._2001.xmlschema.Attribute>());
+               }
+               return s;
+       }
+
+}
index f538bcb42bae58e026e17978d0a5dd4e89945eff..5294e913fce5d13832c484467de8fbb3825489b1 100644 (file)
@@ -40,10 +40,9 @@ public class ExporterGenerator extends JavaGenerator{
 
        public void createParser() throws IOException {
                
-               String parserPackagePostfix = "_exp";
+
                String importerClassPostfix = "Exporter";
                String parserClassPostfix = "Writer";
-               elementPackageName = name+parserPackagePostfix;
                
                importParserDir= new File(converter.getParserDir().getAbsolutePath()+"/"+elementPackageName);
                if (!importParserDir.exists())
@@ -108,6 +107,11 @@ public class ExporterGenerator extends JavaGenerator{
                mainWriter.close();
        }
        
+       @Override
+       protected String getPackagePostFix() {
+               return "_exp";
+       }
+       
 //     @Override
 //     protected void handle(TopLevelAttribute topLevelAttribute) {
 //
@@ -428,13 +432,13 @@ public class ExporterGenerator extends JavaGenerator{
                String attrName;
                if (name != null) {
                        attrName = name;
-                       relationName = ontShort+"has"+name;
+                       relationName = ontShort+"has"+base.getName(attribute);
                        if (parent != null)
-                               relationName = ontShort+getName(parent)+"_has"+name;
+                               relationName = ontShort+getName(parent)+"_has"+base.getName(attribute);
                }
                else if (ref != null && parent != null) {
                        attrName = ref.getLocalPart();
-                       relationName = ontShort+getName(parent)+"_has"+ref.getLocalPart();
+                       relationName = ontShort+getName(parent)+"_has"+base.getName(ref);
                        
                        Attribute referred = base.getRefAttribute(ref);
                        if (referred != null) {
@@ -530,7 +534,7 @@ public class ExporterGenerator extends JavaGenerator{
                        FileWriter fw = getWriter(parent);
                        NamedAttributeGroup group = this.base.getAttributeGroup(attribute.getRef());
                        fw.writer.println(commentTag+"    AttributeGroup " + group.getName());
-                       SchemaObject obj = new SchemaObject(parent,attribute);
+                       SchemaObject obj = new SchemaObject(base,parent,attribute);
                        for (Annotated annotated : group.getAttributeOrAttributeGroup()) {
                                if (annotated instanceof Attribute) {
                                        //handle("AttributeGroups_"+group.getName(),(Attribute)annotated);
index 3e02434e797481d721e9e1f5ac0a0290f5a8e322..58f5748e05e1d4bef1f9af01611c02bcc6dc3128 100644 (file)
@@ -39,10 +39,9 @@ public class ImporterGenerator extends JavaGenerator{
        
        public void createParser() throws IOException {
                
-               String parserPackagePostfix = "_elem";
                String importerClassPostfix = "Importer";
                String parserClassPostfix = "Parser";
-               elementPackageName = name+parserPackagePostfix;
+               
                
                importParserDir= new File(converter.getParserDir().getAbsolutePath()+"/"+elementPackageName);
                if (!importParserDir.exists())
@@ -93,6 +92,11 @@ public class ImporterGenerator extends JavaGenerator{
                mainWriter.close();
        }
        
+       @Override
+       protected String getPackagePostFix() {
+               return "_elem";
+       }
+       
        @Override
        public void handleSimpleType(SchemaObject parent, SchemaObject simpleTypeObj) {
        }
@@ -231,6 +235,8 @@ public class ImporterGenerator extends JavaGenerator{
        
        @Override
        public void createReferenceIndicator(SchemaObject parent, RefType referenceType, String refName, String objectName, String primaryClassName, String secondaryClassName, boolean useElementList, boolean useOriginalList) {
+               if (refName == null)
+                       System.out.println();
                FileWriter fw = getWriter(parent);
                if (referenceType == RefType.Type) {
                // create internal class for handling the element and child attachment
@@ -311,6 +317,8 @@ public class ImporterGenerator extends JavaGenerator{
        
        @Override
        protected void createElementIndicator(SchemaObject parent, boolean useElementList, String refName, String className, boolean useOriginalList) {
+               if (refName == null)
+                       System.out.println();
                FileWriter fw = getWriter(parent);
                //if (!reference)
                fw.writer.println("        addParser(\""+ refName +"\", "+className+".class);");
@@ -370,22 +378,18 @@ public class ImporterGenerator extends JavaGenerator{
        public void handle(SchemaObject parent, Attribute attribute) {
                if (parent == null)
                        return;
-               String name = attribute.getName();
+               String attrName = attribute.getName();
                QName primitiveType = attribute.getType();
                SimpleType simpleType = attribute.getSimpleType();
                QName ref = attribute.getRef();
                
                String relationName;
-               String attrName;
-               if (name != null) {
-                       attrName = name;
-                       relationName = ontShort+"has"+name;
-                       if (parent != null)
-                               relationName = ontShort+getName(parent)+"_has"+name;
+               if (attrName != null) {
+                       relationName = ontShort+getName(parent)+"_has"+base.getName(attribute);
                }
                else if (ref != null && parent != null) {
                        attrName = ref.getLocalPart();
-                       relationName = ontShort+getName(parent)+"_has"+ref.getLocalPart();
+                       relationName = ontShort+getName(parent)+"_has"+base.getName(ref);
                        
                        Attribute referred = this.base.getRefAttribute(ref);
                        if (referred != null) {
@@ -485,7 +489,7 @@ public class ImporterGenerator extends JavaGenerator{
                        FileWriter fw = getWriter(parent);
                        NamedAttributeGroup group = this.base.getAttributeGroup(attribute.getRef());
                        fw.writer.println(commentTag+"    AttributeGroup " + group.getName());
-                       SchemaObject obj = new SchemaObject(parent,attribute);
+                       SchemaObject obj = new SchemaObject(base,parent,attribute);
                        for (Annotated annotated : group.getAttributeOrAttributeGroup()) {
                                if (annotated instanceof Attribute) {
                                        //handle("AttributeGroups_"+group.getName(),(Attribute)annotated);
index 4869e0711e073297f01c390aac96849dcf9d6be3..af97d82162959571c84e6edb7a5e0409095bf0c9 100644 (file)
-package org.simantics.xml.sax;\r
-\r
-import java.io.File;\r
-import java.io.IOException;\r
-import java.io.PrintWriter;\r
-import java.util.ArrayList;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-\r
-import javax.xml.namespace.QName;\r
-\r
-import org.simantics.xml.sax.SchemaConversionBase.Inheritance;\r
-import org.simantics.xml.sax.SchemaConversionBase.RefType;\r
-import org.simantics.xml.sax.SchemaConversionBase.TypeEntry;\r
-import org.w3._2001.xmlschema.AttributeGroupRef;\r
-import org.w3._2001.xmlschema.Element;\r
-import org.w3._2001.xmlschema.Schema;\r
-\r
-//public abstract class JavaGenerator extends SchemaConversionBase{\r
-public abstract class JavaGenerator implements SchemaConversionComponent {\r
-\r
-       String commentTag = "//";\r
-       \r
-       Schema schema;\r
-       String ontologyClassName;\r
-       String ontologyUri;\r
-       SchemaConverter converter;\r
-       SchemaConversionBase base;\r
-       \r
-       List<String> ruleClassNames = new ArrayList<String>();\r
-       \r
-       String ontShort = "ONT"; \r
-       String name;\r
-       \r
-       File importParserDir;\r
-       String elementPackageName;\r
-       \r
-       Map<SchemaObject, FileWriter> writers = new HashMap<SchemaObject, ImporterGenerator.FileWriter>();\r
-       \r
-       public JavaGenerator(SchemaConverter converter, SchemaConversionBase base) {\r
-               this.converter = converter;\r
-               this.base = base;\r
-               \r
-               this.schema = base.schema;\r
-               this.ontologyClassName = base.className;\r
-               this.ontologyUri = base.ontologyURI;\r
-               this.converter = converter;\r
-               this.name = converter.name;\r
-               ontShort = converter.shortName;\r
-               ontShort +=".";\r
-       }\r
-       \r
-       \r
-       protected PrintWriter createFile(File file) throws IOException {\r
-               if (!file.exists())\r
-                       file.createNewFile();\r
-               PrintWriter writer = new PrintWriter(file);\r
-               for (String s : converter.getHeader()) {\r
-                       writer.println(commentTag + " " + s);   \r
-               }\r
-               writer.println();\r
-               return writer;\r
-       }\r
-       \r
-       protected String getValueGetterMethod(TypeEntry binding,String name) {\r
-               if (binding == null)\r
-                       return name+".getValue()";\r
-               return binding.getValueGetterMethod(name);\r
-       }\r
-       \r
-       protected String getValueGetter(TypeEntry binding,String name) {\r
-               if (binding == null)\r
-                       return name;\r
-               return binding.getValueGetter(name);\r
-       }\r
-       \r
-       protected String getValueGetter(TypeEntry binding) {\r
-               if (binding == null)\r
-                       return "value";\r
-               return binding.getValueGetter();\r
-       }\r
-       \r
-       @Override\r
-       public String getSimpleTypePrefix() {\r
-               return "SimpleTypes_";\r
-       }       \r
-       \r
-       @Override\r
-       public String getComplexTypePrefix() {\r
-               return "ComplexTypes_";\r
-       }       \r
-       \r
-       @Override\r
-       public String getAttributeGroupPrefix() {\r
-               return "AttributeGroups_";\r
-       }\r
-       \r
-       @Override\r
-       public String handleChoice(SchemaObject parent, SchemaElement indicator, List<SchemaElement> elements, String name) {\r
-               String baseRelationName = getName(parent) + ".has" + name;\r
-               \r
-//             for (SchemaElement e : elements) {\r
-//                     Element localElement = e.getElement();\r
-//                     if (localElement.getName() != null) {\r
-//                             QName refType = localElement.getType();\r
-//                             if (refType != null)\r
-//                                     //handleIndicator(parent, indicator, e, false, name, refType);\r
-//                                     handleIndicator(parent, indicator, e, name, RefType.Type, baseRelationName);\r
-//                     } else if (localElement.getRef() != null) {\r
-//                             //QName refType = localElement.getRef();\r
-//                             //handleIndicator(parent, indicator, e, true, name, refType);\r
-//                             handleIndicator(parent, indicator, e, name, RefType.Reference, baseRelationName);\r
-//                     }\r
-//             }\r
-               \r
-               return baseRelationName;\r
-       }\r
-       \r
-       protected String getOntologyImport() {\r
-               return this.ontologyClassName+" " +ontShort.substring(0, 3)+" = "+this.ontologyClassName+".getInstance(graph);";\r
-       }\r
-       \r
-       protected static class FileWriter {\r
-               public PrintWriter writer;\r
-               \r
-               public PrintWriter delayedWriter;\r
-               public PrintWriter delayedWriter2;\r
-       }\r
-       \r
-       protected  FileWriter getWriter(SchemaObject obj) {\r
-               SchemaObject s = obj;\r
-               while (s != null) {\r
-                       FileWriter fw = writers.get(s);\r
-                       if (fw != null)\r
-                               return fw;\r
-                       s = s.getParent();\r
-               }\r
-               return null;\r
-       }\r
-       \r
-       @Override\r
-       public String getName(SchemaObject obj) {\r
-               if (obj.getParent() == null) {\r
-                       switch (obj.getType()) {\r
-                       case COMPLEX_TYPE:\r
-                               return getComplexTypePrefix()+obj.getName();\r
-                       case ELEMENT:\r
-                               return obj.getName();\r
-                       case ATTRIBUTE_GROUP:\r
-                               return getAttributeGroupPrefix()+obj.getName();\r
-                       case SIMPLE_TYPE:\r
-                               return getSimpleTypePrefix()+obj.getName();\r
-                       }\r
-               } else {\r
-                       SchemaObject o = obj;\r
-                       SchemaObject prev = null;\r
-                       String name = "";\r
-                       while (o != null){\r
-                               if (o.getName() != null)\r
-                                       name = o.getName()+"_"+name;\r
-                               prev = o;\r
-                               o = o.getParent();\r
-                               if (prev.getObj() instanceof AttributeGroupRef)\r
-                                       o = null;\r
-                       }\r
-                       name = name.substring(0, name.length()-1);\r
-                       switch (prev.getType()) {\r
-                       case COMPLEX_TYPE:\r
-                               return getComplexTypePrefix()+name;\r
-                       case ELEMENT:\r
-                               return name;\r
-                       case ATTRIBUTE_GROUP:\r
-                               return getAttributeGroupPrefix()+name;\r
-                       case SIMPLE_TYPE:\r
-                               return getSimpleTypePrefix()+name;\r
-                       }\r
-               }\r
-               throw new RuntimeException();\r
-               \r
-       }\r
-       \r
-       public String getName(SchemaObject obj, String rel) {\r
-               if (obj.getParent() == null) {\r
-                       switch (obj.getType()) {\r
-                       case COMPLEX_TYPE:\r
-                               return getComplexTypePrefix()+rel+obj.getName();\r
-                       case ELEMENT:\r
-                               return rel+obj.getName();\r
-                       case ATTRIBUTE_GROUP:\r
-                               return getAttributeGroupPrefix()+rel+obj.getName();\r
-                       case SIMPLE_TYPE:\r
-                               return getSimpleTypePrefix()+rel+obj.getName();\r
-                       }\r
-               } else {\r
-                       SchemaObject o = obj;\r
-                       SchemaObject prev = null;\r
-                       String name = "";\r
-                       while (o != null){\r
-                               if (o.getName() != null)\r
-                                       name = o.getName()+"_"+name;\r
-                               prev = o;\r
-                               o = o.getParent();\r
-                       }\r
-                       name = name.substring(0, name.length()-1);\r
-                       switch (prev.getType()) {\r
-                       case COMPLEX_TYPE:\r
-                               return getComplexTypePrefix()+rel+name;\r
-                       case ELEMENT:\r
-                               return rel+name;\r
-                       case ATTRIBUTE_GROUP:\r
-                               return getAttributeGroupPrefix()+rel+name;\r
-                       case SIMPLE_TYPE:\r
-                               return getSimpleTypePrefix()+rel+name;\r
-                       }\r
-               }\r
-               throw new RuntimeException();\r
-       }\r
-       \r
-       \r
-       \r
-       protected void writeClass(PrintWriter writer,boolean abst, String elementId, String className, String baseClass, List<String> interfaces) {\r
-               writer.println("@SuppressWarnings(\"unused\")");\r
-               writer.print("public " +(abst ? "abstract " : "") + "class " + className + " extends "+baseClass);\r
-               if (interfaces.size() > 0) {\r
-                       writer.print(" implements ");\r
-                       for (int i = 0; i < interfaces.size(); i++) {\r
-                               writer.print(interfaces.get(i));\r
-                               if (i < interfaces.size() -1 )\r
-                                       writer.print(",");\r
-                       }\r
-               }\r
-               writer.println("{");\r
-               writer.println();\r
-               writer.println("   @Override");\r
-               writer.println("   public java.lang.String getElementId() {");\r
-               if (elementId != null)\r
-               writer.println("      return \""+elementId+"\";");\r
-               else // complex types cannot be parsed directly with name/id reference.\r
-               writer.println("      return null;");\r
-               writer.println("   }");\r
-               writer.println();\r
-       }\r
-       \r
-       \r
-       protected abstract void createReferenceIndicator(SchemaObject parent, RefType referenceType, String refName, String objectName, String primaryClassName, String secondaryClassName, boolean useElementList, boolean useOriginalList);\r
-       protected abstract void createPrimitiveIndicator(SchemaObject parent, String refName, TypeEntry typeEntry, QName typeName);\r
-       protected abstract void createElementIndicator(SchemaObject parent, boolean useElementList, String refName, String className, boolean useOriginalList);\r
-       \r
-       @Override\r
-       public void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType referenceType, String baseRelationName) {\r
-               String objectName;\r
-               if (referenceType != RefType.Element) {\r
-                       QName refTypeName;\r
-                       SchemaObject refObject = null;\r
-                       if (referenceType == RefType.Type) {\r
-                               refTypeName = element.getElement().getType();\r
-                               if (refName == null)\r
-                                       refName = element.getElement().getName();\r
-                               objectName = element.getElement().getName();\r
-                               refObject = this.base.getComplexType(refTypeName);\r
-                               if (refObject == null) this.base.getSimpleType(refTypeName);\r
-                       } else {\r
-                               refTypeName = element.getElement().getRef();\r
-                               if (refName == null)\r
-                                       refName = refTypeName.getLocalPart();\r
-                               objectName = refTypeName.getLocalPart();\r
-                               refObject = this.base.getElement(refTypeName);\r
-                       }\r
-                       \r
-                       TypeEntry typeEntry = this.base.getTypeEntry(refTypeName);\r
-                       if (typeEntry == null) {\r
-                               // prefer element reference over complex type reference\r
-                               String primaryClassName = null;\r
-                               String secondaryClassName = null;\r
-                               if (refObject != null)\r
-                                       primaryClassName = getName(refObject);\r
-                               else if (this.base.getSimpleType(refTypeName) != null) {\r
-                                       Inheritance inheritance = new Inheritance("");\r
-                                       this.base.getAtomicTypeInheritance(refTypeName, inheritance);\r
-                                       if (inheritance.atomicType != null) {\r
-                                               createPrimitiveIndicator(parent, refName, inheritance.atomicType, refTypeName);\r
-                                               return;\r
-                                       }\r
-                                       else {\r
-                                               throw new RuntimeException("No supported atomic type found for simple type " + refTypeName.toString());\r
-                                       }\r
-                               }\r
-                               else {\r
-                                       throw new RuntimeException("Type that is neither complex nor simple??");\r
-                               }\r
-                               \r
-                               if (refObject != null) {\r
-                                       secondaryClassName = getName(refObject);\r
-                               }\r
-                               boolean useElementList = this.base.useElementList(parent, indicator,element, referenceType == RefType.Reference, refName, refTypeName);\r
-                               boolean useOriginalList = this.base.useOriginalList(parent, indicator,element, referenceType == RefType.Reference, refName, refTypeName);\r
-                               createReferenceIndicator(parent, referenceType, refName, objectName, primaryClassName, secondaryClassName, useElementList, useOriginalList);\r
-                       } else {\r
-                               createPrimitiveIndicator(parent, refName, typeEntry, refTypeName);\r
-                       }\r
-               } else {\r
-                       Element attrs= element.getElement();\r
-                       SchemaObject obj = this.base.getWithObj(parent, attrs);\r
-                       \r
-                       String className = getName(obj);\r
-                       if (refName == null)\r
-                               refName = attrs.getName();\r
-                       \r
-                       boolean useElementList = this.base.useElementList(parent, indicator,element, false, refName, new QName(obj.getName()));\r
-                       boolean useOriginalList = this.base.useOriginalList(parent, indicator,element, false, refName, new QName(obj.getName()));\r
-                       createElementIndicator(parent, useElementList, refName, className, useOriginalList);\r
-               }\r
-       }\r
-\r
-}\r
+package org.simantics.xml.sax;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.xml.namespace.QName;
+
+import org.simantics.xml.sax.SchemaConversionBase.Inheritance;
+import org.simantics.xml.sax.SchemaConversionBase.RefType;
+import org.simantics.xml.sax.SchemaConversionBase.TypeEntry;
+import org.w3._2001.xmlschema.AttributeGroupRef;
+import org.w3._2001.xmlschema.Element;
+import org.w3._2001.xmlschema.Schema;
+
+//public abstract class JavaGenerator extends SchemaConversionBase{
+public abstract class JavaGenerator implements SchemaConversionComponent {
+
+       String commentTag = "//";
+       
+       Schema schema;
+       String ontologyClassName;
+       String ontologyUri;
+       SchemaConverter converter;
+       SchemaConversionBase base;
+       
+       List<String> ruleClassNames = new ArrayList<String>();
+       
+       String ontShort = "ONT"; 
+       String name;
+       
+       File importParserDir;
+       String elementPackageName;
+       
+       Map<SchemaObject, FileWriter> writers = new HashMap<SchemaObject, ImporterGenerator.FileWriter>();
+       
+       public JavaGenerator(SchemaConverter converter, SchemaConversionBase base) {
+               this.converter = converter;
+               this.base = base;
+               
+               this.schema = base.schema;
+               this.ontologyClassName = base.className;
+               this.ontologyUri = base.ontologyURI;
+               this.converter = converter;
+               this.name = converter.name;
+               ontShort = converter.shortName;
+               ontShort +=".";
+               elementPackageName = name+getPackagePostFix();
+       }
+       
+       public String getElementPackageName() {
+               return elementPackageName;
+       }
+       
+       
+       protected PrintWriter createFile(File file) throws IOException {
+               if (!file.exists())
+                       file.createNewFile();
+               PrintWriter writer = new PrintWriter(file);
+               for (String s : converter.getHeader()) {
+                       writer.println(commentTag + " " + s);   
+               }
+               writer.println();
+               return writer;
+       }
+       
+       protected String getValueGetterMethod(TypeEntry binding,String name) {
+               if (binding == null)
+                       return name+".getValue()";
+               return binding.getValueGetterMethod(name);
+       }
+       
+       protected String getValueGetter(TypeEntry binding,String name) {
+               if (binding == null)
+                       return name;
+               return binding.getValueGetter(name);
+       }
+       
+       protected String getValueGetter(TypeEntry binding) {
+               if (binding == null)
+                       return "value";
+               return binding.getValueGetter();
+       }
+       
+       @Override
+       public String getSimpleTypePrefix() {
+               return "SimpleTypes_";
+       }       
+       
+       @Override
+       public String getComplexTypePrefix() {
+               return "ComplexTypes_";
+       }       
+       
+       @Override
+       public String getAttributeGroupPrefix() {
+               return "AttributeGroups_";
+       }
+       
+       @Override
+       public String handleChoice(SchemaObject parent, SchemaElement indicator, List<SchemaElement> elements, String name) {
+               String baseRelationName = getName(parent) + ".has" + name;
+               
+//             for (SchemaElement e : elements) {
+//                     Element localElement = e.getElement();
+//                     if (localElement.getName() != null) {
+//                             QName refType = localElement.getType();
+//                             if (refType != null)
+//                                     //handleIndicator(parent, indicator, e, false, name, refType);
+//                                     handleIndicator(parent, indicator, e, name, RefType.Type, baseRelationName);
+//                     } else if (localElement.getRef() != null) {
+//                             //QName refType = localElement.getRef();
+//                             //handleIndicator(parent, indicator, e, true, name, refType);
+//                             handleIndicator(parent, indicator, e, name, RefType.Reference, baseRelationName);
+//                     }
+//             }
+               
+               return baseRelationName;
+       }
+       
+       protected String getOntologyImport() {
+               return this.ontologyClassName+" " +ontShort.substring(0,ontShort.length()-1)+" = "+this.ontologyClassName+".getInstance(graph);";
+       }
+       
+       protected static class FileWriter {
+               public PrintWriter writer;
+               
+               public PrintWriter delayedWriter;
+               public PrintWriter delayedWriter2;
+       }
+       
+       protected  FileWriter getWriter(SchemaObject obj) {
+               SchemaObject s = obj;
+               while (s != null) {
+                       FileWriter fw = writers.get(s);
+                       if (fw != null)
+                               return fw;
+                       s = s.getParent();
+               }
+               return null;
+       }
+       
+       @Override
+       public String getName(SchemaObject obj) {
+               if (obj.getParent() == null) {
+                       switch (obj.getType()) {
+                       case COMPLEX_TYPE:
+                               return getComplexTypePrefix()+obj.getName();
+                       case ELEMENT:
+                               return obj.getName();
+                       case ATTRIBUTE_GROUP:
+                               return getAttributeGroupPrefix()+obj.getName();
+                       case SIMPLE_TYPE:
+                               return getSimpleTypePrefix()+obj.getName();
+                       }
+               } else {
+                       SchemaObject o = obj;
+                       SchemaObject prev = null;
+                       String name = "";
+                       while (o != null){
+                               if (o.getName() != null)
+                                       name = o.getName()+"_"+name;
+                               prev = o;
+                               o = o.getParent();
+                               if (prev.getObj() instanceof AttributeGroupRef)
+                                       o = null;
+                       }
+                       name = name.substring(0, name.length()-1);
+                       switch (prev.getType()) {
+                       case COMPLEX_TYPE:
+                               return getComplexTypePrefix()+name;
+                       case ELEMENT:
+                               return name;
+                       case ATTRIBUTE_GROUP:
+                               return getAttributeGroupPrefix()+name;
+                       case SIMPLE_TYPE:
+                               return getSimpleTypePrefix()+name;
+                       }
+               }
+               throw new RuntimeException();
+       }
+       
+       public String getName(SchemaObject obj, String rel) {
+               if (obj.getParent() == null) {
+                       switch (obj.getType()) {
+                       case COMPLEX_TYPE:
+                               return getComplexTypePrefix()+rel+obj.getName();
+                       case ELEMENT:
+                               return rel+obj.getName();
+                       case ATTRIBUTE_GROUP:
+                               return getAttributeGroupPrefix()+rel+obj.getName();
+                       case SIMPLE_TYPE:
+                               return getSimpleTypePrefix()+rel+obj.getName();
+                       }
+               } else {
+                       SchemaObject o = obj;
+                       SchemaObject prev = null;
+                       String name = "";
+                       while (o != null){
+                               if (o.getName() != null)
+                                       name = o.getName()+"_"+name;
+                               prev = o;
+                               o = o.getParent();
+                       }
+                       name = name.substring(0, name.length()-1);
+                       switch (prev.getType()) {
+                       case COMPLEX_TYPE:
+                               return getComplexTypePrefix()+rel+name;
+                       case ELEMENT:
+                               return rel+name;
+                       case ATTRIBUTE_GROUP:
+                               return getAttributeGroupPrefix()+rel+name;
+                       case SIMPLE_TYPE:
+                               return getSimpleTypePrefix()+rel+name;
+                       }
+               }
+               throw new RuntimeException();
+       }
+       
+       
+       
+       protected void writeClass(PrintWriter writer,boolean abst, String elementId, String className, String baseClass, List<String> interfaces) {
+               writer.println("@SuppressWarnings(\"unused\")");
+               writer.print("public " +(abst ? "abstract " : "") + "class " + className + " extends "+baseClass);
+               if (interfaces.size() > 0) {
+                       writer.print(" implements ");
+                       for (int i = 0; i < interfaces.size(); i++) {
+                               writer.print(interfaces.get(i));
+                               if (i < interfaces.size() -1 )
+                                       writer.print(",");
+                       }
+               }
+               writer.println("{");
+               writer.println();
+               writer.println("   @Override");
+               writer.println("   public java.lang.String getElementId() {");
+               if (elementId != null)
+               writer.println("      return \""+elementId+"\";");
+               else // complex types cannot be parsed directly with name/id reference.
+               writer.println("      return null;");
+               writer.println("   }");
+               writer.println();
+       }
+       
+       
+       protected abstract void createReferenceIndicator(SchemaObject parent, RefType referenceType, String refName, String objectName, String primaryClassName, String secondaryClassName, boolean useElementList, boolean useOriginalList);
+       protected abstract void createPrimitiveIndicator(SchemaObject parent, String refName, TypeEntry typeEntry, QName typeName);
+       protected abstract void createElementIndicator(SchemaObject parent, boolean useElementList, String refName, String className, boolean useOriginalList);
+       
+       protected abstract String getPackagePostFix();
+       @Override
+       public void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType referenceType, String baseRelationName) {
+               String objectName;
+               if (referenceType != RefType.Element) {
+                       QName refTypeName;
+                       SchemaObject refObject = null;
+                       if (referenceType == RefType.Type) {
+                               refTypeName = element.getElement().getType();
+                               if (refName == null)
+                                       refName = base.getName(element.getElement());
+                               objectName = base.getName(element.getElement());
+                               refObject = this.base.getComplexType(refTypeName);
+                               if (refObject == null) this.base.getSimpleType(refTypeName);
+                       } else {
+                               refTypeName = element.getElement().getRef();
+                               if (refName == null)
+                                       refName = base.getName(refTypeName);
+                               objectName = refTypeName.getLocalPart();
+                               refObject = this.base.getElement(refTypeName);
+                       }
+                       
+                       TypeEntry typeEntry = this.base.getTypeEntry(refTypeName);
+                       if (typeEntry == null) {
+                               // prefer element reference over complex type reference
+                               String primaryClassName = null;
+                               String secondaryClassName = null;
+                               if (refObject != null) {
+                                       primaryClassName = getName(refObject);
+                                       if (refObject.getSc() != base) {
+                                               SchemaConverter con = refObject.getSc().converter;
+                                               primaryClassName = con.getPluginName() + "." + con.name +getPackagePostFix() + "." + primaryClassName;
+                                       }
+                               }
+                               else if (this.base.getSimpleType(refTypeName) != null) {
+                                       Inheritance inheritance = new Inheritance("");
+                                       this.base.getAtomicTypeInheritance(refTypeName, inheritance);
+                                       if (inheritance.atomicType != null) {
+                                               createPrimitiveIndicator(parent, refName, inheritance.atomicType, refTypeName);
+                                               return;
+                                       }
+                                       else {
+                                               throw new RuntimeException("No supported atomic type found for simple type " + refTypeName.toString());
+                                       }
+                               }
+                               else {
+                                       throw new RuntimeException("Type that is neither complex nor simple??");
+                               }
+                               
+                               secondaryClassName = primaryClassName;
+                               boolean useElementList = this.base.useElementList(parent, indicator,element, referenceType == RefType.Reference, refName, refTypeName);
+                               boolean useOriginalList = this.base.useOriginalList(parent, indicator,element, referenceType == RefType.Reference, refName, refTypeName);
+                               createReferenceIndicator(parent, referenceType, refName, objectName, primaryClassName, secondaryClassName, useElementList, useOriginalList);
+                       } else {
+                               createPrimitiveIndicator(parent, refName, typeEntry, refTypeName);
+                       }
+               } else {
+                       Element attrs= element.getElement();
+                       SchemaObject obj = this.base.getWithObj(parent, attrs);
+                       
+                       String className = getName(obj);
+                       if (refName == null)
+                                refName = base.getName(attrs);
+                       
+                       boolean useElementList = this.base.useElementList(parent, indicator,element, false, refName, new QName(obj.getName()));
+                       boolean useOriginalList = this.base.useOriginalList(parent, indicator,element, false, refName, new QName(obj.getName()));
+                       createElementIndicator(parent, useElementList, refName, className, useOriginalList);
+               }
+       }
+
+}
index 74bafb99758af243ac12f8f0997ad0c5559ca6f8..aea2ff050d7fa2dceb39e8a84615b3eefe23d8d5 100644 (file)
@@ -3,6 +3,9 @@ package org.simantics.xml.sax;
 import java.io.FileNotFoundException;
 import java.io.PrintWriter;
 import java.io.StringWriter;
+import java.util.ArrayDeque;
+import java.util.Deque;
+import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Set;
@@ -86,6 +89,23 @@ public class OntologyGenerator implements SchemaConversionComponent {
                writer.println();
                writer.println();
                
+               Set<SchemaConverter> children = new HashSet<>();
+               Deque<SchemaConverter> stack = new ArrayDeque<>();
+               stack.addAll(converter.getSubConverters());
+               while (!stack.isEmpty()) {
+                       SchemaConverter sc = stack.pop();
+                       if (children.contains(sc))
+                               continue;
+                       children.add(sc);
+                       stack.addAll(sc.getSubConverters());
+               }
+               children.remove(converter);
+               for (SchemaConverter sc : children) {
+                       writer.println(sc.shortName + " = <" + sc.ontologyUri +">");
+               }
+               
+               writer.println();
+               
                ontRoot += ".";
                writer.println(ontRoot+"SimpleTypes : L0.Library");
                writer.println(ontRoot+"ComplexTypes : L0.Library");
@@ -93,6 +113,8 @@ public class OntologyGenerator implements SchemaConversionComponent {
                writer.println();
                writer.println(commentTag + " Interpreted from schema");
                writer.println();
+               
+               
                        
                base.handle(this);
        }
@@ -162,7 +184,7 @@ public class OntologyGenerator implements SchemaConversionComponent {
                        } else {
                                referenceType = element.getElement().getRef();
                                if (refName == null)
-                                       refName = referenceType.getLocalPart();
+                                       refName = base.getName(referenceType);
                        }
                        String type = base.getL0TypeFromPrimitiveType(referenceType);
                        SchemaObject obj = null;
@@ -219,7 +241,7 @@ public class OntologyGenerator implements SchemaConversionComponent {
                
        @Override
        public void handle(SchemaObject parent, Attribute attribute) {
-               String name = attribute.getName();
+               String name = base.getName(attribute);
                QName primitiveType = attribute.getType();
                LocalSimpleType simpleType = attribute.getSimpleType();
                QName ref = attribute.getRef();
@@ -233,8 +255,8 @@ public class OntologyGenerator implements SchemaConversionComponent {
                        relationType = "XML.hasAttribute";
                }
                else if (ref != null && parent != null) {
-                       relationName = getName(parent)+".has"+ref.getLocalPart();
-                       relationType = ontRoot+"has"+ref.getLocalPart();
+                       relationName = getName(parent)+".has"+base.getName(ref);
+                       relationType = converter.getShortName(ref.getNamespaceURI())+".has"+base.getName(ref);
                } else {
                        throw new RuntimeException();
                }
@@ -292,7 +314,7 @@ public class OntologyGenerator implements SchemaConversionComponent {
                if (parent == null) {
                        NamedAttributeGroup group = (NamedAttributeGroup)attributeGroup;
                        writer.println(ontRoot+getAttributeGroupPrefix()+group.getName()+ " <T XML.AttributeGroup");
-                       SchemaObject obj = new SchemaObject(parent,attributeGroup);
+                       SchemaObject obj = new SchemaObject(base,parent,attributeGroup);
                        for (Annotated annotated : group.getAttributeOrAttributeGroup()) {
                                if (annotated instanceof Attribute) {
                                        //handle(getAttributeGroupPrefix()+group.getName(),(Attribute)annotated);
@@ -303,7 +325,7 @@ public class OntologyGenerator implements SchemaConversionComponent {
                                }
                        }
                } else {
-                       writer.println(getName(parent) +" L0.Inherits " + ontRoot + getAttributeGroupPrefix() + attributeGroup.getRef().getLocalPart());
+                       writer.println(getName(parent) +" L0.Inherits " + ontRoot + getAttributeGroupPrefix() + base.getName(attributeGroup.getRef()));
                }
                
        }
@@ -443,7 +465,7 @@ public class OntologyGenerator implements SchemaConversionComponent {
                Element element = elementObj.getElement();
                String name = getName(elementObj);//element.getName();
                
-               if ("Text".equals(name))
+               if (name.contains("Canvas"))
                        System.out.println();
                
                String type = "XML.Element";
@@ -459,16 +481,16 @@ public class OntologyGenerator implements SchemaConversionComponent {
                                        throw new RuntimeException("Cannot get L0 type for " + base.getLocalPart());
                                types.add(l0Type);
                        } else if (this.base.isElementRef(base.getLocalPart()))
-                               types.add(ontRoot+base.getLocalPart());
+                               types.add(ontRoot+this.base.getName(base));
                        else
-                               types.add(ontRoot+getComplexTypePrefix()+base.getLocalPart());
+                               types.add(ontRoot+getComplexTypePrefix()+this.base.getName(base));
                }
                QName substitution = element.getSubstitutionGroup();
                if (substitution != null) {
                        if (this.base.isElementRef(substitution.getLocalPart()))
-                               types.add(ontRoot+substitution.getLocalPart());
+                               types.add(ontRoot+this.base.getName(substitution));
                        else
-                               types.add( ontRoot+getComplexTypePrefix()+substitution.getLocalPart());
+                               types.add( ontRoot+getComplexTypePrefix()+this.base.getName(substitution));
                }
                for (String t : types) {
                        type += " <T " + t;
@@ -514,18 +536,23 @@ public class OntologyGenerator implements SchemaConversionComponent {
                throw new RuntimeException("ObjectType " + type + " has no base class");
        }
        
+       
        @Override
        public String getName(SchemaObject obj) {
                if (obj.getParent() == null) {
                        switch (obj.getType()) {
                        case COMPLEX_TYPE:
-                               return ontRoot+getComplexTypePrefix()+obj.getName();
+                               //return ontRoot+getComplexTypePrefix()+obj.getName();
+                               return obj.getLibShortName()+"."+getComplexTypePrefix()+obj.getName();
                        case ELEMENT:
-                               return ontRoot+obj.getName();
+                               //return ontRoot+obj.getName();
+                               return obj.getLibShortName()+"."+obj.getName();
                        case ATTRIBUTE_GROUP:
-                               return ontRoot+getAttributeGroupPrefix()+obj.getName();
+                               //return ontRoot+getAttributeGroupPrefix()+obj.getName();
+                               return obj.getLibShortName()+"."+getAttributeGroupPrefix()+obj.getName();
                        case SIMPLE_TYPE:
-                               return ontRoot+getSimpleTypePrefix()+obj.getName();
+                               //return ontRoot+getSimpleTypePrefix()+obj.getName();
+                               return obj.getLibShortName()+"."+getSimpleTypePrefix()+obj.getName();
                        }
                } else {
                        SchemaObject o = obj;
@@ -540,13 +567,17 @@ public class OntologyGenerator implements SchemaConversionComponent {
                        name = name.substring(0, name.length()-1);
                        switch (prev.getType()) {
                        case COMPLEX_TYPE:
-                               return ontRoot+getComplexTypePrefix()+name;
+                               //return ontRoot+getComplexTypePrefix()+name;
+                               return obj.getLibShortName()+"."+getComplexTypePrefix()+name;
                        case ELEMENT:
-                               return ontRoot+name;
+                               //return ontRoot+name;
+                               return obj.getLibShortName()+"."+name;
                        case ATTRIBUTE_GROUP:
-                               return ontRoot+getAttributeGroupPrefix()+name;
+                               //return ontRoot+getAttributeGroupPrefix()+name;
+                               return obj.getLibShortName()+"."+getAttributeGroupPrefix()+name;
                        case SIMPLE_TYPE:
-                               return ontRoot+getSimpleTypePrefix()+name;
+                               //return ontRoot+getSimpleTypePrefix()+name;
+                               return obj.getLibShortName()+"."+getSimpleTypePrefix()+name;
                        }
                }
                throw new RuntimeException();
@@ -556,7 +587,7 @@ public class OntologyGenerator implements SchemaConversionComponent {
        public String getName(SchemaObject parent, SchemaElement e, String rel) {
                QName ref = e.getElement().getRef();
                if (ref != null) {
-                       return converter.getShortName(ref.getNamespaceURI()) + rel + ref.getLocalPart();
+                       return converter.getShortName(ref.getNamespaceURI()) + rel + base.getName(ref);
                }
                else {
                        return getName(parent, "") + "." + rel + e.getElement().getName();
@@ -567,13 +598,13 @@ public class OntologyGenerator implements SchemaConversionComponent {
                if (obj.getParent() == null) {
                        switch (obj.getType()) {
                        case COMPLEX_TYPE:
-                               return ontRoot+getComplexTypePrefix()+rel+obj.getName();
+                               return obj.getLibShortName()+"."+getComplexTypePrefix()+rel+obj.getName();
                        case ELEMENT:
-                               return ontRoot+rel+obj.getName();
+                               return obj.getLibShortName()+"."+rel+obj.getName();
                        case ATTRIBUTE_GROUP:
-                               return ontRoot+getAttributeGroupPrefix()+rel+obj.getName();
+                               return obj.getLibShortName()+"."+getAttributeGroupPrefix()+rel+obj.getName();
                        case SIMPLE_TYPE:
-                               return ontRoot+getSimpleTypePrefix()+rel+obj.getName();
+                               return obj.getLibShortName()+"."+getSimpleTypePrefix()+rel+obj.getName();
                        }
                } else {
                        SchemaObject o = obj;
@@ -588,13 +619,13 @@ public class OntologyGenerator implements SchemaConversionComponent {
                        name = name.substring(0, name.length()-1);
                        switch (prev.getType()) {
                        case COMPLEX_TYPE:
-                               return ontRoot+getComplexTypePrefix()+rel+name;
+                               return obj.getLibShortName()+"."+getComplexTypePrefix()+rel+name;
                        case ELEMENT:
-                               return ontRoot+rel+name;
+                               return obj.getLibShortName()+"."+rel+name;
                        case ATTRIBUTE_GROUP:
-                               return ontRoot+getAttributeGroupPrefix()+rel+name;
+                               return obj.getLibShortName()+"."+getAttributeGroupPrefix()+rel+name;
                        case SIMPLE_TYPE:
-                               return ontRoot+getSimpleTypePrefix()+rel+name;
+                               return obj.getLibShortName()+"."+getSimpleTypePrefix()+rel+name;
                        }
                }
                throw new RuntimeException();
index cb0f7629f2ca38f28f16a7d9cc11313f6dc6444c..049b08775cb915dfd28628851f28b38634966c11 100644 (file)
@@ -360,17 +360,17 @@ public final class SchemaConversionBase {
                for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) {
                        if (attrs instanceof Element) {
                                Element element = (Element)attrs;
-                               SchemaObject obj = new SchemaObject(element);
+                               SchemaObject obj = new SchemaObject(this,element);
                                obj.setRename(getRename(element));
                                stack.push(obj);
                        } else if (attrs instanceof ComplexType) {
                                ComplexType complexType = (ComplexType)attrs;
-                               SchemaObject obj = new SchemaObject(complexType);
+                               SchemaObject obj = new SchemaObject(this,complexType);
                                obj.setRename(getRename(complexType));
                                stack.push(obj);
                        } else if (attrs instanceof SimpleType) {
                                SimpleType simpleType = (SimpleType)attrs;
-                               SchemaObject obj = new SchemaObject(simpleType);
+                               SchemaObject obj = new SchemaObject(this,simpleType);
                                stack.push(obj);
                        }  else if (attrs instanceof Attribute) {
                                // Attributes are not cached
@@ -378,7 +378,7 @@ public final class SchemaConversionBase {
                                // Attribute groups are not cached
                        } else if (attrs instanceof NamedGroup) {
                                NamedGroup group = (NamedGroup)attrs;
-                               SchemaObject obj = new SchemaObject(group);
+                               SchemaObject obj = new SchemaObject(this,group);
                                stack.push(obj);
                        } else {
                                System.out.println(attrs.getClass().getName());
@@ -448,9 +448,9 @@ public final class SchemaConversionBase {
                                        elementName.put(e.getName(), object);
                                elements.put(e, object);
                                if (e.getComplexType() != null)
-                                       stack.push(new SchemaObject(object,e.getComplexType()));
+                                       stack.push(new SchemaObject(this,object,e.getComplexType()));
                                if (e.getSimpleType() != null)
-                                       stack.push(new SchemaObject(object,e.getSimpleType()));
+                                       stack.push(new SchemaObject(this,object,e.getSimpleType()));
                                break;
                        } 
                        case SIMPLE_TYPE:{
@@ -476,7 +476,7 @@ public final class SchemaConversionBase {
                                JAXBElement<?> element = (JAXBElement<?>)o;
                                Object elemValue = element.getValue();
                                if (elemValue instanceof Element) {
-                                       SchemaObject obj = new SchemaObject(parent,(Element)elemValue);
+                                       SchemaObject obj = new SchemaObject(this,parent,(Element)elemValue);
                                        obj.setRename(getRename((Element)elemValue));
                                        stack.add(obj);
                                } else if (elemValue instanceof ExplicitGroup) {
@@ -497,7 +497,7 @@ public final class SchemaConversionBase {
        private void preload(SchemaObject parent, RealGroup eg, Deque<SchemaObject> stack) {
                System.out.println(eg); 
                if (eg instanceof NamedGroup) {
-                       SchemaObject obj = new SchemaObject(parent,(NamedGroup)eg);
+                       SchemaObject obj = new SchemaObject(this,parent,(NamedGroup)eg);
                        stack.add(obj);
                }
        }
@@ -952,19 +952,31 @@ public final class SchemaConversionBase {
                }
        }
        
-       protected String getElementName(Element localElement) {
+       protected String getName(Element localElement) {
                if (localElement.getName() != null) {
                        String refName = localElement.getName();
-                       QName refType = localElement.getType();
-                       if (refType != null)
-                               return refName;
+                       return refName.replaceAll("\\.", "_");
                } else if (localElement.getRef() != null) {
                        QName refType = localElement.getRef();
                        if (refType != null)
-                               return refType.getLocalPart();
+                               return getName(refType);
+                               
                }
                return null;
        }
+
+       protected String getName(QName ref) {
+               String n = ref.getLocalPart();
+               return n.replaceAll("\\.", "_");
+       }
+       
+       protected String getName(Attribute ref) {
+               String n = ref.getName();
+               if (n != null)
+                       return n.replaceAll("\\.", "_");
+               else
+                       return null;
+       }
        
        protected String getChoiceName(List<SchemaElement> elements) {
                if (elements.size() == 1) {
@@ -973,7 +985,7 @@ public final class SchemaConversionBase {
                else if (elements.size() > 0 && elements.size() <= 3) {
                        List<String> names = new ArrayList<String>();
                        for (SchemaElement e : elements) {
-                               String name = getElementName(e.getElement());
+                               String name = getName(e.getElement());
                                if (name != null)
                                        names.add(name);
                        }
index adab7f2af75112da339adba44dabab01fceb50f5..660b990975f640c66bc093f541fa024ccda7f16f 100644 (file)
@@ -7,16 +7,17 @@ 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;
 import javax.xml.bind.JAXBElement;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Unmarshaller;
-import javax.xml.namespace.QName;
 
 import org.simantics.utils.datastructures.MapList;
 import org.simantics.xml.sax.configuration.Configuration;
@@ -34,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;
        
@@ -56,13 +57,13 @@ public class SchemaConverter {
        private MapList<String,SchemaConverter> schemaNSMap;
        private MapList<String,SchemaConverter> 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;
        
@@ -71,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;
@@ -94,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<>();
                }
@@ -122,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()) {
@@ -134,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<SchemaConverter> processed = new HashSet<>();
+               return _getRoot(processed);
+       }
+       
+       protected SchemaConverter _getRoot(Set<SchemaConverter> 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;
+               }
+               for (SchemaConverter sc : this.parent) {
+                       SchemaConverter root = sc._getRoot(processed);
+                       if (root != null)
+                               return root;
                }
-               return s;
+               return null;
        }
        
        public void convert() throws JAXBException, IOException {
                
                init();
-               doConvert();
+               for (SchemaConverter sc : getRoot().fileMap.values()) {
+                       sc.doConvert();
+               }
        }
        
        boolean init = false;
@@ -196,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();
@@ -231,8 +259,18 @@ public class SchemaConverter {
                        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];
@@ -256,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) {
                                
@@ -280,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();
@@ -289,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();
@@ -337,8 +376,21 @@ public class SchemaConverter {
                return configuration;
        }
        
+       public List<SchemaConverter> getParent() {
+               return parent;
+       }
+       
+       public List<SchemaConverter> getSubConverters() {
+               return subConverters;
+       }
+       
        public boolean isPrimary() {
-               return getRoot() == this;
+               return true;
+//             if (getRoot() == this)
+//                     return true;
+//             List<SchemaConverter> conv = new ArrayList<>(getRoot().fileMap.values());
+//             int current = conv.indexOf(this);
+               
        }
 
        public String getShortName(String namespaceURI) {
index 4138222913bb100923e0f08748d73a7445cfb55d..53f9da9fb0b4a749041f8704440aa5b34627f36f 100644 (file)
-package org.simantics.xml.sax;\r
-\r
-import org.simantics.xml.sax.configuration.Rename;\r
-import org.w3._2001.xmlschema.AttributeGroup;\r
-import org.w3._2001.xmlschema.AttributeGroupRef;\r
-import org.w3._2001.xmlschema.ComplexType;\r
-import org.w3._2001.xmlschema.Element;\r
-import org.w3._2001.xmlschema.NamedAttributeGroup;\r
-import org.w3._2001.xmlschema.NamedGroup;\r
-import org.w3._2001.xmlschema.OpenAttrs;\r
-import org.w3._2001.xmlschema.SimpleType;\r
-\r
-public class SchemaObject {\r
-       enum ObjectType{ELEMENT,COMPLEX_TYPE,SIMPLE_TYPE,ATTRIBUTE_GROUP,MODEL_GROUP};\r
-       \r
-       private SchemaObject parent;\r
-       private ObjectType type;\r
-       private OpenAttrs obj;\r
-       private Rename rename;\r
-       \r
-       public SchemaObject(Element element) {\r
-               this(null,element);\r
-       }\r
-       \r
-       public void setRename(Rename rename) {\r
-               this.rename = rename;\r
-       }\r
-       \r
-       public SchemaObject(ComplexType complexType) {\r
-               this(null, complexType);\r
-       }\r
-       \r
-       public SchemaObject(SimpleType simpleType) {\r
-               this(null, simpleType);\r
-       }\r
-       \r
-       public SchemaObject(NamedGroup namedGroup) {\r
-               this(null, namedGroup);\r
-       }\r
-       \r
-       public SchemaObject(SchemaObject parent, Element element) {\r
-               this.parent = parent;\r
-               this.obj = element;\r
-               this.type = ObjectType.ELEMENT;\r
-       }\r
-       \r
-       public SchemaObject(SchemaObject parent, ComplexType complexType) {\r
-               this.parent = parent;\r
-               this.obj = complexType;\r
-               this.type = ObjectType.COMPLEX_TYPE;\r
-       }\r
-       \r
-       public SchemaObject(SchemaObject parent, AttributeGroup attributeGroup) {\r
-               this.parent = parent;\r
-               this.obj = attributeGroup;\r
-               this.type = ObjectType.ATTRIBUTE_GROUP;\r
-       }\r
-       \r
-       public SchemaObject(SchemaObject parent, NamedGroup namedGroup) {\r
-               this.parent = parent;\r
-               this.obj = namedGroup;\r
-               this.type = ObjectType.MODEL_GROUP;\r
-       }\r
-       \r
-\r
-       public SchemaObject(SchemaObject parent, SimpleType simpleType) {\r
-               this.parent = parent;\r
-               this.obj = simpleType;\r
-               this.type = ObjectType.SIMPLE_TYPE;\r
-       }\r
-       \r
-       public Element getElement() {\r
-               if (type != ObjectType.ELEMENT)\r
-                       return null;\r
-               return (Element)obj;\r
-       }\r
-       \r
-       public ComplexType getComplexType() {\r
-               if (type != ObjectType.COMPLEX_TYPE)\r
-                       return null;\r
-               return (ComplexType)obj;\r
-       }\r
-       \r
-       public SimpleType getSimpleType() {\r
-               if (type != ObjectType.SIMPLE_TYPE)\r
-                       return null;\r
-               return (SimpleType)obj;\r
-       }\r
-       \r
-       public AttributeGroup getAttributeGroup() {\r
-               if (type != ObjectType.ATTRIBUTE_GROUP)\r
-                       return null;\r
-               return (AttributeGroup)obj;\r
-       }\r
-       \r
-       public NamedGroup getModelGroup() {\r
-               if (type != ObjectType.MODEL_GROUP)\r
-                       return null;\r
-               return (NamedGroup)obj;\r
-       }\r
-       \r
-       public SchemaObject getParent() {\r
-               return parent;\r
-       }\r
-       \r
-       public OpenAttrs getObj() {\r
-               return obj;\r
-       }\r
-       \r
-       \r
-       public String getName() {\r
-               switch (type) {\r
-               case ATTRIBUTE_GROUP:\r
-                       if (obj instanceof NamedAttributeGroup)\r
-                               return ((NamedAttributeGroup)obj).getName();\r
-                       else\r
-                               return ((AttributeGroupRef)obj).getRef().getLocalPart();\r
-               case COMPLEX_TYPE:\r
-                       if (rename != null)\r
-                               return rename.getName();\r
-                       return ((ComplexType)obj).getName();\r
-               case ELEMENT:\r
-                       if (rename != null)\r
-                               return rename.getName();\r
-                       return ((Element)obj).getName();\r
-               case SIMPLE_TYPE:\r
-                       return ((SimpleType)obj).getName();\r
-               }\r
-               throw new RuntimeException("Unknown object type " + type);              \r
-       }\r
-       \r
-       public ObjectType getType() {\r
-               return type;\r
-       }\r
-\r
-}\r
+package org.simantics.xml.sax;
+
+import org.simantics.xml.sax.configuration.Rename;
+import org.w3._2001.xmlschema.AttributeGroup;
+import org.w3._2001.xmlschema.AttributeGroupRef;
+import org.w3._2001.xmlschema.ComplexType;
+import org.w3._2001.xmlschema.Element;
+import org.w3._2001.xmlschema.NamedAttributeGroup;
+import org.w3._2001.xmlschema.NamedGroup;
+import org.w3._2001.xmlschema.OpenAttrs;
+import org.w3._2001.xmlschema.SimpleType;
+
+public class SchemaObject {
+       enum ObjectType{ELEMENT,COMPLEX_TYPE,SIMPLE_TYPE,ATTRIBUTE_GROUP,MODEL_GROUP};
+       
+       private SchemaConversionBase sc;
+       private String name;
+       private SchemaObject parent;
+       private ObjectType type;
+       private OpenAttrs obj;
+       private Rename rename;
+       
+       public SchemaObject(SchemaConversionBase sc, Element element) {
+               this(sc,null,element);
+       }
+       
+       public void setRename(Rename rename) {
+               this.rename = rename;
+               _init();
+       }
+       
+       public SchemaObject(SchemaConversionBase sc,ComplexType complexType) {
+               this(sc,null, complexType);
+       }
+       
+       public SchemaObject(SchemaConversionBase sc,SimpleType simpleType) {
+               this(sc,null, simpleType);
+       }
+       
+       public SchemaObject(SchemaConversionBase sc,NamedGroup namedGroup) {
+               this(sc,null, namedGroup);
+       }
+       
+       public SchemaObject(SchemaConversionBase sc,SchemaObject parent, Element element) {
+               this.parent = parent;
+               this.obj = element;
+               this.type = ObjectType.ELEMENT;
+               this.sc = sc;
+               _init();
+       }
+       
+       public SchemaObject(SchemaConversionBase sc,SchemaObject parent, ComplexType complexType) {
+               this.parent = parent;
+               this.obj = complexType;
+               this.type = ObjectType.COMPLEX_TYPE;
+               this.sc = sc;
+               _init();
+       }
+       
+       public SchemaObject(SchemaConversionBase sc,SchemaObject parent, AttributeGroup attributeGroup) {
+               this.parent = parent;
+               this.obj = attributeGroup;
+               this.type = ObjectType.ATTRIBUTE_GROUP;
+               this.sc = sc;
+               _init();
+       }
+       
+       public SchemaObject(SchemaConversionBase sc,SchemaObject parent, NamedGroup namedGroup) {
+               this.parent = parent;
+               this.obj = namedGroup;
+               this.type = ObjectType.MODEL_GROUP;
+               this.sc = sc;
+               _init();
+       }
+       
+
+       public SchemaObject(SchemaConversionBase sc,SchemaObject parent, SimpleType simpleType) {
+               this.parent = parent;
+               this.obj = simpleType;
+               this.type = ObjectType.SIMPLE_TYPE;
+               this.sc = sc;
+               _init();
+       }
+       
+       private void _init() {
+               name = _getName();
+               if (name != null)
+                       name = name.replaceAll("\\.", "_");
+       }
+       
+       public Element getElement() {
+               if (type != ObjectType.ELEMENT)
+                       return null;
+               return (Element)obj;
+       }
+       
+       public ComplexType getComplexType() {
+               if (type != ObjectType.COMPLEX_TYPE)
+                       return null;
+               return (ComplexType)obj;
+       }
+       
+       public SimpleType getSimpleType() {
+               if (type != ObjectType.SIMPLE_TYPE)
+                       return null;
+               return (SimpleType)obj;
+       }
+       
+       public AttributeGroup getAttributeGroup() {
+               if (type != ObjectType.ATTRIBUTE_GROUP)
+                       return null;
+               return (AttributeGroup)obj;
+       }
+       
+       public NamedGroup getModelGroup() {
+               if (type != ObjectType.MODEL_GROUP)
+                       return null;
+               return (NamedGroup)obj;
+       }
+       
+       public SchemaObject getParent() {
+               return parent;
+       }
+       
+       public OpenAttrs getObj() {
+               return obj;
+       }
+       
+       
+       public String getName() {
+               return name;
+       }
+       
+       public String getLibShortName() {
+               return sc.converter.shortName;
+       }
+       
+       
+       private String _getName() {
+               switch (type) {
+               case ATTRIBUTE_GROUP:
+                       if (obj instanceof NamedAttributeGroup)
+                               return ((NamedAttributeGroup)obj).getName();
+                       else
+                               return ((AttributeGroupRef)obj).getRef().getLocalPart();
+               case COMPLEX_TYPE:
+                       if (rename != null)
+                               return rename.getName();
+                       return ((ComplexType)obj).getName();
+               case ELEMENT:
+                       if (rename != null)
+                               return rename.getName();
+                       return ((Element)obj).getName();
+               case SIMPLE_TYPE:
+                       return ((SimpleType)obj).getName();
+               }
+               throw new RuntimeException("Unknown object type " + type);              
+       }
+       
+       public ObjectType getType() {
+               return type;
+       }
+       
+       public SchemaConversionBase getSc() {
+               return sc;
+       }
+
+}