Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Failed on loading xcore model programmatically
Failed on loading xcore model programmatically [message #1809394] Mon, 15 July 2019 12:12 Go to next message
Dimg Cim is currently offline Dimg CimFriend
Messages: 59
Registered: December 2015
Member
Hello together,

I tried to read a xcore file with the following code snippet, but it seems not to work.

						try {
							ResourceSet resourceSet = new ResourceSetImpl();
							resourceSet.getURIConverter().getURIMap().putAll(EcorePlugin.computePlatformURIMap(true));
							Resource resource = resourceSet.getResource(uri, true);
							resource.load(Collections.emptyMap());
							EObject eObject = resource.getContents().get(0);
							System.out.println(eObject);
						} catch (IOException e1) {
							e1.printStackTrace();
						}


Each time I do this I get a

org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: org.xml.sax.SAXParseExceptionpublicId: file:/C:/.... lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.,

maybe the file canot be read by this way?

Best regards
lam
Re: Failed on loading xcore model programmatically [message #1809405 is a reply to message #1809394] Mon, 15 July 2019 17:54 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Are you running this in the IDE?

I see "file:/C:/..." in your exception. A *.xcore resource must be loaded in a context where the claspath is known. That definitely won't be possible using a URI that reads directly from the file system. It needs to use a platform:/resource/<project>/... URI so that the resource's containing Java project can be determined such that the classpath of the project is available.

Where is your *.xcore resource located, how are you creating the URI for loading it, and in what context are you running?


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Failed on loading xcore model programmatically [message #1809412 is a reply to message #1809405] Mon, 15 July 2019 18:50 Go to previous messageGo to next message
Dimg Cim is currently offline Dimg CimFriend
Messages: 59
Registered: December 2015
Member
Hi Ed,

thanks for the quick reply. I load the xcore file in a rcp with jdt plug-ins. I use this dialog to open the xcore file

public class EcoreResourceDialog extends ResourceDialog {
	public EcoreResourceDialog(Shell parent, String title, int style) {
		super(parent, title, style);
	}

	@Override
	protected void prepareBrowseFileSystemButton(Button browseFileSystemButton) {

		browseFileSystemButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent event) {
				FileDialog fileDialog = new FileDialog(getShell(), style);
				fileDialog.setFilterExtensions(new String[] { "*.ecore" });
				fileDialog.open();

				String filterPath = fileDialog.getFilterPath();
				if (isMulti()) {
					String[] fileNames = fileDialog.getFileNames();
					StringBuffer uris = new StringBuffer();

					for (int i = 0, len = fileNames.length; i < len; i++) {
						uris.append(URI.createFileURI(filterPath + File.separator + fileNames[i]).toString());
						uris.append("  ");
					}
					uriField.setText((uriField.getText() + "  " + uris.toString()).trim());
				} else {
					String fileName = fileDialog.getFileName();
					if (fileName != null) {
						uriField.setText(URI.createFileURI(filterPath + File.separator + fileName).toString());
					}
				}
			}
		});
	}

	@Override
	protected void prepareBrowseWorkspaceButton(Button browseWorkspaceButton) {
		browseWorkspaceButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent event) {
				List<ViewerFilter> filters = new ArrayList<ViewerFilter>();
				filters.add(new ViewerFilter() {
					@Override
					public boolean select(Viewer viewer, Object parentElement, Object element) {
						if (element instanceof IFile) {
							return "ecore".equals(((IFile) element).getFileExtension());
						}
						return true;
					}
				});
				if (isMulti()) {
					StringBuffer uris = new StringBuffer();

					IFile[] files = WorkspaceResourceDialog.openFileSelection(getShell(), null, null, true,
							getContextSelection(), filters);
					for (int i = 0, len = files.length; i < len; i++) {
						uris.append(URI.createPlatformResourceURI(files[i].getFullPath().toString(), true));
						uris.append("  ");
					}
					uriField.setText((uriField.getText() + "  " + uris.toString()).trim());
				} else {
					IFile file = null;

					if (isSave()) {
						String path = getContextPath();
						file = WorkspaceResourceDialog.openNewFile(getShell(), null, null,
								path != null ? new Path(path) : null, filters);
					} else {
						IFile[] files = WorkspaceResourceDialog.openFileSelection(getShell(), null, null, false,
								getContextSelection(), filters);
						if (files.length != 0) {
							file = files[0];
						}
					}

					if (file != null) {
						uriField.setText(URI.createPlatformResourceURI(file.getFullPath().toString(), true).toString());
					}
				}
			}

			private String getContextPath() {
				return context != null && context.isPlatformResource()
						? URI.createURI(".").resolve(context).path().substring(9)
						: null;
			}

			private Object[] getContextSelection() {
				String path = getContextPath();
				if (path != null) {
					IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
					IResource resource = root.findMember(path);
					if (resource != null && resource.isAccessible()) {
						return new Object[] { resource };
					}
				}
				return null;
			}
		});
	}
}


and with the upper code to load the xcore file.

This dialog is opened by a command via context menu in PackageExplorer.

[Updated on: Mon, 15 July 2019 19:18]

Report message to a moderator

Re: Failed on loading xcore model programmatically [message #1809436 is a reply to message #1809412] Tue, 16 July 2019 07:44 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Given your initial message, it appears that it's trying to parse the *.xcore as an XMI/XMI resource. That suggests to me that you don't have org.eclipse.emf.ecore.xcore.ui in your application. This is where the resource factory is registered:
    <extension
        point="org.eclipse.emf.ecore.extension_parser">
        <parser
            class="org.eclipse.emf.ecore.xcore.ui.XcoreExecutableExtensionFactory:org.eclipse.xtext.resource.IResourceFactory"
            type="xcore">
        </parser>
    </extension>
You also won't be able to process it unless you use a platform:/resource URI and the *.xcore resource is in a Java project.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Failed on loading xcore model programmatically [message #1809439 is a reply to message #1809436] Tue, 16 July 2019 08:41 Go to previous messageGo to next message
Dimg Cim is currently offline Dimg CimFriend
Messages: 59
Registered: December 2015
Member
Thank you for your help, it seems to work now. It worked with the file:/c:/ URI.

Another question to the xcore. I modelled this

@GenModel(multipleEditorPages="false", 
	creationIcons="false", 
	editDirectory="/de.example.edit/src-gen",
	editorDirectory="/de.example.editor/src-gen", 
	richClientPlatform="true", 
	codeFormatting="true",
	importerID="org.eclipse.emf.importer.ecore", 
	runtimePlatform="RCP", 
	fileExtensions="dec", 
	tableProviders="true", dataTypeConverters="true")
package de.example

class DecProject{
	contains Section[0..*] sections
}


* How can I access the package "de.example"
* After loading the xcore model, I get a XPackageImpl, from this object I cannot found the package path "de.example"
* How can I access the XPackage#eStorage, in the XPackageMapping are a lot of information I want to read out, e.g. here one of the information would be the baspackage.
* If a file extension is defined I can read it out via
String fileExtensions = xPackage.getAnnotations().get(1).getDetails().get("fileExtensions");
but if not, should I use the XPackageMapping?


P.S. With som experementing I got this
							EList<Adapter> adapters = xPackage.eAdapters();
							Adapter adapter = adapters.get(2);
							if (adapter instanceof XPackageMapping) {
								XPackageMapping mapping = (XPackageMapping) adapter;
								String basePackage = mapping.getGenPackage().getBasePackage();
							}

Are there a nicer way to get this?

[Updated on: Tue, 16 July 2019 10:47]

Report message to a moderator

Re: Failed on loading xcore model programmatically [message #1809475 is a reply to message #1809439] Tue, 16 July 2019 17:42 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
I would suggest you using Open With -> Reflective Xcore Model Editor to understand better and quick the structure of the objects in this resource. I think you're spending too much time with your best friend, the debugger, which isn't a bad approach, but you seem to be getting bogged down in implementation details.

I don't see any reason why you should need to access underlying implementation details such as the mappers. A *.xcore resource contains not only an XPackage (with a name feature, so xPackage.getName() should be easily available), but also a GenModel and an Ecore model. The mappers are used to help build those so the information you might want will in those artifacts.. E.g., the annotations in your Xcore are easily available via the GenModel's API.

The Xcore model itself is "just a syntactic" model for providing a nice high level syntax for Ecore and GenModel. If you get bogged down wading through the syntax, I'm sure you'll miss whatever important semantic information you seek.

What exactly is you goal?


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Failed on loading xcore model programmatically [message #1809483 is a reply to message #1809412] Tue, 16 July 2019 18:24 Go to previous messageGo to next message
Dimg Cim is currently offline Dimg CimFriend
Messages: 59
Registered: December 2015
Member
My goal is to generate a tree view which is worked for all defined emf xcore model with emf.edit support like undo, redo, editing support, dnd, etc. Currently I implement this for JavaFX, later for more frontend apis like vaadin. I know there are a lot of rendering framework for EMF, but for better understanding is to work in there. Further I defined a small dsl in xtext, which generate all classes required for the treeview and it needs {Model]Factory, {Model}Package, {Model}Switch etc. e.g. for the context menu to create new items in the treeview.

Re: Failed on loading xcore model programmatically [message #1809490 is a reply to message #1809483] Tue, 16 July 2019 20:09 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
You should focus on the GenModel then.

Although I should point out that all the generated *.edit support (the item providers) is already platform neutral and can be used by any rendering engine. In fact that whole *.edit framework, including commands to support undo/redo, is platform agnostic, so it sounds a bit like you are reinventing the wheel.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Failed on loading xcore model programmatically [message #1809493 is a reply to message #1809490] Tue, 16 July 2019 21:03 Go to previous message
Dimg Cim is currently offline Dimg CimFriend
Messages: 59
Registered: December 2015
Member
Thanks for your advice, I think now I am on the right way.
Previous Topic:Issue when trying to add Custom Date Type
Next Topic:Set reference dynamically
Goto Forum:
  


Current Time: Fri Mar 29 00:38:28 GMT 2024

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

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

Back to the top