KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > mapping > ecore2xml > Ecore2XMLRegistry


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2005 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: Ecore2XMLRegistry.java,v 1.2 2005/06/21 16:17:03 khussey Exp $
16  */

17 package org.eclipse.emf.mapping.ecore2xml;
18
19
20 import java.util.Map JavaDoc;
21
22 import org.eclipse.core.runtime.IConfigurationElement;
23 import org.eclipse.core.runtime.IExtensionPoint;
24 import org.eclipse.core.runtime.IExtensionRegistry;
25 import org.eclipse.emf.common.util.URI;
26 import org.eclipse.emf.common.util.WrappedException;
27 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
28 import org.eclipse.emf.ecore.util.EcoreUtil;
29 import org.eclipse.emf.ecore.xmi.XMLResource;
30 import org.eclipse.emf.mapping.ecore2xml.impl.Ecore2XMLRegistryImpl;
31
32
33 /**
34  * A registry for mappings from Ecore to XML.
35  */

36 public interface Ecore2XMLRegistry extends Map JavaDoc
37 {
38
39   /**
40    * A descriptor for Ecore2XML mappings registered via an extension point.
41    */

42   public interface Descriptor
43   {
44     XMLResource.XMLMap getXMLMap();
45   }
46
47   /**
48    * Reads and registers Ecore2XML mappings in the extension registry.
49    */

50   public static class Reader
51   {
52
53     protected static class Ecore2XMLDescriptor implements Descriptor
54     {
55       private final IConfigurationElement element;
56
57       private XMLResource.XMLMap xmlMap = null;
58
59       protected Ecore2XMLDescriptor(IConfigurationElement element)
60       {
61         super();
62
63         this.element = element;
64       }
65
66       public XMLResource.XMLMap getXMLMap()
67       {
68         if (xmlMap == null)
69         {
70           try
71           {
72             xmlMap = (XMLResource.XMLMap)EcoreUtil.getObjectByType(new ResourceSetImpl().getResource(
73               URI.createURI(element.getAttribute(ATT_XMLMAP)),
74               true).getContents(), Ecore2XMLPackage.eINSTANCE.getXMLMap());
75           }
76           catch (Exception JavaDoc e)
77           {
78             throw new WrappedException(e);
79           }
80         }
81
82         return xmlMap;
83       }
84     }
85
86     protected static final String JavaDoc TAG_ECORE2XML = "ecore2xml"; //$NON-NLS-1$
87

88     protected static final String JavaDoc ATT_URI = "uri"; //$NON-NLS-1$
89

90     protected static final String JavaDoc ATT_XMLMAP = "xmlmap"; //$NON-NLS-1$
91

92     private final IExtensionRegistry extensionRegistry;
93
94     private final String JavaDoc namespace;
95
96     private final String JavaDoc extensionPointID;
97
98     public Reader(IExtensionRegistry extensionRegistry, String JavaDoc namespace, String JavaDoc extensionPointID)
99     {
100       super();
101
102       this.extensionRegistry = extensionRegistry;
103       this.namespace = namespace;
104       this.extensionPointID = extensionPointID;
105     }
106
107     protected void readElement(IConfigurationElement element)
108     {
109       if (TAG_ECORE2XML.equals(element.getName()))
110       {
111         String JavaDoc uri = element.getAttribute(ATT_URI);
112
113         if (uri == null || element.getAttribute(ATT_XMLMAP) == null)
114         {
115           // missing attribute
116
}
117         else
118         {
119           Ecore2XMLRegistry.INSTANCE.put(uri, new Ecore2XMLDescriptor(element));
120         }
121       }
122       else
123       {
124         // invalid element
125
}
126     }
127
128     public void readRegistry()
129     {
130       IExtensionPoint extensionPoint = extensionRegistry.getExtensionPoint(namespace, extensionPointID);
131
132       if (extensionPoint != null)
133       {
134         IConfigurationElement[] elements = extensionPoint.getConfigurationElements();
135
136         for (int i = 0; i < elements.length; i++)
137         {
138           readElement(elements[i]);
139         }
140       }
141     }
142
143   }
144
145   Ecore2XMLRegistry INSTANCE = new Ecore2XMLRegistryImpl();
146
147   XMLResource.XMLMap getXMLMap(String JavaDoc nsURI);
148
149 }
Popular Tags