View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.gui.control;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.NodeData;
38  import info.magnolia.cms.util.NodeDataUtil;
39  import info.magnolia.context.MgnlContext;
40  import info.magnolia.context.WebContext;
41  
42  import java.io.IOException;
43  import java.io.Writer;
44  import java.util.ArrayList;
45  import java.util.Hashtable;
46  import java.util.Iterator;
47  import java.util.List;
48  import java.util.Map;
49  
50  import javax.jcr.PropertyType;
51  import javax.servlet.http.HttpServletRequest;
52  
53  import org.apache.commons.lang.StringUtils;
54  
55  
56  /**
57   * @author Vinzenz Wyser
58   * @version 2.0
59   */
60  public class ControlImpl implements Control {
61  
62      public static final int BUTTONTYPE_PUSHBUTTON = 0;
63  
64      public static final int BUTTONTYPE_CHECKBOX = 1;
65  
66      public static final int BUTTONTYPE_RADIO = 2;
67  
68      public static final int BUTTONSTATE_NORMAL = 0;
69  
70      public static final int BUTTONSTATE_MOUSEOVER = 1;
71  
72      public static final int BUTTONSTATE_MOUSEDOWN = 2;
73  
74      public static final int BUTTONSTATE_PUSHED = 3;
75  
76      public static final int BUTTONSTATE_DISABLED = 4; // not yet supported
77  
78      public static final int VALUETYPE_SINGLE = 0;
79  
80      public static final int VALUETYPE_MULTIPLE = 1;
81  
82      public static final int ENCRYPTION_NO = 0;
83  
84      /**
85       * @deprecated since 4.5. Use {@link #ENCRYPTION_NO} instead.
86       */
87      @Deprecated
88      public static final int ENCODING_NO = ENCRYPTION_NO;
89  
90      public static final int ENCRYPTION_NO_ENCODING_BASE64 = 1;
91  
92      public static final int ENCRYPTION_HASH_SHA = 2;
93  
94      public static final int ENCRYPTION_HASH_BCRYPT = 3;
95  
96      public static final int RICHEDIT_NONE = 0;
97  
98      public static final int RICHEDIT_KUPU = 1;
99  
100     public static final int RICHEDIT_FCK = 2;
101 
102     public static final String CSSCLASS_CONTROLBUTTON = "mgnlControlButton"; //$NON-NLS-1$
103 
104     public static final String CSSCLASS_CONTROLBUTTONSMALL = "mgnlControlButtonSmall"; //$NON-NLS-1$
105 
106     public static final String CSSCLASS_CONTROLBAR = "mgnlControlBar"; //$NON-NLS-1$
107 
108     public static final String CSSCLASS_CONTROLBARSMALL = "mgnlControlBarSmall"; //$NON-NLS-1$
109 
110     private int valueType = VALUETYPE_SINGLE;
111 
112     private int encoding = ENCRYPTION_NO;
113 
114     private int isRichEditValue = RICHEDIT_NONE;
115 
116     private String label;
117 
118     private String name;
119 
120     private String id;
121 
122     private String value;
123 
124     private List values = new ArrayList(); // mulitple values (checkbox)
125 
126     private Map events = new Hashtable();
127 
128     private Content websiteNode;
129 
130     private String htmlPre;
131 
132     private String htmlInter;
133 
134     private String htmlPost;
135 
136     private int type = PropertyType.STRING;
137 
138     private boolean saveInfo = true;
139 
140     private String cssClass = StringUtils.EMPTY;
141 
142     private Map cssStyles = new Hashtable();
143 
144     private String path;
145 
146     private String nodeCollectionName;
147 
148     private String nodeName;
149 
150     private final String newLine;
151 
152     public ControlImpl() {
153         this.newLine = System.getProperty("line.separator");
154     }
155 
156     public ControlImpl(String name, String value) {
157         this();
158         this.setName(name);
159         this.setValue(value);
160     }
161 
162     public ControlImpl(String name, List values) {
163         this();
164         this.setName(name);
165         this.setValues(values);
166     }
167 
168     public ControlImpl(String name, Content websiteNode) {
169         this();
170         this.setName(name);
171         this.setWebsiteNode(websiteNode);
172     }
173 
174     public void setPath(String path) {
175         this.path = path;
176     }
177 
178     public String getPath() {
179         return this.path;
180     }
181 
182     public void setNodeCollectionName(String nodeCollectionName) {
183         this.nodeCollectionName = nodeCollectionName;
184     }
185 
186     public String getNodeCollectionName() {
187         return this.nodeCollectionName;
188     }
189 
190     public String getNodeCollectionName(String nullOrEmptyValue) {
191         if (StringUtils.isEmpty(this.getNodeCollectionName())) {
192             return nullOrEmptyValue;
193         }
194 
195         return this.getNodeCollectionName();
196     }
197 
198     public void setNodeName(String nodeName) {
199         this.nodeName = nodeName;
200     }
201 
202     public String getNodeName() {
203         return this.nodeName;
204     }
205 
206     public String getNodeName(String nullOrEmptyValue) {
207         if (StringUtils.isEmpty(this.getNodeName())) {
208             return nullOrEmptyValue;
209         }
210 
211         return this.getNodeName();
212     }
213 
214     /**
215      * @deprecated
216      */
217     @Deprecated
218     public void setRequest(HttpServletRequest request) {
219     }
220 
221     /**
222      * @return
223      */
224     public HttpServletRequest getRequest() {
225         return ((WebContext)MgnlContext.getInstance()).getRequest();
226     }
227 
228     public void setName(String s) {
229         this.name = s;
230     }
231 
232     public String getName() {
233         return this.name;
234     }
235 
236     public void setId(String s) {
237         this.id = s;
238     }
239 
240     public String getId() {
241         return this.id;
242     }
243 
244     public String getHtmlId() {
245         if (StringUtils.isNotEmpty(this.getId())) {
246             return " id=\"" + this.getId() + "\""; //$NON-NLS-1$ //$NON-NLS-2$
247         }
248 
249         return StringUtils.EMPTY;
250     }
251 
252     public void setValue(String value) {
253         this.value = value;
254     }
255 
256     public String getValue() {
257         if (this.value != null) {
258             return this.value;
259         }
260 
261         try {
262             return this.getWebsiteNode().getNodeData(this.getName()).getString();
263         }
264         catch (Exception e) {
265             return StringUtils.EMPTY;
266         }
267 
268     }
269 
270     public void setValues(List values) {
271         this.values = values;
272     }
273 
274     public List getValues() {
275         if (this.values.size() != 0) {
276             return this.values;
277         }
278         try {
279             NodeData node = this.getWebsiteNode().getNodeData(this.getName());
280             if(node.isMultiValue() != NodeData.MULTIVALUE_FALSE) {
281                 return NodeDataUtil.getValuesStringList(node.getValues());
282             } else {
283                 Iterator it = this.getWebsiteNode().getContent(this.getName()).getNodeDataCollection().iterator();
284                 List l = new ArrayList();
285                 while (it.hasNext()) {
286                     NodeData data = (NodeData) it.next();
287                     l.add(data.getString());
288                 }
289                 return l;
290             }
291         }
292         catch (Exception re) {
293             return this.values;
294         }
295     }
296 
297     public void setWebsiteNode(Content c) {
298         this.websiteNode = c;
299     }
300 
301     public Content getWebsiteNode() {
302         return this.websiteNode;
303     }
304 
305     public void setLabel(String label) {
306         this.label = label;
307     }
308 
309     public String getLabel() {
310         return this.label;
311     }
312 
313     public void setEvent(String event, String action) {
314         setEvent(event, action, false);
315     }
316 
317     public void setEvent(String event, String action, boolean removeExisting) {
318         String eventLower = event.toLowerCase();
319         String existing = null;
320         if (!removeExisting) {
321             existing = (String) this.getEvents().get(eventLower);
322         }
323         if (existing == null) {
324             existing = StringUtils.EMPTY;
325         }
326 
327         this.getEvents().put(eventLower, existing + action);
328     }
329 
330     public void setEvents(Map h) {
331         this.events = h;
332     }
333 
334     public Map getEvents() {
335         return this.events;
336     }
337 
338     public String getHtmlEvents() {
339         StringBuffer html = new StringBuffer();
340         Iterator en = this.getEvents().keySet().iterator();
341         while (en.hasNext()) {
342             String key = (String) en.next();
343             html.append(" " + key + "=\"" + this.getEvents().get(key) + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
344         }
345         return html.toString();
346     }
347 
348     /**
349      * Returns an empty string.
350      * @see info.magnolia.cms.gui.control.Control#getHtml()
351      */
352     @Override
353     public String getHtml() {
354         return StringUtils.EMPTY;
355     }
356 
357     public void setHtmlPre(String s) {
358         this.htmlPre = s;
359     }
360 
361     public String getHtmlPre() {
362         return this.getHtmlPre(StringUtils.EMPTY);
363     }
364 
365     public String getHtmlPre(String nullValue) {
366         if (this.htmlPre != null) {
367             return this.htmlPre;
368         }
369 
370         return nullValue;
371     }
372 
373     public void setHtmlInter(String s) {
374         this.htmlInter = s;
375     }
376 
377     public String getHtmlInter() {
378         return this.getHtmlInter(StringUtils.EMPTY);
379     }
380 
381     public String getHtmlInter(String nullValue) {
382         if (this.htmlInter != null) {
383             return this.htmlInter;
384         }
385 
386         return nullValue;
387     }
388 
389     public void setHtmlPost(String s) {
390         this.htmlPost = s;
391     }
392 
393     public String getHtmlPost() {
394         return this.getHtmlPost(StringUtils.EMPTY);
395     }
396 
397     public String getHtmlPost(String nullValue) {
398         if (this.htmlPost != null) {
399             return this.htmlPost;
400         }
401 
402         return nullValue;
403     }
404 
405     public void setType(int i) {
406         this.type = i;
407     }
408 
409     public void setType(String s) {
410         this.type = PropertyType.valueFromName(s);
411     }
412 
413     public int getType() {
414         return this.type;
415     }
416 
417     public void setSaveInfo(boolean b) {
418         this.saveInfo = b;
419     }
420 
421     public boolean getSaveInfo() {
422         return this.saveInfo;
423     }
424 
425     public void setCssClass(String s) {
426         this.cssClass = s;
427     }
428 
429     public String getCssClass() {
430         return this.cssClass;
431     }
432 
433     public String getHtmlCssClass() {
434         if (StringUtils.isNotEmpty(this.getCssClass())) {
435             return " class=\"" + this.getCssClass() + "\""; //$NON-NLS-1$ //$NON-NLS-2$
436         }
437 
438         return StringUtils.EMPTY;
439     }
440 
441     public String getHtmlSaveInfo() {
442         StringBuffer html = new StringBuffer();
443         if (this.getSaveInfo()) {
444             html.append("<input type=\"hidden\""); //$NON-NLS-1$
445             html.append(" name=\"mgnlSaveInfo\""); //$NON-NLS-1$
446             html.append(" value=\"" //$NON-NLS-1$
447                     + ControlImpl.escapeHTML(this.getName())
448                     + "," //$NON-NLS-1$
449                     + PropertyType.nameFromValue(this.getType())
450                     + "," //$NON-NLS-1$
451                     + this.getValueType()
452                     + "," //$NON-NLS-1$
453                     + this.getIsRichEditValue()
454                     + "," //$NON-NLS-1$
455                     + this.getEncoding()
456                     + "\""); //$NON-NLS-1$
457             html.append(" />"); //$NON-NLS-1$
458         }
459         return html.toString();
460     }
461 
462     public void setCssStyles(Map h) {
463         this.cssStyles = h;
464     }
465 
466     public void setCssStyles(String key, String value) {
467         this.getCssStyles().put(key, value);
468     }
469 
470     public Map getCssStyles() {
471         return this.cssStyles;
472     }
473 
474     public String getCssStyles(String key, String nullValue) {
475         if (this.getCssStyles().containsKey(key)) {
476             return (String) this.getCssStyles().get(key);
477         }
478         return nullValue;
479     }
480 
481     public String getCssStyles(String key) {
482         return this.getCssStyles(key, StringUtils.EMPTY);
483     }
484 
485     public String getHtmlCssStyles() {
486         StringBuffer html = new StringBuffer();
487         Iterator en = this.getCssStyles().keySet().iterator();
488         while (en.hasNext()) {
489             String key = (String) en.next();
490             html.append(key + ":" + this.getCssStyles().get(key) + ";"); //$NON-NLS-1$ //$NON-NLS-2$
491         }
492         if (html.length() > 0) {
493             return " style=\"" + html + "\""; //$NON-NLS-1$ //$NON-NLS-2$
494         }
495         return StringUtils.EMPTY;
496     }
497 
498     public void setValueType(int i) {
499         this.valueType = i;
500     }
501 
502     public int getValueType() {
503         return this.valueType;
504     }
505 
506     public void setEncoding(int i) {
507         this.encoding = i;
508     }
509 
510     public int getEncoding() {
511         return this.encoding;
512     }
513 
514     public void setIsRichEditValue(int i) {
515         this.isRichEditValue = i;
516     }
517 
518     public int getIsRichEditValue() {
519         return this.isRichEditValue;
520     }
521 
522     public static String escapeHTML(String str) {
523         return str.replace("&", "&amp;").replace("\"", "&quot;").replace("<", "&lt;").replace(">", "&gt;");
524     }
525 
526     protected void println(Writer out, String s) throws IOException {
527         out.write(s);
528         out.write(newLine);
529     }
530 
531 }