KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > xmi > impl > EMOFHelperImpl


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2003-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: EMOFHelperImpl.java,v 1.5 2005/06/08 06:16:07 nickb Exp $
16  */

17 package org.eclipse.emf.ecore.xmi.impl;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.eclipse.emf.common.util.TreeIterator;
25 import org.eclipse.emf.ecore.EAnnotation;
26 import org.eclipse.emf.ecore.EClass;
27 import org.eclipse.emf.ecore.EDataType;
28 import org.eclipse.emf.ecore.EFactory;
29 import org.eclipse.emf.ecore.EObject;
30 import org.eclipse.emf.ecore.EPackage;
31 import org.eclipse.emf.ecore.EReference;
32 import org.eclipse.emf.ecore.EStructuralFeature;
33 import org.eclipse.emf.ecore.ETypedElement;
34 import org.eclipse.emf.ecore.EcoreFactory;
35 import org.eclipse.emf.ecore.EcorePackage;
36 import org.eclipse.emf.ecore.util.EContentsEList;
37 import org.eclipse.emf.ecore.util.EcoreUtil;
38 import org.eclipse.emf.ecore.xmi.XMLResource;
39
40
41 public class EMOFHelperImpl extends XMLHelperImpl implements EMOFHandler.Helper
42 {
43   protected EClass propertyClass = null;
44   protected List JavaDoc propertyFeatureList = null;
45
46   public EMOFHelperImpl(XMLResource resource)
47   {
48     super(resource);
49     packages.put(EcorePackage.eINSTANCE, EMOFExtendedMetaData.EMOF_PACKAGE_NS_PREFIX);
50   }
51
52   public Object JavaDoc getValue(EObject object, EStructuralFeature feature)
53   {
54     if (feature == EcorePackage.eINSTANCE.getEStructuralFeature_Changeable())
55     {
56       return ((EStructuralFeature)object).isChangeable() ? Boolean.FALSE : Boolean.TRUE; // EMOF.isReadOnly = !Ecore.changeable
57
}
58     else
59     {
60       return super.getValue(object, feature);
61     }
62   }
63
64   public void setValue(EObject object, EStructuralFeature feature, Object JavaDoc value, int position)
65   {
66     if (feature == EcorePackage.eINSTANCE.getEStructuralFeature_Changeable())
67     {
68       ((EStructuralFeature)object).setChangeable(Boolean.FALSE.toString().equals(value)); // Ecore.changeable = !EMOF.isReadOnly
69
}
70     else if (feature == EcorePackage.eINSTANCE.getETypedElement_UpperBound() && "*".equals(value))
71     {
72       ((ETypedElement)object).setUpperBound(-1);
73     }
74     else
75     {
76       super.setValue(object, feature, value, position);
77     }
78   }
79
80   public String JavaDoc getHREF(EObject obj)
81   {
82     String JavaDoc href = super.getHREF(obj);
83     if (href.startsWith(EMOFExtendedMetaData.ECORE_EDATATYPE_HREF_PREFIX))
84     {
85       String JavaDoc dataType = href.substring(EMOFExtendedMetaData.ECORE_EDATATYPE_HREF_PREFIX.length());
86       for (int i = 0; i < EMOFExtendedMetaData.MAPPED_ECORE_EDATATYPES.length; i++)
87       {
88         if (dataType.equals(EMOFExtendedMetaData.MAPPED_ECORE_EDATATYPES[i]))
89         {
90           return EMOFExtendedMetaData.MAPPED_EMOF_EDATATYPE_HREF_PREFIX + EMOFExtendedMetaData.MAPPED_EMOF_EDATATYPES[i];
91         }
92       }
93       return EMOFExtendedMetaData.UNMAPPED_EMOF_EDATATYPE_HREF_PREFIX + dataType;
94     }
95
96     return href;
97   }
98
99   public EStructuralFeature getFeature(EClass eClass, String JavaDoc namespaceURI, String JavaDoc name, boolean isElement)
100   {
101     if (eClass == EcorePackage.eINSTANCE.getEAnnotation() && EMOFExtendedMetaData.EMOF_TAG_ELEMENT.equals(name))
102     {
103       return EcorePackage.eINSTANCE.getEAnnotation_References();
104     }
105
106     for (int i = 0, size = eClass.getFeatureCount(); i < size; ++i)
107     {
108       EStructuralFeature eStructuralFeature = eClass.getEStructuralFeature(i);
109       if (name.equals(extendedMetaData.getName(eStructuralFeature)) &&
110             (namespaceURI == null ?
111              extendedMetaData.getNamespace(eStructuralFeature) == null :
112              namespaceURI.equals(extendedMetaData.getNamespace(eStructuralFeature))))
113       {
114         computeFeatureKind(eStructuralFeature);
115         return eStructuralFeature;
116       }
117     }
118
119     return null;
120   }
121
122   public EObject createObject(EFactory eFactory, String JavaDoc classXMIName)
123   {
124     EPackage ePackage = eFactory.getEPackage();
125     if (ePackage == EcorePackage.eINSTANCE)
126     {
127       if (EcorePackage.eINSTANCE.getEStructuralFeature().getName().equals(classXMIName))
128       {
129         if (propertyClass == null)
130         {
131           propertyClass = EcoreFactory.eINSTANCE.createEClass();
132           propertyClass.getESuperTypes().add(EcorePackage.eINSTANCE.getEReference());
133           propertyClass.getESuperTypes().add(EcorePackage.eINSTANCE.getEAttribute());
134           propertyClass.setName("EMOFProperty");
135
136           EPackage propertyPackage = EcoreFactory.eINSTANCE.createEPackage();
137           propertyPackage.getEClassifiers().add(propertyClass);
138
139           propertyFeatureList = new ArrayList JavaDoc();
140         }
141         EObject property = propertyClass.getEPackage().getEFactoryInstance().create(propertyClass);
142         propertyFeatureList.add(property);
143         return property;
144       }
145       else if (EMOFExtendedMetaData.TAG.equals(classXMIName))
146       {
147         EAnnotation annotation = EcoreFactory.eINSTANCE.createEAnnotation();
148         annotation.setSource(EMOFExtendedMetaData.EMOF_PACKAGE_NS_URI);
149         return annotation;
150       }
151     }
152     return super.createObject(eFactory, classXMIName);
153   }
154
155   public void convertPropertyFeatures()
156   {
157     if (propertyFeatureList != null)
158     {
159       EcoreUtil.Copier copier =
160         new EcoreUtil.Copier()
161         {
162           protected EObject createCopy(EObject eObject)
163           {
164             EClass eClass =
165               ((EStructuralFeature)eObject).getEType() instanceof EDataType
166                 ? EcorePackage.eINSTANCE.getEAttribute()
167                 : EcorePackage.eINSTANCE.getEReference();
168             return EcoreUtil.create(eClass);
169           }
170           protected void copyContainment(EReference eReference, EObject eObject, EObject copyEObject)
171           {
172             // eAnnotations is the only possible containment reference. We'll move the annotations instead of copy
173
((EStructuralFeature)copyEObject).getEAnnotations().addAll(((EStructuralFeature)eObject).getEAnnotations());
174           }
175         };
176       copier.copyAll(propertyFeatureList);
177       copier.copyReferences();
178
179       for (Iterator JavaDoc iter = copier.entrySet().iterator(); iter.hasNext(); )
180       {
181         Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
182         EStructuralFeature emofFeature = (EStructuralFeature)entry.getKey();
183         EStructuralFeature ecoreFeature = (EStructuralFeature)entry.getValue();
184         EClass eClass = emofFeature.getEContainingClass();
185         eClass.getEStructuralFeatures().set(eClass.getEStructuralFeatures().indexOf(emofFeature), ecoreFeature);
186       }
187
188       for (TreeIterator contents = EcoreUtil.getAllContents(resource.getContents()); contents.hasNext(); )
189       {
190         EObject eObject = (EObject)contents.next();
191         for (EContentsEList.FeatureIterator featureIterator =
192              (EContentsEList.FeatureIterator)eObject.eCrossReferences().iterator(); featureIterator.hasNext(); )
193         {
194           EObject targetEObject = (EObject)featureIterator.next();
195           EObject copyEObject = (EObject)copier.get(targetEObject);
196           if (copyEObject != null)
197           {
198             EReference eReference = (EReference)featureIterator.feature();
199             if (eReference.isMany())
200             {
201               List JavaDoc list = (List JavaDoc)eObject.eGet(eReference);
202               list.set(list.indexOf(targetEObject), copyEObject);
203             }
204             else
205             {
206               eObject.eSet(eReference, copyEObject);
207             }
208           }
209         }
210       }
211     }
212   }
213 }
214
Popular Tags