View Javadoc

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;
22  import java.util.Iterator;
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
32  {
33      private Vector mimetypesVector = new Vector();
34  
35      private transient Vector 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 object)
46      {
47          if (object==null)
48          {
49              return false;
50          }
51  
52          BaseMimetypeMap obj = (BaseMimetypeMap)object;
53  
54          Iterator i = mimetypesVector.iterator();
55          Iterator i2 = obj.mimetypesVector.iterator();
56          while(i.hasNext())
57          {
58              String c1 = (String)i.next();
59              String c2 = null;
60  
61              if (i2.hasNext())
62              {
63                  c2 = (String)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 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 name)
105     {
106         if (!mimetypesVector.contains(name))
107         {
108             mimetypesVector.add(name);
109             buildMimetable();
110         }
111     }
112 
113     public void removeMimetype(String name)
114     {
115         mimetypesVector.remove(name);
116         buildMimetable();
117     }
118 
119     protected void buildMimetable()
120     {
121         Vector types = new Vector();
122         Iterator i = mimetypesVector.iterator();
123 
124         while(i.hasNext())
125         {
126             String mime = (String)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 getMimetypesVector()
136     {
137         return mimetypesVector;
138     }
139 
140 }