KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > xmi > impl > EMOFSaveImpl


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2003-2004 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: EMOFSaveImpl.java,v 1.5 2005/06/08 06:16:07 nickb Exp $
16  */

17 package org.eclipse.emf.ecore.xmi.impl;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.eclipse.emf.ecore.EAnnotation;
23 import org.eclipse.emf.ecore.EClass;
24 import org.eclipse.emf.ecore.EDataType;
25 import org.eclipse.emf.ecore.EObject;
26 import org.eclipse.emf.ecore.EReference;
27 import org.eclipse.emf.ecore.EStructuralFeature;
28 import org.eclipse.emf.ecore.EcoreFactory;
29 import org.eclipse.emf.ecore.EcorePackage;
30 import org.eclipse.emf.ecore.util.InternalEList;
31 import org.eclipse.emf.ecore.xmi.XMLHelper;
32 import org.eclipse.emf.ecore.xmi.XMLResource;
33
34
35 public class EMOFSaveImpl extends XMISaveImpl
36 {
37   public EMOFSaveImpl(XMLHelper helper)
38   {
39     super(helper);
40     idAttributeName = XMI_ID_NS;
41   }
42
43   protected void saveTypeAttribute(EClass eClass)
44   {
45     if (eClass != EcorePackage.eINSTANCE.getEAttribute() && eClass != EcorePackage.eINSTANCE.getEReference())
46     {
47       super.saveTypeAttribute(eClass);
48     }
49   }
50
51   protected void saveDataTypeElementSingle(EObject o, EStructuralFeature f)
52   {
53     if (f == EcorePackage.eINSTANCE.getEPackage_NsPrefix() ||
54         f == EcorePackage.eINSTANCE.getEStructuralFeature_Unsettable() ||
55         f == EcorePackage.eINSTANCE.getEStructuralFeature_Transient() ||
56         f == EcorePackage.eINSTANCE.getEStructuralFeature_Volatile() ||
57         f == EcorePackage.eINSTANCE.getEEnumLiteral_Value() ||
58         f == EcorePackage.eINSTANCE.getEReference_ResolveProxies() ||
59         f == EcorePackage.eINSTANCE.getEClassifier_InstanceClassName() ||
60         f == EcorePackage.eINSTANCE.getEDataType_Serializable() ||
61         f == EcorePackage.eINSTANCE.getEClass_Interface())
62     {
63       if (f == EcorePackage.eINSTANCE.getEStructuralFeature_Unsettable() &&
64           o.eIsSet(EcorePackage.eINSTANCE.getEStructuralFeature_Volatile())) return;
65       if (f == EcorePackage.eINSTANCE.getEStructuralFeature_Transient() &&
66           (o.eIsSet(EcorePackage.eINSTANCE.getEStructuralFeature_Unsettable()) ||
67            o.eIsSet(EcorePackage.eINSTANCE.getEStructuralFeature_Volatile()))) return;
68       if (f == EcorePackage.eINSTANCE.getEReference_ResolveProxies() &&
69           (o.eIsSet(EcorePackage.eINSTANCE.getEStructuralFeature_Transient()) ||
70            o.eIsSet(EcorePackage.eINSTANCE.getEStructuralFeature_Unsettable()) ||
71            o.eIsSet(EcorePackage.eINSTANCE.getEStructuralFeature_Volatile()))) return;
72       if (f == EcorePackage.eINSTANCE.getEDataType_Serializable() &&
73           o.eIsSet(EcorePackage.eINSTANCE.getEClassifier_InstanceClassName())) return;
74       if (f == EcorePackage.eINSTANCE.getEClass_Interface() &&
75           o.eIsSet(EcorePackage.eINSTANCE.getEClassifier_InstanceClassName())) return;
76
77       doc.startElement(EMOFExtendedMetaData.XMI_EXTENSION_ELEMENT);
78       doc.addAttribute(EMOFExtendedMetaData.XMI_EXTENDER_ATTRIBUTE, EcorePackage.eNS_URI);
79       saveExtensionFeature(o, f);
80       if (f == EcorePackage.eINSTANCE.getEClassifier_InstanceClassName())
81       {
82         if (o instanceof EDataType)
83         {
84           if (o.eIsSet(EcorePackage.eINSTANCE.getEDataType_Serializable()))
85           {
86             saveExtensionFeature(o, EcorePackage.eINSTANCE.getEDataType_Serializable());
87           }
88         }
89         else if (o instanceof EClass)
90         {
91           if (o.eIsSet(EcorePackage.eINSTANCE.getEClass_Interface()))
92           {
93             saveExtensionFeature(o, EcorePackage.eINSTANCE.getEClass_Interface());
94           }
95         }
96       }
97       else if (f == EcorePackage.eINSTANCE.getEStructuralFeature_Transient())
98       {
99         if (o instanceof EReference && o.eIsSet(EcorePackage.eINSTANCE.getEReference_ResolveProxies()))
100         {
101           saveExtensionFeature(o, EcorePackage.eINSTANCE.getEReference_ResolveProxies());
102         }
103       }
104       else if (f == EcorePackage.eINSTANCE.getEStructuralFeature_Unsettable())
105       {
106         if (o.eIsSet(EcorePackage.eINSTANCE.getEStructuralFeature_Transient()))
107         {
108           saveExtensionFeature(o, EcorePackage.eINSTANCE.getEStructuralFeature_Transient());
109         }
110         if (o instanceof EReference && o.eIsSet(EcorePackage.eINSTANCE.getEReference_ResolveProxies()))
111         {
112           saveExtensionFeature(o, EcorePackage.eINSTANCE.getEReference_ResolveProxies());
113         }
114       }
115       else if (f == EcorePackage.eINSTANCE.getEStructuralFeature_Volatile())
116       {
117         if (f == EcorePackage.eINSTANCE.getEStructuralFeature_Unsettable())
118         {
119           saveExtensionFeature(o, EcorePackage.eINSTANCE.getEStructuralFeature_Unsettable());
120         }
121         if (o.eIsSet(EcorePackage.eINSTANCE.getEStructuralFeature_Transient()))
122         {
123           saveExtensionFeature(o, EcorePackage.eINSTANCE.getEStructuralFeature_Transient());
124         }
125         if (o instanceof EReference && o.eIsSet(EcorePackage.eINSTANCE.getEReference_ResolveProxies()))
126         {
127           saveExtensionFeature(o, EcorePackage.eINSTANCE.getEReference_ResolveProxies());
128         }
129       }
130       doc.endElement();
131     }
132     else
133     {
134       super.saveDataTypeElementSingle(o, f);
135     }
136   }
137
138   protected String JavaDoc getDatatypeValue(Object JavaDoc value, EStructuralFeature f, boolean isAttribute)
139   {
140     String JavaDoc result = super.getDatatypeValue(value, f, isAttribute);
141     if (f == EcorePackage.eINSTANCE.getETypedElement_UpperBound() && "-1".equals(result))
142     {
143       result = "*";
144     }
145     return result;
146   }
147
148   protected void saveExtensionFeature(EObject o, EStructuralFeature f)
149   {
150     doc.startElement(f.getName());
151     EDataType eDataType = (EDataType)f.getEType();
152     doc.endContentElement(EcoreFactory.eINSTANCE.convertToString(eDataType, o.eGet(f)));
153   }
154
155   protected void saveContainedMany(EObject o, EStructuralFeature f)
156   {
157     if (f == EcorePackage.eINSTANCE.getEModelElement_EAnnotations())
158     {
159       doc.startElement(EMOFExtendedMetaData.XMI_EXTENSION_ELEMENT);
160       doc.addAttribute(EMOFExtendedMetaData.XMI_EXTENDER_ATTRIBUTE, EcorePackage.eNS_URI);
161       super.saveContainedMany(o, f);
162       doc.endElement();
163     }
164     else
165     {
166       super.saveContainedMany(o, f);
167     }
168   }
169
170   public Object JavaDoc writeTopObjects(List JavaDoc contents)
171   {
172     doc.startElement(XMI_TAG_NS);
173     Object JavaDoc mark = doc.mark();
174
175     for (int i = 0, size = contents.size(); i < size; i++)
176     {
177       EObject top = (EObject)contents.get(i);
178       EClass eClass = top.eClass();
179       if (eClass == EcorePackage.eINSTANCE.getEAnnotation())
180       {
181         EAnnotation annotation = (EAnnotation)top;
182         if (!annotation.getSource().equals(EMOFExtendedMetaData.EMOF_PACKAGE_NS_URI)) continue;
183
184         doc.startElement(EMOFExtendedMetaData.EMOF_TAG);
185         doc.addAttribute(idAttributeName, helper.getID(annotation));
186         doc.addAttribute(EMOFExtendedMetaData.EMOF_TAG_NAME, (String JavaDoc)annotation.getDetails().get(EMOFExtendedMetaData.EMOF_TAG_NAME));
187         doc.addAttribute(EMOFExtendedMetaData.EMOF_TAG_VALUE, (String JavaDoc)annotation.getDetails().get(EMOFExtendedMetaData.EMOF_TAG_VALUE));
188
189         InternalEList values = (InternalEList)annotation.getReferences();
190         if (!values.isEmpty())
191         {
192           if (sameDocMany(annotation, EcorePackage.eINSTANCE.getEAnnotation_References()) == CROSS_DOC)
193           {
194             for (Iterator JavaDoc iter = values.basicIterator(); iter.hasNext(); )
195             {
196               EObject value = (EObject)iter.next();
197               doc.startElement(EMOFExtendedMetaData.EMOF_TAG_ELEMENT);
198               doc.addAttribute(XMLResource.HREF, helper.getHREF(value));
199               doc.endEmptyElement();
200             }
201           }
202           else
203           {
204             StringBuffer JavaDoc ids = new StringBuffer JavaDoc();
205             for (Iterator JavaDoc iter = values.basicIterator();; )
206             {
207               EObject value = (EObject)iter.next();
208               ids.append(helper.getIDREF(value));
209               if (!iter.hasNext()) break;
210               ids.append(" ");
211             }
212             doc.addAttribute(EMOFExtendedMetaData.EMOF_TAG_ELEMENT, ids.toString());
213           }
214         }
215         doc.endElement();
216       }
217       else
218       {
219         String JavaDoc name = helper.getQName(eClass);
220         doc.startElement(name);
221         saveElementID(top);
222       }
223     }
224
225     doc.endElement();
226     return mark;
227   }
228 }
229
Popular Tags