1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.ecm.webui.form.validator;
18
19 import org.exoplatform.ecm.webui.utils.Utils;
20 import org.exoplatform.web.application.ApplicationMessage;
21 import org.exoplatform.webui.exception.MessageException;
22 import org.exoplatform.webui.form.UIFormInput;
23 import org.exoplatform.webui.form.validator.Validator;
24
25
26
27
28
29
30
31
32 public class StandardNameValidator implements Validator {
33
34 public void validate(UIFormInput uiInput) throws Exception {
35 String inputValue = ((String)uiInput.getValue()).trim();
36 if (inputValue == null || inputValue.length() == 0) {
37 throwException("ECMStandardPropertyNameValidator.msg.empty-input", uiInput);
38 }
39 switch (inputValue.length()) {
40 case 1:
41 checkOneChar(inputValue, uiInput);
42 break;
43 case 2:
44 checkTwoChars(inputValue, uiInput);
45 default:
46 checkMoreChars(inputValue, uiInput);
47 break;
48 }
49
50 }
51
52
53
54
55
56
57
58 private boolean checkArr(String s, String[] arrFilterChars) {
59 for (String filter : arrFilterChars) {
60 if (s.equals(filter)) {
61 return true;
62 }
63 }
64 return false;
65 }
66
67
68
69
70
71
72
73 private void checkOneChar(String s, UIFormInput uiInput) throws MessageException {
74 if (checkArr(s, Utils.SPECIALCHARACTER)) {
75 throwException("ECMStandardPropertyNameValidator.msg.Invalid-char", uiInput);
76 }
77 }
78
79
80
81
82
83
84 private void checkTwoChars(String s, UIFormInput uiInput) throws MessageException {
85 String s2 = "";
86 if (s.startsWith(".")) {
87 s2 = s.substring(1, 2);
88 checkOneChar(s2, uiInput);
89 } else if (s.endsWith(".")) {
90 s2 = s.substring(0, 1);
91 checkOneChar(s2, uiInput);
92 } else {
93 String s3 = s.substring(0, 1);
94 String s4 = s.substring(1, 2);
95
96 String[] arrFilterChars = {".", "/", ":", "[", "]", "*", "'", "|", "\""} ;
97 if (checkArr(s3, arrFilterChars)) {
98 throwException("ECMStandardPropertyNameValidator.msg.Invalid-char", uiInput);
99 } else {
100 if (checkArr(s4, arrFilterChars)) {
101 throwException("ECMStandardPropertyNameValidator.msg.Invalid-char", uiInput);
102 }
103 }
104 }
105 }
106
107
108
109
110
111
112 private void checkMoreChars(String s, UIFormInput uiInput) throws MessageException {
113
114 String[] arrFilterChars = {"/", ":", "[", "]", "*", "'", "|", "\""} ;
115
116 String s1 = s.substring(0, 1);
117 String s2 = s.substring(s.length() - 1, s.length());
118 if (checkArr(s1, arrFilterChars)) {
119 throwException("ECMStandardPropertyNameValidator.msg.Invalid-char", uiInput);
120 } else if (checkArr(s2, arrFilterChars)){
121 throwException("ECMStandardPropertyNameValidator.msg.Invalid-char", uiInput);
122 } else {
123 String s3 = s.substring(1, s.length() - 1);
124 for(String filterChar : arrFilterChars) {
125 if(s3.indexOf(filterChar) > -1) {
126 throwException("ECMStandardPropertyNameValidator.msg.Invalid-char", uiInput);
127 }
128 }
129 }
130 }
131
132 private void throwException(String s, UIFormInput uiInput) throws MessageException {
133 Object[] args = { uiInput.getName() };
134 throw new MessageException(new ApplicationMessage(s, args, ApplicationMessage.WARNING));
135 }
136
137 }