KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > mapping > ecore2xml > util > Ecore2XMLExtendedMetaData


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2005 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: Ecore2XMLExtendedMetaData.java,v 1.2 2005/06/21 16:17:03 khussey Exp $
16  */

17 package org.eclipse.emf.mapping.ecore2xml.util;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.eclipse.emf.ecore.EClassifier;
24 import org.eclipse.emf.ecore.ENamedElement;
25 import org.eclipse.emf.ecore.EPackage;
26 import org.eclipse.emf.ecore.EStructuralFeature;
27 import org.eclipse.emf.ecore.util.BasicExtendedMetaData;
28 import org.eclipse.emf.ecore.util.ExtendedMetaData;
29 import org.eclipse.emf.ecore.xmi.XMLResource;
30 import org.eclipse.emf.mapping.ecore2xml.Ecore2XMLRegistry;
31
32 /**
33  * Maps XML elements to Ecore named elements based on registered Ecore2XML mappings.
34  */

35 public class Ecore2XMLExtendedMetaData extends BasicExtendedMetaData
36 {
37   
38   protected final Ecore2XMLRegistry ecore2xmlRegistry;
39   
40   protected final Map JavaDoc xmlMaps = new HashMap JavaDoc();
41   
42   public Ecore2XMLExtendedMetaData()
43   {
44     this(Ecore2XMLRegistry.INSTANCE);
45   }
46   
47   public Ecore2XMLExtendedMetaData(Ecore2XMLRegistry ecore2xmlRegistry)
48   {
49     this(EPackage.Registry.INSTANCE, ecore2xmlRegistry);
50   }
51   
52   public Ecore2XMLExtendedMetaData(EPackage.Registry ePackageRegistry, Ecore2XMLRegistry ecore2xmlRegistry)
53   {
54     this(ANNOTATION_URI, ePackageRegistry, ecore2xmlRegistry);
55   }
56   
57   public Ecore2XMLExtendedMetaData(String JavaDoc annotationURI, EPackage.Registry ePackageRegistry, Ecore2XMLRegistry ecore2xmlRegistry)
58   {
59     super(annotationURI, ePackageRegistry);
60     
61     this.ecore2xmlRegistry = ecore2xmlRegistry;
62   }
63   
64   protected XMLResource.XMLInfo getInfo(ENamedElement element)
65   {
66     XMLResource.XMLInfo xmlInfo = null;
67     
68     for (Iterator JavaDoc maps = xmlMaps.values().iterator(); xmlInfo == null && maps.hasNext();)
69     {
70       xmlInfo = ((XMLResource.XMLMap)maps.next()).getInfo(element);
71     }
72     
73     return xmlInfo;
74   }
75   
76   protected EClassifier getClassifier(String JavaDoc namespaceURI, String JavaDoc name)
77   {
78     EClassifier classifier = null;
79     
80     for (Iterator JavaDoc maps = xmlMaps.values().iterator(); classifier == null && maps.hasNext();)
81     {
82       classifier = ((XMLResource.XMLMap)maps.next()).getClassifier(namespaceURI, name);
83     }
84     
85     return classifier;
86   }
87   
88   /*
89    * (non-Javadoc)
90    *
91    * @see org.eclipse.emf.ecore.util.ExtendedMetaData#getPackage(java.lang.String)
92    */

93   public EPackage getPackage(String JavaDoc namespace)
94   {
95     XMLResource.XMLMap xmlMap = ecore2xmlRegistry.getXMLMap(namespace);
96     
97     if (xmlMap != null)
98     {
99       xmlMaps.put(namespace, xmlMap);
100     }
101     
102     return super.getPackage(namespace);
103   }
104   
105   /*
106    * (non-Javadoc)
107    *
108    * @see org.eclipse.emf.ecore.util.ExtendedMetaData#getName(org.eclipse.emf.ecore.EClassifier)
109    */

110   public String JavaDoc getName(EClassifier eClassifier)
111   {
112     XMLResource.XMLInfo xmlInfo = getInfo(eClassifier);
113     
114     if (xmlInfo != null)
115     {
116       String JavaDoc name = xmlInfo.getName();
117       
118       if (name != null)
119       {
120         return name;
121       }
122     }
123     
124     return super.getName(eClassifier);
125   }
126   
127   /*
128    * (non-Javadoc)
129    *
130    * @see org.eclipse.emf.ecore.util.ExtendedMetaData#getName(org.eclipse.emf.ecore.EStructuralFeature)
131    */

132   public String JavaDoc getName(EStructuralFeature eStructuralFeature)
133   {
134     XMLResource.XMLInfo xmlInfo = getInfo(eStructuralFeature);
135     
136     if (xmlInfo != null)
137     {
138       String JavaDoc name = xmlInfo.getName();
139       
140       if (name != null)
141       {
142         return name;
143       }
144     }
145     
146     return super.getName(eStructuralFeature);
147   }
148   
149   /*
150    * (non-Javadoc)
151    *
152    * @see org.eclipse.emf.ecore.util.ExtendedMetaData#getNamespace(org.eclipse.emf.ecore.EPackage)
153    */

154   public String JavaDoc getNamespace(EPackage ePackage)
155   {
156     XMLResource.XMLInfo xmlInfo = getInfo(ePackage);
157     
158     if (xmlInfo != null)
159     {
160       String JavaDoc namespace = xmlInfo.getTargetNamespace();
161       
162       if (namespace != null)
163       {
164         return namespace;
165       }
166     }
167     
168     return super.getNamespace(ePackage);
169   }
170   
171   /*
172    * (non-Javadoc)
173    *
174    * @see org.eclipse.emf.ecore.util.ExtendedMetaData#getType(org.eclipse.emf.ecore.EPackage,
175    * java.lang.String)
176    */

177   public EClassifier getType(EPackage ePackage, String JavaDoc name)
178   {
179     EClassifier type = super.getType(ePackage, name);
180     
181     if (type == null)
182     {
183       type = getClassifier(ePackage.getNsURI(), name);
184     }
185     
186     return type;
187   }
188   
189   /*
190    * (non-Javadoc)
191    *
192    * @see org.eclipse.emf.ecore.util.ExtendedMetaData#getFeatureKind(org.eclipse.emf.ecore.EStructuralFeature)
193    */

194   public int getFeatureKind(EStructuralFeature eStructuralFeature)
195   {
196     XMLResource.XMLInfo xmlInfo = getInfo(eStructuralFeature);
197     
198     if (xmlInfo != null)
199     {
200       switch (xmlInfo.getXMLRepresentation())
201       {
202         case XMLResource.XMLInfo.ELEMENT:
203           return ExtendedMetaData.ELEMENT_FEATURE;
204         case XMLResource.XMLInfo.ATTRIBUTE:
205           return ExtendedMetaData.ATTRIBUTE_FEATURE;
206       }
207     }
208     
209     return super.getFeatureKind(eStructuralFeature);
210   }
211   
212   /*
213    * (non-Javadoc)
214    *
215    * @see org.eclipse.emf.ecore.util.BasicExtendedMetaData#isFeatureKindSpecific()
216    */

217   protected boolean isFeatureKindSpecific()
218   {
219     return false;
220   }
221   
222 }
223
Popular Tags