KickJava   Java API By Example, From Geeks To Geeks.

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


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: EMOFResourceImpl.java,v 1.2 2005/06/08 06:16:07 nickb Exp $
16  */

17 package org.eclipse.emf.ecore.xmi.impl;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.ListIterator JavaDoc;
22
23 import org.eclipse.emf.common.util.URI;
24 import org.eclipse.emf.ecore.EClass;
25 import org.eclipse.emf.ecore.ENamedElement;
26 import org.eclipse.emf.ecore.EObject;
27 import org.eclipse.emf.ecore.EcorePackage;
28 import org.eclipse.emf.ecore.InternalEObject;
29 import org.eclipse.emf.ecore.xmi.XMIResource;
30 import org.eclipse.emf.ecore.xmi.XMLHelper;
31 import org.eclipse.emf.ecore.xmi.XMLLoad;
32 import org.eclipse.emf.ecore.xmi.XMLSave;
33
34
35 /**
36  * This class represents an Ecore model serialized as an EMOF XMI file.
37  */

38 public class EMOFResourceImpl extends XMIResourceImpl implements XMIResource
39 {
40   public EMOFResourceImpl()
41   {
42     super();
43   }
44
45   public EMOFResourceImpl(URI uri)
46   {
47     super(uri);
48   }
49
50   protected XMLHelper createXMLHelper()
51   {
52     return null;
53   }
54
55   protected XMLLoad createXMLLoad()
56   {
57     return new EMOFLoadImpl(new EMOFHelperImpl(this));
58   }
59   
60   protected XMLSave createXMLSave()
61   {
62     return new EMOFSaveImpl(new EMOFHelperImpl(this));
63   }
64
65   public String JavaDoc getID(EObject eObject)
66   {
67     String JavaDoc id = super.getID(eObject);
68     if (id == null)
69     {
70       EClass eClass = eObject.eClass();
71       if ((eClass != EcorePackage.eINSTANCE.getEAnnotation() || eObject.eContainer() == null) &&
72           eClass != EcorePackage.eINSTANCE.getEStringToStringMapEntry())
73       {
74         id = makeID(eObject);
75         getEObjectToIDMap().put(eObject, id);
76       }
77     }
78     return id;
79   }
80
81   protected String JavaDoc makeID(EObject eObject)
82   {
83     List JavaDoc uriFragmentPath = new ArrayList JavaDoc();
84     for (EObject container = eObject.eContainer(); container != null; container = eObject.eContainer())
85     {
86       uriFragmentPath.add(((InternalEObject)container).eURIFragmentSegment(eObject.eContainmentFeature(), eObject));
87       eObject = container;
88     }
89
90     StringBuffer JavaDoc result = new StringBuffer JavaDoc(eObject instanceof ENamedElement ?
91                                            ((ENamedElement)eObject).getName() :
92                                            "_" + Integer.toString(getContents().indexOf(eObject)));
93     for (ListIterator JavaDoc i = uriFragmentPath.listIterator(uriFragmentPath.size()); i.hasPrevious(); )
94     {
95       result.append('.');
96       result.append((String JavaDoc)i.previous());
97     }
98
99     return result.toString();
100   }
101 }
102
Popular Tags