| /** |
| * <copyright> |
| * |
| * Copyright (c) 2013, 2018 Willink Transformations and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v2.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v20.html |
| * |
| * Contributors: |
| * E.D.Willink - initial API and implementation |
| * |
| * </copyright> |
| */ |
| package org.eclipse.ocl.examples.build.utilities; |
| |
| import java.util.ArrayList; |
| import java.util.Collections; |
| import java.util.Comparator; |
| import java.util.List; |
| |
| import org.apache.log4j.Logger; |
| import org.eclipse.emf.ecore.EClass; |
| import org.eclipse.emf.ecore.ENamedElement; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.EPackage; |
| import org.eclipse.emf.ecore.EcorePackage; |
| 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.mwe.core.WorkflowContext; |
| import org.eclipse.emf.mwe.core.issues.Issues; |
| import org.eclipse.emf.mwe.core.lib.WorkflowComponentWithModelSlot; |
| import org.eclipse.emf.mwe.core.monitor.ProgressMonitor; |
| import org.eclipse.jdt.annotation.NonNull; |
| |
| /** |
| * Changes the NsURI in an Ecore <tt>modelSlot</tt> by |
| * <br> alphabeticizing packages/classifiers/attributes/operations/constraints |
| * <br> eliminating comments. |
| */ |
| public class SetNsURI extends WorkflowComponentWithModelSlot |
| { |
| private Logger log = Logger.getLogger(getClass()); |
| private ResourceSet resourceSet = null; |
| private String nsURI = null; |
| |
| public @NonNull ResourceSet getResourceSet() { |
| ResourceSet resourceSet2 = resourceSet; |
| if (resourceSet2 == null) { |
| resourceSet = resourceSet2 = new ResourceSetImpl(); |
| } |
| return resourceSet2; |
| } |
| |
| @Override |
| public void invokeInternal(WorkflowContext ctx, ProgressMonitor arg1, Issues arg2) { |
| Resource oldResource = (Resource) ctx.get(getModelSlot()); |
| log.info("Setting '" + oldResource.getURI() + "' to '" + nsURI + "'"); |
| for (EObject eObject : oldResource.getContents()) { |
| if (eObject instanceof EPackage) { |
| ((EPackage)eObject).setNsURI(nsURI); |
| break; |
| } |
| } |
| /// ctx.set(getModelSlot(), newResource); |
| } |
| |
| protected <@NonNull T extends ENamedElement> void sortList(List<T> list) { |
| List<T> newList = new ArrayList<T>(list); |
| Collections.sort(newList, new Comparator<T>() |
| { |
| public int compare(T o1, T o2) { |
| EClass e1 = o1.eClass(); |
| EClass e2 = o2.eClass(); |
| if (e1 != e2) { |
| if (EcorePackage.Literals.EATTRIBUTE.isSuperTypeOf(e1)) { |
| return -1; |
| } |
| else if (EcorePackage.Literals.EATTRIBUTE.isSuperTypeOf(e2)) { |
| return 1; |
| } |
| if (EcorePackage.Literals.EDATA_TYPE.isSuperTypeOf(e1)) { |
| return -1; |
| } |
| else if (EcorePackage.Literals.EDATA_TYPE.isSuperTypeOf(e2)) { |
| return 1; |
| } |
| if (EcorePackage.Literals.EENUM.isSuperTypeOf(e1)) { |
| return -1; |
| } |
| else if (EcorePackage.Literals.EENUM.isSuperTypeOf(e2)) { |
| return 1; |
| } |
| if (EcorePackage.Literals.ECLASS.isSuperTypeOf(e1)) { |
| return -1; |
| } |
| else if (EcorePackage.Literals.ECLASS.isSuperTypeOf(e2)) { |
| return 1; |
| } |
| } |
| String n1 = o1.getName(); |
| String n2 = o2.getName(); |
| return n1.compareTo(n2); |
| } |
| }); |
| list.clear(); |
| list.addAll(newList); |
| } |
| |
| public void setNsURI(@NonNull String nsURI) { |
| this.nsURI = nsURI; |
| } |
| |
| public void setResourceSet(@NonNull ResourceSet resourceSet) { |
| this.resourceSet = resourceSet; |
| } |
| } |