| /***************************************************************************** |
| * Copyright (c) 2015 CEA LIST and others. |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * CEA LIST - Initial API and implementation |
| * |
| *****************************************************************************/ |
| package org.eclipse.papyrus.tools.uml.graph.standalone.core; |
| |
| import java.util.Map; |
| |
| import org.eclipse.emf.common.util.URI; |
| import org.eclipse.emf.ecore.resource.Resource; |
| import org.eclipse.emf.ecore.resource.ResourceSet; |
| import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| import org.eclipse.emf.ecore.util.EcoreUtil; |
| import org.eclipse.emf.ecore.xmi.XMLResource; |
| import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl; |
| import org.eclipse.uml2.uml.UMLPackage; |
| import org.eclipse.uml2.uml.UMLPlugin; |
| import org.eclipse.uml2.uml.resource.UMLResource; |
| import org.eclipse.uml2.uml.resources.util.UMLResourcesUtil; |
| |
| public class UmlStandalone { |
| |
| |
| |
| protected ResourceSet RESOURCE_SET; |
| |
| public UmlStandalone(){ |
| //https://www.eclipse.org/forums/index.php/t/667590/ |
| Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("uml", new XMIResourceFactoryImpl()); //$NON-NLS-1$ |
| RESOURCE_SET = new ResourceSetImpl(); |
| prepareEnv(); |
| } |
| |
| |
| public static void main(String[] args) { |
| |
| URI uri = URI.createURI(args[0]); |
| UmlStandalone myUmlStandalone = new UmlStandalone(); |
| |
| Resource resource = myUmlStandalone.getResource(uri); |
| org.eclipse.uml2.uml.Package package_ = (org.eclipse.uml2.uml.Package) EcoreUtil.getObjectByType(resource.getContents(), UMLPackage.Literals.PACKAGE); |
| System.out.println(package_.getName()); |
| |
| |
| } |
| |
| public Resource getResource(URI uri){ |
| Resource resource = RESOURCE_SET.getResource(uri, true); |
| return resource; |
| } |
| |
| public Resource getResource(String path){ |
| URI uri = URI.createURI(path); |
| return getResource(uri); |
| } |
| |
| |
| |
| public ResourceSet prepareEnv(){ |
| ResourceSet resourceSet = new ResourceSetImpl(); |
| UMLResourcesUtil.init(resourceSet); |
| /*synchronized (UMLUtil.class) { |
| UMLUtil.init(resourceSet); |
| }*/ |
| |
| resourceSet.getLoadOptions().put(XMLResource.OPTION_DEFER_ATTACHMENT, true); |
| resourceSet.getLoadOptions().put(XMLResource.OPTION_DEFER_IDREF_RESOLUTION, true); |
| resourceSet.getLoadOptions().put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE, Boolean.TRUE); |
| resourceSet.getLoadOptions().put(XMLResource.OPTION_USE_PACKAGE_NS_URI_AS_LOCATION, Boolean.FALSE); |
| resourceSet.getLoadOptions().put(XMLResource.OPTION_MISSING_PACKAGE_HANDLER, Boolean.TRUE); |
| |
| resourceSet.getPackageRegistry().put(org.eclipse.uml2.types.TypesPackage.eINSTANCE.getNsURI(), org.eclipse.uml2.types.TypesPackage.eINSTANCE); |
| //resourceSet.getPackageRegistry().put(org.eclipse.papyrus.C_Cpp.C_CppPackage.eINSTANCE.getNsURI(), org.eclipse.papyrus.C_Cpp.C_CppPackage.eINSTANCE); |
| resourceSet.getPackageRegistry().put(org.eclipse.emf.ecore.EcorePackage.eINSTANCE.getNsURI(), org.eclipse.emf.ecore.EcorePackage.eINSTANCE); |
| |
| resourceSet.getPackageRegistry().put(org.eclipse.uml2.uml.UMLPackage.eINSTANCE.getNsURI(), org.eclipse.uml2.uml.UMLPackage.eINSTANCE); |
| Map<URI, URI> uriMap = resourceSet.getURIConverter().getURIMap(); |
| System.out.println("jar:file:"+UMLPlugin.INSTANCE.getBaseURL().getFile()); |
| URI umlJarUri = URI.createURI("jar:file:"+UMLPlugin.INSTANCE.getBaseURL().getFile()); |
| uriMap.put(URI.createURI(UMLResource.LIBRARIES_PATHMAP), umlJarUri.appendSegment("libraries").appendSegment("")); |
| uriMap.put(URI.createURI(UMLResource.METAMODELS_PATHMAP), umlJarUri.appendSegment("metamodels").appendSegment("")); |
| uriMap.put(URI.createURI(UMLResource.PROFILES_PATHMAP), umlJarUri.appendSegment("profiles").appendSegment("")); |
| |
| return resourceSet; |
| } |
| |
| } |