diff options
Diffstat (limited to 'sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src')
10 files changed, 982 insertions, 0 deletions
diff --git a/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/Activator.java b/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/Activator.java new file mode 100644 index 00000000000..5875dc1d46a --- /dev/null +++ b/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/Activator.java @@ -0,0 +1,55 @@ +package org.eclipse.papyrus.uml.profile.externalresource; + +import org.eclipse.papyrus.infra.core.log.LogHelper; +import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.osgi.framework.BundleContext; + +/** + * The activator class controls the plug-in life cycle + */ +public class Activator extends AbstractUIPlugin { + + // The plug-in ID + public static final String PLUGIN_ID = "org.eclipse.papyrus.uml.profile.externalresource"; //$NON-NLS-1$ + + // The shared instance + private static Activator plugin; + + /** logger helper */ + public static LogHelper log; + + /** + * The constructor + */ + public Activator() { + } + + /** + * {@inheritDoc} + */ + @Override + public void start(BundleContext context) throws Exception { + super.start(context); + plugin = this; + log = new LogHelper(this); + } + + /** + * {@inheritDoc} + */ + @Override + public void stop(BundleContext context) throws Exception { + plugin = null; + log = null; + super.stop(context); + } + /** + * Returns the shared instance + * + * @return the shared instance + */ + public static Activator getDefault() { + return plugin; + } + +} diff --git a/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/AbstractStereotypeApplicationLocationStrategy.java b/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/AbstractStereotypeApplicationLocationStrategy.java new file mode 100644 index 00000000000..f6521a180a6 --- /dev/null +++ b/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/AbstractStereotypeApplicationLocationStrategy.java @@ -0,0 +1,35 @@ +/***************************************************************************** + * Copyright (c) 2013 CEA LIST. + * + * 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: + * Remi Schnekenburger (CEA LIST) - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.uml.profile.externalresource.helper; + +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.uml2.uml.Element; + + +/** + * Abstract strategy class for stereotype application location + */ +public abstract class AbstractStereotypeApplicationLocationStrategy implements IStereotypeApplicationLocationStrategy { + + /** + * @param element + * @param definition + * @return + */ + protected EList<EObject> getDefaultContainmentList(Element element, EClass definition) { + return element.eResource().getContents(); + } + +} diff --git a/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/ExtendedProfileApplicationHelper.java b/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/ExtendedProfileApplicationHelper.java new file mode 100644 index 00000000000..d95aab82d65 --- /dev/null +++ b/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/ExtendedProfileApplicationHelper.java @@ -0,0 +1,34 @@ +/***************************************************************************** + * Copyright (c) 2013 CEA LIST. + * + * 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: + * Remi Schnekenburger (CEA LIST) - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.uml.profile.externalresource.helper; + +import org.eclipse.uml2.uml.Package; +import org.eclipse.uml2.uml.util.UMLUtil.ProfileApplicationHelper; + + +/** + * Extended profile application helper for Papyrus tool. + * <P> + * When this helper is activated, profiles are located in external resources + * </P> + */ +public class ExtendedProfileApplicationHelper extends ProfileApplicationHelper { + + /** + * {@inheritDoc} + */ + @Override + public Iterable<Package> getOtherApplyingPackages(Package package_) { + return super.getOtherApplyingPackages(package_); + } +} diff --git a/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/IStereotypeApplicationLocationStrategy.java b/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/IStereotypeApplicationLocationStrategy.java new file mode 100644 index 00000000000..2f880b9db54 --- /dev/null +++ b/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/IStereotypeApplicationLocationStrategy.java @@ -0,0 +1,44 @@ +/***************************************************************************** + * Copyright (c) 2013 CEA LIST. + * + * 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: + * Remi Schnekenburger (CEA LIST) - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.uml.profile.externalresource.helper; + +import java.util.Set; + +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.uml2.uml.Element; + + +/** + * Interface implemented by all strategies in charge of the management of resources for stereotype applications + */ +public interface IStereotypeApplicationLocationStrategy { + + /** + * Returns the containment list in which stereotype applications should be read/added. + * @param element the stereotyped element + * @param definition the definition of the stereotype to apply + * @return the containment list for the new stereotype application. if any problem, this will be the default location (direct content of the resource containing the element) + */ + public EList<EObject> getContainmentList(Element element, EClass definition); + + /** + * Returns the list of URIs of the resources in which stereotype applications are placed. + * @param root element of the model from which all profile applications are found + * @return the list of URIS, an empty collection if not URIs are found. + */ + public Set<URI> getProfileApplicationResourceURIs(EObject root); + +} diff --git a/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/OneResourceOnlyStrategy.java b/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/OneResourceOnlyStrategy.java new file mode 100644 index 00000000000..1ea97abe14a --- /dev/null +++ b/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/OneResourceOnlyStrategy.java @@ -0,0 +1,104 @@ +/***************************************************************************** + * Copyright (c) 2013 CEA LIST. + * + * 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: + * Remi Schnekenburger (CEA LIST) - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.uml.profile.externalresource.helper; + +import java.util.Collections; +import java.util.Set; + +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.papyrus.infra.core.resource.ModelSet; +import org.eclipse.papyrus.infra.core.resource.ModelUtils; +import org.eclipse.uml2.uml.Element; +import org.eclipse.uml2.uml.resource.UMLResource; + +/** + * Strategy for only one resource for all profile applications + */ +public class OneResourceOnlyStrategy extends AbstractStereotypeApplicationLocationStrategy { + + /** identifier of this strategy */ + public static final String ID = "OneResourceOnlyStrategy"; //$NON-NLS-1$ + + /** default extension for the profile URI */ + public static final String PROFILE_DEFAULT_EXTENSION = "profiles"; + + /** singleton instance */ + private static OneResourceOnlyStrategy instance; + + /** + * Returns the singleton instance for this strategy + * + * @return the singleton instance for this strategy + */ + public static OneResourceOnlyStrategy getInstance() { + if(instance == null) { + instance = new OneResourceOnlyStrategy(); + } + return instance; + } + + /** + * {@inheritDoc} + */ + @Override + public EList<EObject> getContainmentList(Element element, EClass definition) { + // 1. only one resource => look if there is a custom name. Otherwise, use a standard name for the resource + // compute the URI of the resource + Resource baseResource = element.eResource(); + if(baseResource == null) { + return null; + } + URI newURI = getProfileResourceURI(element, definition); + ResourceSet resourceSet = baseResource.getResourceSet(); + if(resourceSet instanceof ModelSet && newURI != null) { + ModelSet modelSet = (ModelSet)resourceSet; + Resource resource = ModelUtils.getOrCreateResource(modelSet, true, newURI, UMLResource.UML_CONTENT_TYPE_IDENTIFIER); + return resource.getContents(); + } + // by default, return the resource containing the element + return getDefaultContainmentList(element, definition); + } + + /** + * @param element + * @param definition + * @return + */ + protected URI getProfileResourceURI(Element element, EClass definition) { + if(element.eResource() !=null) { + URI uri = element.eResource().getURI(); + if(uri !=null) { + return uri.trimFileExtension().appendFileExtension(PROFILE_DEFAULT_EXTENSION); + } + } + return null; + } + + /** + * {@inheritDoc} + */ + @Override + public Set<URI> getProfileApplicationResourceURIs(EObject root) { + URI uri = root.eResource().getURI(); + if(uri !=null) { + return Collections.singleton(uri.trimFileExtension().appendFileExtension(PROFILE_DEFAULT_EXTENSION)); + } + return Collections.emptySet(); + } + +} diff --git a/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/PapyrusStereotypeApplicationHelper.java b/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/PapyrusStereotypeApplicationHelper.java new file mode 100644 index 00000000000..f1398a885fe --- /dev/null +++ b/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/PapyrusStereotypeApplicationHelper.java @@ -0,0 +1,93 @@ +/***************************************************************************** + * Copyright (c) 2013 CEA LIST. + * + * 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: + * Remi Schnekenburger (CEA LIST) - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.uml.profile.externalresource.helper; + +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.ecore.EAnnotation; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.uml2.uml.Element; +import org.eclipse.uml2.uml.util.UMLUtil.StereotypeApplicationHelper; + + +/** + * Specific Stereotype Application Helper for Papyrus tool. If it detects the model is not a model handled by Papyrus, it will delegate to the standard Stereotype application helper. + */ +public class PapyrusStereotypeApplicationHelper extends StereotypeApplicationHelper { + + /** Key for the location strategy value */ + public static final String LOCATION_STRATEGY_KEY = "locationStrategy"; + + /** source for the eannotation that is used to store specific URI for stereotype application resource */ + public static final String PAPYRUS_EXTERNAL_RESOURCE_EANNOTATION_SOURCE = "http://www.eclipse.org/papyrus/uml/profile/externalresource"; //$NON-NLS-1$ + + /** + * {@inheritDoc} + */ + @Override + public EObject applyStereotype(Element element, EClass definition) { + return super.applyStereotype(element, definition); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean addToContainmentList(Element element, EObject stereotypeApplication) { + return super.addToContainmentList(element, stereotypeApplication); + } + + /** + * {@inheritDoc} + */ + @Override + protected EList<EObject> getContainmentList(Element element, EClass definition) { + // 1. find the current strategy used for the model element + // 2. use the strategy to find the resource and the containment list + // 3. if no strategy found, return usual strategy ==> stereotype application are directly contained by the resource containing the stereotyped element. + IStereotypeApplicationLocationStrategy locationStrategy = getCurrentLocationStrategy(element); + if(locationStrategy ==null) { + return StandardApplicationLocationStrategy.getInstance().getContainmentList(element, definition); + } + return locationStrategy.getContainmentList(element, definition); + } + + /** + * Returns the location strategy to use for the given couple element/definition + * @param element the stereotyped element + * @param definition the definition of the stereotype to be applied + * @return the location strategy or <code>null</code> if none was found + */ + public static IStereotypeApplicationLocationStrategy getCurrentLocationStrategy(EObject element) { + EObject container = EcoreUtil.getRootContainer(element, true); + if(container instanceof Element) { + EAnnotation annotation = ((Element)container).getEAnnotation(PAPYRUS_EXTERNAL_RESOURCE_EANNOTATION_SOURCE); + if(annotation == null) { + return null; + } + + String location = annotation.getDetails().get(LOCATION_STRATEGY_KEY); + if(location == null) { + return null; + } + + IStereotypeApplicationLocationStrategy strategy = StrategyRegistry.getInstance().getStrategy(location); + if(strategy != null) { + return strategy; + } + } + return null; + } + +} diff --git a/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/ResourcePerProfileStrategy.java b/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/ResourcePerProfileStrategy.java new file mode 100644 index 00000000000..6fb861b60f7 --- /dev/null +++ b/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/ResourcePerProfileStrategy.java @@ -0,0 +1,253 @@ +/***************************************************************************** + * Copyright (c) 2013 CEA LIST. + * + * 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: + * Remi Schnekenburger (CEA LIST) - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.uml.profile.externalresource.helper; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EAnnotation; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EClassifier; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EPackage; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.papyrus.infra.core.resource.ModelSet; +import org.eclipse.papyrus.infra.core.resource.ModelUtils; +import org.eclipse.uml2.uml.Element; +import org.eclipse.uml2.uml.Package; +import org.eclipse.uml2.uml.Profile; +import org.eclipse.uml2.uml.ProfileApplication; +import org.eclipse.uml2.uml.resource.UMLResource; + +/** + * Strategy that uses one resource per profile application + */ +public class ResourcePerProfileStrategy implements IStereotypeApplicationLocationStrategy { + + /** default resource name */ + protected static final String DEFAULT_RESOURCE_NAME = "default"; //$NON-NLS-1$ + + /** default resource extension */ + protected static final String DEFAULT_RESOURCE_EXTENSION = "profile"; //$NON-NLS-1$ + + /** key for the value of the eannotation that is used to store specific URI for stereotype application resource */ + public static final String SPECIFIC_URI_EANNOTATION_KEY = "specificURI"; //$NON-NLS-1$ + + /** identifier of this strategy */ + public static final String ID = "ResourcePerProfileStrategy"; //$NON-NLS-1$ + + /** singleton instance */ + private static ResourcePerProfileStrategy instance; + + /** + * Returns the singleton instance for this strategy + * + * @return the singleton instance for this strategy + */ + public static ResourcePerProfileStrategy getInstance() { + if(instance == null) { + instance = new ResourcePerProfileStrategy(); + } + return instance; + } + + /** + * Returns the resource where the stereotype application should be added for the given couple element/stereotype + * + * @param element + * the stereotyped element + * @param definition + * the definition of the stereotype to apply + * @return the resource where the stereotype application should be added + */ + public Resource getStereotypeApplicationResource(Element element, EClass definition) { + // in this case, the resource is specific to the profile application. + // the profile application should give the right path. + // 1. retrieve profile application + // 2. ask it for the resource name (by default, it should give the name of the profile) + // 3. compute the resource name + // 4. ask to create on demand ? + // 5. return the content of this resource + ProfileApplication profileApplication = getProfileApplication(element, definition); + Resource baseResource = element.eResource(); + if(baseResource==null) { + return null; + } + ResourceSet resourceSet = baseResource.getResourceSet(); + if(resourceSet instanceof ModelSet) { + ModelSet modelSet = (ModelSet)resourceSet; + URI newURI = getProfileApplicationResourceURI(profileApplication, baseResource); + Resource resource = ModelUtils.getOrCreateResource(modelSet, true, newURI, UMLResource.UML_CONTENT_TYPE_IDENTIFIER); + return resource; + } + // by default, return the resource containing the element + return element.eResource(); + } + + /** + * Returns the specific URI indicated on the profile application + * + * @param profileApplication + * the profile application that should have stereotype applications in resource with specific URI + * @return the specific URI or <code>null</code> + */ + protected URI getSpecificURI(ProfileApplication profileApplication, Resource baseResource) { + EAnnotation annotation = profileApplication.getEAnnotation(PapyrusStereotypeApplicationHelper.PAPYRUS_EXTERNAL_RESOURCE_EANNOTATION_SOURCE); + if(annotation != null) { + String value = annotation.getDetails().get(SPECIFIC_URI_EANNOTATION_KEY); + URI newURI = baseResource.getURI(); + newURI = newURI.trimSegments(1); + newURI = newURI.appendSegment(value); + return newURI; + } + return null; + } + + /** + * Checks if the profile application should use a specific URI for the steroetype application resources + * + * @param profileApplication + * profile application that should have stereotype applications in resource with specific URI + * @return <code>true</code> if a specific URI is specified for the profile application + */ + protected boolean hasSpecificURI(ProfileApplication profileApplication) { + EAnnotation annotation = profileApplication.getEAnnotation(PapyrusStereotypeApplicationHelper.PAPYRUS_EXTERNAL_RESOURCE_EANNOTATION_SOURCE); + return annotation != null; + } + + protected String getStereotypeApplicationResourceExtension(ProfileApplication profileApplication, boolean basedOnMainProfile) { + // 1. check if nested profiles have the same resource or if each nested profile has its own resource + // 2. deduce the name from the profile application + // 3. return the string. This will be the extension of the file + String resourceExtension = null; + if(profileApplication != null) { + Profile profile = profileApplication.getAppliedProfile(); + if(profile != null) { + // if based on main profile => retrieve the main profile name. + // otherwise, returns the name of the profile + if(basedOnMainProfile) { + Profile parentProfile = profile; + while(parentProfile.getNestingPackage() instanceof Profile) { + parentProfile = (Profile)parentProfile.getNestingPackage(); + } + resourceExtension = parentProfile.getName() + "Profile"; // warning, can not return an extension which is linked to another factory than a UML Factory + } else { + resourceExtension = profile.getName() + "Profile"; + } + } + } + if(resourceExtension == null) { + resourceExtension = DEFAULT_RESOURCE_EXTENSION; + } + return resourceExtension; + } + + protected String getStereotypeApplicationResourceName(ProfileApplication profileApplication, boolean basedOnMainProfile) { + // 1. check if nested profiles have the same resource or if each nested profile has its own resource + // 2. deduce the name from the profile application. + // 3. return the string. This will be the extension of the file + String resourceName = null; + if(profileApplication != null) { + Package applyingPackage = profileApplication.getApplyingPackage(); + if(applyingPackage != null) { + resourceName = applyingPackage.eResource().getURI().trimFileExtension().lastSegment(); + } + } + if(resourceName == null) { + resourceName = DEFAULT_RESOURCE_NAME; + } + return resourceName; + } + + /** + * Returns the profile application for the given element on which the stereotype with specified definition is or will be applied + * + * @param element + * the UML element on which stereotype is applied + * @return the profile application or <code>null</code> if none was found. + */ + protected ProfileApplication getProfileApplication(Element element, EClass definition) { + Package nearestPackage = element.getNearestPackage(); + if(nearestPackage == null) { + return null; + } + List<ProfileApplication> profileApplications = nearestPackage.getAllProfileApplications(); + for(ProfileApplication profileApplication : profileApplications) { + EPackage appliedDefinition = profileApplication.getAppliedDefinition(); + List<EClassifier> classifiers = appliedDefinition.getEClassifiers(); + if(classifiers != null && !classifiers.isEmpty() && classifiers.contains(definition)) { + return profileApplication; + } + } + return null; + } + + /** + * {@inheritDoc} + */ + @Override + public EList<EObject> getContainmentList(Element element, EClass definition) { + Resource stereotypeApplicationResource = getStereotypeApplicationResource(element, definition); + if(stereotypeApplicationResource != null) { + return stereotypeApplicationResource.getContents(); + } + // fallback: adds stereotype at the root of the UML resource. + Resource elementResource = element.eResource(); + if(elementResource != null) { + return elementResource.getContents(); + } + return null; + } + + /** + * {@inheritDoc} + */ + @Override + public Set<URI> getProfileApplicationResourceURIs(EObject root) { + Set<URI> uris = new HashSet<URI>(); + // 1. retrieve all profile applications + // 2. retrieve all URI from all this profile applications + if(root instanceof Element && root.eResource()!=null) { + Package nearestPackage = ((Element)root).getNearestPackage(); + if(nearestPackage != null) { + List<ProfileApplication> profileApplications = nearestPackage.getAllProfileApplications(); + for(ProfileApplication profileApplication : profileApplications) { + URI newURI = getProfileApplicationResourceURI(profileApplication, root.eResource()); + uris.add(newURI); + } + } + } + return uris; + } + + protected URI getProfileApplicationResourceURI(ProfileApplication profileApplication, Resource baseResource) { + // test if it needs a specific name. Otherwise, it will use a default name + URI newURI = null; + if(hasSpecificURI(profileApplication)) { + newURI = getSpecificURI(profileApplication, baseResource); + } + if(newURI == null) { + URI baseURI = baseResource.getURI(); + newURI = baseURI.trimSegments(1); + String resourceName = getStereotypeApplicationResourceName(profileApplication, true); + newURI = newURI.appendSegment(resourceName); + String extension = getStereotypeApplicationResourceExtension(profileApplication, true); + newURI = newURI.appendFileExtension(extension); + } + return newURI; + } +} diff --git a/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/StandardApplicationLocationStrategy.java b/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/StandardApplicationLocationStrategy.java new file mode 100644 index 00000000000..1d75eaba828 --- /dev/null +++ b/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/StandardApplicationLocationStrategy.java @@ -0,0 +1,64 @@ +/***************************************************************************** + * Copyright (c) 2013 CEA LIST. + * + * 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: + * Remi Schnekenburger (CEA LIST) - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.uml.profile.externalresource.helper; + +import java.util.Collections; +import java.util.Set; + +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.uml2.uml.Element; + + +/** + * Usual stereotype application strategy + */ +public class StandardApplicationLocationStrategy implements IStereotypeApplicationLocationStrategy { + + /** identifier of this strategy */ + public static final String ID = "standardStrategy"; //$NON-NLS-1$ + + /** singleton instance */ + protected static StandardApplicationLocationStrategy instance; + + /** + * {@inheritDoc} + */ + @Override + public EList<EObject> getContainmentList(Element element, EClass definition) { + if(element.eResource() !=null) { + return element.eResource().getContents(); + } + return null; + } + + /** + * @return + */ + public static StandardApplicationLocationStrategy getInstance() { + if(instance ==null) { + instance = new StandardApplicationLocationStrategy(); + } + return instance; + } + + /** + * {@inheritDoc} + */ + @Override + public Set<URI> getProfileApplicationResourceURIs(EObject root) { + return Collections.emptySet(); + } +} diff --git a/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/StrategyRegistry.java b/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/StrategyRegistry.java new file mode 100644 index 00000000000..07af6bfd0b7 --- /dev/null +++ b/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/helper/StrategyRegistry.java @@ -0,0 +1,62 @@ +/***************************************************************************** + * Copyright (c) 2013 CEA LIST. + * + * 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: + * Remi Schnekenburger (CEA LIST) - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.uml.profile.externalresource.helper; + +import java.util.HashMap; +import java.util.Map; + + +/** + * Registry for all {@link IStereotypeApplicationLocationStrategy}. + */ +public class StrategyRegistry { + + /** singleton instance */ + private static StrategyRegistry instance; + + /** map identifier - strategies */ + private Map<String, IStereotypeApplicationLocationStrategy> registry; + + /** + * Returns the singleton instance for this strategy + * + * @return the singleton instance for this strategy + */ + public static StrategyRegistry getInstance() { + if(instance == null) { + instance = new StrategyRegistry(); + instance.init(); + } + return instance; + } + + /** + * Initialize the strategy registry, reading extension points. + */ + protected void init() { + // TODO: read extension points. + registry = new HashMap<String, IStereotypeApplicationLocationStrategy>(); + registry.put(StandardApplicationLocationStrategy.ID, StandardApplicationLocationStrategy.getInstance()); + registry.put(OneResourceOnlyStrategy.ID, OneResourceOnlyStrategy.getInstance()); + registry.put(ResourcePerProfileStrategy.ID, ResourcePerProfileStrategy.getInstance()); + } + + /** + * Returns the strategy for the given identifier + * @param identifier identifier of the strategy + * @return the strategy found or <code>null</code> if none was found + */ + public IStereotypeApplicationLocationStrategy getStrategy(String identifier) { + return registry.get(identifier); + } +} diff --git a/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/model/StereotypeApplicationExternalResourceModel.java b/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/model/StereotypeApplicationExternalResourceModel.java new file mode 100644 index 00000000000..60a2f0b5ecb --- /dev/null +++ b/sandbox/ExternalResourceStereotypeApplication/org.eclipse.papyrus.uml.profile.externalresource/src/org/eclipse/papyrus/uml/profile/externalresource/model/StereotypeApplicationExternalResourceModel.java @@ -0,0 +1,238 @@ +/***************************************************************************** + * Copyright (c) 2013 CEA LIST. + * + * 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: + * Remi Schnekenburger (CEA LIST) - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.uml.profile.externalresource.model; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.eclipse.core.runtime.IPath; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.xmi.XMIResource; +import org.eclipse.papyrus.infra.core.resource.AbstractBaseModel; +import org.eclipse.papyrus.infra.core.resource.AbstractModel; +import org.eclipse.papyrus.infra.core.resource.IModel; +import org.eclipse.papyrus.infra.core.resource.IModelSnippet; +import org.eclipse.papyrus.infra.core.resource.ModelUtils; +import org.eclipse.papyrus.infra.core.resource.NotFoundException; +import org.eclipse.papyrus.uml.profile.externalresource.Activator; +import org.eclipse.papyrus.uml.profile.externalresource.helper.IStereotypeApplicationLocationStrategy; +import org.eclipse.papyrus.uml.profile.externalresource.helper.PapyrusStereotypeApplicationHelper; +import org.eclipse.papyrus.uml.tools.model.UmlModel; +import org.eclipse.uml2.uml.resource.UMLResource; + +/** + * Model to manage stereotype applications in external resources. + */ +public class StereotypeApplicationExternalResourceModel extends AbstractModel implements IModel { + + /** identifier of this IModel */ + public static final String ID = "ExternalStereotypeApplicationModel"; + + /** list of resources for stereotype applications */ + List<Resource> profileApplicationResources = new ArrayList<Resource>(); + + /** + * {@inheritDoc} + */ + @Override + public String getIdentifier() { + return ID; + } + + /** + * {@inheritDoc} + */ + @Override + public void createModel(IPath fullPath) { + throw new UnsupportedOperationException("CreateModel_IPath is not supported for " + getClass().getCanonicalName()); + } + + /** + * {@inheritDoc} + */ + @Override + public void createModel(URI uri) { + throw new UnsupportedOperationException("CreateModel_URI is not supported for " + getClass().getCanonicalName()); + } + + /** + * {@inheritDoc} + */ + @Override + public void loadModel(IPath path) { + loadModel(getPlatformURI(path)); + } + + /** + * Returns a platform resource URI of the given path + * + * @param path + * the path + * @return the uri + */ + protected URI getPlatformURI(IPath path) { + return URI.createPlatformResourceURI(path.toString(), true); + } + + /** + * {@inheritDoc} + */ + @Override + public void loadModel(URI uri) { + List<Resource> resources = getResources(true); + for(Resource resource : resources) { + try { + if(!resource.isLoaded()) { + resource.load(null); + } + configureResource(resource); + profileApplicationResources.add(resource); + } catch (IOException e) { + Activator.log.error(e); + } + } + // call registered snippets + snippets.performStart(this); + } + + /** + * Returns the dynamically computed list of resources in which stereotype applications should be located + * + * @return the list of resources in which stereotype applications should be located + */ + protected List<Resource> getResources(boolean loadOnDemand) { + List<Resource> resources = new ArrayList<Resource>(); + // get all profile applications resource to load from the strategy. do not care of the URI ? + UmlModel umlModel = (UmlModel)modelSet.getModel(UmlModel.MODEL_ID); + try { + if(umlModel != null && umlModel.lookupRoot() != null) { + EObject root = umlModel.lookupRoot(); + IStereotypeApplicationLocationStrategy strategy = PapyrusStereotypeApplicationHelper.getCurrentLocationStrategy(root); + if(strategy != null) { + Set<URI> profileApplicationResourceURIs = strategy.getProfileApplicationResourceURIs(root); + for(URI resourceURI : profileApplicationResourceURIs) { + Resource resource = ModelUtils.getOrCreateResource(modelSet, loadOnDemand, resourceURI, UMLResource.UML_CONTENT_TYPE_IDENTIFIER); + resources.add(resource); + } + } + } + } catch (NotFoundException e) { + Activator.log.debug(e.getMessage()); + return Collections.emptyList(); + } + return resources; + } + + protected void configureResource(Resource resource) { + if(resource instanceof XMIResource) { + ((XMIResource)resource).getDefaultSaveOptions().putAll(getSaveOptions()); + ((XMIResource)resource).setEncoding(AbstractBaseModel.ENCODING); + } + } + + protected Map<Object, Object> getSaveOptions() { + Map<Object, Object> saveOptions = new HashMap<Object, Object>(); + // default save options. + saveOptions.put(XMIResource.OPTION_DECLARE_XML, Boolean.TRUE); + saveOptions.put(XMIResource.OPTION_PROCESS_DANGLING_HREF, XMIResource.OPTION_PROCESS_DANGLING_HREF_DISCARD); + saveOptions.put(XMIResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE); + saveOptions.put(XMIResource.OPTION_USE_XMI_TYPE, Boolean.TRUE); + saveOptions.put(XMIResource.OPTION_SAVE_TYPE_INFORMATION, Boolean.TRUE); + saveOptions.put(XMIResource.OPTION_SKIP_ESCAPE_URI, Boolean.FALSE); + saveOptions.put(XMIResource.OPTION_ENCODING, "UTF-8"); + //see bug 397987: [Core][Save] The referenced plugin models are saved using relative path + saveOptions.put(XMIResource.OPTION_URI_HANDLER, new org.eclipse.emf.ecore.xmi.impl.URIHandlerImpl.PlatformSchemeAware()); + return saveOptions; + } + + /** + * {@inheritDoc} + */ + @Override + public void importModel(IPath path) { + throw new UnsupportedOperationException("ImportModel_IPath is not supported for " + getClass().getCanonicalName()); + } + + /** + * {@inheritDoc} + */ + @Override + public void importModel(URI uri) { + throw new UnsupportedOperationException("ImportModel_URI is not supported for " + getClass().getCanonicalName()); + } + + /** + * {@inheritDoc} + */ + @Override + public void saveModel() throws IOException { + List<Resource> resources = getResources(true); + for(Resource resource : resources) { + if(!getModelManager().getTransactionalEditingDomain().isReadOnly(resource) && !ModelUtils.resourceFailedOnLoad(resource)) { + resource.save(getSaveOptions()); + } + } + } + + /** + * {@inheritDoc} + */ + @Override + public void changeModelPath(IPath fullPath) { + throw new UnsupportedOperationException("changeModelPath is not supported for " + getClass().getCanonicalName()); + } + + /** + * {@inheritDoc} + */ + @Override + public void setModelURI(URI uri) { + throw new UnsupportedOperationException("setModelURI is not supported for " + getClass().getCanonicalName()); + } + + /** + * {@inheritDoc} + */ + @Override + public void unload() { + // call registered snippets + snippets.performDispose(this); + List<Resource> resources = getResources(false); + for(Resource resource : resources) { + resource.unload(); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void addModelSnippet(IModelSnippet snippet) { + snippets.add(snippet); + } + + /** + * {@inheritDoc} + */ + @Override + public Set<URI> getModifiedURIs() { + return Collections.emptySet(); + } +} |