org.openxma.xmadsl.LinkerUtil Maven / Gradle / Ivy
package org.openxma.xmadsl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.openxma.dsl.common.logging.LogUtil;
import org.openxma.dsl.pom.naming.PomNameProvider;
/**
* Utility for linking
*
*/
public class LinkerUtil {
private static final HashMap> elementsWithReferences = new HashMap>();
/**
* Get a list with the names of all done linkings for the rootNode
*
* @param rootNode
* @return
*/
public static ArrayList getLinkingsDone(EObject eObj) {
if (eObj == null) {
throw new IllegalArgumentException("Root node has to be defined");
}
ArrayList list = elementsWithReferences.get(eObj);
if (list == null) {
list = new ArrayList();
elementsWithReferences.put(eObj,list);
}
return list;
}
/**
*
* @param rootNode
*/
public static void initLinker(EObject rootNode) {
elementsWithReferences.clear();
//LogUtil.log("Init linker "+rootNode.getClass()+rootNode.toString());
}
public static boolean equalsObject(EObject e1, EObject e2) {
if (e1.getClass() != e2.getClass()) {
return false;
} else {
String id1 = getId(e1);
String id2 = getId(e2);
if (id1 != null && id2 != null) {
return id1.equals(id2);
} else {
if (id1==null) LogUtil.log("!! Id not found: e1= "+e1.getClass()+", id= "+id1);
if (id2==null) LogUtil.log("!! Id not found: e2= "+e2.getClass()+", id= "+id2);
return false;
}
}
}
public static String getId(EObject obj) {
return PomNameProvider.getXmaName(obj);
}
/**
*
* @param obj
* @return
*/
public static List getModelObjectPath(EObject obj) {
if (obj.eContainer() != null) {
List list =getModelObjectPath(obj.eContainer());
list.add(obj);
return list;
} else {
List newList = new ArrayList();
newList.add(obj);
return newList;
}
}
/**
*
* @param currentElement
* @param path
* @param index
* @return
*/
public static EObject findXMAPathElement(EObject currentElement, List path) {
return findXMAPathElement(currentElement,path,0);
}
public static EObject findXMAPathElement(EObject currentElement, List path, Integer index) {
EObject pathNodeToMatch = path.get(index);
if (LinkerUtil.equalsObject(currentElement,pathNodeToMatch)) {
if (index < path.size()-1) {
return findXMAPathElementInChildren(currentElement,path,index+1);
} else{
return currentElement;
}
}
return null;
}
/**
*
*/
public static EObject findXMAPathElementInChildren(EObject currentElement, List path, Integer index) {
EObject pathNodeToMatch = path.get(index);
List currentChildren = currentElement.eContents();
EObject matchingChild = null;
for (EObject elem : currentChildren) {
if (LinkerUtil.equalsObject(elem,pathNodeToMatch)) {
matchingChild = elem;
break;
}
}
//logListElements("currentChildren",currentChildren)->
if (matchingChild != null) {
return findXMAPathElement(matchingChild,path,index);
}
return null;
}
}