PGR,
Resource.save is the normal way to serialize things to a file. You'd
put the different objects in different resources to support a
multi-document model. Things like
archive:<URI-of-jar>!/<path-in-jar> could be used to save
resources into a jar.
Here's simple example for a test case I was working on:
Resource resource =
resourceSet.createResource(URI.createURI("file:/c:/stuff/zip.library"));
DocumentRoot documentRoot =
LibraryFactory.eINSTANCE.createDocumentRoot();
Library root =
LibraryFactory.eINSTANCE.createLibrary();
Book book = LibraryFactory.eINSTANCE.createBook();
root.getBooks().add(book);
documentRoot.setLibrary(root);
resource.getContents().add(documentRoot);
Resource resource2 =
resourceSet.createResource(URI.createURI("file:/c:/stuff/zip2.library"));
DocumentRoot documentRoot2 =
LibraryFactory.eINSTANCE.createDocumentRoot();
Library root2 =
LibraryFactory.eINSTANCE.createLibrary();
Writer writer2 =
LibraryFactory.eINSTANCE.createWriter();
root2.getWriters().add(writer2);
documentRoot2.setLibrary(root2);
resource2.getContents().add(documentRoot2);
book.setAuthor(writer2);
Map<Object,Object> options = new
HashMap<Object, Object>();
options.put(XMLResource.OPTION_RESOURCE_ENTITY_HANDLER, new
ResourceEntityHandlerImpl("Foo"));
resource.save(System.out, options);
resource2.save(System.out, options);
options.put(Resource.OPTION_ZIP, Boolean.TRUE);
resource.save(options);
resource2.save(options);
options.put(Resource.OPTION_SAVE_ONLY_IF_CHANGED,
Resource.OPTION_SAVE_ONLY_IF_CHANGED_FILE_BUFFER);
resource.save(options);
resource2.save(options);
options.put(Resource.OPTION_SAVE_ONLY_IF_CHANGED,
Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER);
resource.save(options);
resource2.save(options);
phantomjinx wrote:
Hi
all,
Would someone mind telling me where to start in exporting a model
instance to file.
The model will have been loaded either from file or from CDO.
The model elements will be contained in multiple resources.
I would like to be able to select some container element in the loaded
model instance and export it to file/zip/jar ... This should be able to
determine references spanning multiple resources and bring those in too
if desired. In effect, I will have exported a subset of the model
instance and is in itself fully valid (no broken refs).
Has anything been done before in this area that I could re-use or am I
breaking new ground here, please.
Any help and pointers much appreciated.
Regards
PGR
|