Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Org.xml.sax.SAXException when saving resource(Problem only in RCP application/declarative service)
Org.xml.sax.SAXException when saving resource [message #1471931] Thu, 13 November 2014 13:06 Go to next message
Thomas Elskens is currently offline Thomas ElskensFriend
Messages: 159
Registered: September 2014
Location: Brussels - Belgium
Senior Member
Hello,

I'm new to EMF and still studying the basics.

When I try to save a a resource to an xmi-file in a "normal" plugin, this works fine.

However, when I try to do the same in a declarative OSGi-service inside an e4-application, the file remains empty and Eclipse throws an SAXException, with following stack :

Quote:

!STACK 0
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: org.xml.sax.SAXParseExceptionpublicId: platform:/plugin/be.groups.test.EMFEdit.modelservice/ressources/employer_resource.xmi; systemId: platform:/plugin/be.groups.test.EMFEdit.modelservice/ressources/employer_resource.xmi; lineNumber: 1; columnNumber: 1; Premature end of file.
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDemandLoadException(ResourceSetImpl.java:319)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:278)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResource(ResourceSetImpl.java:406)
at internal.ModelServiceImpl.<init>(ModelServiceImpl.java:44)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at org.eclipse.equinox.internal.ds.model.ServiceComponent.createInstance(ServiceComponent.java:493)
//...
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$38/1581147548.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.xml.sax.SAXParseExceptionpublicId: platform:/plugin/be.groups.test.EMFEdit.modelservice/ressources/employer_resource.xmi; systemId: platform:/plugin/be.groups.test.EMFEdit.modelservice/ressources/employer_resource.xmi; lineNumber: 1; columnNumber: 1; Premature end of file.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source)
at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:175)
at org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLResourceImpl.java:261)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1518)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1297)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoad(ResourceSetImpl.java:259)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:274)
... 78 more


My code is as follows :

public class ModelServiceImpl implements IModelService
{
	private ComposedAdapterFactory factory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE) ;
	private EditingDomain domain = new AdapterFactoryEditingDomain(factory, new BasicCommandStack()) ;
	private ResourceSet rs  = domain.getResourceSet() ; 
	private Resource resource = rs.createResource(URI.createURI("ressources/employer_resource.xmi")) ;
		
	public ModelServiceImpl()
	{		
                Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl()) ;
	
		populateResource() ;

		resource.getContents().forEach(System.out::println);
		try
		{
			resource.save(Collections.EMPTY_MAP); 
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
//...
private void populateResource()
	{
		EmployerFactory factory = EmployerFactory.eINSTANCE ;
		Portefeuille p = factory.createPortefeuille() ;
                //...
                resource.getContents().add(p) ;
	}
}        


Any idea why this isn't working in a declarative service... ?

Thomas
Re: Org.xml.sax.SAXException when saving resource [message #1471964 is a reply to message #1471931] Thu, 13 November 2014 13:39 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30653
Registered: July 2009
Senior Member
Thomas,

Comments below.

On 13/11/2014 2:06 PM, Thomas Elskens wrote:
> Hello,
>
> I'm new to EMF and still studying the basics.
> When I try to save a a resource to an xmi-file in a "normal" plugin,
> this works fine.
> However, when I try to do the same in a declarative OSGi-service
> inside an e4-application, the file remains empty and Eclipse throws an
> SAXException, with following stack :
>
> Quote:
>> !STACK 0
>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException:
>> org.xml.sax.SAXParseExceptionpublicId:
>> platform:/plugin/be.groups.test.EMFEdit.modelservice/ressources/employer_resource.xmi;
>> systemId:
>> platform:/plugin/be.groups.test.EMFEdit.modelservice/ressources/employer_resource.xmi;
>> lineNumber: 1; columnNumber: 1; Premature end of file.
So you're loading from platform:/plugin... Is that correct/intended?
>> at
>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDemandLoadException(ResourceSetImpl.java:319)
>> at
>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:278)
>> at
>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResource(ResourceSetImpl.java:406)
>> at internal.ModelServiceImpl.<init>(ModelServiceImpl.java:44)
>> at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
>> Method)
>> at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown
>> Source)
>> at
>> sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown
>> Source)
>> at java.lang.reflect.Constructor.newInstance(Unknown Source)
>> at java.lang.Class.newInstance(Unknown Source)
>> at
>> org.eclipse.equinox.internal.ds.model.ServiceComponent.createInstance(ServiceComponent.java:493)
>> //...
>> at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
>> at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
>> at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown
>> Source)
>> at
>> com.sun.glass.ui.win.WinApplication$$Lambda$38/1581147548.run(Unknown
>> Source)
>> at java.lang.Thread.run(Unknown Source)
>> Caused by: org.xml.sax.SAXParseExceptionpublicId:
>> platform:/plugin/be.groups.test.EMFEdit.modelservice/ressources/employer_resource.xmi;
>> systemId:
>> platform:/plugin/be.groups.test.EMFEdit.modelservice/ressources/employer_resource.xmi;
>> lineNumber: 1; columnNumber: 1; Premature end of file.
>> at
>> com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown
>> Source)
>> at
>> org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:175)
>> at
>> org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLResourceImpl.java:261)
>> at
>> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1518)
>> at
>> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1297)
>> at
>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoad(ResourceSetImpl.java:259)
>> at
>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:274)
>> ... 78 more
>
>
> My code is as follows :
>
>
> public class ModelServiceImpl implements IModelService
> {
> private ComposedAdapterFactory factory = new
> ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE)
> ;
> private EditingDomain domain = new
> AdapterFactoryEditingDomain(factory, new BasicCommandStack()) ;
> private ResourceSet rs = domain.getResourceSet() ; private
> Resource resource =
> rs.createResource(URI.createURI("ressources/employer_resource.xmi")) ;
This URI looks suspicious as well. Where are you expecting this to be
saved? Generally I'd expect URI.createPlatformResourceURI (with the
full path in the workspace) or URI.createFileURI (with the absolute file
system path) to be used.
>
> public ModelServiceImpl()
> {
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION,
> new XMIResourceFactoryImpl()) ;
Doesn't ever modify the global registry in an Eclipse application;
that's only something you'd do in a stand alone program where you don't
need to interact with anyone else. And even then, in this case you
have a resource set, so you can register factories in the resource set's
local registry.
>
> populateResource() ;
>
> resource.getContents().forEach(System.out::println);
> try
> {
> resource.save(Collections.EMPTY_MAP); }
> catch (IOException e)
> {
> e.printStackTrace();
> }
> }
> //...
> private void populateResource()
> {
> EmployerFactory factory = EmployerFactory.eINSTANCE ;
> Portefeuille p = factory.createPortefeuille() ;
> //...
> resource.getContents().add(p) ;
> }
> }
>
> Any idea why this isn't working in a declarative service... ?
Above you show code that does a save, but in the stack trace earlier
you're doing a load (from an apparently empty file)... I can't say how
the two are related. Is the resource you're saving gove included in the
binary bundle from which you are trying to load it in that earlier stack
trace?
>
> Thomas
Re: Org.xml.sax.SAXException when saving resource [message #1473059 is a reply to message #1471964] Fri, 14 November 2014 09:57 Go to previous messageGo to next message
Thomas Elskens is currently offline Thomas ElskensFriend
Messages: 159
Registered: September 2014
Location: Brussels - Belgium
Senior Member
Hello,

My explanation about the error was indeed a bit confused : I got it in two cases :

  1. when using getResource instead of createResource
  2. when trying to open the (empty) file with the Ecore Editor

About the URI : createURI("resources/employer_resource.xmi") yields the same result as createFileURI("be.groups.test.EMFEdit.modelservice/ressources/employer_resource.xmi"), i.e. an empty file. URI.createPlatformPluginURI("be.groups.test.EMFEdit.modelservice/ressources/employer_resource.xmi") throws an java.net.UnknownServiceException (protocol doesn't support output). Ressources is a map located at the pluginroot (just as in my original plugintest).

I've changed the registry to resourceSet.getResourceFactoryRegistry(), without result.

To make sure, I've done a copy paste of my plugin project (where the resource saving works) in a class of the e4-application, which gives the following code :

public class Debugging
{		
	public Debugging()
	{		
		EditingDomain domain = new AdapterFactoryEditingDomain(new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE), new BasicCommandStack());
		EmployerFactory factory = EmployerFactory.eINSTANCE ;
		Resource.Factory.Registry registry = domain.getResourceSet().getResourceFactoryRegistry() ;
		registry.getExtensionToFactoryMap().put("testmodel", new XMIResourceFactoryImpl()) ;
		Resource resource = domain.getResourceSet().createResource(URI.createURI("ressources/test.testmodel")) ;	
		
		Portefeuille p = factory.createPortefeuille() ;
		Employer emp = factory.createEmployer() ;
		Employer emp1 = factory.createEmployer() ;
		p.getClients().add(emp) ;
		p.getClients().add(emp1) ;
		resource.getContents().add(p) ;		
		
		emp.setDenomination("KartoffelFabrik");
		emp.setId(23700);
		emp.setLocalite("Eupen");
		emp.setNoTva("blablabla");
		emp.setAdresse("KurFurstenDam 61");				
		emp1.setDenomination("Au paradis des oignons");		
		p.setId(50000);
		
		resource.getContents().forEach(System.out::println);
		try
		{
			resource.save(null); 
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}


There are only two minor differences:

  1. the code here is invoked in a constructor, and in the plugin project, in a public-static-void-main(String[] args)-method
  2. as suggested, the default registry is no longer used

Hence I'm truly wondering if there is some configuration I should do, some options I should provide to factory and/or registry, to make the saving work ?

Thomas
Re: Org.xml.sax.SAXException when saving resource [message #1473086 is a reply to message #1473059] Fri, 14 November 2014 10:23 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30653
Registered: July 2009
Senior Member
Thomas,

Comments below

On 14/11/2014 10:57 AM, Thomas Elskens wrote:
> Hello,
>
> My explanation about the error was indeed a bit confused : I got it in
> two cases :
>
> when using getResource instead of createResource
Demand loading instead of creating a fresh new resource...

> when trying to open the (empty) file with the Ecore Editor
An empty file is not well formed XML; all XML parses will barf...
> About the URI : createURI("resources/employer_resource.xmi") yields
> the same result as
> createFileURI("be.groups.test.EMFEdit.modelservice/ressources/employer_resource.xmi"),
> i.e. an empty file.
URI's don't yield anything... The refer to resources and that reference
is interpreted by the URI converter to interact with the resource.
> URI.createPlatformPluginURI("be.groups.test.EMFEdit.modelservice/ressources/employer_resource.xmi")
> throws an java.net.UnknownServiceException (protocol doesn't support
> output).
So again I'm getting confused of whether you're talking about reading or
writing. Clearly you should not be trying to write into your running
bundle...
> Ressources is a map located at the pluginroot (just as in my original
> plugintest).
A Map?
> I've changed the registry to resourceSet.getResourceFactoryRegistry(),
> without result.
That's more an issue of corrupting the globally used registry with your
local needs than about whether you'll see the behavior you want.
> To make sure, I've done a copy paste of my plugin project (where the
> resource saving works) in a class of the e4-application, which gives
> the following code :
>
>
> public class Debugging
> {
> public Debugging()
> {
> EditingDomain domain = new AdapterFactoryEditingDomain(new
> ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE),
> new BasicCommandStack());
> EmployerFactory factory = EmployerFactory.eINSTANCE ;
> Resource.Factory.Registry registry =
> domain.getResourceSet().getResourceFactoryRegistry() ;
> registry.getExtensionToFactoryMap().put("testmodel", new
> XMIResourceFactoryImpl()) ;
> Resource resource =
> domain.getResourceSet().createResource(URI.createURI("ressources/test.testmodel"))
> ;
Again with this relative URI. It's relative to what? In what content
are you running? Don't use relative URIs for resources.
>
> Portefeuille p = factory.createPortefeuille() ;
> Employer emp = factory.createEmployer() ;
> Employer emp1 = factory.createEmployer() ;
> p.getClients().add(emp) ;
> p.getClients().add(emp1) ;
> resource.getContents().add(p) ;
>
> emp.setDenomination("KartoffelFabrik");
> emp.setId(23700);
> emp.setLocalite("Eupen");
> emp.setNoTva("blablabla");
> emp.setAdresse("KurFurstenDam 61");
> emp1.setDenomination("Au paradis des oignons");
> p.setId(50000);
>
> resource.getContents().forEach(System.out::println);
> try
> {
> resource.save(null); }
> catch (IOException e)
> {
> e.printStackTrace();
> }
> }
> }
>
>
> There are only two minor differences:
>
> the code here is invoked in a constructor, and in the plugin project,
> in a public-static-void-main(String[] args)-method
Yes, that's a major concern with you relative URIs. What do they mean?
Use an absolute URI (one with a schema) and if the scheme is file: make
sure the file system path itself is absolute as well. Until you get the
right, you will not get good results.
> as suggested, the default registry is no longer used
>
> Hence I'm truly wondering if there is some configuration I should do,
> some options I should provide to factory and/or registry, to make the
> saving work ?
No, you should be very clear on what your URI denotes. Unless you make
it absolute, it's just not going to give you consistent results in
different scenarios. So again, platform:/resource is used to
accessing the workspace, file:/ is used for accessing the file system,
and platform:/plugin is used to accessing resources in the running
bundle (but don't expect to be able to write to that, only to read from it).
>
> Thomas
Re: Org.xml.sax.SAXException when saving resource [message #1473307 is a reply to message #1473086] Fri, 14 November 2014 14:13 Go to previous messageGo to next message
Thomas Elskens is currently offline Thomas ElskensFriend
Messages: 159
Registered: September 2014
Location: Brussels - Belgium
Senior Member
Thanks for the information. Finally it works. Maybe useful information for others who are used to Eclipse URL's:

As suggested, put the resource file in a separate plugin.

And then I got it to work with the following code :
URI uri = URI.createPlatformPluginURI(bundle_symbolic_name, false) ; // or true, doesn't seem to matter
uri = uri.appendSegments(new String[] {name_of_folder,name_of_file}) ;
resource = domain.getResourceSet().createResource(CommonPlugin.resolve(uri)) ;


Thanks a lot for the hints !

Thomas
Re: Org.xml.sax.SAXException when saving resource [message #1473419 is a reply to message #1473307] Fri, 14 November 2014 16:09 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 30653
Registered: July 2009
Senior Member
Thomas,

Comments below.

On 14/11/2014 3:13 PM, Thomas Elskens wrote:
> Thanks for the information. Finally it works. Maybe useful information
> for others who are used to Eclipse URL's:
>
> As suggested, put the resource file in a separate plugin.
> And then I got it to work with the following code :
>
> URI uri = URI.createPlatformPluginURI(bundle_symbolic_name, false) ;
> // or true, doesn't seem to matter
> uri = uri.appendSegments(new String[] {name_of_folder,name_of_file}) ;
> resource =
> domain.getResourceSet().createResource(CommonPlugin.resolve(uri)) ;
That will likely unjar a jarred bundle, but what's not clear is why you
are trying save to a bundle. That's like trying to update a .class file
in a jar of a running program. It's just not proper design. You ought
not to save state in this way...
>
>
> Thanks a lot for the hints !
You never did explain what you're trying to accomplish, but what I see
above seems very odd to me...
>
> Thomas
Previous Topic:[EMF Validation] My Dynamic constraint provider gets never called
Next Topic:[CDO] Installation and Setup instructions
Goto Forum:
  


Current Time: Sat Nov 23 01:30:11 GMT 2019

Powered by FUDForum. Page generated in 0.01931 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top