1 /*******************************************************************************
2  * Copyright (c) 2014 TwelveTone LLC and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  * Steven Spungin <steven@spungin.tv> - initial API and implementation, Ongoing Maintenance
13  *******************************************************************************/
14 
15 package org.eclipse.e4.tools.emf.ui.internal.common.component.tabs;
16 
17 import org.eclipse.emf.ecore.EAttribute;
18 import org.eclipse.emf.ecore.EObject;
19 
20 /**
21  * Convenience methods for accessing EMF Objects
22  *
23  * @author Steven Spungin
24  *
25  */
26 public class EmfUtil {
27 	/**
28 	 * Returns the EAttribute with the given name for the specified object, or
29 	 * null if non-existent
30 	 *
31 	 * @param eObject
32 	 * @param attName
33 	 * @return the EAttribute with the given name for the specified object, or
34 	 *         null if non-existent
35 	 */
getAttribute(EObject eObject, String attName)36 	static public EAttribute getAttribute(EObject eObject, String attName) {
37 		if (attName == null || attName.isEmpty()) {
38 			return null;
39 		}
40 		// return (EAttribute)
41 		// eObject.eGet(eObject.eClass().getEStructuralFeature(attName));
42 		for (final EAttribute att : eObject.eClass().getEAllAttributes()) {
43 			if (attName.equals(att.getName())) {
44 				return att;
45 			}
46 		}
47 		return null;
48 	}
49 
50 	/**
51 	 * Returns the EAttribute value with the given attribute name for the
52 	 * specified object. Returns null if the attribute is not define, or has
53 	 * null as the value.
54 	 *
55 	 * @param eObject
56 	 * @param attName
57 	 * @return the EAttribute value with the given attribute name for the
58 	 *         specified object, Returns null if the attribute is not defined,
59 	 *         or has null as the value.
60 	 */
getAttributeValue(EObject eObject, String attName)61 	static public Object getAttributeValue(EObject eObject, String attName) {
62 		final EAttribute att = getAttribute(eObject, attName);
63 		if (att == null) {
64 			return null;
65 		}
66 		return eObject.eGet(att);
67 	}
68 
69 	/**
70 	 * Returns the EAttribute value with the given attribute name for the
71 	 * specified object. Throws if the attribute is not defined.
72 	 *
73 	 * @param eObject
74 	 * @param attName
75 	 * @throws Exception
76 	 */
getAttributeValueThrows(EObject eObject, String attName)77 	static public Object getAttributeValueThrows(EObject eObject, String attName) throws Exception {
78 		final EAttribute att = getAttribute(eObject, attName);
79 		if (att == null) {
80 			throw new Exception(Messages.EmfUtil_ex_attribute_not_found + " : " + attName); //$NON-NLS-1$
81 		}
82 		return eObject.eGet(att);
83 	}
84 }