KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-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: XMLMapImpl.java,v 1.3 2005/06/08 06:16:07 nickb Exp $
16  */

17 package org.eclipse.emf.ecore.xmi.impl;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 import org.eclipse.emf.common.util.EList;
28 import org.eclipse.emf.common.util.EMap;
29 import org.eclipse.emf.common.util.UniqueEList;
30 import org.eclipse.emf.ecore.EAnnotation;
31 import org.eclipse.emf.ecore.EClass;
32 import org.eclipse.emf.ecore.EClassifier;
33 import org.eclipse.emf.ecore.ENamedElement;
34 import org.eclipse.emf.ecore.EPackage;
35 import org.eclipse.emf.ecore.EStructuralFeature;
36 import org.eclipse.emf.ecore.xmi.XMLResource;
37
38 /**
39  * This class represents a mapping from Ecore constructs to the
40  * XML representation of those constructs. It is used by the
41  * XML serializer and deserializer to load and save XML files.
42  */

43 public class XMLMapImpl implements XMLResource.XMLMap
44 {
45   protected static final String JavaDoc XSD2ECORE = "http:///org/eclipse/emf/mapping/xsd2ecore/XSD2Ecore";
46
47   protected Map JavaDoc ecoreToXMLInfo;
48   protected EPackage noNamespacePkg;
49   protected String JavaDoc idAttributeName;
50   protected Map JavaDoc urisToNamesToClassifiers;
51   protected Map JavaDoc eClassToFeatures;
52   protected Set JavaDoc processedEPackages;
53   protected EPackage.Registry packageRegistry = EPackage.Registry.INSTANCE;
54
55   /**
56    * Constructor for XMLMap.
57    */

58   public XMLMapImpl()
59   {
60     super();
61     ecoreToXMLInfo = new HashMap JavaDoc();
62     processedEPackages = new HashSet JavaDoc();
63     eClassToFeatures = new HashMap JavaDoc();
64   }
65
66   /**
67    * Add an XMLInfo object for an Ecore construct to
68    * the map.
69    */

70   public void add(ENamedElement element, XMLResource.XMLInfo info)
71   {
72     ecoreToXMLInfo.put(element, info);
73   }
74
75   /**
76    * Returns the XMLInfo object for the given Ecore
77    * construct, if there is one.
78    */

79   public XMLResource.XMLInfo getInfo(ENamedElement element)
80   {
81     XMLResource.XMLInfo result = (XMLResource.XMLInfo)ecoreToXMLInfo.get(element);
82     if (result == null)
83     {
84       for (Iterator JavaDoc i = element.getEAnnotations().iterator(); i.hasNext(); )
85       {
86         EAnnotation eAnnotation = (EAnnotation)i.next();
87         if (XSD2ECORE.equals(eAnnotation.getSource()))
88         {
89           result = new XMLInfoImpl();
90           EMap details = eAnnotation.getDetails();
91           result.setName((String JavaDoc)details.get("name"));
92           result.setTargetNamespace((String JavaDoc)details.get("targetNamespace"));
93           String JavaDoc representation = (String JavaDoc)details.get("representation");
94           if ("element".equals(representation))
95           {
96             result.setXMLRepresentation(XMLResource.XMLInfo.ELEMENT);
97           }
98           else if ("attribute".equals(representation))
99           {
100             result.setXMLRepresentation(XMLResource.XMLInfo.ATTRIBUTE);
101           }
102           else if ("simple-content".equals(representation))
103           {
104             result.setXMLRepresentation(XMLResource.XMLInfo.CONTENT);
105           }
106         }
107       }
108
109       if (result == null)
110       {
111         // result = new XMLResource.XMLInfoImpl();
112
// result.setXMLRepresentation(XMLResource.XMLInfo.UNSPECIFIED);
113
}
114       else
115       {
116         ecoreToXMLInfo.put(element, result);
117       }
118     }
119
120     return result;
121   }
122
123   public void setNoNamespacePackage(EPackage pkg)
124   {
125     noNamespacePkg = pkg;
126   }
127
128   public EPackage getNoNamespacePackage()
129   {
130     return noNamespacePkg;
131   }
132
133   public void setIDAttributeName(String JavaDoc name)
134   {
135     idAttributeName = name;
136   }
137
138   public String JavaDoc getIDAttributeName()
139   {
140     return idAttributeName;
141   }
142
143   public void setPackageRegistry(EPackage.Registry packageRegistry)
144   {
145     this.packageRegistry = packageRegistry;
146   }
147
148   public EPackage.Registry getPackageRegistry()
149   {
150     return packageRegistry;
151   }
152
153   public EClassifier getClassifier(String JavaDoc namespaceURI, String JavaDoc name)
154   {
155     EPackage ePackage = packageRegistry.getEPackage(namespaceURI);
156     if (ePackage != null)
157     {
158       if (processedEPackages.add(ePackage))
159       {
160         if (urisToNamesToClassifiers == null)
161         {
162           urisToNamesToClassifiers = new HashMap JavaDoc();
163         }
164
165         getInfoForClassifiers(ePackage);
166
167         for (Iterator JavaDoc i = ecoreToXMLInfo.entrySet().iterator(); i.hasNext(); )
168         {
169           Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
170           Object JavaDoc key = entry.getKey();
171
172           // Only handle classifiers from this package.
173
//
174
if (key instanceof EClassifier)
175           {
176             EClassifier eClassifier = (EClassifier)key;
177             if (eClassifier.getEPackage() == ePackage)
178             {
179               XMLResource.XMLInfo info = (XMLResource.XMLInfo) entry.getValue();
180               String JavaDoc uri = info.getTargetNamespace();
181               if (uri == null)
182               {
183                 uri = namespaceURI;
184               }
185     
186               if (key instanceof EClassifier && info.getName() != null)
187               {
188                 Map JavaDoc namesToClassifiers = (Map JavaDoc) urisToNamesToClassifiers.get(uri);
189     
190                 if (namesToClassifiers == null)
191                 {
192                   namesToClassifiers = new HashMap JavaDoc();
193                   urisToNamesToClassifiers.put(uri, namesToClassifiers);
194                 }
195     
196                 namesToClassifiers.put(info.getName(), key);
197               }
198             }
199           }
200         }
201       }
202
203       Map JavaDoc namesToClassifiers = (Map JavaDoc) urisToNamesToClassifiers.get(namespaceURI);
204       if (namesToClassifiers != null)
205       {
206         return (EClassifier) namesToClassifiers.get(name);
207       }
208     }
209
210     return null;
211   }
212
213   /**
214    * Get the XMLInfo for all of the classifiers of the
215    * package with the given name.
216    */

217   private void getInfoForClassifiers(EPackage ePackage)
218   {
219     for (Iterator JavaDoc i = ePackage.getEClassifiers().iterator(); i.hasNext(); )
220     {
221       getInfo((ENamedElement) i.next());
222     }
223   }
224
225   public EStructuralFeature getFeature(EClass eClass, String JavaDoc namespaceURI, String JavaDoc name)
226   {
227     for (int i = 0, size = eClass.getFeatureCount(); i < size; ++i)
228     {
229       EStructuralFeature feature = eClass.getEStructuralFeature(i);
230       XMLResource.XMLInfo info = getInfo(feature);
231
232       if (info != null)
233       {
234         String JavaDoc infoURI = info.getTargetNamespace();
235         String JavaDoc infoName = info.getName();
236         if (namespaceURI == null)
237         {
238           // We effectively passed null, "" to look up the general-content feature.
239
//
240
if (infoURI == null && (name.equals(infoName) || infoName == null && name.length() == 0))
241           {
242             return feature;
243           }
244         }
245         else if (namespaceURI.equals(infoURI) && name.equals(infoName))
246         {
247           return feature;
248         }
249       }
250     }
251
252     return null;
253   }
254
255   public List JavaDoc getFeatures(EClass eClass)
256   {
257     EList result = (EList)eClassToFeatures.get(eClass);
258     if (result == null)
259     {
260       result = new UniqueEList();
261       for (Iterator JavaDoc i = eClass.getESuperTypes().iterator(); i.hasNext(); )
262       {
263         result.addAll(getFeatures((EClass)i.next()));
264       }
265       List JavaDoc eAttributes = eClass.getEAttributes();
266       result.addAll(eAttributes);
267       List JavaDoc eReferences = eClass.getEReferences();
268       result.addAll(eReferences);
269
270       EAnnotation eAnnotation = eClass.getEAnnotation(XSD2ECORE);
271       if (eAnnotation != null)
272       {
273         String JavaDoc featureOrder = (String JavaDoc)eAnnotation.getDetails().get("feature-order");
274         if (featureOrder != null)
275         {
276           int size = result.size();
277           int index = size - eReferences.size() - eAttributes.size();
278           for (StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc(featureOrder); stringTokenizer.hasMoreTokens(); ++index)
279           {
280             String JavaDoc featureName = stringTokenizer.nextToken();
281             for (int i = index; i < size; ++i)
282             {
283               EStructuralFeature eStructuralFeature = (EStructuralFeature)result.get(i);
284               if (featureName.equals(eStructuralFeature.getName()))
285               {
286                 result.move(index, eStructuralFeature);
287                 break;
288               }
289             }
290           }
291         }
292       }
293       eClassToFeatures.put(eClass, result);
294     }
295
296     return result;
297   }
298 }
299
Popular Tags