View Javadoc

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