KickJava   Java API By Example, From Geeks To Geeks.

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


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

17 package org.eclipse.emf.ecore.xmi.impl;
18
19
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
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 JavaDoc;
31
32
33 public class XMISaveImpl extends XMLSaveImpl
34 {
35   protected boolean xmiType;
36
37   protected static final String JavaDoc XMI_ID_NS = XMIResource.XMI_NS + ":" + XMIResource.XMI_ID; // xmi:id
38
protected static final String JavaDoc XMI_TAG_NS = XMIResource.XMI_NS + ":" + XMIResource.XMI_TAG_NAME; // xmi:XMI
39
protected static final String JavaDoc XMI_TYPE_NS = XMIResource.XMI_NS + ":" + XMLResource.TYPE; // xmi:type
40
protected static final String JavaDoc XMI_VER_NS = XMIResource.XMI_NS + ":" + XMIResource.VERSION_NAME; // xmi:version
41
protected static final String JavaDoc XMI_XMLNS = XMLResource.XML_NS + ":" + XMIResource.XMI_NS; // xmlns:xmi
42

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 JavaDoc options, XMLHelper helper, String JavaDoc 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 JavaDoc 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 JavaDoc writeTopObjects(List JavaDoc contents)
66   {
67     if (!toDOM)
68     {
69       doc.startElement(XMI_TAG_NS);
70       Object JavaDoc 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 JavaDoc name = helper.getQName(eClass);
77         doc.startElement(name);
78         saveElementID(top);
79       }
80
81       doc.endElement();
82       return mark;
83     }
84     else
85     {
86       // create dummy documentElement
87
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 JavaDoc)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 JavaDoc)currentNode).setAttributeNS(XMIResource.XMI_URI, XMI_VER_NS, XMIResource.VERSION_VALUE);
131       ((Element JavaDoc)currentNode).setAttributeNS(ExtendedMetaData.XMLNS_URI, XMI_XMLNS, XMIResource.XMI_URI);
132     }
133     super.addNamespaceDeclarations();
134   }
135
136   public boolean isDuplicateURI(String JavaDoc 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