KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

31 public class URIMappingRegistryImpl extends BasicEMap
32 {
33   /**
34    * The implementation of the global mapping registry.
35    * @see org.eclipse.emf.ecore.resource.URIConverter#URI_MAP
36    */

37   public static final URIMappingRegistryImpl INSTANCE = new URIMappingRegistryImpl();
38
39   /**
40    * A list of lists of prefix URIs;
41    * it is indexed by segment count to yield a list of prefixes of that length.
42    */

43   protected BasicEList prefixMaps = new BasicEList();
44
45   /**
46    * Creates an instance.
47    */

48   public URIMappingRegistryImpl()
49   {
50   }
51
52   /**
53    * Creates an {@link MappingEntryImpl}.
54    */

55   protected Entry newEntry(int hash, Object JavaDoc key, Object JavaDoc value)
56   {
57     validateKey(key);
58     validateValue(value);
59     return new MappingEntryImpl(hash, key, value);
60   }
61
62   /**
63    * An extended implementation that maintains a bit
64    * indicating if the entry represents a folder to folder mapping.
65    */

66   protected class MappingEntryImpl extends EntryImpl
67   {
68     /**
69      * The indicator whether this entry represents a folder to folder mapping.
70      */

71     public boolean isPrefixMapEntry;
72
73     /**
74      * Creates an instance.
75      */

76     public MappingEntryImpl(int hash, Object JavaDoc key, Object JavaDoc value)
77     {
78       super(hash, key, value);
79       determineEntryType();
80     }
81
82     /**
83      * Computes whether this entry represents a folder to folder mapping.
84      */

85     public void determineEntryType()
86     {
87       isPrefixMapEntry = ((URI)key).isPrefix() && ((URI)value).isPrefix();
88     }
89   }
90
91   /**
92    * Returns the remapped URI, or the URI itself.
93    * This implementation uses the map to find an exact match.
94    * Failing that, it matches the {@link #prefixMaps} prefixes in order.
95    * And failing that, it delegates to {@link #delegatedGetURI(URI) delegatedGetURI}.
96    * @param uri the URI to remap.
97    * @return the remapped URI, or the URI itself.
98    */

99   public URI getURI(URI uri)
100   {
101     URI result = (URI)get(uri);
102     if (result == null)
103     {
104       if (prefixMaps != null)
105       {
106         for (int i = Math.min(prefixMaps.size() - 1, uri.segmentCount()); i >= 0; --i)
107         {
108           List JavaDoc prefixes = (List JavaDoc)prefixMaps.get(i);
109           for (int j = prefixes.size() - 1; j >= 0; --j)
110           {
111             Entry entry = (Entry)prefixes.get(j);
112             result = uri.replacePrefix((URI)entry.getKey(), (URI)entry.getValue());
113
114             if (result != null)
115             {
116               return result;
117             }
118           }
119         }
120       }
121
122       result = delegatedGetURI(uri);
123     }
124
125     return result;
126   }
127
128   /**
129    * Returns the mapped URI for the given URI, when standard alternatives fail.
130    * <p>
131    * This implementation returns <code>uri</code>.
132    * </p>
133    * @param uri the URI.
134    * @return the mapped URI.
135    * @see #getURI(URI)
136    */

137   protected URI delegatedGetURI(URI uri)
138   {
139     return uri;
140   }
141
142   /**
143    * A map that is a {@link URIConverterImpl.URIMap}.
144    */

145   protected class URIMapImpl extends DelegatingMap implements URIConverterImpl.URIMap
146   {
147     /**
148      * Creates an isntance.
149      */

150     public URIMapImpl()
151     {
152     }
153
154     /**
155      * Returns the remapped URI, or the URI itself.
156      * This implementation delegates to the containing {@link URIMappingRegistryImpl}.
157      * @param uri the URI to remap.
158      * @return the remapped URI, or the URI itself.
159      */

160     public URI getURI(URI uri)
161     {
162       return URIMappingRegistryImpl.this.getURI(uri);
163     }
164   }
165
166   /**
167    * Returns a map view that implements {@link URIConverterImpl.URIMap}.
168    */

169   public Map JavaDoc map()
170   {
171     if (view == null)
172     {
173       view = new View();
174     }
175     if (view.map == null)
176     {
177       view.map = new URIMapImpl();
178     }
179
180     return view.map;
181   }
182
183   /**
184    * Validates that the key is a URI.
185    */

186   protected void validateKey(Object JavaDoc key)
187   {
188     if (!(key instanceof URI))
189     {
190       throw new IllegalArgumentException JavaDoc();
191     }
192   }
193
194   /**
195    * Validates that the value is a URI.
196    */

197   protected void validateValue(Object JavaDoc value)
198   {
199     if (!(value instanceof URI))
200     {
201       throw new IllegalArgumentException JavaDoc();
202     }
203   }
204
205   /**
206    * Checks for folder mappings to populate the {@link #prefixMaps prefix maps}.
207    */

208   protected void didAdd(Entry entry)
209   {
210     if (((MappingEntryImpl)entry).isPrefixMapEntry)
211     {
212       int length = ((URI)entry.getKey()).segmentCount();
213       if (prefixMaps == null)
214       {
215         prefixMaps = new BasicEList();
216       }
217
218       for (int i = prefixMaps.size() - 1; i <= length; ++i)
219       {
220         prefixMaps.add(new BasicEList());
221       }
222
223       ((List JavaDoc)prefixMaps.get(length)).add(entry);
224     }
225   }
226
227   /**
228    * Checks for folder mappings to update the {@link #prefixMaps prefix maps}.
229    */

230   protected void didModify(Entry entry, Object JavaDoc oldValue)
231   {
232     didRemove(entry);
233     ((MappingEntryImpl)entry).determineEntryType();
234     didAdd(entry);
235   }
236
237   /**
238    * Checks for folder mappings to cleanup the {@link #prefixMaps prefix maps}.
239    */

240   protected void didRemove(Entry entry)
241   {
242     if (((MappingEntryImpl)entry).isPrefixMapEntry)
243     {
244       int length = ((URI)entry.getKey()).segmentCount();
245       ((List JavaDoc)prefixMaps.get(length)).remove(entry);
246     }
247   }
248
249   /**
250    * Discards all the {@link #prefixMaps prefix maps}.
251    */

252   protected void didClear(BasicEList [] oldEntryData)
253   {
254     prefixMaps = null;
255   }
256 }
257
Popular Tags