1 17 package org.eclipse.emf.ecore.sdo.util; 18 19 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import org.w3c.dom.Element ; 26 import org.xml.sax.helpers.DefaultHandler ; 27 28 import org.eclipse.emf.common.util.EMap; 29 import org.eclipse.emf.common.util.URI; 30 import org.eclipse.emf.ecore.EClass; 31 import org.eclipse.emf.ecore.EFactory; 32 import org.eclipse.emf.ecore.EObject; 33 import org.eclipse.emf.ecore.EPackage; 34 import org.eclipse.emf.ecore.EReference; 35 import org.eclipse.emf.ecore.resource.Resource; 36 import org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl; 37 import org.eclipse.emf.ecore.sdo.EChangeSummary; 38 import org.eclipse.emf.ecore.sdo.EDataGraph; 39 import org.eclipse.emf.ecore.sdo.SDOFactory; 40 import org.eclipse.emf.ecore.sdo.SDOPackage; 41 import org.eclipse.emf.ecore.sdo.impl.DynamicEDataObjectImpl; 42 import org.eclipse.emf.ecore.util.ExtendedMetaData; 43 import org.eclipse.emf.ecore.xmi.NameInfo; 44 import org.eclipse.emf.ecore.xmi.XMLHelper; 45 import org.eclipse.emf.ecore.xmi.XMLLoad; 46 import org.eclipse.emf.ecore.xmi.XMLResource; 47 import org.eclipse.emf.ecore.xmi.XMLSave; 48 import org.eclipse.emf.ecore.xmi.impl.SAXWrapper; 49 import org.eclipse.emf.ecore.xmi.impl.SAXXMLHandler; 50 import org.eclipse.emf.ecore.xmi.impl.XMLHelperImpl; 51 import org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl; 52 import org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl; 53 import org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl; 54 55 56 public class DataGraphResourceFactoryImpl extends ResourceFactoryImpl 57 { 58 61 public DataGraphResourceFactoryImpl() 62 { 63 super(); 64 } 65 66 public Resource createResource(URI uri) 67 { 68 XMLResourceImpl result = new DataGraphResourceImpl(uri); 69 70 result.setEncoding("UTF-8"); 71 72 result.getDefaultLoadOptions().put(XMLResource.OPTION_USE_LEXICAL_HANDLER, Boolean.TRUE); 73 74 result.getDefaultLoadOptions().put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE); 75 result.getDefaultSaveOptions().put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE); 76 77 result.getDefaultSaveOptions().put(XMLResource.OPTION_USE_ENCODED_ATTRIBUTE_STYLE, Boolean.TRUE); 78 result.getDefaultSaveOptions().put(XMLResource.OPTION_LINE_WIDTH, new Integer (80)); 79 80 result.getDefaultLoadOptions().put(XMLResource.OPTION_ANY_TYPE, SDOPackage.eINSTANCE.getEDataObjectAnyType()); 81 result.getDefaultSaveOptions().put(XMLResource.OPTION_ANY_TYPE, SDOPackage.eINSTANCE.getEDataObjectAnyType()); 82 83 result.getDefaultLoadOptions().put(XMLResource.OPTION_ANY_SIMPLE_TYPE, SDOPackage.eINSTANCE.getEDataObjectSimpleAnyType()); 84 result.getDefaultSaveOptions().put(XMLResource.OPTION_ANY_SIMPLE_TYPE, SDOPackage.eINSTANCE.getEDataObjectSimpleAnyType()); 85 86 return result; 87 } 88 89 public static class DataGraphResourceImpl extends XMLResourceImpl 90 { 91 public DataGraphResourceImpl(URI uri) 92 { 93 super(uri); 94 } 95 96 public static class HelperImpl extends XMLHelperImpl 97 { 98 protected EDataGraph eDataGraph; 99 100 protected List resources; 101 protected List uris; 102 103 public HelperImpl(XMLResource xmlResource) 104 { 105 super(xmlResource); 106 } 107 108 public void setResource(XMLResource resource) 109 { 110 super.setResource(resource); 111 if (!resource.getContents().isEmpty()) 112 { 113 eDataGraph = (EDataGraph)resource.getContents().get(0); 114 115 resources = new ArrayList (); 116 uris = new ArrayList (); 117 118 resources.add(eDataGraph.getRootResource()); 119 uris.add("#" + resource.getURIFragment(eDataGraph) + "/@eRootObject"); 120 121 if (eDataGraph.getEChangeSummary() != null) 122 { 123 resources.add(((EObject)eDataGraph.getChangeSummary()).eResource()); 126 uris.add("#" + resource.getURIFragment(eDataGraph) + "/@eChangeSummary"); 127 } 128 129 if (eDataGraph.eResource() != null && eDataGraph.eResource().getResourceSet() != null) 130 { 131 int count = 0; 132 for (Iterator i = eDataGraph.eResource().getResourceSet().getResources().iterator(); i.hasNext(); ) 133 { 134 Resource ePackageResource = (Resource)i.next(); 135 List resourceContents = ePackageResource.getContents(); 136 if (resourceContents.size() == 1 && resourceContents.get(0) instanceof EPackage) 137 { 138 resources.add(ePackageResource); 139 uris.add("#" + resource.getURIFragment(eDataGraph) + "/@models." + count++); 140 } 141 } 142 } 143 } 144 } 145 146 public String getID(EObject eObject) 147 { 148 return super.getID(eObject); 149 } 150 151 public String getIDREF(EObject eObject) 152 { 153 return super.getIDREF(eObject); 154 } 155 156 public String getHREF(EObject eObject) 157 { 158 return super.getHREF(eObject); 159 } 160 161 protected URI getHREF(Resource otherResource, EObject obj) 162 { 163 int index = resources.indexOf(otherResource); 164 if (index == -1) 165 { 166 return super.getHREF(otherResource, obj); 167 } 168 else 169 { 170 return createHREF((String )uris.get(index), otherResource.getURIFragment(obj)); 171 } 172 } 173 174 protected URI createHREF(String baseURI, String fragment) 175 { 176 if (fragment.startsWith("/")) 177 { 178 return URI.createURI(baseURI + fragment.substring(1)); 179 } 180 else 181 { 182 return URI.createURI("#" + fragment); 183 } 184 } 185 186 public void populateNameInfo(NameInfo nameInfo, EClass c) 187 { 188 if (c == SDOPackage.eINSTANCE.getEDataGraph()) 189 { 190 if (extendedMetaData != null) 191 { 192 extendedMetaData.demandPackage("commonj.sdo").setNsPrefix("sdo"); 193 } 194 nameInfo.setQualifiedName(getQName("commonj.sdo", "datagraph")); 195 nameInfo.setNamespaceURI("commonj.sdo"); 196 nameInfo.setLocalPart("datagraph"); 197 } 198 else if (c == SDOPackage.eINSTANCE.getEChangeSummary()) 199 { 200 if (extendedMetaData != null) 201 { 202 extendedMetaData.demandPackage("commonj.sdo").setNsPrefix("sdo"); 203 } 204 nameInfo.setQualifiedName("changeSummary"); 205 nameInfo.setNamespaceURI(null); 206 nameInfo.setLocalPart("changeSummary"); 207 } 208 else 209 { 210 super.populateNameInfo(nameInfo, c); 211 } 212 } 213 214 public String getQName(EClass c) 215 { 216 if (c == SDOPackage.eINSTANCE.getEDataGraph()) 217 { 218 if (extendedMetaData != null) 219 { 220 extendedMetaData.demandPackage("commonj.sdo").setNsPrefix("sdo"); 221 } 222 return getQName("commonj.sdo", "datagraph"); 223 } 224 else if (c == SDOPackage.eINSTANCE.getEChangeSummary()) 225 { 226 if (extendedMetaData != null) 227 { 228 extendedMetaData.demandPackage("commonj.sdo").setNsPrefix("sdo"); 229 } 230 return getQName((String )null, "changeSummary"); 231 } 232 else 233 { 234 return super.getQName(c); 235 } 236 } 237 } 238 239 protected XMLHelper createXMLHelper() 240 { 241 return new HelperImpl(this); 242 } 243 244 protected EObject getEObjectByID(String id) 245 { 246 List contents = getContents(); 247 if (contents.size() >= 1) 248 { 249 Object rootObject = contents.get(0); 250 if (rootObject instanceof EDataGraph) 251 { 252 EDataGraph eDataGraph = (EDataGraph)rootObject; 253 EObject result = eDataGraph.getRootResource().getEObject(id); 254 if (result != null) 255 { 256 return result; 257 } 258 else 259 { 260 EChangeSummary eChangeSummary = eDataGraph.getEChangeSummary(); 261 if (eChangeSummary != null) 262 { 263 result = ((EObject)eDataGraph.getChangeSummary()).eResource().getEObject(id); 264 if (result != null) 265 { 266 return result; 267 } 268 } 269 } 270 } 271 } 272 return super.getEObjectByID(id); 273 } 274 275 public static class SaveImpl extends XMLSaveImpl 276 { 277 protected EDataGraph eDataGraph; 278 279 public SaveImpl(XMLHelper xmlHelper) 280 { 281 super(xmlHelper); 282 } 283 284 public void traverse(List contents) 285 { 286 if (contents.size() >= 1 && contents.get(0) instanceof EDataGraph) 287 { 288 eDataGraph = (EDataGraph)contents.get(0); 289 290 Object datagraphMark = null; 291 if (!toDOM) 292 { 293 if (declareXML) 294 { 295 doc.add("<?xml version=\"" + XML_VERSION + "\" encoding=\"" + encoding + "\"?>"); 296 doc.addLine(); 297 } 298 String elementName = helper.getQName(eDataGraph.eClass()); 299 doc.startElement(elementName); 300 datagraphMark = doc.mark(); 301 } 302 else 303 { 304 helper.populateNameInfo(nameInfo, eDataGraph.eClass()); 305 currentNode = document.createElementNS(nameInfo.getNamespaceURI(), nameInfo.getQualifiedName()); 306 document.appendChild(currentNode); 307 } 309 310 if (eDataGraph.eResource() != null && eDataGraph.eResource().getResourceSet() != null) 311 { 312 List ePackages = new ArrayList (); 313 for (Iterator i = eDataGraph.eResource().getResourceSet().getResources().iterator(); i.hasNext(); ) 314 { 315 List resourceContents = ((Resource)i.next()).getContents(); 316 if (resourceContents.size() == 1 && resourceContents.get(0) instanceof EPackage) 317 { 318 ePackages.add(resourceContents.get(0)); 319 } 320 } 321 if (!ePackages.isEmpty()) 322 { 323 if (!toDOM) 324 { 325 doc.startElement("models"); 326 doc.addAttribute("xmlns", ""); 327 } 328 else 329 { 330 currentNode = currentNode.appendChild(document.createElementNS(null, "models")); 331 ((Element )currentNode).setAttributeNS(ExtendedMetaData.XMLNS_URI, ExtendedMetaData.XMLNS_PREFIX, ""); 332 } 334 for (Iterator i = ePackages.iterator(); i.hasNext(); ) 335 { 336 writeTopObject((EPackage)i.next()); 337 } 338 if (!toDOM) 339 { 340 doc.endElement(); 341 } 342 else 343 { 344 currentNode = currentNode.getParentNode(); 345 } 346 } 347 } 348 349 EObject eRootObject = eDataGraph.getERootObject(); 351 EReference xmlnsPrefixMapFeature = extendedMetaData.getXMLNSPrefixMapFeature(eRootObject.eClass()); 352 if (xmlnsPrefixMapFeature != null) 353 { 354 EMap xmlnsPrefixMap = (EMap)eRootObject.eGet(xmlnsPrefixMapFeature); 355 helper.setPrefixToNamespaceMap(xmlnsPrefixMap); 356 } 357 EChangeSummary changeSummary = eDataGraph.getEChangeSummary(); 358 359 if (changeSummary != null) 360 { 361 helper.setMustHavePrefix(true); 362 if (changeSummary.isLogging()) 363 { 364 changeSummary.summarize(); 365 writeTopObject(changeSummary); 366 } 367 else 368 { 369 writeTopObject(changeSummary); 370 } 371 helper.setMustHavePrefix(false); 372 } 373 374 375 if (eRootObject != null && writeTopObject(eRootObject) == null && !toDOM) 376 { 377 doc.addLine(); 378 doc.setMixed(false); 379 } 380 if (!toDOM) 381 { 382 doc.endElement(); 383 doc.resetToMark(datagraphMark); 386 } 387 else 388 { 389 currentNode = document.getFirstChild(); 390 } 391 addNamespaceDeclarations(); 392 } 393 else 394 { 395 super.traverse(contents); 396 } 397 } 398 399 protected void writeTopAttributes(EObject top) 400 { 401 if (top == eDataGraph.getEChangeSummary()) 402 { 403 if (!toDOM) 404 { 405 doc.addAttribute("xmlns", ""); 406 } 407 else 408 { 409 ((Element )currentNode).setAttributeNS(ExtendedMetaData.XMLNS_URI, ExtendedMetaData.XMLNS_PREFIX, ""); 410 } 411 } 412 } 413 } 414 415 protected XMLSave createXMLSave() 416 { 417 return new SaveImpl(createXMLHelper()); 418 } 419 420 public static class LoadImpl extends XMLLoadImpl 421 { 422 public LoadImpl(XMLHelper xmlHelper) 423 { 424 super(xmlHelper); 425 } 426 427 protected DefaultHandler makeDefaultHandler() 428 { 429 return 430 new SAXWrapper 431 (new SAXXMLHandler(resource, helper, options) 432 { 433 protected EDataGraph eDataGraph; 434 protected boolean isInModels; 435 protected List ePackages = new ArrayList (); 436 437 protected EMap recordNamespacesSchemaLocations(EObject root) 438 { 439 EObject dgroot = eDataGraph.getERootObject(); 440 if (dgroot == null) 441 { 442 return null; 443 } 444 EMap prefixToNamespaceMap = super.recordNamespacesSchemaLocations(dgroot); 445 if (prefixToNamespaceMap != null) 446 { 447 for (Iterator i = prefixToNamespaceMap.iterator(); i.hasNext(); ) 448 { 449 Map.Entry entry = (Map.Entry )i.next(); 450 String prefix = (String )entry.getKey(); 451 String namespace = (String )entry.getValue(); 452 if (namespace.equals("commonj.sdo")) 453 { 454 prefixToNamespaceMap.removeKey(prefix); 455 break; 456 } 457 } 458 } 459 return prefixToNamespaceMap; 460 } 461 462 protected void handleFeature(String prefix, String name) 463 { 464 if (isInModels && objects.size() == 2) 465 { 466 EObject modelObject = createObjectByType(prefix, name, false); 467 processObject(modelObject); 468 ePackages.add(modelObject); 469 } 470 else if (objects.size() == 1) 471 { 472 eDataGraph = (EDataGraph)objects.peek(); 473 eDataGraph.getResourceSet(); 474 if ("".equals(prefix) && "changeSummary".equals(name)) 475 { 476 EChangeSummary eChangeSummary = (EChangeSummary)createObjectFromFactory(SDOFactory.eINSTANCE, "EChangeSummary"); 477 eDataGraph.setEChangeSummary(eChangeSummary); 478 processObject(eChangeSummary); 479 } 480 else if ("".equals(prefix) && "models".equals(name)) 481 { 482 isInModels = true; 483 types.push(OBJECT_TYPE); 484 objects.push(eDataGraph); 485 mixedTargets.push(null); 486 } 487 else if (eDataGraph.getERootObject() == null) 488 { 489 EObject rootObject = createObjectByType(prefix, name, false); 490 eDataGraph.setERootObject(rootObject); 491 processObject(rootObject); 492 if (rootObject != null && 493 rootObject.eClass() == ExtendedMetaData.INSTANCE.getDocumentRoot(rootObject.eClass().getEPackage())) 494 { 495 super.handleFeature(prefix, name); 496 497 Object mixedTarget = mixedTargets.pop(); 500 Object object = objects.pop(); 501 mixedTargets.pop(); 502 objects.pop(); 503 mixedTargets.push(mixedTarget); 504 objects.push(object); 505 } 506 } 507 } 508 else 509 { 510 super.handleFeature(prefix, name); 511 } 512 } 513 514 public void endElement(String uri, String localName, String name) 515 { 516 if (isInModels && objects.size() == 2) 517 { 518 if (!ePackages.isEmpty()) 519 { 520 for (Iterator i = ePackages.iterator(); i.hasNext(); ) 521 { 522 EPackage ePackage = (EPackage)i.next(); 523 ePackage.setEFactoryInstance(new DynamicEDataObjectImpl.FactoryImpl()); 524 Resource resource = resourceSet.createResource(URI.createURI("*.ecore")); 525 resource.getContents().add(ePackage); 526 if (ePackage.getNsURI() != null) 527 { 528 resource.setURI(URI.createURI(ePackage.getNsURI())); 529 } 530 531 if (extendedMetaData != null) 532 { 533 extendedMetaData.putPackage(extendedMetaData.getNamespace(ePackage), ePackage); 534 } 535 else 536 { 537 packageRegistry.put(ePackage.getNsURI(), ePackage); 538 } 539 } 540 handleForwardReferences(); 541 } 542 isInModels = false; 543 } 544 super.endElement(uri, localName, name); 545 } 546 547 protected EPackage getPackageForURI(String uriString) 548 { 549 if ("commonj.sdo".equals(uriString)) 550 { 551 return SDOPackage.eINSTANCE; 552 } 553 else 554 { 555 return super.getPackageForURI(uriString); 556 } 557 } 558 559 protected EObject createObjectFromFactory(EFactory factory, String typeName) 560 { 561 if (factory == SDOFactory.eINSTANCE) 562 { 563 if ("datagraph".equals(typeName)) 564 { 565 return super.createObjectFromFactory(factory, "EDataGraph"); 566 } 567 } 568 return super.createObjectFromFactory(factory, typeName); 569 } 570 }); 571 } 572 } 573 574 protected XMLLoad createXMLLoad() 575 { 576 return new LoadImpl(createXMLHelper()); 577 } 578 } 579 } 580 | Popular Tags |