1 17 package org.eclipse.emf.mapping.ecore2xml.util; 18 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 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 35 public class Ecore2XMLExtendedMetaData extends BasicExtendedMetaData 36 { 37 38 protected final Ecore2XMLRegistry ecore2xmlRegistry; 39 40 protected final Map xmlMaps = new HashMap (); 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 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 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 namespaceURI, String name) 77 { 78 EClassifier classifier = null; 79 80 for (Iterator 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 93 public EPackage getPackage(String 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 110 public String getName(EClassifier eClassifier) 111 { 112 XMLResource.XMLInfo xmlInfo = getInfo(eClassifier); 113 114 if (xmlInfo != null) 115 { 116 String name = xmlInfo.getName(); 117 118 if (name != null) 119 { 120 return name; 121 } 122 } 123 124 return super.getName(eClassifier); 125 } 126 127 132 public String getName(EStructuralFeature eStructuralFeature) 133 { 134 XMLResource.XMLInfo xmlInfo = getInfo(eStructuralFeature); 135 136 if (xmlInfo != null) 137 { 138 String name = xmlInfo.getName(); 139 140 if (name != null) 141 { 142 return name; 143 } 144 } 145 146 return super.getName(eStructuralFeature); 147 } 148 149 154 public String getNamespace(EPackage ePackage) 155 { 156 XMLResource.XMLInfo xmlInfo = getInfo(ePackage); 157 158 if (xmlInfo != null) 159 { 160 String namespace = xmlInfo.getTargetNamespace(); 161 162 if (namespace != null) 163 { 164 return namespace; 165 } 166 } 167 168 return super.getNamespace(ePackage); 169 } 170 171 177 public EClassifier getType(EPackage ePackage, String 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 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 217 protected boolean isFeatureKindSpecific() 218 { 219 return false; 220 } 221 222 } 223 | Popular Tags |