Add userinput to existing XMI [message #810753] |
Thu, 01 March 2012 09:10  |
Eclipse User |
|
|
|
Hello all,
I hope this is the right place to ask this question. I am new to use EMF and am trying to save the data that a user inputs in my application to an XMI file.
I have found a way to save 1 input. But everytime there is a new input, it gets overwritten and not added to it.
Here is the code to save that I have so far:
btnAddPerson.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
System.out.println(person.getFirstName());
ResourceSet resourceSet = new ResourceSetImpl();
URI fileURI = URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.Persons));
Resource resource = resourceSet.createResource(fileURI);
resource.getContents().add(person);
try {
final Map<Object, Object> saveOptions = new HashMap<Object, Object>();
saveOptions.put(Resource.OPTION_SAVE_ONLY_IF_CHANGED, Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER);
saveOptions.put(XMLResource.OPTION_KEEP_DEFAULT_CONTENT, true);
resource.save(saveOptions);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
setTextTextbox();
}
});
So just to be clear:
The persondata gets added to the file. When I try to add another person, it overwrites the previous record instead of adding a new.
|
|
|
Re: Add userinput to existing XMI [message #811281 is a reply to message #810753] |
Fri, 02 March 2012 01:16   |
Eclipse User |
|
|
|
Comments below.
On 01/03/2012 3:10 PM, El Shorty wrote:
> Hello all,
>
> I hope this is the right place to ask this question. I am new to use
> EMF and am trying to save the data that a user inputs in my
> application to an XMI file.
> I have found a way to save 1 input. But everytime there is a new
> input, it gets overwritten and not added to it.
> Here is the code to save that I have so far:
>
>
>
> btnAddPerson.addMouseListener(new MouseAdapter() {
> @Override
> public void mouseDown(MouseEvent e) {
> System.out.println(person.getFirstName());
> ResourceSet resourceSet = new ResourceSetImpl();
> URI fileURI =
> URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.Persons));
> Resource resource = resourceSet.createResource(fileURI);
Do you want to read what's there at that URI so you can add to it?
> resource.getContents().add(person);
>
> try {
> final Map<Object, Object> saveOptions = new
> HashMap<Object, Object>();
>
> saveOptions.put(Resource.OPTION_SAVE_ONLY_IF_CHANGED,
> Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER);
>
> saveOptions.put(XMLResource.OPTION_KEEP_DEFAULT_CONTENT, true);
>
> resource.save(saveOptions);
> } catch (IOException e1) {
> // TODO Auto-generated catch block
> e1.printStackTrace();
> }
> setTextTextbox();
> }
> });
>
>
>
> So just to be clear:
>
> The persondata gets added to the file. When I try to add another
> person, it overwrites the previous record instead of adding a new.
Think about how you'd do this without EMF or just in general. Clearly
you need to check whether the file exists already and you need to load
it in that case so you can add to it and save the combined results.
ResourceSet.getURIConverter().exists is useful for checking if the
resource at that URI really exists. ResourceSet.getResource(uri, true)
is useful for loading already existing contents.
|
|
|
|
|
|
Re: Add userinput to existing XMI [message #813614 is a reply to message #813546] |
Mon, 05 March 2012 08:59   |
Eclipse User |
|
|
|
I tried that but it still gives me errors. Here is the error
!SESSION 2012-03-05 14:45:39.082 -----------------------------------------------
eclipse.buildId=unknown
java.version=1.7.0_03
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86, WS=win32, NL=en_US
Framework arguments: -product smartapps.smartsignature.ui.product
Command-line arguments: -product smartapps.smartsignature.ui.product -data C:\../../runtime-smartsignature.product -dev file:C:/.../.metadata/.plugins/org.eclipse.pde.core/smartsignature.product/dev.properties -os win32 -ws win32 -arch x86 -consoleLog
!ENTRY org.eclipse.ui 4 0 2012-03-05 14:50:33.074
!MESSAGE Unhandled event loop exception
!STACK 0
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: org.xml.sax.SAXParseExceptionpublicId: file:/C:/.../Groups.smartsigndatabase; systemId: file:/C:/.../Groups.smartsigndatabase; lineNumber: 1; columnNumber: 1; Premature end of file.
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDemandLoadException(ResourceSetImpl.java:315)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:274)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResource(ResourceSetImpl.java:397)
at smartapps.smartsignature.ui.views.SaveMethod.save(SaveMethod.java:55)
at smartapps.smartsignature.ui.views.GroupView$2.mouseDown(GroupView.java:135)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:191)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4165)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3754)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2696)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2660)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2494)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:674)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:667)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at smartapps.smartsignature.ui.Application.start(Application.java:20)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
at org.eclipse.equinox.launcher.Main.main(Main.java:1386)
Caused by: org.xml.sax.SAXParseExceptionpublicId: file:/C:/.../Groups.smartsigndatabase; systemId: file:/C:/.../Groups.smartsigndatabase; 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:181)
at org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLResourceImpl.java:242)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1511)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1290)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoad(ResourceSetImpl.java:255)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:270)
... 29 more
And this is the code that gives the error:
public void save(EObject object)
{
ResourceSet resourceSet = new ResourceSetImpl();
URI fileURI;
EList<EObject> list = null;
Resource loadResource = null;
if (object instanceof Person)
{
fileURI = URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.Persons));
}
else if (object instanceof Group)
{
fileURI = URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.Groups));
}
else if (object instanceof Address)
{
fileURI = URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.Addresses));
}
else if (object instanceof Contact)
{
fileURI = URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.Contacts));
}
else
{
fileURI = null;
}
if(resourceSet.getURIConverter().exists(fileURI, null))
{
loadResource = resourceSet.getResource(fileURI, true); //<--- here is the error
list = loadResource.getContents();
}
list.add(object);
Resource resource = resourceSet.createResource(fileURI);
resource.getContents().addAll(list);
try {
final Map<Object, Object> saveOptions = new HashMap<Object, Object>();
saveOptions.put(Resource.OPTION_SAVE_ONLY_IF_CHANGED, Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER);
saveOptions.put(XMLResource.OPTION_KEEP_DEFAULT_CONTENT, true);
resource.save(saveOptions);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
I check if it exists, if it does it should load, problem is the empty file exists so it tries to load that and that it errors
|
|
|
Re: Add userinput to existing XMI [message #813638 is a reply to message #813614] |
Mon, 05 March 2012 09:34   |
Eclipse User |
|
|
|
What's happening here?
at
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResource(ResourceSetImpl.java:397)
at
smartapps.smartsignature.ui.views.SaveMethod.save(SaveMethod.java:55)
On 05/03/2012 2:59 PM, El Shorty wrote:
> I tried that but it still gives me errors. Here is the error
>
>
> !SESSION 2012-03-05 14:45:39.082
> -----------------------------------------------
> eclipse.buildId=unknown
> java.version=1.7.0_03
> java.vendor=Oracle Corporation
> BootLoader constants: OS=win32, ARCH=x86, WS=win32, NL=en_US
> Framework arguments: -product smartapps.smartsignature.ui.product
> Command-line arguments: -product smartapps.smartsignature.ui.product
> -data C:\../../runtime-smartsignature.product -dev
> file:C:/.../.metadata/.plugins/org.eclipse.pde.core/smartsignature.product/dev.properties
> -os win32 -ws win32 -arch x86 -consoleLog
>
> !ENTRY org.eclipse.ui 4 0 2012-03-05 14:50:33.074
> !MESSAGE Unhandled event loop exception
> !STACK 0
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException:
> org.xml.sax.SAXParseExceptionpublicId:
> file:/C:/.../Groups.smartsigndatabase; systemId:
> file:/C:/.../Groups.smartsigndatabase; lineNumber: 1; columnNumber: 1;
> Premature end of file.
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDemandLoadException(ResourceSetImpl.java:315)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:274)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResource(ResourceSetImpl.java:397)
> at
> smartapps.smartsignature.ui.views.SaveMethod.save(SaveMethod.java:55)
> at
> smartapps.smartsignature.ui.views.GroupView$2.mouseDown(GroupView.java:135)
> at
> org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:191)
> at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
> at
> org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4165)
> at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3754)
> at
> org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2696)
> at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2660)
> at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2494)
> at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:674)
> at
> org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
> at
> org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:667)
> at
> org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
> at smartapps.smartsignature.ui.Application.start(Application.java:20)
> at
> org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
> at
> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
> at
> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
> at
> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
> at
> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
> at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
> at java.lang.reflect.Method.invoke(Unknown Source)
> at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
> at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
> at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
> at org.eclipse.equinox.launcher.Main.main(Main.java:1386)
> Caused by: org.xml.sax.SAXParseExceptionpublicId:
> file:/C:/.../Groups.smartsigndatabase; systemId:
> file:/C:/.../Groups.smartsigndatabase; 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:181)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLResourceImpl.java:242)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1511)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1290)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoad(ResourceSetImpl.java:255)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:270)
> ... 29 more
>
>
> And this is the code that gives the error:
>
>
> public void save(EObject object)
> {
>
> ResourceSet resourceSet = new ResourceSetImpl();
> URI fileURI;
> EList<EObject> list = null;
> Resource loadResource = null;
> if (object instanceof Person)
> {
> fileURI =
> URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.Persons));
> }
> else if (object instanceof Group)
> {
> fileURI =
> URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.Groups));
> }
> else if (object instanceof Address)
> {
> fileURI =
> URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.Addresses));
> }
> else if (object instanceof Contact)
> {
> fileURI =
> URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.Contacts));
> }
> else
> {
> fileURI = null;
> }
> if(resourceSet.getURIConverter().exists(fileURI, null))
> {
> loadResource = resourceSet.getResource(fileURI,
> true); //<--- here is the error
> list = loadResource.getContents();
> }
> list.add(object);
> Resource resource = resourceSet.createResource(fileURI);
> resource.getContents().addAll(list);
>
> try {
> final Map<Object, Object> saveOptions = new
> HashMap<Object, Object>();
> saveOptions.put(Resource.OPTION_SAVE_ONLY_IF_CHANGED,
> Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER);
> saveOptions.put(XMLResource.OPTION_KEEP_DEFAULT_CONTENT,
> true);
>
> resource.save(saveOptions);
> } catch (IOException e1) {
> // TODO Auto-generated catch block
> e1.printStackTrace();
> }
> }
>
>
> I check if it exists, if it does it should load, problem is the empty
> file exists so it tries to load that and that it errors
|
|
|
|
Re: Add userinput to existing XMI [message #813736 is a reply to message #813645] |
Mon, 05 March 2012 11:53  |
Eclipse User |
|
|
|
I didn't see that at the end. So it looks like the file exists but
doesn't have a complete XML document, as you said. You can look at the
file to see, so you don't need to guess. Do you expect it to be in that
state? If you expect to deal with bad files, you can use logic like
what you see in the generated editor (with a catch block that calls
getResource(uri, false) to fetch the resource that failed to load). I
would have expected the createResource to be in an else branch...
On 05/03/2012 3:50 PM, El Shorty wrote:
> Ed Merks wrote on Mon, 05 March 2012 09:34
>> What's happening here?
>>
>> at
>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResource(ResourceSetImpl.java:397)
>
>
> That's the imported method that should get my data from my file, it
> works when there is something in it to retrieve, but it doesn't work
> on an empty file. (I think)
>
> Ed Merks wrote on Mon, 05 March 2012 09:34
>> at smartapps.smartsignature.ui.views.SaveMethod.save(SaveMethod.java:55)
>
>
> that's the method I just copy/pasted
>
>
|
|
|
Powered by
FUDForum. Page generated in 0.04969 seconds