KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > media > urlcomposers > URLComposer


1 /*
2  
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5  
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8  
9  */

10
11 package org.mmbase.applications.media.urlcomposers;
12
13 import org.mmbase.applications.media.builders.MediaProviders;
14 import org.mmbase.applications.media.builders.MediaSources;
15 import org.mmbase.module.core.MMObjectNode;
16 import org.mmbase.util.HashCodeUtil;
17 import org.mmbase.applications.media.Format;
18
19 import java.util.*;
20
21 /**
22  * URLComposer is a wrapper/container class around an URL. It contains besides the
23  * URL some extra meta information about it, like the original source
24  * object of the resource it represents and if it is currently
25  * available or not. An URL can be unavailable because of two
26  * reasons: Because the provider is offline, or because the fragment
27  * where it belongs to is not valid (e.g. because of publishtimes)
28  *
29  * It is used by the Media builders to pass around information (mainly
30  * as entry in Lists)
31  *
32  * @author Michiel Meeuwissen
33  * @author Rob Vermeulen (VPRO)
34  */

35 public class URLComposer {
36     protected MMObjectNode source;
37     protected MMObjectNode provider;
38     protected Map info;
39     
40     public void init(MMObjectNode provider, MMObjectNode source, MMObjectNode fragment, Map info, Set cacheExpireObjects) {
41         if(cacheExpireObjects!=null) {
42             cacheExpireObjects.add(provider);
43             cacheExpireObjects.add(source);
44             
45         }
46         
47         if (source == null) throw new RuntimeException JavaDoc("Source may not be null in a URLComposer object");
48         if (provider == null) throw new RuntimeException JavaDoc("Provider may not be null in a URLComposer object");
49         this.provider = provider;
50         this.source = source;
51         this.info = info;
52         if (this.info == null) this.info = new java.util.Hashtable JavaDoc();
53     }
54     
55     public MMObjectNode getSource() { return source; }
56     public MMObjectNode getProvider() { return provider;}
57     public Map getInfo() { return info; }
58     
59     /**
60      * The format of the produced URL. This is not necessarily the format of the source.
61      * (Though it normally would be)
62      */

63     public Format getFormat() {
64         return Format.get(source.getIntValue("format"));
65     }
66
67     /**
68      * The mime-type of the produced URL. This is not necessarily the mimetype of the source.
69      * (Though it normally would be)
70      */

71     public String JavaDoc getMimeType() {
72         return getFormat().getMimeType();
73     }
74
75     
76     
77     public String JavaDoc getGUIIndicator(Map options) {
78         Locale locale = (Locale) options.get("locale");
79         return getFormat().getGUIIndicator(locale);
80     }
81     
82     public String JavaDoc getDescription(Map options) {
83         return null; // no informative description
84
}
85     
86     /**
87      * Returns true. This can be overridden if the URLComposer not
88      * always can do it's job. It then returns false if it is (can be?)
89      * taken from consideration.
90      */

91     public boolean canCompose() {
92         return true;
93     }
94     
95     /**
96      * Extension will normally create URL's differently. They override this function.
97      *
98      */

99     protected StringBuffer JavaDoc getURLBuffer() {
100         StringBuffer JavaDoc buff = new StringBuffer JavaDoc(provider.getStringValue("protocol") + "://" + provider.getStringValue("host") + provider.getStringValue("rootpath") + source.getStringValue("url"));
101         return buff;
102     }
103     /**
104      * Returns the URL as a String. To encourage efficient coding,
105      * this method is final. Override getURLBuffer instead.
106      */

107     
108     public final String JavaDoc getURL() {
109         return getURLBuffer().toString();
110     }
111     
112     public boolean isAvailable() {
113         boolean sourceAvailable = (source != null && source.getIntValue("state") == MediaSources.STATE_DONE);
114         boolean providerAvailable = (provider.getIntValue("state") == MediaProviders.STATE_ON);
115         return providerAvailable && sourceAvailable;
116     }
117     
118     public String JavaDoc toString() {
119         // for verboseness:
120
String JavaDoc className = getClass().getName().substring(getClass().getName().lastIndexOf(".") + 1);
121         if (isAvailable()) {
122             return className + "/" + getFormat() + ": " + getURL();
123         } else {
124             return "{" + className + "/" + getFormat() + ": " + getURL() + "}";
125         }
126     }
127     
128     /**
129      * @see java.lang.Object#equals(java.lang.Object)
130      */

131     public boolean equals(Object JavaDoc o) {
132         if (o == this) return true;
133         if (o == null) return false;
134         if (o.getClass().equals(getClass())) {
135             URLComposer r = (URLComposer) o;
136             return
137             (source == null ? r.source == null : source.getNumber() == r.source.getNumber()) &&
138             (provider == null ? r.provider == null : provider.getNumber() == r.provider.getNumber()) &&
139             (info == null ? r.info == null : info.equals(r.info));
140         }
141         return false;
142     }
143     
144     
145     /**
146      * @see java.lang.Object#hashCode()
147      */

148     public int hashCode() {
149         int result = 0;
150         result = HashCodeUtil.hashCode(result, source == null ? 0 : source.getNumber());
151         result = HashCodeUtil.hashCode(result, provider == null ? 0 : provider.getNumber());
152         result = HashCodeUtil.hashCode(result, info == null ? 0 : info.hashCode());
153         return result;
154     }
155 }
156
Popular Tags