KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > wsrp > utils > Modes


1 /**
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowledgment may appear in the software itself,
24  * if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "WSRP4J", and "Apache Software
27  * Foundation" must not be used to endorse or promote products
28  * derived from this software without prior written permission. For
29  * written permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * or "WSRP4J", nor may "Apache" or "WSRP4J" appear in their name,
33  * without prior written permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  *
48  * This software consists of voluntary contributions made by many
49  * individuals on behalf of the Apache Software Foundation. For more
50  * information on the Apache Software Foundation, please see
51  * <http://www.apache.org/>.
52  */

53
54
55 package org.exoplatform.services.wsrp.utils;
56
57 import javax.portlet.PortletMode;
58
59 public class Modes implements java.io.Serializable JavaDoc {
60   private java.lang.String JavaDoc _value_;
61   private static java.util.HashMap JavaDoc _table_ = new java.util.HashMap JavaDoc();
62
63   // Constructor
64
protected Modes(java.lang.String JavaDoc value) {
65     _value_ = value;
66     _table_.put(_value_, this);
67   }
68
69   // define the modes we can currently handle
70
public static final java.lang.String JavaDoc _view = "wsrp:view";
71   public static final java.lang.String JavaDoc _edit = "wsrp:edit";
72   public static final java.lang.String JavaDoc _help = "wsrp:help";
73   public static final java.lang.String JavaDoc _preview = "wsrp:preview";
74   public static final Modes view = new Modes(_view);
75   public static final Modes edit = new Modes(_edit);
76   public static final Modes help = new Modes(_help);
77   public static final Modes preview = new Modes(_preview);
78
79   public java.lang.String JavaDoc getValue() {
80     return _value_;
81   }
82
83   /**
84    * Returns the WSRP mode build from a string representation
85    * If a not supported Mode is requested, null is returned
86    *
87    * @param <code>String</string> representation of the WSRP mode
88    * @return The WSRP <code>Mode</code> represented by the passed string
89    */

90   public static Modes fromValue(java.lang.String JavaDoc value) {
91     return (Modes) _table_.get(value);
92   }
93
94   /**
95    * Returns the WSRP mode build from a string representation
96    * If a not supported Mode is requested, null is returned
97    *
98    * @param <code>String</string> representation of the WSRP mode
99    * @return The WSRP <code>Mode</code> represented by the passed string
100    */

101   public static Modes fromString(java.lang.String JavaDoc value) {
102     return fromValue(value);
103   }
104
105   public boolean equals(java.lang.Object JavaDoc obj) {
106     return (obj == this);
107   }
108
109   public int hashCode() {
110     return toString().hashCode();
111   }
112
113   public java.lang.String JavaDoc toString() {
114     return _value_;
115   }
116
117   public java.lang.Object JavaDoc readResolve() throws java.io.ObjectStreamException JavaDoc {
118     return fromValue(_value_);
119   }
120
121   /**
122    * This helper method maps portlet modes defined in wsrp to portlet modes
123    * defined in the java portlet standard (JSR-168). If the passed wsrp mode
124    * is null or can not be mapped the view mode is returned.
125    *
126    * @return The <code>javax.portlet.PortletMode</code> which corresponds to the given wsrp mode.
127    */

128   public static PortletMode getJsrPortletModeFromWsrpMode(Modes wsrpMode) {
129     if (wsrpMode == null) {
130       return PortletMode.VIEW;
131     } else if (wsrpMode.equals(Modes.edit)) {
132       return PortletMode.EDIT;
133     } else if (wsrpMode.equals(Modes.help)) {
134       return PortletMode.HELP;
135     } else if (wsrpMode.equals(Modes.view)) {
136       return PortletMode.VIEW;
137     }
138
139     return PortletMode.VIEW;
140   }
141
142   /**
143    * This helper method maps portlet modes defined in tha java portlet standard (JSR-168)
144    * to modes defined in wsrp. If the passed portlet mode can not be resolved wsrp:view mode
145    * is returned.
146    *
147    * @param portletMode The <code>javax.portlet.PortletMode</code> which should be resolved as
148    * as portlet mode defined in wsrp.
149    * @return
150    */

151   public static Modes getWsrpModeFromJsrPortletMode(PortletMode portletMode) {
152     if (portletMode == null) {
153       throw new IllegalArgumentException JavaDoc("Portlet mode must not be null.");
154     }
155     if (portletMode.equals(PortletMode.EDIT)) {
156       return Modes.edit;
157     } else if (portletMode.equals(PortletMode.HELP)) {
158       return Modes.help;
159     } else if (portletMode.equals(PortletMode.VIEW)) {
160       return Modes.view;
161     }
162     return Modes.view;
163   }
164 }
165
Popular Tags