KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > emb > MediaFormatRegistry


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license. See terms of license at gnu.org.
5  */

6
7 package javax.emb;
8
9 import java.util.Collections;
10 import java.util.HashMap;
11 import java.util.Iterator;
12 import java.util.Map;
13
14 /**
15  * This singleton class manages the different header types and allows the
16  * generic selection of a format bean suitable for a given media. The selection
17  * criterion is the file extension associated with the media, and a media
18  * format can be registered multiple times if many file extensions are
19  * typically associated with it.
20  *
21  * <p>The class has a private default constructor to prevent direct
22  * construction of instances in applications. Instead, the <code>SINGLETON</code>
23  * constant provides the one and only instance of this class. Implementations
24  * are free to initialize the registry with mappings of their choice. However,
25  * the design allows applications to plug in additional mappings.
26  *
27  * @version <tt>$Revision: 1.7 $</tt>
28  * @author <a HREF="mailto:ricardoarguello@users.sourceforge.net">Ricardo
29  * Argüello</a>
30  */

31 public final class MediaFormatRegistry
32 {
33    public static final MediaFormatRegistry SINGLETON =
34       new MediaFormatRegistry();
35
36    private final Map mediaFormats = Collections.synchronizedMap(new HashMap());
37
38    /**
39     * Private default constructor to prevent direct construction of instances
40     * in applications. Use <code>SINGLETON</code> instead.
41     */

42    private MediaFormatRegistry()
43    {
44    }
45
46    /**
47     * This operation associates the given file extension with the given media
48     * format instance.
49     *
50     * @param fileExtension a file extension identifying the format. Note that
51     * such extensions should not start with a leading dot.
52     * @param mediaFormat the media format instance to be registered.
53     * @throws javax.embFormatAlreadyBoundException if a mapping already exists
54     * for the given file extension.
55     * @throws java.lang.NullPointerException if one of the values passed is
56     * <code>null</code>.
57     */

58    public void bind(String fileExtension, MediaFormat mediaFormat)
59       throws FormatAlreadyBoundException
60    {
61       if (fileExtension == null || mediaFormat == null)
62       {
63          throw new NullPointerException();
64       }
65
66       fileExtension = fileExtension.toLowerCase();
67
68       if (mediaFormats.containsKey(fileExtension))
69       {
70          throw new FormatAlreadyBoundException();
71       }
72
73       mediaFormats.put(fileExtension, mediaFormat);
74    }
75
76    /**
77     * This operation returns the file extensions for which bindings exist in
78     * the registry.
79     *
80     * @return the file extensions.
81     */

82    public Iterator getFileExtensions()
83    {
84       synchronized (mediaFormats)
85       {
86          return mediaFormats.keySet().iterator();
87       }
88    }
89
90    /**
91     * Returns the media format instance registered under the given file
92     * extension.
93     *
94     * @param fileExtension a file extension identifying the format. Note that
95     * such extensions should not start with a leading dot.
96     * @param javax.emb.FormatNotFoundException if there is no mapping for the
97     * given file extension.
98     * @throws java.lang.NullPointerException if the value passed is <code>null</code>.
99     */

100    public MediaFormat lookup(String fileExtension)
101       throws FormatNotFoundException
102    {
103       fileExtension = fileExtension.toLowerCase();
104
105       if (!mediaFormats.containsKey(fileExtension))
106       {
107          throw new FormatNotFoundException();
108       }
109
110       return (MediaFormat) mediaFormats.get(fileExtension);
111    }
112
113    /**
114     * This operation associates the given file extension with the given media
115     * format instance. If a mapping already exists for the given file
116     * extension, it is replaced by the new one.
117     *
118     * @param fileExtension a file extension identifying the format. Note that
119     * such extensions should not start with a leading dot.
120     * @param mediaFormat the media format instance to be registered.
121     * @throws java.lang.NullPointerException if one of the values passed is
122     * <code>null</code>.
123     */

124    public void rebind(String fileExtension, MediaFormat mediaFormat)
125    {
126       if (fileExtension == null || mediaFormat == null)
127       {
128          throw new NullPointerException();
129       }
130
131       fileExtension = fileExtension.toLowerCase();
132
133       mediaFormats.put(fileExtension, mediaFormat);
134    }
135
136    /**
137     * This method removes the mapping defined by the given file extension.
138     *
139     * @param fileExtension a file extension identifying the format. Note that
140     * such extensions should not start with a leading dot.
141     * @throws javax.emb.FormatNotFoundException if there is no mapping for the
142     * given file extension.
143     * @throws java.lang.NullPointerException if the value passed is <code>null</code>.
144     */

145    public void unbind(String fileExtension) throws FormatNotFoundException
146    {
147       if (fileExtension == null)
148       {
149          throw new NullPointerException();
150       }
151
152       fileExtension = fileExtension.toLowerCase();
153
154       if (!mediaFormats.containsKey(fileExtension))
155       {
156          throw new FormatNotFoundException();
157       }
158
159       mediaFormats.remove(fileExtension);
160    }
161 }
Popular Tags