KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > om > registry > base > BaseMimetypeMap


1 /*
2  * Copyright 2000-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.jetspeed.om.registry.base;
18
19 import org.apache.jetspeed.om.registry.MimetypeMap;
20 import org.apache.jetspeed.util.MimeType;
21 import java.util.Vector JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 /**
25  * Simple bean-like implementation of the CapabilityMap
26  *
27  * @author <a HREF="shesmer@raleigh.ibm.com">Stephan Hesmer</a>
28  * @author <a HREF="mailto:raphael@apache.org">RaphaŽl Luta</a>
29  * @version $Id: BaseMimetypeMap.java,v 1.3 2004/02/23 03:08:26 jford Exp $
30  */

31 public class BaseMimetypeMap implements MimetypeMap, java.io.Serializable JavaDoc
32 {
33     private Vector JavaDoc mimetypesVector = new Vector JavaDoc();
34
35     private transient Vector JavaDoc mimes;
36
37     public BaseMimetypeMap()
38     {
39     }
40
41     /**
42      * Implements the equals operation so that 2 elements are equal if
43      * all their member values are equal.
44      */

45     public boolean equals(Object JavaDoc object)
46     {
47         if (object==null)
48         {
49             return false;
50         }
51
52         BaseMimetypeMap obj = (BaseMimetypeMap)object;
53
54         Iterator JavaDoc i = mimetypesVector.iterator();
55         Iterator JavaDoc i2 = obj.mimetypesVector.iterator();
56         while(i.hasNext())
57         {
58             String JavaDoc c1 = (String JavaDoc)i.next();
59             String JavaDoc c2 = null;
60
61             if (i2.hasNext())
62             {
63                 c2 = (String JavaDoc)i2.next();
64             }
65             else
66             {
67                 return false;
68             }
69
70             if (!c1.equals(c2))
71             {
72                 return false;
73             }
74         }
75
76         if (i2.hasNext())
77         {
78             return false;
79         }
80
81         return true;
82     }
83
84     public Iterator JavaDoc getMimetypes()
85     {
86         if (mimes == null)
87         {
88             buildMimetable();
89         }
90
91         return mimes.iterator();
92     }
93
94     public MimeType getPreferredMimetype()
95     {
96         if (mimes == null)
97         {
98             buildMimetable();
99         }
100
101         return (MimeType)mimes.get(0);
102     }
103
104     public void addMimetype(String JavaDoc name)
105     {
106         if (!mimetypesVector.contains(name))
107         {
108             mimetypesVector.add(name);
109             buildMimetable();
110         }
111     }
112
113     public void removeMimetype(String JavaDoc name)
114     {
115         mimetypesVector.remove(name);
116         buildMimetable();
117     }
118
119     protected void buildMimetable()
120     {
121         Vector JavaDoc types = new Vector JavaDoc();
122         Iterator JavaDoc i = mimetypesVector.iterator();
123
124         while(i.hasNext())
125         {
126             String JavaDoc mime = (String JavaDoc)i.next();
127             types.add(new MimeType(mime));
128         }
129
130         this.mimes = types;
131     }
132
133     // castor related method definitions
134

135     public Vector JavaDoc getMimetypesVector()
136     {
137         return mimetypesVector;
138     }
139
140 }
141
Popular Tags