From 81ede25bbf4e80f6363c16fc13ecb1add3720acb Mon Sep 17 00:00:00 2001 From: luukkainen Date: Wed, 9 Nov 2016 14:41:24 +0000 Subject: [PATCH] Using XML ontology as base of converted XML schemas Date/Time literal implementations refs #6801 git-svn-id: https://www.simantics.org/svn/simantics/interoperability/trunk@33376 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../META-INF/MANIFEST.MF | 8 +- org.simantics.xml.sax.base/adapters.xml | 15 ++ org.simantics.xml.sax.base/build.properties | 3 +- .../xml/sax/base/AbstractImporter.java | 2 + .../datatypes/adapter/DateRelatedAdapter.java | 16 +++ .../adapter/DateTimeRelatedAdapter.java | 16 +++ .../datatypes/adapter/TimeRelatedAdapter.java | 17 +++ .../xml/sax/base/datatypes/adt/Date.java | 14 ++ .../xml/sax/base/datatypes/adt/DateTime.java | 19 +++ .../xml/sax/base/datatypes/adt/Time.java | 16 +++ .../xml/sax/base/datatypes/literal/Date.java | 103 ++++++++++++++ .../sax/base/datatypes/literal/DateTime.java | 134 ++++++++++++++++++ .../xml/sax/base/datatypes/literal/Time.java | 108 ++++++++++++++ .../build.properties | 3 +- .../simantics/xml/sax/ImporterGenerator.java | 101 ++++++++----- .../simantics/xml/sax/OntologyGenerator.java | 75 +++++----- .../xml/sax/SchemaConversionBase.java | 82 +++++++---- 17 files changed, 622 insertions(+), 110 deletions(-) create mode 100644 org.simantics.xml.sax.base/adapters.xml create mode 100644 org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adapter/DateRelatedAdapter.java create mode 100644 org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adapter/DateTimeRelatedAdapter.java create mode 100644 org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adapter/TimeRelatedAdapter.java create mode 100644 org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adt/Date.java create mode 100644 org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adt/DateTime.java create mode 100644 org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adt/Time.java create mode 100644 org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/Date.java create mode 100644 org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/DateTime.java create mode 100644 org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/Time.java diff --git a/org.simantics.xml.sax.base/META-INF/MANIFEST.MF b/org.simantics.xml.sax.base/META-INF/MANIFEST.MF index b846036..a27afd7 100644 --- a/org.simantics.xml.sax.base/META-INF/MANIFEST.MF +++ b/org.simantics.xml.sax.base/META-INF/MANIFEST.MF @@ -9,5 +9,9 @@ Require-Bundle: org.eclipse.core.runtime, org.simantics.db;bundle-version="1.1.0", org.simantics.db.common;bundle-version="1.1.0", org.simantics.db.layer0;bundle-version="1.1.0", - org.simantics.message;bundle-version="1.1.0" -Export-Package: org.simantics.xml.sax.base + org.simantics.message;bundle-version="1.1.0", + org.simantics.xml.sax.ontology;bundle-version="1.0.0" +Export-Package: org.simantics.xml.sax.base, + org.simantics.xml.sax.base.datatypes.adapter, + org.simantics.xml.sax.base.datatypes.adt, + org.simantics.xml.sax.base.datatypes.literal diff --git a/org.simantics.xml.sax.base/adapters.xml b/org.simantics.xml.sax.base/adapters.xml new file mode 100644 index 0000000..e697ba7 --- /dev/null +++ b/org.simantics.xml.sax.base/adapters.xml @@ -0,0 +1,15 @@ + + + + + \ No newline at end of file diff --git a/org.simantics.xml.sax.base/build.properties b/org.simantics.xml.sax.base/build.properties index 41eb6ad..d7586c2 100644 --- a/org.simantics.xml.sax.base/build.properties +++ b/org.simantics.xml.sax.base/build.properties @@ -1,4 +1,5 @@ source.. = src/ output.. = bin/ bin.includes = META-INF/,\ - . + .,\ + adapters.xml diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/AbstractImporter.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/AbstractImporter.java index 9e0d57d..f175fc4 100644 --- a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/AbstractImporter.java +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/AbstractImporter.java @@ -57,6 +57,8 @@ public abstract class AbstractImporter { @Override public void perform(WriteGraph graph) throws DatabaseException { + Layer0Utils.addCommentMetadata(graph, "Import file " + file.getName()); + graph.markUndoPoint(); result = doImport(graph); } diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adapter/DateRelatedAdapter.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adapter/DateRelatedAdapter.java new file mode 100644 index 0000000..36594af --- /dev/null +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adapter/DateRelatedAdapter.java @@ -0,0 +1,16 @@ +package org.simantics.xml.sax.base.datatypes.adapter; + +import org.simantics.db.ReadGraph; +import org.simantics.db.RelationContext; +import org.simantics.db.Resource; +import org.simantics.db.common.adaption.SimpleContextualAdapter; +import org.simantics.db.exception.DatabaseException; +import org.simantics.xml.sax.base.datatypes.adt.Date; + +public class DateRelatedAdapter extends SimpleContextualAdapter { + + @Override + public Date adapt(ReadGraph g, Resource source, RelationContext context) throws DatabaseException { + return g.getValue(context.getStatement().getObject(), org.simantics.xml.sax.base.datatypes.literal.Date.BINDING); + } +} diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adapter/DateTimeRelatedAdapter.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adapter/DateTimeRelatedAdapter.java new file mode 100644 index 0000000..7b5a167 --- /dev/null +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adapter/DateTimeRelatedAdapter.java @@ -0,0 +1,16 @@ +package org.simantics.xml.sax.base.datatypes.adapter; + +import org.simantics.db.ReadGraph; +import org.simantics.db.RelationContext; +import org.simantics.db.Resource; +import org.simantics.db.common.adaption.SimpleContextualAdapter; +import org.simantics.db.exception.DatabaseException; +import org.simantics.xml.sax.base.datatypes.adt.DateTime; + +public class DateTimeRelatedAdapter extends SimpleContextualAdapter { + + @Override + public DateTime adapt(ReadGraph g, Resource source, RelationContext context) throws DatabaseException { + return g.getValue(context.getStatement().getObject(), org.simantics.xml.sax.base.datatypes.literal.DateTime.BINDING); + } +} diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adapter/TimeRelatedAdapter.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adapter/TimeRelatedAdapter.java new file mode 100644 index 0000000..487ca24 --- /dev/null +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adapter/TimeRelatedAdapter.java @@ -0,0 +1,17 @@ +package org.simantics.xml.sax.base.datatypes.adapter; + +import org.simantics.db.ReadGraph; +import org.simantics.db.RelationContext; +import org.simantics.db.Resource; +import org.simantics.db.common.adaption.SimpleContextualAdapter; +import org.simantics.db.exception.DatabaseException; +import org.simantics.xml.sax.base.datatypes.adt.Time; + +public class TimeRelatedAdapter extends SimpleContextualAdapter { + + + @Override + public Time adapt(ReadGraph g, Resource source, RelationContext context) throws DatabaseException { + return g.getValue(context.getStatement().getObject(), org.simantics.xml.sax.base.datatypes.literal.Time.BINDING); + } +} diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adt/Date.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adt/Date.java new file mode 100644 index 0000000..9bd392d --- /dev/null +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adt/Date.java @@ -0,0 +1,14 @@ +package org.simantics.xml.sax.base.datatypes.adt; + +public interface Date { + + public java.util.Date getJavaDate(); + + public int getYear(); + public int getMonth(); + public int getDate(); + public int getTimezoneOffset(); + + public int compare(Date date); + +} diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adt/DateTime.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adt/DateTime.java new file mode 100644 index 0000000..57e32d6 --- /dev/null +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adt/DateTime.java @@ -0,0 +1,19 @@ +package org.simantics.xml.sax.base.datatypes.adt; + +import java.util.Date; + +public interface DateTime { + + public Date getJavaDate(); + + public int getYear(); + public int getMonth(); + public int getDate(); + public int getHours(); + public int getMinutes(); + public int getSeconds(); + public int getTimezoneOffset(); + + public int compare(DateTime dateTime); + +} diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adt/Time.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adt/Time.java new file mode 100644 index 0000000..5f86675 --- /dev/null +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/adt/Time.java @@ -0,0 +1,16 @@ +package org.simantics.xml.sax.base.datatypes.adt; + +import java.util.Date; + +public interface Time { + + public Date getJavaDate(); + + public int getHours(); + public int getMinutes(); + public int getSeconds(); + public int getTimezoneOffset(); + + public int compare(Time time); + +} diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/Date.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/Date.java new file mode 100644 index 0000000..8f0f2c2 --- /dev/null +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/Date.java @@ -0,0 +1,103 @@ +package org.simantics.xml.sax.base.datatypes.literal; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Locale; +import java.util.TimeZone; + +import org.simantics.databoard.Bindings; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.util.Bean; + +public class Date extends Bean implements org.simantics.xml.sax.base.datatypes.adt.Date { + + public static final Binding BINDING = Bindings.getBindingUnchecked(Date.class); + + public int year; + public byte month; + public byte day; + public int offset; + + public Date(){ + super(BINDING); + } + + public Date(int year, int month, int day) { + this(year,month,day, java.lang.Integer.MIN_VALUE); + } + + public Date(int year, int month, int day, int offset) { + super(BINDING); + if (month <= 0 || month > 12) throw new IllegalArgumentException("Month must be between 1 - 12, got " + month); + if (day <= 0 || day > 31) throw new IllegalArgumentException("Day must be between 1 - 31, got " + day); + this.year = year; + this.month = (byte)month; + this.day = (byte)day; + this.offset = offset; + } + + @Override + public int getYear() { + return year; + } + + @Override + public int getMonth() { + return month; + } + + @Override + public int getDate() { + return day; + } + + @Override + public int getTimezoneOffset() { + return offset; + } + + @Override + public java.util.Date getJavaDate() { + Calendar c = Calendar.getInstance(); + c.set(year, month, day); + if (offset != Integer.MIN_VALUE) { + c.setTimeZone(TimeZone.getTimeZone(TimeZone.getAvailableIDs(offset*60*1000)[0])); + } + return c.getTime(); + } + + @Override + public int compare(org.simantics.xml.sax.base.datatypes.adt.Date o) { + if (getYear() != o.getYear()) + return getYear() - o.getYear(); + if (getMonth() != o.getMonth()) + return getMonth() - o.getMonth(); + if (getDate() != o.getDate()) + return getDate() - o.getDate(); + return 0; + } + + public static Date parseDate(String dateString) { + String[] formats = new String[]{"yyyy-MM-ddXXX", "yyyy-MM-dd"}; + boolean[] timezone = new boolean[]{true, false}; + for (int i = 0; i< formats.length; i++) { + String format = formats[i]; + try { + SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.ROOT); + java.util.Date n = dateFormat.parse(dateString); + if (timezone[i]) { + return new Date(n.getYear()+1900, n.getMonth()+1, n.getDate(), n.getTimezoneOffset()); + } + return new Date(n.getYear()+1900, n.getMonth()+1, n.getDate()); + } catch (ParseException e) { + + } + } + + throw new IllegalArgumentException("Time is not in proper format " + dateString); + + } + + +} diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/DateTime.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/DateTime.java new file mode 100644 index 0000000..ba50b20 --- /dev/null +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/DateTime.java @@ -0,0 +1,134 @@ +package org.simantics.xml.sax.base.datatypes.literal; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.TimeZone; + +import org.simantics.databoard.Bindings; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.util.Bean; + +public class DateTime extends Bean implements org.simantics.xml.sax.base.datatypes.adt.DateTime { + + public static final Binding BINDING = Bindings.getBindingUnchecked(DateTime.class); + + public int year; + public byte month; + public byte day; + public byte hours; + public byte minutes; + public byte seconds; + public int offset; + + public DateTime(){ + super(BINDING); + } + + public DateTime(int year, int month, int day, int hours, int minutes, int seconds) { + this(year, month, day, hours, minutes, seconds, java.lang.Integer.MIN_VALUE); + } + + public DateTime(int year, int month, int day, int hours, int minutes, int seconds, int offset) { + super(BINDING); + if (month <= 0 || month > 12) throw new IllegalArgumentException("Month must be between 1 - 12, got " + month); + if (day <= 0 || day > 31) throw new IllegalArgumentException("Day must be between 1 - 31, got " + day); + if (hours < 0 || hours > 23) throw new IllegalArgumentException("Hours must be between 0 - 23, got " + hours); + if (minutes < 0 || minutes > 59) throw new IllegalArgumentException("Minutes must be between 0 - 59, got " + minutes); + if (seconds < 0 || seconds > 59) throw new IllegalArgumentException("Seconds must be between 0 - 59, got " + seconds); + this.year = year; + this.month = (byte)month; + this.day = (byte)day; + this.hours = (byte)hours; + this.minutes = (byte)minutes; + this.seconds = (byte)seconds; + this.offset = offset; + } + + @Override + public int getYear() { + return year; + } + + @Override + public int getMonth() { + return month; + } + + @Override + public int getDate() { + return day; + } + + @Override + public int getHours() { + return hours; + } + + @Override + public int getMinutes() { + return minutes; + } + + @Override + public int getSeconds() { + return seconds; + } + + @Override + public int getTimezoneOffset() { + return offset; + } + + @Override + public Date getJavaDate() { + Calendar c = Calendar.getInstance(); + c.set(year, month, day, hours, minutes, seconds); + if (offset != Integer.MIN_VALUE) { + c.setTimeZone(TimeZone.getTimeZone(TimeZone.getAvailableIDs(offset*60*1000)[0])); + } + return c.getTime(); + } + + @Override + public int compare(org.simantics.xml.sax.base.datatypes.adt.DateTime o) { + // FIXME: take account time offset + if (getYear() != o.getYear()) + return getYear() - o.getYear(); + if (getMonth() != o.getMonth()) + return getMonth() - o.getMonth(); + if (getDate() != o.getDate()) + return getDate() - o.getDate(); + if (getHours() != o.getHours()) + return getHours() - o.getHours(); + if (getMinutes() != o.getMinutes()) + return getMinutes() - o.getMinutes(); + if (getSeconds() != o.getSeconds()) + return getSeconds() - o.getSeconds(); + return 0; + } + + public static DateTime parseDateTime(String dateString) { + // FIXME: XML specification allows any number of second decimals! + // FIXME: endOfDayFrag handling is missing. + String[] formats = new String[]{"yyyy-MM-dd'T'HH:mm:ssXXX", "yyyy-MM-dd'T'HH:mm:ssSSSSSSSXXX","yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ssSSSSSSS"}; + boolean[] timezone = new boolean[]{true, true,false,false}; + for (int i = 0; i< formats.length; i++) { + String format = formats[i]; + try { + SimpleDateFormat dateFormat = new SimpleDateFormat(format); + java.util.Date n = dateFormat.parse(dateString); + if (timezone[i]) { + return new DateTime(n.getYear()+1900, n.getMonth()+1, n.getDate(), n.getHours(),n.getMinutes(), n.getSeconds(), n.getTimezoneOffset()); + } + return new DateTime(n.getYear()+1900, n.getMonth()+1, n.getDate(), n.getHours(),n.getMinutes(), n.getSeconds()); + } catch (ParseException e) { + + } + } + + throw new IllegalArgumentException("Time is not in proper format " + dateString); + } + +} diff --git a/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/Time.java b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/Time.java new file mode 100644 index 0000000..03b3911 --- /dev/null +++ b/org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/datatypes/literal/Time.java @@ -0,0 +1,108 @@ +package org.simantics.xml.sax.base.datatypes.literal; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.TimeZone; + +import org.simantics.databoard.Bindings; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.util.Bean; + +public class Time extends Bean implements org.simantics.xml.sax.base.datatypes.adt.Time { + + public static final Binding BINDING = Bindings.getBindingUnchecked(Time.class); + + public byte hours; + public byte minutes; + public byte seconds; + public int offset; + + + public Time(){ + super(BINDING); + } + + public Time(int hours, int minutes, int seconds) { + this(hours,minutes,seconds, java.lang.Integer.MIN_VALUE); + } + + public Time(int hours, int minutes, int seconds, int offset) { + super(BINDING); + if (hours < 0 || hours > 23) throw new IllegalArgumentException("Hours must be between 0 - 23, got " + hours); + if (minutes < 0 || minutes > 59) throw new IllegalArgumentException("Minutes must be between 0 - 59, got " + minutes); + if (seconds < 0 || seconds > 59) throw new IllegalArgumentException("Seconds must be between 0 - 59, got " + seconds); + this.hours = (byte)hours; + this.minutes = (byte)minutes; + this.seconds = (byte)seconds; + this.offset = offset; + } + + @Override + public int getHours() { + return hours; + } + + @Override + public int getMinutes() { + return minutes; + } + + @Override + public int getSeconds() { + return seconds; + } + + @Override + public int getTimezoneOffset() { + return offset; + } + + @Override + public Date getJavaDate() { + Calendar c = Calendar.getInstance(); + c.set(Calendar.HOUR_OF_DAY, hours); + c.set(Calendar.MINUTE, minutes); + c.set(Calendar.SECOND, seconds); + if (offset != Integer.MIN_VALUE) { + c.setTimeZone(TimeZone.getTimeZone(TimeZone.getAvailableIDs(offset*60*1000)[0])); + } + return c.getTime(); + } + + @Override + public int compare(org.simantics.xml.sax.base.datatypes.adt.Time o) { + // FIXME: take account time offset + if (getHours() != o.getHours()) + return getHours() - o.getHours(); + if (getMinutes() != o.getMinutes()) + return getMinutes() - o.getMinutes(); + if (getSeconds() != o.getSeconds()) + return getSeconds() - o.getSeconds(); + return 0; + } + + public static Time parseTime(String timeString) { + // FIXME: XML specification allows any number of second decimals! + String[] formats = new String[]{"HH:mm:ss.SSSSSSSXXX","HH:mm:ss.SSSSSSS","HH:mm:ss.SSXXX","HH:mm:ss.SS","HH:mm:ssXXX", "HH:mm:ss"}; + boolean[] timezone = new boolean[]{true, false,true,false,true,false}; + for (int i = 0; i< formats.length; i++) { + String format = formats[i]; + try { + SimpleDateFormat dateFormat = new SimpleDateFormat(format); + Date n = dateFormat.parse(timeString); + if (timezone[i]) { + return new Time(n.getHours(), n.getMinutes(), n.getSeconds(), n.getTimezoneOffset()); + } + return new Time(n.getHours(), n.getMinutes(), n.getSeconds()); + } catch (ParseException e) { + + } + } + + throw new IllegalArgumentException("Time is not in proper format " + timeString); + + } + +} diff --git a/org.simantics.xml.sax.ontology/build.properties b/org.simantics.xml.sax.ontology/build.properties index 41eb6ad..ecdc7c3 100644 --- a/org.simantics.xml.sax.ontology/build.properties +++ b/org.simantics.xml.sax.ontology/build.properties @@ -1,4 +1,5 @@ source.. = src/ output.. = bin/ bin.includes = META-INF/,\ - . + .,\ + graph.tg 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 c312ab8..f3279e2 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 @@ -113,36 +113,45 @@ public class ImporterGenerator extends SchemaConversionBase{ return writer; } - protected String getValueGetter(String binding,String name) { +// protected String getValueGetter(String binding,String name) { +// if (binding == null) +// return name+".getValue()"; +// if ("STRING".equals(binding)) +// return name+".getValue()"; +// if ("BOOLEAN".equals(binding)) +// return "Boolean.parseBoolean("+name+".getValue())"; +// if ("INTEGER".equals(binding)) +// return "Integer.parseInt("+name+".getValue())"; +// if ("DOUBLE".equals(binding)) +// return "Double.parseDouble("+name+".getValue())"; +// if ("FLOAT".equals(binding)) +// return "Float.parseFloat("+name+".getValue())"; +// return name+".getValue()"; +// } +// protected String getValueGetter(String binding) { +// if (binding == null) +// return "value"; +// if ("STRING".equals(binding)) +// return "value"; +// if ("BOOLEAN".equals(binding)) +// return "Boolean.parseBoolean(value)"; +// if ("INTEGER".equals(binding)) +// return "Integer.parseInt(value)"; +// if ("DOUBLE".equals(binding)) +// return "Double.parseDouble(value)"; +// if ("FLOAT".equals(binding)) +// return "Float.parseFloat(value)"; +// return "value"; +// } + protected String getValueGetter(TypeEntry binding,String name) { if (binding == null) return name+".getValue()"; - if ("STRING".equals(binding)) - return name+".getValue()"; - if ("BOOLEAN".equals(binding)) - return "Boolean.parseBoolean("+name+".getValue())"; - if ("INTEGER".equals(binding)) - return "Integer.parseInt("+name+".getValue())"; - if ("DOUBLE".equals(binding)) - return "Double.parseDouble("+name+".getValue())"; - if ("FLOAT".equals(binding)) - return "Float.parseFloat("+name+".getValue())"; - return name+".getValue()"; + return binding.getValueGetter(name); } - - protected String getValueGetter(String binding) { + protected String getValueGetter(TypeEntry binding) { if (binding == null) return "value"; - if ("STRING".equals(binding)) - return "value"; - if ("BOOLEAN".equals(binding)) - return "Boolean.parseBoolean(value)"; - if ("INTEGER".equals(binding)) - return "Integer.parseInt(value)"; - if ("DOUBLE".equals(binding)) - return "Double.parseDouble(value)"; - if ("FLOAT".equals(binding)) - return "Float.parseFloat(value)"; - return "value"; + return binding.getValueGetter(); } protected void handle(TopLevelAttribute topLevelAttribute) { @@ -363,10 +372,13 @@ public class ImporterGenerator extends SchemaConversionBase{ if (useOriginalList(parent, indicator,element, reference, ref, refType)) { // generic list fw.delayedWriter.println(" {"); - fw.delayedWriter.println(" Resource list = graph.getPossibleObject(element.getData(),"+ontShort+"XML_hasOriginalElementList);"); + //fw.delayedWriter.println(" Resource list = graph.getPossibleObject(element.getData(),"+ontShort+"XML_hasOriginalElementList);"); + fw.delayedWriter.println(" XMLResource XML = XMLResource.getInstance(graph);"); + fw.delayedWriter.println(" Resource list = graph.getPossibleObject(element.getData(), XML.hasOriginalElementList);"); fw.delayedWriter.println(" if (list == null) {"); fw.delayedWriter.println(" list = org.simantics.db.common.utils.ListUtils.create(graph, java.util.Collections.singletonList(child.getData()));"); - fw.delayedWriter.println(" graph.claim(element.getData(),"+ontShort+"XML_hasOriginalElementList,list);"); + //fw.delayedWriter.println(" graph.claim(element.getData(),"+ontShort+"XML_hasOriginalElementList,list);"); + fw.delayedWriter.println(" graph.claim(element.getData(), XML.hasOriginalElementList,list);"); fw.delayedWriter.println(" } else {"); fw.delayedWriter.println(" org.simantics.db.common.utils.ListUtils.insertBack(graph, list, java.util.Collections.singletonList(child.getData()));"); fw.delayedWriter.println(" }"); @@ -381,7 +393,7 @@ public class ImporterGenerator extends SchemaConversionBase{ fw.delayedWriter2.println(" public static class " + getName(parent) +"_" +ref+" extends org.simantics.xml.sax.base.ValueElementParser {"); fw.delayedWriter2.println(" "+ getName(parent) +"_" +ref +"(){"); - fw.delayedWriter2.println(" super(\""+ref+"\"," +this.ontologyClassName+".URIs."+getName(parent) + "_has"+ref+", Bindings."+binding+");"); + fw.delayedWriter2.println(" super(\""+ref+"\"," +this.ontologyClassName+".URIs."+getName(parent) + "_has"+ref+", "+binding+");"); fw.delayedWriter2.println(" }"); fw.delayedWriter2.println(" }"); } @@ -427,10 +439,13 @@ public class ImporterGenerator extends SchemaConversionBase{ if (useOriginalList(parent, indicator,element, reference, ref, new QName(obj.getName()))) { // generic list fw.delayedWriter.println(" {"); - fw.delayedWriter.println(" Resource list = graph.getPossibleObject(element.getData(),"+ontShort+"XML_hasOriginalElementList);"); + //fw.delayedWriter.println(" Resource list = graph.getPossibleObject(element.getData(),"+ontShort+"XML_hasOriginalElementList);"); + fw.delayedWriter.println(" XMLResource XML = XMLResource.getInstance(graph);"); + fw.delayedWriter.println(" Resource list = graph.getPossibleObject(element.getData(), XML.hasOriginalElementList);"); fw.delayedWriter.println(" if (list == null) {"); fw.delayedWriter.println(" list = org.simantics.db.common.utils.ListUtils.create(graph, java.util.Collections.singletonList(child.getData()));"); - fw.delayedWriter.println(" graph.claim(element.getData(),"+ontShort+"XML_hasOriginalElementList,list);"); + //fw.delayedWriter.println(" graph.claim(element.getData(),"+ontShort+"XML_hasOriginalElementList,list);"); + fw.delayedWriter.println(" graph.claim(element.getData(), XML.hasOriginalElementList,list);"); fw.delayedWriter.println(" } else {"); fw.delayedWriter.println(" org.simantics.db.common.utils.ListUtils.insertBack(graph, list, java.util.Collections.singletonList(child.getData()));"); fw.delayedWriter.println(" }"); @@ -502,7 +517,8 @@ public class ImporterGenerator extends SchemaConversionBase{ FileWriter fw = getWriter(parent); if (primitiveType != null) { - String binding = getBindingFromPrimitiveType(primitiveType); + //String binding = getBindingFromPrimitiveType(primitiveType); + TypeEntry binding = getTypeEntry(primitiveType); if (binding != null) { writeAttribute(fw, attrName, relationName, binding, isReference); @@ -522,21 +538,24 @@ public class ImporterGenerator extends SchemaConversionBase{ QName base = restriction.getBase(); - String binding = getBindingFromPrimitiveType(base); + //String binding = getBindingFromPrimitiveType(base); + TypeEntry binding = getTypeEntry(base); writeAttribute(fw, attrName, relationName, binding, isReference); } else { // TODO : using default String attribute should be configured with rules. //throw new RuntimeException("Cannot resolve type for Attribute " + attrName + " -> " + primitiveType.getLocalPart()); fw.writer.println(" //FIXME: Cannot resolve type for Attribute " + attrName + " Using default type String"); - writeAttribute(fw, attrName, relationName, "STRING", isReference); + //writeAttribute(fw, attrName, relationName, "STRING", isReference); + writeAttribute(fw, attrName, relationName, getTypeEntry("string"), isReference); } } - private void writeAttribute(FileWriter fw, String attrName, String relationName, String binding, boolean isReference) { + //private void writeAttribute(FileWriter fw, String attrName, String relationName, String binding, boolean isReference) { + private void writeAttribute(FileWriter fw, String attrName, String relationName, TypeEntry binding, boolean isReference) { fw.writer.println(" {"); fw.writer.println(" Attribute a = element.getAttribute(\"" +attrName+"\");"); fw.writer.println(" if (a != null) {"); - fw.writer.println(" graph.claimLiteral(element.getData(),"+relationName+","+getValueGetter(binding,"a")+", Bindings."+binding+");"); + fw.writer.println(" graph.claimLiteral(element.getData(),"+relationName+","+getValueGetter(binding,"a")+", "+binding.binding+");"); if (isReference) fw.writer.println(" idProviderValue = a.getValue();"); fw.writer.println(" }"); @@ -555,10 +574,12 @@ public class ImporterGenerator extends SchemaConversionBase{ QName base = restriction.getBase(); - String binding = getBindingFromPrimitiveType(base); + //String binding = getBindingFromPrimitiveType(base); + TypeEntry binding = getTypeEntry(base); fw.writer.println(" @Override"); fw.writer.println(" public void configure(WriteGraph graph, Element element, java.lang.String value) throws DatabaseException {"); - fw.writer.println(" graph.claimValue(element.getData(),"+getValueGetter(binding)+", Bindings."+binding+");"); + //fw.writer.println(" graph.claimValue(element.getData(),"+getValueGetter(binding)+", Bindings."+binding+");"); + fw.writer.println(" graph.claimValue(element.getData(),"+getValueGetter(binding)+", "+binding.binding +");"); fw.writer.println(" }"); } @@ -616,7 +637,8 @@ public class ImporterGenerator extends SchemaConversionBase{ if (defaultValue == null) defaultValue = getDefaultValue(atype); - String binding = getBindingFromPrimitiveType(atype); + //String binding = getBindingFromPrimitiveType(atype); + TypeEntry binding = getTypeEntry(atype); if (i > 0) fw.writer.print(","); if (defaultValue != null) @@ -625,7 +647,7 @@ public class ImporterGenerator extends SchemaConversionBase{ fw.writer.print(getValueGetter(binding,"a"+(i++))); } fw.writer.println("};"); - fw.writer.println(" graph.claimLiteral(element.getData(),"+relationName+", value, Bindings."+arrayBinding+");"); + fw.writer.println(" graph.claimLiteral(element.getData(),"+relationName+", value, "+arrayBinding+");"); fw.writer.println(" }"); } @@ -911,6 +933,7 @@ public class ImporterGenerator extends SchemaConversionBase{ writer.println("import org.simantics.db.exception.DatabaseException;"); writer.println("import org.simantics.xml.sax.base.Attribute;"); writer.println("import org.simantics.xml.sax.base.Element;"); + writer.println("import org.simantics.xml.sax.ontology.XMLResource;"); if (!isList) { writer.println("import org.simantics.layer0.Layer0;"); } else { 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 0b7aa3c..5ecb02e 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 @@ -109,6 +109,7 @@ public class OntologyGenerator extends SchemaConversionBase { } writer.println(); writer.println("L0 = "); + writer.println("XML = "); writer.println(); writer.println(ontRoot + " = <" + ontologyName +"-"+ version+"> : L0.Ontology"); writer.println(" @L0.new"); @@ -118,24 +119,24 @@ public class OntologyGenerator extends SchemaConversionBase { ontRoot += "."; // TODO : these could be created in separate base ontology. - writer.println(commentTag + " Built-in types"); - writer.println(); - writer.println(ontRoot+"XML : L0.Library"); - writer.println(ontRoot+"XML.hasAttribute L0.String"); - writer.println(ontRoot+"XML.ComplexType L0.String"); +// writer.println(ontRoot+"XML.ComplexType " + ontRoot+getName(obj)); } else { - writer.println(ontRoot+getName(parent)+".has"+refName + " " + getType(refType)); } if (useElementList(parent, indicator,element, reference, refName, refType)) { if (type == null) { - writer.println(ontRoot+getName(parent)+"."+refName + "List " + ontRoot+getName(obj)); if (useElementList(parent, indicator,element, reference, refName, new QName(obj.getName()))) { - writer.println(ontRoot+getName(parent)+"."+refName + "List " + ontType); @@ -427,12 +428,12 @@ public class OntologyGenerator extends SchemaConversionBase { String relationName = ontRoot+getName(topLevelComplexType,"has");//ontRoot+"has"+name; - writer.println(relationName+ " " + ontRoot+getComplexTypePrefix()+name); writer.println(" --> " + ontRoot+name); writer.println(); - String baseType = ontRoot+"XML.ComplexType"; + String baseType = "XML.ComplexType"; QName base = getComplexTypeBase(topLevelComplexType.getComplexType()); if (base != null) { @@ -459,7 +460,7 @@ public class OntologyGenerator extends SchemaConversionBase { // if (parent != null) // name = parent +"_"+name; - String type = ontRoot+"XML.Element"; + String type = "XML.Element"; Set types = new LinkedHashSet(); if (element.getType() != null) { types.add(getType(element.getType())); @@ -492,8 +493,8 @@ public class OntologyGenerator extends SchemaConversionBase { // //relationName = ontRoot+getComplexTypePrefix()+"has"+name.substring(getComplexTypePrefix().length()); // relationName = ontRoot+getName(elementObj.getParent()) + "has"+element.getName(); // } - writer.println(relationName+ " references = getIDReferences(element); for (IDReference ref : references) { - writer.println(ontRoot+name+"."+ref.getReference().getName()+ " l0Types = new HashMap(); typeMap.put(CONVERSION_NS, l0Types); - schemaTypes.put("string", new TypeEntry("L0.String", "STRING", "String", "")); - schemaTypes.put("NMTOKEN", new TypeEntry("L0.String", "STRING", "String", "")); - schemaTypes.put("token", new TypeEntry("L0.String", "STRING", "String", "")); - schemaTypes.put("ID", new TypeEntry("L0.String", "STRING", "String", "",true)); - schemaTypes.put("IDREF", new TypeEntry("L0.String", "STRING", "String", "")); - schemaTypes.put("date", new TypeEntry("L0.String", "STRING", "String", "")); - schemaTypes.put("time", new TypeEntry("L0.String", "STRING", "String", "")); - schemaTypes.put("anyURI", new TypeEntry("L0.URI", "STRING", "String", "")); - schemaTypes.put("double", new TypeEntry("L0.Double", "DOUBLE", "double", "Double.NaN")); - schemaTypes.put("float", new TypeEntry("L0.Float", "FLOAT", "float", "Float.NaN")); - schemaTypes.put("decimal", new TypeEntry("L0.Double", "DOUBLE", "double", "Double.NaN")); - schemaTypes.put("boolean", new TypeEntry("L0.Boolean", "BOOLEAN", "boolean", "false")); - schemaTypes.put("integer", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); - schemaTypes.put("positiveInteger", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); - schemaTypes.put("nonPositiveInteger", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); - schemaTypes.put("nonNegativeInteger", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); - schemaTypes.put("negativeInteger", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); - schemaTypes.put("unsignedInt", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); - schemaTypes.put("int", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); - schemaTypes.put("short", new TypeEntry("L0.Integer", "INTEGER", "int", "0")); - schemaTypes.put("unsignedShort",new TypeEntry("L0.Integer", "INTEGER", "int", "0")); - schemaTypes.put("byte", new TypeEntry("L0.Byte", "BYTE", "byte", "0")); - schemaTypes.put("unsignedByte", new TypeEntry("L0.Byte", "BYTE", "byte", "0")); - schemaTypes.put("long", new TypeEntry("L0.Long", "LONG", "long", "0")); - schemaTypes.put("unsignedLong", new TypeEntry("L0.Long", "LONG", "long", "0")); + schemaTypes.put("string", new TypeEntry("L0.String", "Bindings.STRING", "String", "","","")); + schemaTypes.put("NMTOKEN", new TypeEntry("L0.String", "Bindings.STRING", "String", "","","")); + schemaTypes.put("token", new TypeEntry("L0.String", "Bindings.STRING", "String", "","","")); + schemaTypes.put("ID", new TypeEntry("L0.String", "Bindings.STRING", "String", "","","",true)); + schemaTypes.put("IDREF", new TypeEntry("L0.String", "Bindings.STRING", "String", "","","")); + schemaTypes.put("date", new TypeEntry("XML.Date", "org.simantics.xml.sax.base.datatypes.literal.Date.BINDING", "org.simantics.xml.sax.base.datatypes.literal.Date", "","org.simantics.xml.sax.base.datatypes.literal.Date.parseDate(",")")); + schemaTypes.put("time", new TypeEntry("XML.Time", "org.simantics.xml.sax.base.datatypes.literal.Time.BINDING", "org.simantics.xml.sax.base.datatypes.literal.Time", "","org.simantics.xml.sax.base.datatypes.literal.Time.parseTime(",")")); + schemaTypes.put("dateTime", new TypeEntry("XML.DateTime", "org.simantics.xml.sax.base.datatypes.literal.DateTime.BINDING", "org.simantics.xml.sax.base.datatypes.literal.DateTime", "","org.simantics.xml.sax.base.datatypes.literal.DateTime.parseDateTime(",")")); + schemaTypes.put("anyURI", new TypeEntry("L0.URI", "Bindings.STRING", "String", "","","")); + schemaTypes.put("double", new TypeEntry("L0.Double", "Bindings.DOUBLE", "double", "Double.NaN","Double.parseDouble(",")")); + schemaTypes.put("float", new TypeEntry("L0.Float", "Bindings.FLOAT", "float", "Float.NaN","Float.parseFloat(",")")); + schemaTypes.put("decimal", new TypeEntry("L0.Double", "Bindings.DOUBLE", "double", "Double.NaN","Double.parseDouble(",")")); + schemaTypes.put("boolean", new TypeEntry("L0.Boolean", "Bindings.BOOLEAN", "boolean", "false","Boolean.parseBoolean(",")")); + schemaTypes.put("integer", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","Integer.parseInt(",")")); + schemaTypes.put("positiveInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","Integer.parseInt(",")")); + schemaTypes.put("nonPositiveInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","Integer.parseInt(",")")); + schemaTypes.put("nonNegativeInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","Integer.parseInt(",")")); + schemaTypes.put("negativeInteger", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","Integer.parseInt(",")")); + schemaTypes.put("unsignedInt", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","Integer.parseInt(",")")); + schemaTypes.put("int", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","Integer.parseInt(",")")); + schemaTypes.put("short", new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","Integer.parseInt(",")")); + schemaTypes.put("unsignedShort",new TypeEntry("L0.Integer", "Bindings.INTEGER", "int", "0","Integer.parseInt(",")")); + schemaTypes.put("byte", new TypeEntry("L0.Byte", "Bindings.BYTE", "byte", "0","Byte.parseByte(",")")); + schemaTypes.put("unsignedByte", new TypeEntry("L0.Byte", "Bindings.BYTE", "byte", "0","Byte.parseByte(",")")); + schemaTypes.put("long", new TypeEntry("L0.Long", "Bindings.LONG", "long", "0","Long.parseLong(",")")); + schemaTypes.put("unsignedLong", new TypeEntry("L0.Long", "Bindings.LONG", "long", "0","Long.parseLong(",")")); - l0Types.put("doubleArray", new TypeEntry("L0.DoubleArray", "DOUBLE_ARRAY", "double[]", null)); - l0Types.put("stringArray", new TypeEntry("L0.StringArray", "STRING_ARRAY", "string[]", null)); + l0Types.put("doubleArray", new TypeEntry("L0.DoubleArray", "Bindings.DOUBLE_ARRAY", "double[]", null,null,null)); + l0Types.put("stringArray", new TypeEntry("L0.StringArray", "Bindings.STRING_ARRAY", "string[]", null,null,null)); } @@ -101,7 +102,14 @@ public abstract class SchemaConversionBase { return null; TypeEntry entry = types.get(type.getLocalPart()); return entry; - + } + protected TypeEntry getTypeEntry(String type) { + for (Map types : typeMap.values()) { + TypeEntry entry = types.get(type); + if (entry != null) + return entry; + } + return null; } protected String getL0TypeFromPrimitiveType(QName primitiveType) { @@ -930,22 +938,36 @@ public abstract class SchemaConversionBase { String javaType; String defaultValue; boolean id; - public TypeEntry(String l0Type, String binding, String javaType, String defaultValue) { + String getterPrefix; + String getterPostfix; + public TypeEntry(String l0Type, String binding, String javaType, String defaultValue, String getterPrefix, String getterPostfix) { super(); this.l0Type = l0Type; this.binding = binding; this.javaType = javaType; this.defaultValue = defaultValue; this.id = false; + this.getterPrefix = getterPrefix; + this.getterPostfix = getterPostfix; + } - public TypeEntry(String l0Type, String binding, String javaType, String defaultValue, boolean id) { + public TypeEntry(String l0Type, String binding, String javaType, String defaultValue, String getterPrefix, String getterPostfix, boolean id) { super(); this.l0Type = l0Type; this.binding = binding; this.javaType = javaType; this.defaultValue = defaultValue; this.id = id; + this.getterPrefix = getterPrefix; + this.getterPostfix = getterPostfix; + } + + public String getValueGetter(String name) { + return getterPrefix + name + ".getValue()"+getterPostfix; + } + public String getValueGetter() { + return getterPrefix + "value"+getterPostfix; } } -- 2.47.1