001package org.andromda.schema2xmi; 002 003import java.util.Collection; 004import org.apache.commons.collections.CollectionUtils; 005import org.apache.commons.collections.Predicate; 006import org.apache.commons.lang.StringUtils; 007import org.omg.uml.foundation.core.ModelElement; 008import org.omg.uml.modelmanagement.Model; 009import org.omg.uml.modelmanagement.UmlPackage; 010 011/** 012 * Finds model elements by their names. 013 * 014 * @author Chad Brandon 015 */ 016public class ModelElementFinder 017{ 018 /** 019 * Finds the model element having the <code>fullyQualifiedName</code> in 020 * the <code>model</code>, returns <code>null</code> if not found. 021 * 022 * @param model The model to search 023 * @param fullyQualifiedName the fully qualified name to find. 024 * @return the found model element. 025 */ 026 public static Object find( 027 Model model, 028 String fullyQualifiedName) 029 { 030 Object modelElement = null; 031 if (model != null) 032 { 033 String[] names = fullyQualifiedName.split(Schema2XMIGlobals.PACKAGE_SEPARATOR); 034 if (names != null && names.length > 0) 035 { 036 Object element = model; 037 for (int ctr = 0; ctr < names.length && element != null; ctr++) 038 { 039 String name = names[ctr]; 040 if (UmlPackage.class.isAssignableFrom(element.getClass())) 041 { 042 element = getElement( 043 ((UmlPackage)element).getOwnedElement(), 044 name); 045 } 046 modelElement = element; 047 } 048 } 049 } 050 return modelElement; 051 } 052 053 /** 054 * Finds and returns the first model element having the given 055 * <code>name</code> in the <code>modelPackage</code>, returns 056 * <code>null</code> if not found. 057 * 058 * @param modelPackage The modelPackage to search 059 * @param name the name to find. 060 * @return the found model element. 061 */ 062 public static Object find( 063 org.omg.uml.UmlPackage modelPackage, 064 final String name) 065 { 066 return CollectionUtils.find( 067 modelPackage.getCore().getModelElement().refAllOfType(), 068 new Predicate() 069 { 070 public boolean evaluate(Object object) 071 { 072 return StringUtils.trimToEmpty(((ModelElement)object).getName()).equals(name); 073 } 074 }); 075 } 076 077 /** 078 * Finds and returns the first model element having the given 079 * <code>name</code> in the <code>umlPackage</code>, returns 080 * <code>null</code> if not found. 081 * 082 * @param umlPackage The modelPackage to search 083 * @param name the name to find. 084 * @return the found model element. 085 */ 086 public static Object find( 087 org.omg.uml.modelmanagement.UmlPackage umlPackage, 088 final String name) 089 { 090 return CollectionUtils.find( 091 umlPackage.getOwnedElement(), 092 new Predicate() 093 { 094 public boolean evaluate(Object object) 095 { 096 return StringUtils.trimToEmpty(((ModelElement)object).getName()).equals(name); 097 } 098 }); 099 } 100 101 /** 102 * Finds the model element having the <code>name</code> contained within 103 * the <code>elements</code>, returns null if it can't be found. 104 * 105 * @param elements the collection of model elements to search 106 * @param name the name of the model element. 107 * @return the found model element or null if not found. 108 */ 109 private static Object getElement( 110 Collection elements, 111 final String name) 112 { 113 return CollectionUtils.find( 114 elements, 115 new Predicate() 116 { 117 public boolean evaluate(Object object) 118 { 119 return StringUtils.trimToEmpty(((ModelElement)object).getName()).equals(name); 120 } 121 }); 122 } 123}