1 17 package org.eclipse.emf.ecore.xmi.impl; 18 19 import java.util.HashMap ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 import java.util.Set ; 25 import java.util.StringTokenizer ; 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 43 public class XMLMapImpl implements XMLResource.XMLMap 44 { 45 protected static final String XSD2ECORE = "http:///org/eclipse/emf/mapping/xsd2ecore/XSD2Ecore"; 46 47 protected Map ecoreToXMLInfo; 48 protected EPackage noNamespacePkg; 49 protected String idAttributeName; 50 protected Map urisToNamesToClassifiers; 51 protected Map eClassToFeatures; 52 protected Set processedEPackages; 53 protected EPackage.Registry packageRegistry = EPackage.Registry.INSTANCE; 54 55 58 public XMLMapImpl() 59 { 60 super(); 61 ecoreToXMLInfo = new HashMap (); 62 processedEPackages = new HashSet (); 63 eClassToFeatures = new HashMap (); 64 } 65 66 70 public void add(ENamedElement element, XMLResource.XMLInfo info) 71 { 72 ecoreToXMLInfo.put(element, info); 73 } 74 75 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 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 )details.get("name")); 92 result.setTargetNamespace((String )details.get("targetNamespace")); 93 String representation = (String )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 } 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 name) 134 { 135 idAttributeName = name; 136 } 137 138 public String 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 namespaceURI, String 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 (); 163 } 164 165 getInfoForClassifiers(ePackage); 166 167 for (Iterator i = ecoreToXMLInfo.entrySet().iterator(); i.hasNext(); ) 168 { 169 Map.Entry entry = (Map.Entry ) i.next(); 170 Object key = entry.getKey(); 171 172 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 uri = info.getTargetNamespace(); 181 if (uri == null) 182 { 183 uri = namespaceURI; 184 } 185 186 if (key instanceof EClassifier && info.getName() != null) 187 { 188 Map namesToClassifiers = (Map ) urisToNamesToClassifiers.get(uri); 189 190 if (namesToClassifiers == null) 191 { 192 namesToClassifiers = new HashMap (); 193 urisToNamesToClassifiers.put(uri, namesToClassifiers); 194 } 195 196 namesToClassifiers.put(info.getName(), key); 197 } 198 } 199 } 200 } 201 } 202 203 Map namesToClassifiers = (Map ) urisToNamesToClassifiers.get(namespaceURI); 204 if (namesToClassifiers != null) 205 { 206 return (EClassifier) namesToClassifiers.get(name); 207 } 208 } 209 210 return null; 211 } 212 213 217 private void getInfoForClassifiers(EPackage ePackage) 218 { 219 for (Iterator i = ePackage.getEClassifiers().iterator(); i.hasNext(); ) 220 { 221 getInfo((ENamedElement) i.next()); 222 } 223 } 224 225 public EStructuralFeature getFeature(EClass eClass, String namespaceURI, String 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 infoURI = info.getTargetNamespace(); 235 String infoName = info.getName(); 236 if (namespaceURI == null) 237 { 238 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 getFeatures(EClass eClass) 256 { 257 EList result = (EList)eClassToFeatures.get(eClass); 258 if (result == null) 259 { 260 result = new UniqueEList(); 261 for (Iterator i = eClass.getESuperTypes().iterator(); i.hasNext(); ) 262 { 263 result.addAll(getFeatures((EClass)i.next())); 264 } 265 List eAttributes = eClass.getEAttributes(); 266 result.addAll(eAttributes); 267 List eReferences = eClass.getEReferences(); 268 result.addAll(eReferences); 269 270 EAnnotation eAnnotation = eClass.getEAnnotation(XSD2ECORE); 271 if (eAnnotation != null) 272 { 273 String featureOrder = (String )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 stringTokenizer = new StringTokenizer (featureOrder); stringTokenizer.hasMoreTokens(); ++index) 279 { 280 String 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 |