diff options
Diffstat (limited to 'plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus')
8 files changed, 765 insertions, 0 deletions
diff --git a/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/Activator.java b/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/Activator.java new file mode 100644 index 00000000000..da4e0add9ab --- /dev/null +++ b/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/Activator.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * Copyright (c) 2014 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: + * Juan Cadavid <juan.cadavid@cea.fr> - Initial API and implementation + * + ******************************************************************************/ +package org.eclipse.papyrus.mwe2.utils; + +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; + +public class Activator implements BundleActivator { + + private static BundleContext context; + + static BundleContext getContext() { + return context; + } + + /* + * (non-Javadoc) + * + * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) + */ + @Override + public void start(BundleContext bundleContext) throws Exception { + Activator.context = bundleContext; + } + + /* + * (non-Javadoc) + * + * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) + */ + @Override + public void stop(BundleContext bundleContext) throws Exception { + Activator.context = null; + } + +} diff --git a/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/XtendWorkflow.java b/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/XtendWorkflow.java new file mode 100644 index 00000000000..66276e05b71 --- /dev/null +++ b/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/XtendWorkflow.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2014, 2015 CEA LIST, Christian W. Damus, 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 + * Christian W. Damus - bug 464647 + * + ******************************************************************************/ +package org.eclipse.papyrus.mwe2.utils; + +import java.util.List; + +import org.eclipse.emf.mwe2.runtime.workflow.IWorkflowComponent; +import org.eclipse.emf.mwe2.runtime.workflow.Workflow; + +import com.google.common.collect.Lists; + + +/** + * A workflow for Xtend transformations. Primarily, it provides Xtend-compatible access to + * the collections manipulated by the inherited {@link Workflow#addBean(Object)} and + * {@link Workflow#addComponent(IWorkflowComponent)} APIs. + */ +public class XtendWorkflow extends Workflow { + + protected List<Object> getBeans() { + return Lists.newArrayList(); + } + + protected List<IWorkflowComponent> getComponents() { + return getChildren(); + } + +}
\ No newline at end of file diff --git a/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/components/MultiReader.java b/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/components/MultiReader.java new file mode 100644 index 00000000000..5f16d9dead2 --- /dev/null +++ b/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/components/MultiReader.java @@ -0,0 +1,74 @@ +/***************************************************************************** + * Copyright (c) 2015 Christian W. Damus 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: + * Christian W. Damus - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.mwe2.utils.components; + +import java.util.Collection; + +import org.eclipse.emf.mwe.core.WorkflowContext; +import org.eclipse.emf.mwe.core.issues.Issues; +import org.eclipse.emf.mwe.core.monitor.ProgressMonitor; +import org.eclipse.emf.mwe.utils.Reader; + +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; + +/** + * A {@link Reader} component that accumulates multiple models into a collection in the + * model slot. + */ +public class MultiReader extends Reader { + + public MultiReader() { + super(); + } + + @Override + public void invokeInternal(WorkflowContext ctx, ProgressMonitor monitor, Issues issues) { + final String slot = getModelSlot(); + + Object currentModelSlotContents = ctx.get(slot); + + super.invokeInternal(ctx, monitor, issues); + + Object newModelSlotContents = ctx.get(slot); + + Object contents; + if (newModelSlotContents == null) { + contents = currentModelSlotContents; + } else if (currentModelSlotContents == null) { + contents = newModelSlotContents; + } else if (currentModelSlotContents instanceof Iterable<?>) { + Collection<Object> collection = Lists.newArrayList((Iterable<?>) currentModelSlotContents); + if (newModelSlotContents instanceof Iterable<?>) { + Iterables.addAll(collection, (Iterable<?>) newModelSlotContents); + } else if (newModelSlotContents != null) { + collection.add(newModelSlotContents); + } + contents = collection; + } else if (newModelSlotContents instanceof Iterable<?>) { + Collection<Object> collection = Lists.newArrayList(); + collection.add(currentModelSlotContents); + Iterables.addAll(collection, (Iterable<?>) newModelSlotContents); + contents = collection; + } else { + Collection<Object> collection = Lists.newArrayListWithExpectedSize(2); + collection.add(currentModelSlotContents); + collection.add(newModelSlotContents); + contents = collection; + } + + ctx.set(slot, contents); + } + +} diff --git a/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/components/QvtoTransformationWorkflow.java b/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/components/QvtoTransformationWorkflow.java new file mode 100644 index 00000000000..a7b329fea48 --- /dev/null +++ b/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/components/QvtoTransformationWorkflow.java @@ -0,0 +1,144 @@ +/******************************************************************************* + * Copyright (c) 2014 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: + * Juan Cadavid <juan.cadavid@cea.fr> implementation + ******************************************************************************/ +package org.eclipse.papyrus.mwe2.utils.components; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.eclipse.emf.common.util.Diagnostic; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.mwe.core.WorkflowContext; +import org.eclipse.emf.mwe.core.issues.Issues; +import org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent; +import org.eclipse.emf.mwe.core.monitor.ProgressMonitor; +import org.eclipse.m2m.qvt.oml.BasicModelExtent; +import org.eclipse.m2m.qvt.oml.ExecutionContextImpl; +import org.eclipse.m2m.qvt.oml.ExecutionDiagnostic; +import org.eclipse.m2m.qvt.oml.ModelExtent; +import org.eclipse.m2m.qvt.oml.TransformationExecutor; +import org.eclipse.papyrus.mwe2.utils.messages.Messages; + +/** + * MWE Component to execute QVTO transformations. An example of use is: + * <blockquote> + * component = org.eclipse.papyrus.mwe.utils.components.QvtoTransformationWorkflowComponent { + * inputSlots = 'inputGmfgen' + * inputSlots = 'gmfgenUmlMetamodel' + * transformationURI = 'platform:/resource/org.eclipse.papyrus.tests.framework/qvto/gmfgen2uml.qvto' + * outputSlot = 'inputGmfgenUml' + * } + * </blockquote> + * where the multi-valued input slots are the input parameters for the transformation; + * there should be as many as there are declared in the qvto file; + * transformationURI is the location of the .qvto file and outputSlot the slot where + * the list of resulting eObjects of the transformation will be placed. + * + */ +public class QvtoTransformationWorkflow extends AbstractWorkflowComponent { + + private String transformationURI; + + private String outputSlot; + + private ArrayList<String> inputSlots = new ArrayList<String>(); + + private Log log = LogFactory.getLog(getClass()); + + public String getTransformationURI() { + return transformationURI; + } + + public void setTransformationURI(String transformationURI) { + this.transformationURI = transformationURI; + } + + public String getOutputSlot() { + return outputSlot; + } + + public void setOutputSlot(String outputSlot) { + this.outputSlot = outputSlot; + } + + public boolean addInputSlots(String c) { + return inputSlots.add(c); + } + + public ExecutionDiagnostic generate(List<? extends EObject> inObjects, URI transformationURI, WorkflowContext ctx, Issues issues) throws IOException { + // resolveProxies(inObjects); + log.info("Executing QVTO transformation " + getTransformationURI()); + + TransformationExecutor executor = new TransformationExecutor(transformationURI); + Diagnostic loadTransformationDiagnostic = executor.loadTransformation(); + if(!loadTransformationDiagnostic.getMessage().equals("OK")) { //$NON-NLS-1$ + log.error(Messages.QvtoTransformationWorkflowComponent_1 + loadTransformationDiagnostic); + return null; + } + ExecutionContextImpl context = new ExecutionContextImpl(); + ArrayList<ModelExtent> input = new ArrayList<ModelExtent>(); + for(EObject eObject : inObjects) { + BasicModelExtent basicModelExtent = new BasicModelExtent(); + basicModelExtent.add(eObject); + input.add(basicModelExtent); + } + issues.addInfo(new File("..").getAbsolutePath()); //$NON-NLS-1$ + ModelExtent output = new BasicModelExtent(); + input.add(output); + ModelExtent[] modelParameters = input.toArray(new ModelExtent[]{}); + context = new ExecutionContextImpl(); + context.setConfigProperty("keepModeling", true); //$NON-NLS-1$ + ExecutionDiagnostic result = executor.execute(context, modelParameters); + if(result.getSeverity() == Diagnostic.OK) { + List<EObject> outObjects = output.getContents(); + ctx.set(getOutputSlot(), outObjects); + log.info(Messages.QvtoTransformationWorkflowComponent_4 + getTransformationURI()); + } else { + issues.addError(result.toString()); + } + return result; + } + + @Override + protected void invokeInternal(WorkflowContext ctx, ProgressMonitor monitor, Issues issues) { + List<EObject> inObjects = new ArrayList<EObject>(); + for(String inputSlot : inputSlots) { + + Object inputObject = ctx.get(inputSlot); + if(inputObject instanceof EObject) { + inObjects.add((EObject)inputObject); + } else if(inputObject instanceof List<?>) { + List<EObject> list = (List<EObject>)inputObject; + inObjects.add(list.get(0)); + } + } + try { + generate(inObjects, URI.createURI(transformationURI), ctx, issues); + } catch (IOException e) { + log.error(e.getLocalizedMessage()); + } + + } + + @Override + public void checkConfiguration(Issues issues) { + if(inputSlots.isEmpty()) { + issues.addError(Messages.QvtoTransformationWorkflowComponent_5); + } + } + + + +} diff --git a/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/components/RegisterUmlProfile.java b/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/components/RegisterUmlProfile.java new file mode 100644 index 00000000000..fd12dfb411b --- /dev/null +++ b/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/components/RegisterUmlProfile.java @@ -0,0 +1,123 @@ +/******************************************************************************* + * Copyright (c) 2014 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: + * Juan Cadavid <juan.cadavid@cea.fr> implementation + ******************************************************************************/ +package org.eclipse.papyrus.mwe2.utils.components; + + +import java.lang.reflect.Field; + +import org.apache.commons.logging.LogFactory; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EPackage; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.emf.mwe.core.WorkflowContext; +import org.eclipse.emf.mwe.core.issues.Issues; +import org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent; +import org.eclipse.emf.mwe.core.monitor.ProgressMonitor; +import org.eclipse.emf.mwe.core.resources.ResourceLoaderFactory; +import org.eclipse.papyrus.mwe2.utils.messages.Messages; +import org.eclipse.uml2.uml.Profile; +import org.eclipse.uml2.uml.UMLPlugin; + +import com.google.common.base.Strings; + +/** + * This MWE component registers the EPackage definition of a UML profile in the global EPackage registry. + * An example of its usage is: + * + * <pre> + * component = org.eclipse.papyrus.mwe.utils.components.RegisterUmlProfile { + * profileSlot = 'ecoreprofile' + * } + * </pre> + * + * where profileSlot is a slot that contains the uml Profile object to register. + * Or, for a statically generated profile: + * + * <pre> + * component = org.eclipse.papyrus.mwe.utils.components.RegisterUmlProfile { + * profileSlot = 'utp' + * generatedPackageInterfaceName = 'org.eclipse.upr.utp.UTPPackage' + * } + * </pre> + * + * where profileSlot is a slot that contains the uml Profile object to register. + */ +public class RegisterUmlProfile extends AbstractWorkflowComponent { + private org.apache.commons.logging.Log log = LogFactory.getLog(getClass()); + + + private String profileSlot; + + private String generatedPackageInterfaceName; + + public String getProfileSlot() { + return profileSlot; + } + + public void setProfileSlot(String profileSlot) { + this.profileSlot = profileSlot; + } + + public String getGeneratedPackageInterfaceName() { + return generatedPackageInterfaceName; + } + + public void setGeneratedPackageInterfaceName(String generatedPackageInterfaceName) { + this.generatedPackageInterfaceName = generatedPackageInterfaceName; + } + + @Override + public void checkConfiguration(Issues issues) { + if (profileSlot == null || profileSlot.equals("")) { //$NON-NLS-1$ + issues.addError(Messages.RegisterUmlProfile_1); + } + } + + @Override + protected void invokeInternal(WorkflowContext ctx, ProgressMonitor monitor, + Issues issues) { + EObject eObject = (EObject) ctx.get(profileSlot); + if (!(eObject instanceof Profile)) { + log.error(Messages.RegisterUmlProfile_2); + return; + } + + Profile profile = (Profile) eObject; + + if (!Strings.isNullOrEmpty(getGeneratedPackageInterfaceName())) { + // It's a statically generated Profile + try { + Class<?> clazz = ResourceLoaderFactory.createResourceLoader().loadClass(getGeneratedPackageInterfaceName()); + if (clazz == null) { + throw new ClassNotFoundException(getGeneratedPackageInterfaceName()); + } + + Class<? extends EPackage> packageInterface = clazz.asSubclass(EPackage.class); + Field eNS_URI = packageInterface.getDeclaredField("eNS_URI"); //$NON-NLS-1$ + String nsURI = (String) eNS_URI.get(null); + UMLPlugin.getEPackageNsURIToProfileLocationMap().put(nsURI, EcoreUtil.getURI(profile)); + } catch (Exception e) { + log.error(String.format("Failed to register generated profile %s", getGeneratedPackageInterfaceName()), e); + } + } else { + // It's a dynamically defined profile + EPackage definition = profile.getDefinition(); + if (definition == null) { + log.error(Messages.RegisterUmlProfile_3); + return; + } + EPackage.Registry.INSTANCE.put(definition.getNsURI(), definition); + } + } + + + +} diff --git a/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/components/UMLWriter.java b/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/components/UMLWriter.java new file mode 100644 index 00000000000..3e61bc2a902 --- /dev/null +++ b/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/components/UMLWriter.java @@ -0,0 +1,274 @@ +/******************************************************************************* + * Copyright (c) 2005, 2009 committers of openArchitectureWare 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: + * committers of openArchitectureWare - initial API and implementation + * Christian W. Damus - adapt for UML resources + *******************************************************************************/ + +package org.eclipse.papyrus.mwe2.utils.components; + +import java.io.IOException; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.eclipse.emf.common.util.TreeIterator; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.InternalEObject; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.emf.ecore.xmi.XMLResource; +import org.eclipse.emf.ecore.xmi.impl.URIHandlerImpl; +import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl; +import org.eclipse.emf.mwe.core.WorkflowContext; +import org.eclipse.emf.mwe.core.WorkflowInterruptedException; +import org.eclipse.emf.mwe.core.issues.Issues; +import org.eclipse.emf.mwe.core.monitor.ProgressMonitor; +import org.eclipse.emf.mwe.utils.AbstractEMFWorkflowComponent; +import org.eclipse.emf.mwe.utils.Writer; +import org.eclipse.uml2.common.util.UML2Util; +import org.eclipse.uml2.uml.Element; +import org.eclipse.uml2.uml.resource.UMLResource; +import org.eclipse.xtext.xbase.lib.Pair; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterators; +import com.google.common.collect.Maps; + +/** + * An alternative to the core resource {@link Writer} component that provides + * UML-appropriate behaviour. + */ +public class UMLWriter extends AbstractEMFWorkflowComponent { + + private boolean platformSchemeAware = true; + + private boolean cloneSlotContents = false; + + private boolean ignoreEmptySlot = false; + + private boolean useSameResource = false; + + private boolean xmiIdentifiers = false; + + // Priority according to the order of insertion + private Map<Matcher, String> xmiIdentifierFilters = Maps.newLinkedHashMap(); + + @Override + public String getComponentName() { + return UMLWriter.class.getSimpleName(); + } + + @Override + public String getLogMessage() { + return "Writing UML model to " + getUri(); + } + + public boolean isPlatformSchemeAware() { + return platformSchemeAware; + } + + public void setPlatformSchemeAware(boolean platformSchemeAware) { + this.platformSchemeAware = platformSchemeAware; + } + + public boolean isIgnoreEmptySlot() { + return ignoreEmptySlot; + } + + public void setIgnoreEmptySlot(boolean ignoreEmptySlot) { + this.ignoreEmptySlot = ignoreEmptySlot; + } + + public boolean isCloneSlotContents() { + return cloneSlotContents; + } + + public void setCloneSlotContents(final boolean b) { + this.cloneSlotContents = b; + } + + public boolean isUseSameResource() { + return useSameResource; + } + + public void setUseSameResource(boolean useSameResource) { + this.useSameResource = useSameResource; + } + + public boolean isXmiIdentifiers() { + return xmiIdentifiers; + } + + public void setXmiIdentifiers(boolean setXMIIdentifiers) { + this.xmiIdentifiers = setXMIIdentifiers; + } + + public void addXmiIdentifierFilters(Pair<String, String> filter, Pair<String, String>... more) { + setXmiIdentifiers(true); // XMI identifiers are implicit if we're filtering them + + xmiIdentifierFilters.put(Pattern.compile(filter.getKey()).matcher(""), filter.getValue()); + for (int i = 0; i < more.length; i++) { + xmiIdentifierFilters.put(Pattern.compile(more[i].getKey()).matcher(""), more[i].getValue()); + } + } + + @SuppressWarnings("unchecked") + @Override + public void invokeInternal(final WorkflowContext ctx, final ProgressMonitor monitor, final Issues issues) { + Object slotContent = ctx.get(getModelSlot()); + if (slotContent == null) { + if (isIgnoreEmptySlot()) { + issues.addWarning(this, "slot '" + getModelSlot() + "' is empty. Not writing anything."); + } else { + issues.addError(this, "slot '" + getModelSlot() + "' is empty."); + } + return; + } + if (!((slotContent instanceof Collection<?>) || (slotContent instanceof EObject))) { + issues.addError(this, "slot '" + getModelSlot() + "' neither contains an EList nor an EObject", + slotContent, null, null); + return; + } + + if (slotContent instanceof EObject) { + final EObject sc = (EObject) slotContent; + if (isCloneSlotContents()) { + if (sc.eResource() == null) { + issues.addWarning(this, "model in slot '" + getModelSlot() + + "' is not yet associated with a resource; cloning it is most likely an error!"); + } else { + final EcoreUtil.Copier copier = new EcoreUtil.Copier(); + final EObject copy = copier.copy(sc); + copier.copyReferences(); + slotContent = copy; + } + } else { + if ((sc.eResource() != null) && !isUseSameResource()) { + issues.addWarning(this, "the element in slot '" + getModelSlot() + + "' is already contained in a resource and will be taken out of that resource!"); + } + } + } + + getResourceSet().getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl()); + getResourceSet().getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE); + + final URI uri = URI.createURI(getUri(), true); + final Resource r; + final EObject sc = (slotContent instanceof Collection<?>) + ? (EObject) ((Collection<?>) slotContent).iterator().next() + : (EObject) slotContent; + if ((sc.eResource() != null) && isUseSameResource()) { + // Just save this resource + r = sc.eResource(); + r.setURI(uri); + } else { + r = getResourceSet().createResource(uri); + + if (slotContent instanceof Collection<?>) { + r.getContents().addAll((Collection<EObject>) slotContent); + } else { + r.getContents().add((EObject) slotContent); + } + + addStereotypes(r); + } + + write(r, issues); + } + + protected void addStereotypes(Resource r) { + for (TreeIterator<EObject> iter = EcoreUtil.getAllContents(ImmutableList.copyOf(r.getContents())); iter.hasNext();) { + EObject next = iter.next(); + + if (next instanceof Element) { + r.getContents().addAll(((Element) next).getStereotypeApplications()); + } else { + iter.prune(); + } + } + } + + private void write(final Resource r, final Issues issues) { + try { + if (isXmiIdentifiers()) { + if (!(r instanceof XMLResource)) { + issues.addWarning(this, "resource is not an XML resource; cannot set XMI identifiers", r); + } else if (!xmiIdentifierFilters.isEmpty()) { + setXMIIdentifiers((XMLResource) r, xmiIdentifierFilters); + } else { + setXMIIdentifiers((XMLResource) r); + } + } + + final Map<String, Object> options = new HashMap<String, Object>(); + + if (isPlatformSchemeAware()) { + options.put(XMLResource.OPTION_URI_HANDLER, new URIHandlerImpl.PlatformSchemeAware()); + } + + r.save(options); + } catch (final IOException e) { + throw new WorkflowInterruptedException("Problems writing xmi file to " + getUri() + " : " + e.getMessage()); + } + } + + protected void setXMIIdentifiers(XMLResource resource) { + for (Iterator<InternalEObject> iter = Iterators.filter(resource.getAllContents(), InternalEObject.class); iter.hasNext();) { + InternalEObject next = iter.next(); + resource.setID(next, UML2Util.getXMIIdentifier(next)); + } + } + + protected void setXMIIdentifiers(XMLResource resource, Map<Matcher, String> filters) { + for (Iterator<InternalEObject> iter = Iterators.filter(resource.getAllContents(), InternalEObject.class); iter.hasNext();) { + InternalEObject next = iter.next(); + + String id = filter(resource, filters, next); + if (id != null) { + // Can use the filtered ID + resource.setID(next, id); + } + } + } + + private String filter(XMLResource resource, Map<Matcher, String> filters, InternalEObject object) { + String id = UML2Util.getXMIIdentifier(object); + String result = id; + + for (Map.Entry<Matcher, String> filter : filters.entrySet()) { + Matcher m = filter.getKey(); + m.reset(id); + if (m.find()) { + String filtered = m.replaceFirst(filter.getValue()); + EObject collision = resource.getEObject(filtered); + if ((collision == null) || (collision == object)) { + result = filtered; + break; + } + } + } + + if (result.equals(id)) { + // Check this for a collision, too, because filtering could introduce duplication + EObject collision = resource.getEObject(result); + int counter = 0; + while ((collision != null) && (collision != object)) { + result = id + '$' + ++counter; + collision = resource.getEObject(result); + } + } + + return result; + } +} diff --git a/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/messages/Messages.java b/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/messages/Messages.java new file mode 100644 index 00000000000..7887428b52d --- /dev/null +++ b/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/messages/Messages.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (c) 2014, 2015 CEA LIST, Christian W. Damus, 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 + * Christian W. Damus - bug 464647 + * + ******************************************************************************/ +package org.eclipse.papyrus.mwe2.utils.messages; + +import org.eclipse.osgi.util.NLS; + +/** + * Localized strings for the bundle. + */ +public class Messages extends NLS { + + private static final String BUNDLE_NAME = "org.eclipse.papyrus.mwe2.utils.messages.messages"; //$NON-NLS-1$ + + public static String DeleteFile_0; + + public static String DeleteFile_1; + + public static String DeleteFile_2; + + public static String DeleteFile_3; + + public static String DeleteFile_4; + + public static String DeleteFile_5; + + public static String QvtoTransformationWorkflowComponent_1; + + public static String QvtoTransformationWorkflowComponent_4; + + public static String QvtoTransformationWorkflowComponent_5; + + public static String RegisterUmlProfile_1; + + public static String RegisterUmlProfile_2; + + public static String RegisterUmlProfile_3; + + static { + // initialize resource bundle + NLS.initializeMessages(BUNDLE_NAME, Messages.class); + } + + private Messages() { + } +} diff --git a/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/messages/messages.properties b/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/messages/messages.properties new file mode 100644 index 00000000000..f5a2d43d52b --- /dev/null +++ b/plugins/toolsmiths/org.eclipse.papyrus.mwe2.utils/src/org/eclipse/papyrus/mwe2/utils/messages/messages.properties @@ -0,0 +1,12 @@ +DeleteFile_0=No sourceFile set. +DeleteFile_1=Source file +DeleteFile_2=\ does not exist. Skipping. +DeleteFile_3=Cannot write in +DeleteFile_4=\ is a directory. It will be completely deleted. +DeleteFile_5=\ was not deleted\! +QvtoTransformationWorkflowComponent_1=Error loading the model transformation: +QvtoTransformationWorkflowComponent_4=Successful QVTO transformation +QvtoTransformationWorkflowComponent_5=No input slots given for the QVTO transformation +RegisterUmlProfile_1=Profile URI not provided +RegisterUmlProfile_2=The profile URI does not contain a Profile object +RegisterUmlProfile_3=The profile has not been defined |