1 16 17 package org.apache.jetspeed.capability; 18 19 import org.apache.jetspeed.util.MimeType; 21 import org.apache.jetspeed.om.registry.ClientEntry; 22 import org.apache.jetspeed.om.registry.MediaTypeEntry; 23 import org.apache.jetspeed.om.registry.MediaTypeRegistry; 24 import org.apache.jetspeed.services.Registry; 25 26 import java.util.Vector ; 28 import java.util.Iterator ; 29 import java.util.Enumeration ; 30 31 38 public class BaseCapabilityMap implements CapabilityMap 39 { 40 41 private String useragent; 42 private ClientEntry entry; 43 44 protected BaseCapabilityMap(String agent, ClientEntry entry) 45 { 46 this.useragent = agent; 47 this.entry = entry; 48 } 49 50 53 public MimeType getPreferredType() 54 { 55 return entry.getMimetypeMap().getPreferredMimetype(); 56 } 57 58 61 public String getPreferredMediaType() 62 { 63 Iterator i = listMediaTypes(); 64 65 if (i.hasNext()) 66 { 67 return (String )i.next(); 68 } 69 70 return null; 71 } 72 73 77 public Iterator listMediaTypes() 78 { 79 Vector results = new Vector (); 80 Vector types = new Vector (); 81 82 Enumeration en = ((MediaTypeRegistry)Registry.get(Registry.MEDIA_TYPE)).getEntries(); 84 while (en.hasMoreElements()) 85 { 86 types.add(en.nextElement()); 87 } 88 89 92 Iterator mimes = entry.getMimetypeMap().getMimetypes(); 93 94 while(mimes.hasNext()) 96 { 97 String mime = ((MimeType)mimes.next()).getContentType(); 98 Iterator i = types.iterator(); 99 100 while(i.hasNext()) 101 { 102 MediaTypeEntry mte = (MediaTypeEntry)i.next(); 103 104 if (mime.equals(mte.getMimeType())) 105 { 106 if (entry.getCapabilityMap().containsAll(mte.getCapabilityMap())) 107 { 108 results.add(mte.getName()); 109 } 110 } 111 } 112 } 113 114 return results.iterator(); 115 } 116 117 120 public String getAgent() 121 { 122 return this.useragent; 123 } 124 125 128 public boolean hasCapability( int cap ) 129 { 130 return false; 131 } 132 133 136 public boolean hasCapability( String capability ) 137 { 138 Iterator i = entry.getCapabilityMap().getCapabilities(); 139 140 while (i.hasNext()) 141 { 142 String cap = (String )i.next(); 143 144 if (cap.equals(capability)) 145 { 146 return true; 147 } 148 } 149 150 return false; 151 } 152 153 156 public MimeType[] getMimeTypes() 157 { 158 Vector v = new Vector (); 159 Iterator i = entry.getMimetypeMap().getMimetypes(); 160 161 while (i.hasNext()) 162 { 163 MimeType mime = (MimeType)i.next(); 164 v.add(mime); 165 } 166 167 return (MimeType[])v.toArray(); 168 } 169 170 173 public boolean supportsMimeType( MimeType mimeType ) 174 { 175 Iterator i = entry.getMimetypeMap().getMimetypes(); 176 177 while (i.hasNext()) 178 { 179 MimeType mime = (MimeType)i.next(); 180 181 if (mime.equals(mimeType)) 182 { 183 return true; 184 } 185 } 186 187 return false; 188 189 } 190 191 194 public boolean supportsMediaType( String media ) 195 { 196 if (media == null) 197 { 198 return true; 199 } 200 201 MediaTypeEntry mte = (MediaTypeEntry)Registry.getEntry(Registry.MEDIA_TYPE, media); 202 203 if (!supportsMimeType(new MimeType(mte.getMimeType()))) 204 { 205 return false; 206 } 207 208 return entry.getCapabilityMap().containsAll(mte.getCapabilityMap()); 209 210 } 211 212 215 public String toString() 216 { 217 StringBuffer desc = new StringBuffer (entry.getName()); 218 219 Iterator i = entry.getMimetypeMap().getMimetypes(); 220 221 while (i.hasNext()) 222 { 223 MimeType mime = (MimeType)i.next(); 224 desc.append( mime ).append("-"); 225 } 226 227 i = entry.getCapabilityMap().getCapabilities(); 228 229 while ( i.hasNext() ) 230 { 231 String capa = (String )i.next(); 232 desc.append(capa).append("/"); 233 } 234 235 return desc.toString(); 236 } 237 238 } 239 240 | Popular Tags |