1 17 package org.eclipse.emf.ecore.xmi.impl; 18 19 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.eclipse.emf.ecore.EClass; 24 import org.eclipse.emf.ecore.EObject; 25 import org.eclipse.emf.ecore.EReference; 26 import org.eclipse.emf.ecore.util.ExtendedMetaData; 27 import org.eclipse.emf.ecore.xmi.XMIResource; 28 import org.eclipse.emf.ecore.xmi.XMLHelper; 29 import org.eclipse.emf.ecore.xmi.XMLResource; 30 import org.w3c.dom.Element ; 31 32 33 public class XMISaveImpl extends XMLSaveImpl 34 { 35 protected boolean xmiType; 36 37 protected static final String XMI_ID_NS = XMIResource.XMI_NS + ":" + XMIResource.XMI_ID; protected static final String XMI_TAG_NS = XMIResource.XMI_NS + ":" + XMIResource.XMI_TAG_NAME; protected static final String XMI_TYPE_NS = XMIResource.XMI_NS + ":" + XMLResource.TYPE; protected static final String XMI_VER_NS = XMIResource.XMI_NS + ":" + XMIResource.VERSION_NAME; protected static final String XMI_XMLNS = XMLResource.XML_NS + ":" + XMIResource.XMI_NS; 43 public XMISaveImpl(XMLHelper helper) 44 { 45 super(helper); 46 idAttributeName = XMI_ID_NS; 47 idAttributeNS = XMIResource.XMI_NS; 48 } 49 50 public XMISaveImpl(Map options, XMLHelper helper, String encoding) 51 { 52 super(options, helper, encoding); 53 this.xmiType = Boolean.TRUE.equals(options.get(XMIResource.OPTION_USE_XMI_TYPE)); 54 idAttributeName = XMI_ID_NS; 55 idAttributeNS = XMIResource.XMI_NS; 56 } 57 58 protected void init(XMLResource resource, Map options) 59 { 60 super.init(resource, options); 61 this.xmiType = Boolean.TRUE.equals(options.get(XMIResource.OPTION_USE_XMI_TYPE)); 62 helper.getPrefixToNamespaceMap().put(XMIResource.XMI_NS, XMIResource.XMI_URI); 63 } 64 65 public Object writeTopObjects(List contents) 66 { 67 if (!toDOM) 68 { 69 doc.startElement(XMI_TAG_NS); 70 Object mark = doc.mark(); 71 72 for (int i = 0, size = contents.size(); i < size; i++) 73 { 74 EObject top = (EObject)contents.get(i); 75 EClass eClass = top.eClass(); 76 String name = helper.getQName(eClass); 77 doc.startElement(name); 78 saveElementID(top); 79 } 80 81 doc.endElement(); 82 return mark; 83 } 84 else 85 { 86 currentNode = document.createElementNS(XMIResource.XMI_URI, XMI_TAG_NS); 88 document.appendChild(currentNode); 89 for (int i = 0, size = contents.size(); i < size; i++) 90 { 91 EObject top = (EObject)contents.get(i); 92 EClass eClass = top.eClass(); 93 helper.populateNameInfo(nameInfo, eClass); 94 currentNode = currentNode.appendChild(document.createElementNS(nameInfo.getNamespaceURI(), nameInfo.getQualifiedName())); 95 handler.recordValues(currentNode, null, null, top); 96 saveElementID(top); 97 } 98 return null; 99 } 100 } 101 102 protected void saveTypeAttribute(EClass eClass) 103 { 104 if (xmiType) 105 { 106 if (!toDOM) 107 { 108 doc.addAttribute(XMI_TYPE_NS, helper.getQName(eClass)); 109 } 110 else 111 { 112 ((Element )currentNode).setAttributeNS(XMIResource.XMI_URI, XMI_TYPE_NS, helper.getQName(eClass)); 113 } 114 } 115 else 116 { 117 super.saveTypeAttribute(eClass); 118 } 119 } 120 121 public void addNamespaceDeclarations() 122 { 123 if (!toDOM) 124 { 125 doc.addAttribute(XMI_VER_NS, XMIResource.VERSION_VALUE); 126 doc.addAttribute(XMI_XMLNS, XMIResource.XMI_URI); 127 } 128 else 129 { 130 ((Element )currentNode).setAttributeNS(XMIResource.XMI_URI, XMI_VER_NS, XMIResource.VERSION_VALUE); 131 ((Element )currentNode).setAttributeNS(ExtendedMetaData.XMLNS_URI, XMI_XMLNS, XMIResource.XMI_URI); 132 } 133 super.addNamespaceDeclarations(); 134 } 135 136 public boolean isDuplicateURI(String nsURI) 137 { 138 return XMIResource.XMI_URI.equals(nsURI); 139 } 140 141 protected void saveFeatureMapElementReference(EObject o, EReference f) 142 { 143 if (extendedMetaData == null || extendedMetaData.getFeatureKind(f) != ExtendedMetaData.ELEMENT_FEATURE) 144 { 145 saveHref(o, f); 146 } 147 else 148 { 149 saveElementReference(o, f); 150 } 151 } 152 } 153 | Popular Tags |