From: Marko Luukkainen Date: Fri, 24 Aug 2018 09:45:10 +0000 (+0300) Subject: XML data based schema and ontology generation X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=fc60d91049c46ee5b6107da4d4c54eada4a9f21c;p=simantics%2Finterop.git XML data based schema and ontology generation gitlab #3 Change-Id: Ibc1821ede1bafb1aa9b68b32aff2f57faffa5a1f --- diff --git a/org.simantics.xml.sax.ui/plugin.xml b/org.simantics.xml.sax.ui/plugin.xml index f88644b..dcd3c4c 100644 --- a/org.simantics.xml.sax.ui/plugin.xml +++ b/org.simantics.xml.sax.ui/plugin.xml @@ -1,16 +1,26 @@ - - - - - - - Converts XML Schemas to Simantics ontologies and generates SAX Parser based importer for Schema compliant XML files - - - - - + + + + + + + Converts XML Schemas to Simantics ontologies and generates SAX Parser based importer for Schema compliant XML files + + + + + 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. + + + + + 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 index 0000000..ada9602 --- /dev/null +++ b/org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/datawizard/DataConversionWizard.java @@ -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 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 index 0000000..c42f682 --- /dev/null +++ b/org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/datawizard/ImportProcessPage.java @@ -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 index 0000000..0af558c --- /dev/null +++ b/org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/datawizard/InputSelectionPage.java @@ -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]; + } + +} diff --git a/org.simantics.xml.sax/META-INF/MANIFEST.MF b/org.simantics.xml.sax/META-INF/MANIFEST.MF index d8e6357..62fbab9 100644 --- a/org.simantics.xml.sax/META-INF/MANIFEST.MF +++ b/org.simantics.xml.sax/META-INF/MANIFEST.MF @@ -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 index 0000000..e94f93e --- /dev/null +++ b/org.simantics.xml.sax/src/org/simantics/xml/data/DataSchemaConverter.java @@ -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 schemaMap; + private Map 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 schemaMap) { + this.schemaMap = schemaMap; + } + + public void setFileMap(Map 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 index 0000000..b4e7c21 --- /dev/null +++ b/org.simantics.xml.sax/src/org/simantics/xml/data/XmlDataConverter.java @@ -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 inputFiles; + + String pluginName; + + private String[] header; + + public XmlDataConverter(List 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 fileMap = new HashMap<>(); + JAXBContext jc = JAXBContext.newInstance("org.w3._2001.xmlschema"); + Marshaller m = jc.createMarshaller(); + m.setProperty("jaxb.formatted.output", true); + Set 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 schemaMap = new LinkedHashMap<>(); + Map> elementMap = new HashMap<>(); + Map elementNsMap = new HashMap<>(); + Map> attributeMap = new HashMap<>(); + + protected void doConvert() throws IOException, XMLStreamException, JAXBException { + XMLInputFactory input = XMLInputFactory.newInstance(); + Deque 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 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 currentAttributes = new HashMap<>(); + Iterator 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 el = (JAXBElement)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(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()); + attributeMap.put(s, new HashMap()); + } + return s; + } + +} diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/ExporterGenerator.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/ExporterGenerator.java index f538bcb..5294e91 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/ExporterGenerator.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/ExporterGenerator.java @@ -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); diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/ImporterGenerator.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/ImporterGenerator.java index 3e02434..58f5748 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/ImporterGenerator.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/ImporterGenerator.java @@ -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); diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/JavaGenerator.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/JavaGenerator.java index 4869e07..af97d82 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/JavaGenerator.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/JavaGenerator.java @@ -1,316 +1,324 @@ -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 ruleClassNames = new ArrayList(); - - String ontShort = "ONT"; - String name; - - File importParserDir; - String elementPackageName; - - Map writers = new HashMap(); - - 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 +="."; - } - - - 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 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, 3)+" = "+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 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); - - @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 = element.getElement().getName(); - objectName = element.getElement().getName(); - refObject = this.base.getComplexType(refTypeName); - if (refObject == null) this.base.getSimpleType(refTypeName); - } else { - refTypeName = element.getElement().getRef(); - if (refName == null) - refName = refTypeName.getLocalPart(); - 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); - 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??"); - } - - if (refObject != null) { - secondaryClassName = getName(refObject); - } - 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 = attrs.getName(); - - 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); - } - } - -} +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 ruleClassNames = new ArrayList(); + + String ontShort = "ONT"; + String name; + + File importParserDir; + String elementPackageName; + + Map writers = new HashMap(); + + 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 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 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); + } + } + +} diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java index 74bafb9..aea2ff0 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyGenerator.java @@ -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 children = new HashSet<>(); + Deque 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()+ " 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 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 elements) { if (elements.size() == 1) { @@ -973,7 +985,7 @@ public final class SchemaConversionBase { else if (elements.size() > 0 && elements.size() <= 3) { List names = new ArrayList(); for (SchemaElement e : elements) { - String name = getElementName(e.getElement()); + String name = getName(e.getElement()); if (name != null) names.add(name); } diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConverter.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConverter.java index adab7f2..660b990 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConverter.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConverter.java @@ -7,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 schemaNSMap; private MapList shortNameMap; - String schemaNs; - String ontologyUri; - String className; - String name; - String shortName; + protected String schemaNs; + protected String ontologyUri; + protected String className; + protected String name; + protected String shortName; - SchemaConversionBase base; + protected SchemaConversionBase base; private ManualSchemaFileImport fileImport; @@ -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 processed = new HashSet<>(); + return _getRoot(processed); + } + + protected SchemaConverter _getRoot(Set processed) { + if (processed.contains(this)) + return null; + if (this.fileMap != null) + return this; + processed.add(this); + + for (SchemaConverter sc : this.parent) { + if (sc.fileMap != null) + return sc; + } + 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 getParent() { + return parent; + } + + public List getSubConverters() { + return subConverters; + } + public boolean isPrimary() { - return getRoot() == this; + return true; +// if (getRoot() == this) +// return true; +// List conv = new ArrayList<>(getRoot().fileMap.values()); +// int current = conv.indexOf(this); + } public String getShortName(String namespaceURI) { diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaObject.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaObject.java index 4138222..53f9da9 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaObject.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaObject.java @@ -1,136 +1,168 @@ -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 SchemaObject parent; - private ObjectType type; - private OpenAttrs obj; - private Rename rename; - - public SchemaObject(Element element) { - this(null,element); - } - - public void setRename(Rename rename) { - this.rename = rename; - } - - public SchemaObject(ComplexType complexType) { - this(null, complexType); - } - - public SchemaObject(SimpleType simpleType) { - this(null, simpleType); - } - - public SchemaObject(NamedGroup namedGroup) { - this(null, namedGroup); - } - - public SchemaObject(SchemaObject parent, Element element) { - this.parent = parent; - this.obj = element; - this.type = ObjectType.ELEMENT; - } - - public SchemaObject(SchemaObject parent, ComplexType complexType) { - this.parent = parent; - this.obj = complexType; - this.type = ObjectType.COMPLEX_TYPE; - } - - public SchemaObject(SchemaObject parent, AttributeGroup attributeGroup) { - this.parent = parent; - this.obj = attributeGroup; - this.type = ObjectType.ATTRIBUTE_GROUP; - } - - public SchemaObject(SchemaObject parent, NamedGroup namedGroup) { - this.parent = parent; - this.obj = namedGroup; - this.type = ObjectType.MODEL_GROUP; - } - - - public SchemaObject(SchemaObject parent, SimpleType simpleType) { - this.parent = parent; - this.obj = simpleType; - this.type = ObjectType.SIMPLE_TYPE; - } - - 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() { - 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; - } - -} +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; + } + +}