Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » XMLTypeFactoryImpl doesn't support XSD compliant representations of infinity
XMLTypeFactoryImpl doesn't support XSD compliant representations of infinity [message #899277] Tue, 31 July 2012 06:12 Go to next message
Martin Weiss is currently offline Martin Weiss
Messages: 9
Registered: July 2012
Junior Member
Hello world,

I use EMF for some kind of technical data editor where i have to support infinite numeric values. XMLTypeFactoryImpl however doesn't support the schema compliant representations of infinite values INF and -INF, it uses Infinity and -Infinity instead: The XML generated by EMF is invalid according to schema rules.

The only solution I found so far is to get the XMLTypePackage from the global package registry and set a new custom XMLTypeFactory for it.

Is this a bug or do I miss something?
Re: XMLTypeFactoryImpl doesn't support XSD compliant representations of infinity [message #899428 is a reply to message #899277] Tue, 31 July 2012 14:55 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 25949
Registered: July 2009
Senior Member
Martin,

Comments below.
On 31/07/2012 12:12 PM, Martin Weiss wrote:
> Hello world,
>
> I use EMF for some kind of technical data editor where i have to
> support infinite numeric values. XMLTypeFactoryImpl however doesn't
> support the schema compliant representations of infinite values INF
> and -INF, it uses Infinity and -Infinity instead: The XML generated by
> EMF is invalid according to schema rules.
Hmmm, yes I fixed this in the XSD models literal conversion but I should
have fixed it in XMLTypeFactoryImpl as well (and I need to be careful
that I accept the old syntax so I don't break clients relying on the old
arguably broken behavior).
>
> The only solution I found so far is to get the XMLTypePackage from the
> global package registry and set a new custom XMLTypeFactory for it.
>
> Is this a bug or do I miss something?
Yes it looks like a bug. Please open a bugzilla.
Re: XMLTypeFactoryImpl doesn't support XSD compliant representations of infinity [message #899515 is a reply to message #899428] Wed, 01 August 2012 05:16 Go to previous messageGo to next message
Martin Weiss is currently offline Martin Weiss
Messages: 9
Registered: July 2012
Junior Member
Hi Ed,

I created a bug report. For anyone interested, here's my workaround. It still allows loading of documents that use the non-compliant representation but of course can't save them. Maybe a factory configuration option would be a way to retain the old behavior?

// ATTENTION: In order to support double or float infinity we have to replace the
// global default XMLTypePackageImpl in the registry with a custom implementation.
XMLTypePackage xmlTypePkg = (XMLTypePackage) EPackage.Registry.INSTANCE.get(XMLTypePackageImpl.eNS_URI);
xmlTypePkg.setEFactoryInstance(new XMLTypeFactoryImpl() {
	@Override
	public Double createDoubleObject(String literal) {
		if ("INF".equals(collapseWhiteSpace(literal))) {
			return Double.POSITIVE_INFINITY;
		}
		if ("-INF".equals(collapseWhiteSpace(literal))) {
			return Double.NEGATIVE_INFINITY;
		}
		return super.createDoubleObject(literal);
	}
	
	@Override
	public Float createFloatObject(String literal) {
		if ("INF".equals(collapseWhiteSpace(literal))) {
			return Float.POSITIVE_INFINITY;
		}
		if ("-INF".equals(collapseWhiteSpace(literal))) {
			return Float.NEGATIVE_INFINITY;
		}
		return super.createFloatObject(literal);
	}
			
	@Override
	public String convertDoubleToString(EDataType eDataType, Object instanceValue) {
		if (Double.valueOf(Double.POSITIVE_INFINITY).equals(instanceValue)) {
			return "INF";
		}
		if (Double.valueOf(Double.NEGATIVE_INFINITY).equals(instanceValue)) {
			return "-INF";
		}
		return super.convertDoubleToString(eDataType, instanceValue);
	}
	
	@Override
	public String convertFloatToString(EDataType eDataType, Object instanceValue) {
		if (Float.valueOf(Float.POSITIVE_INFINITY).equals(instanceValue)) {
			return "INF";
		}
		if (Float.valueOf(Float.NEGATIVE_INFINITY).equals(instanceValue)) {
			return "-INF";
		}
		return super.convertToString(eDataType, instanceValue);
	}
});

Re: XMLTypeFactoryImpl doesn't support XSD compliant representations of infinity [message #899759 is a reply to message #899515] Thu, 02 August 2012 04:44 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 25949
Registered: July 2009
Senior Member
Martin,

This looks good. The four calls to valueOf could be optimized by storing
that value as a static final constant...


On 01/08/2012 11:16 AM, Martin Weiss wrote:
> Hi Ed,
>
> I created a bug report. For anyone interested, here's my workaround.
> It still allows loading of documents that use the non-compliant
> representation but of course can't save them. Maybe a factory
> configuration option would be a way to retain the old behavior?
>
>
> // ATTENTION: In order to support double or float infinity we have to
> replace the
> // global default XMLTypePackageImpl in the registry with a custom
> implementation.
> XMLTypePackage xmlTypePkg = (XMLTypePackage)
> EPackage.Registry.INSTANCE.get(XMLTypePackageImpl.eNS_URI);
> xmlTypePkg.setEFactoryInstance(new XMLTypeFactoryImpl() {
> @Override
> public Double createDoubleObject(String literal) {
> if ("INF".equals(collapseWhiteSpace(literal))) {
> return Double.POSITIVE_INFINITY;
> }
> if ("-INF".equals(collapseWhiteSpace(literal))) {
> return Double.NEGATIVE_INFINITY;
> }
> return super.createDoubleObject(literal);
> }
>
> @Override
> public Float createFloatObject(String literal) {
> if ("INF".equals(collapseWhiteSpace(literal))) {
> return Float.POSITIVE_INFINITY;
> }
> if ("-INF".equals(collapseWhiteSpace(literal))) {
> return Float.NEGATIVE_INFINITY;
> }
> return super.createFloatObject(literal);
> }
>
> @Override
> public String convertDoubleToString(EDataType eDataType, Object
> instanceValue) {
> if
> (Double.valueOf(Double.POSITIVE_INFINITY).equals(instanceValue)) {
> return "INF";
> }
> if
> (Double.valueOf(Double.NEGATIVE_INFINITY).equals(instanceValue)) {
> return "-INF";
> }
> return super.convertDoubleToString(eDataType, instanceValue);
> }
>
> @Override
> public String convertFloatToString(EDataType eDataType, Object
> instanceValue) {
> if
> (Float.valueOf(Float.POSITIVE_INFINITY).equals(instanceValue)) {
> return "INF";
> }
> if
> (Float.valueOf(Float.NEGATIVE_INFINITY).equals(instanceValue)) {
> return "-INF";
> }
> return super.convertToString(eDataType, instanceValue);
> }
> });
>
>
Re: XMLTypeFactoryImpl doesn't support XSD compliant representations of infinity [message #900359 is a reply to message #899759] Mon, 06 August 2012 11:36 Go to previous messageGo to next message
Martin Weiss is currently offline Martin Weiss
Messages: 9
Registered: July 2012
Junior Member
Hi Ed,

Ed Merks wrote on Thu, 02 August 2012 04:44

The four calls to valueOf could be optimized by storing
that value as a static final constant...


great idea, thanks!

I noticed there are more conversion methods in XMLTypeFactory:
String convertFloat(float instanceValue);
String convertDouble(double instanceValue);
String convertFloatObject(Float instanceValue);
String convertDoubleObject(Double instanceValue);

It seems these are never called during loading or saving the model. What are these methods for? Do I have to override them, too?

[Updated on: Mon, 06 August 2012 11:37]

Report message to a moderator

Re: XMLTypeFactoryImpl doesn't support XSD compliant representations of infinity [message #900446 is a reply to message #900359] Tue, 07 August 2012 03:03 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 25949
Registered: July 2009
Senior Member
Martin,

XMLTypeFactory is generated with Data Type Converters set to true, so it
generates a full API for all data types, including the primitives. When
reflection is used, all values are passed around as objects, so
primitives are wrapped. Therefore you won't see these methods used
directly by the framework, only when clients call them directly in their
hand written code.

On 06/08/2012 5:36 PM, Martin Weiss wrote:
> Hi Ed,
>
> Ed Merks wrote on Thu, 02 August 2012 04:44
>> The four calls to valueOf could be optimized by storing that value as
>> a static final constant...
>
>
> great idea, thanks!
>
> I noticed there are more conversion methods in XMLTypeFactory:
>
> String convertFloat(float instanceValue);
> String convertDouble(double instanceValue);
> String convertFloatObject(Float instanceValue);
> String convertDoubleObject(Double instanceValue)
>
> It seems these are never called during loading or saving the model.
> What are these methods for? Do I have to override them, too?
>
Previous Topic:EMF Code generation problem
Next Topic:[EMF] [CommandStack] Best Practices to use ChangeCommand around UI Dialog
Goto Forum:
  


Current Time: Fri Aug 01 12:17:05 EDT 2014

Powered by FUDForum. Page generated in 0.01874 seconds