LayoutHelper.java
4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.http.HttpServletRequest
* org.apache.commons.lang3.StringEscapeUtils
* org.apache.sling.api.SlingHttpServletRequest
*/
package com.day.cq.wcm.foundation.forms;
import com.day.cq.wcm.foundation.forms.ValidationInfo;
import java.io.IOException;
import java.io.Writer;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.sling.api.SlingHttpServletRequest;
public class LayoutHelper {
private LayoutHelper() {
}
public static void printTitle(String fieldId, String title, boolean required, Writer out) throws IOException {
LayoutHelper.printTitle(fieldId, title, required, false, out);
}
public static void printTitle(String fieldId, String title, boolean required, boolean hideLabel, Writer out) throws IOException {
out.write("<div class=\"form_leftcol\"");
if (hideLabel) {
out.write(" style=\"display: none;\"");
}
title = title != null && title.length() > 0 ? StringEscapeUtils.escapeHtml4((String)title) : " ";
out.write(">");
out.write("<div class=\"form_leftcollabel\">");
if (fieldId != null) {
fieldId = StringEscapeUtils.escapeHtml4((String)fieldId);
out.write("<label for=\"" + fieldId + "\">" + title + "</label>");
} else {
out.write("<span>" + title + "</span>");
}
out.write("</div>");
out.write("<div class=\"form_leftcolmark\">");
if (!hideLabel) {
if (required) {
out.write(" *");
} else {
out.write(" ");
}
}
out.write("</div>");
out.write("</div>\n");
}
public static void printDescription(String descr, Writer out) throws IOException {
LayoutHelper.printDescription(null, descr, out);
}
public static void printDescription(String fieldId, String descr, Writer out) throws IOException {
out.write("<div class=\"form_row_description\">");
if (descr != null && descr.length() > 0) {
descr = StringEscapeUtils.escapeHtml4((String)descr);
if (fieldId != null) {
fieldId = StringEscapeUtils.escapeHtml4((String)fieldId);
out.write("<label for=\"" + fieldId + "\">" + descr + "</label>");
} else {
out.write("<span>" + descr + "</span>");
}
}
out.write("</div>\n");
}
public static void printErrors(SlingHttpServletRequest request, String fieldName, Writer out) throws IOException {
LayoutHelper.printErrors(request, fieldName, false, out);
}
public static boolean printErrors(SlingHttpServletRequest request, String fieldName, Writer out, int valueIndex) throws IOException {
return LayoutHelper.printErrors(request, fieldName, false, out, valueIndex);
}
public static void printErrors(SlingHttpServletRequest request, String fieldName, boolean hideLabel, Writer out) throws IOException {
String[] msgs;
ValidationInfo info = ValidationInfo.getValidationInfo((HttpServletRequest)request);
if (info != null && (msgs = info.getErrorMessages(fieldName)) != null) {
for (String msg : msgs) {
out.write("<div class=\"form_row\">");
LayoutHelper.printTitle(null, null, false, hideLabel, out);
out.write("<div class=\"form_rightcol form_error\">");
String[] msgParas = msg.split("\n");
for (int i = 0; i < msgParas.length; ++i) {
out.write(StringEscapeUtils.escapeHtml4((String)msgParas[i]));
if (i + 1 >= msgParas.length) continue;
out.write("<br>");
}
out.write("</div>");
out.write("</div>");
}
}
}
public static boolean printErrors(SlingHttpServletRequest request, String fieldName, boolean hideLabel, Writer out, int valueIndex) throws IOException {
String[] msgs;
ValidationInfo info = ValidationInfo.getValidationInfo((HttpServletRequest)request);
if (info != null && (msgs = info.getErrorMessages(fieldName, valueIndex)) != null) {
for (String msg : msgs) {
out.write("<div class=\"form_row\">");
LayoutHelper.printTitle(null, null, false, hideLabel, out);
out.write("<div class=\"form_rightcol form_error\">");
String[] msgParas = msg.split("\n");
for (int i = 0; i < msgParas.length; ++i) {
out.write(StringEscapeUtils.escapeHtml4((String)msgParas[i]));
if (i + 1 >= msgParas.length) continue;
out.write("<br>");
}
out.write("</div>");
out.write("</div>");
}
return true;
}
return false;
}
}