Skip to main content
summaryrefslogtreecommitdiffstats
blob: f44c0e38b38fe3113fce5bb45e6c1ce78d55d008 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*******************************************************************************
 *  Copyright (c) 2007 Oracle. All rights reserved. This
 *  program and the accompanying materials are made available under the terms of
 *  the Eclipse Public License v1.0 which accompanies this distribution, and is
 *  available at http://www.eclipse.org/legal/epl-v10.html
 *  
 *  Contributors: Oracle. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jpt.core.internal.content.orm.resource;

import java.util.HashMap;
import java.util.Map;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.jpt.core.internal.content.orm.OrmPackage;
import org.eclipse.jpt.core.internal.content.orm.resource.AssociationOverrideTranslator.AssociationOverrideBuilder;
import org.eclipse.wst.common.internal.emf.resource.MultiObjectTranslator;
import org.eclipse.wst.common.internal.emf.resource.Translator;

/**
 * Created this class so there would be a 1-1 association between 
 * AssociationOverride and AssociationOverrideTranslator.  Without this the state
 * stored on AssociationOverrideTranslator will be invalid for multiple AssociationOverrides.
 * see bug 188901.
 * 
 * TODO 189767 - memory leak if an associationOverride is removed from the model, it is still
 * stored along with its translator in the translator map
 */
public class AssociationOverridesTranslator extends MultiObjectTranslator implements OrmXmlMapper
{
	private AssociationOverrideBuilder associationOverrideBuilder;

	private Map<EObject, AssociationOverrideTranslator> translatorMap;
	
	public AssociationOverridesTranslator(String domNameAndPath, EStructuralFeature aFeature, AssociationOverrideBuilder associationOverrideBuilder) {
		super(domNameAndPath, aFeature);
		this.associationOverrideBuilder = associationOverrideBuilder;
		this.translatorMap = new HashMap<EObject, AssociationOverrideTranslator>();
	}
	@Override
	public EObject createEMFObject(String nodeName, String readAheadName) {
		AssociationOverrideTranslator translator = (AssociationOverrideTranslator) getDelegateFor(nodeName, readAheadName);
		EObject eObject = translator.createEMFObject(nodeName, readAheadName);
		this.translatorMap.put(eObject, translator);
		return eObject;
	}
	
	/* (non-Javadoc)
	 * @see MultiObjectTranslator#getDelegateFor(EObject)
	 */
	@Override
	public Translator getDelegateFor(EObject o) {
		Translator translator = translatorMap.get(o);
		if (translator != null) {
			return translator;
		}
		
		switch (o.eClass().getClassifierID()) {
			case OrmPackage.XML_ASSOCIATION_OVERRIDE :
				return new AssociationOverrideTranslator(this.domNameAndPath, getFeature(), this.associationOverrideBuilder);
		}
		
		return null;
	}
	
	@Override
	public Translator getDelegateFor(String domName, String readAheadName) {
		if (domName.equals(ENTITY__ASSOCIATION_OVERRIDE)) {
			return new AssociationOverrideTranslator(this.domNameAndPath, getFeature(), this.associationOverrideBuilder);
		}
		throw new IllegalStateException("Illegal dom name: " + domName); //$NON-NLS-1$
	}

}

Back to the top