KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > resource > impl > ResourceFactoryRegistryImpl


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: ResourceFactoryRegistryImpl.java,v 1.3 2005/06/12 13:29:22 emerks Exp $
16  */

17 package org.eclipse.emf.ecore.resource.impl;
18
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.eclipse.emf.common.util.URI;
24 import org.eclipse.emf.ecore.resource.Resource;
25
26
27 /**
28  * An extensible implementation of a resource factory registry.
29  */

30 public class ResourceFactoryRegistryImpl implements Resource.Factory.Registry
31 {
32   /**
33    * The protocol map.
34    */

35   protected Map JavaDoc protocolToFactoryMap = new HashMap JavaDoc();
36
37   /**
38    * The extension map.
39    */

40   protected Map JavaDoc extensionToFactoryMap = new HashMap JavaDoc();
41
42   /**
43    * Creates an instance.
44    */

45   public ResourceFactoryRegistryImpl()
46   {
47   }
48
49   /**
50    * Returns the resource factory appropriate for the given URI.
51    * <p>
52    * This implementation does the {@link org.eclipse.emf.ecore.resource.Resource.Factory.Registry#getFactory typical} thing.
53    * It will delegate to {@link #delegatedGetFactory(URI)}
54    * in the case that the typical behaviour doesn't produce a result;
55    * clients are encouraged to override that method only.
56    * </p>
57    * @param uri the URI.
58    * @return the resource factory appropriate for the given URI.
59    * @see org.eclipse.emf.ecore.resource.ResourceSet#createResource(URI)
60    */

61   public Resource.Factory getFactory(URI uri)
62   {
63     String JavaDoc protocol = uri.scheme();
64     Object JavaDoc resourceFactory = protocolToFactoryMap.get(protocol);
65     if (resourceFactory == null)
66     {
67       String JavaDoc extension = uri.fileExtension();
68       resourceFactory = extensionToFactoryMap.get(extension);
69       if (resourceFactory == null)
70       {
71         resourceFactory = extensionToFactoryMap.get("*");
72         if (resourceFactory == null)
73         {
74           resourceFactory = delegatedGetFactory(uri);
75         }
76       }
77     }
78
79     return
80       resourceFactory instanceof Resource.Factory.Descriptor ?
81         ((Resource.Factory.Descriptor)resourceFactory).createFactory() :
82         (Resource.Factory)resourceFactory;
83   }
84
85   /**
86    * Returns the resource factory appropriate for the given URI, when standard alternatives fail.
87    * <p>
88    * This implementation returns <code>null</code>;
89    * clients are encouraged to override it.
90    * </p>
91    * @param uri the URI.
92    * @return the resource factory appropriate for the given URI.
93    * @see #getFactory(URI)
94    */

95   protected Resource.Factory delegatedGetFactory(URI uri)
96   {
97     return null;
98   }
99
100   /*
101    * Javadoc copied from interface.
102    */

103   public Map JavaDoc getExtensionToFactoryMap()
104   {
105     return extensionToFactoryMap;
106   }
107
108   /*
109    * Javadoc copied from interface.
110    */

111   public Map JavaDoc getProtocolToFactoryMap()
112   {
113     return protocolToFactoryMap;
114   }
115 }
116
Popular Tags