cbridgha | 5420bad | 2004-11-11 21:11:43 +0000 | [diff] [blame] | 1 | /*************************************************************************************************** |
| 2 | * Copyright (c) 2003, 2004 IBM Corporation and others. All rights reserved. This program and the |
| 3 | * accompanying materials are made available under the terms of the Eclipse Public License v1.0 |
| 4 | * which accompanies this distribution, and is available at |
| 5 | * http://www.eclipse.org/legal/epl-v10.html |
| 6 | * |
| 7 | * Contributors: IBM Corporation - initial API and implementation |
| 8 | **************************************************************************************************/ |
| 9 | package org.eclipse.wst.common.internal.emf.resource; |
| 10 | |
| 11 | |
| 12 | import java.util.Iterator; |
| 13 | import java.util.List; |
| 14 | |
| 15 | import org.eclipse.emf.ecore.EObject; |
| 16 | import org.eclipse.emf.ecore.EStructuralFeature; |
| 17 | |
| 18 | |
| 19 | public class TranslatorPath { |
| 20 | protected Translator[] mapInfoPath; |
| 21 | |
| 22 | /** |
| 23 | * Construct with an array of Translator that specifies the path to follow from an object to |
| 24 | * another object |
| 25 | */ |
| 26 | public TranslatorPath(Translator[] path) { |
| 27 | mapInfoPath = path; |
| 28 | } |
| 29 | |
| 30 | public List findObjects(EObject startObject) { |
| 31 | Object cur = startObject; |
| 32 | for (int i = 0; i < mapInfoPath.length; i++) { |
| 33 | Translator curMap = mapInfoPath[i]; |
| 34 | if (cur instanceof EObject) { |
| 35 | EStructuralFeature curAttr = curMap.getFeature(); |
| 36 | if (curAttr == Translator.CONTAINER_FEATURE) { |
| 37 | curAttr = ((EObject) cur).eContainmentFeature(); |
| 38 | cur = ((EObject) cur).eContainer(); |
| 39 | } else if (curAttr == Translator.ROOT_FEATURE) { |
| 40 | cur = ((TranslatorResource) startObject.eResource()).getRootObject(); |
| 41 | } else { |
| 42 | cur = ((EObject) cur).eGet(curAttr); |
| 43 | } |
| 44 | if (curMap.isMultiValued()) { |
| 45 | return (List) cur; |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | public Object findObject(EObject startObject, Object matchValue) { |
| 53 | List objects = findObjects(startObject); |
| 54 | if (objects == null) |
| 55 | return null; |
| 56 | return findObject(objects, getLastMap(), matchValue); |
| 57 | } |
| 58 | |
| 59 | private Object findObject(List objectList, Translator map, Object matchValue) { |
| 60 | for (Iterator iter = objectList.iterator(); iter.hasNext();) { |
| 61 | EObject mofObject = (EObject) iter.next(); |
| 62 | Object curMatchValue = mofObject.eGet(map.getFeature()); |
| 63 | if (matchValue.equals(curMatchValue)) |
| 64 | return mofObject; |
| 65 | } |
| 66 | return null; |
| 67 | } |
| 68 | |
| 69 | public Translator getLastMap() { |
| 70 | return mapInfoPath[mapInfoPath.length - 1]; |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * (non-Javadoc) |
| 75 | * |
| 76 | * @see java.lang.Object#toString() |
| 77 | */ |
jsholl | 42fbbe5 | 2009-06-09 19:28:41 +0000 | [diff] [blame] | 78 | @Override |
cbridgha | 5420bad | 2004-11-11 21:11:43 +0000 | [diff] [blame] | 79 | public String toString() { |
| 80 | StringBuffer sb = new StringBuffer(); |
| 81 | String cn = getClass().getName(); |
| 82 | int i = cn.lastIndexOf('.'); |
| 83 | cn = cn.substring(++i, cn.length()); |
| 84 | sb.append(cn); |
| 85 | sb.append('('); |
| 86 | sb.append(mapInfoPath[0]); |
| 87 | for (int j = 1; j < mapInfoPath.length; j++) { |
| 88 | sb.append('\n'); |
| 89 | sb.append(mapInfoPath[j]); |
| 90 | } |
| 91 | sb.append(')'); |
| 92 | return sb.toString(); |
| 93 | } |
| 94 | |
| 95 | } |