dfecf927d8e33c447f03c1b41c431c5fc3a8c821
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / util / ValidationUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.common.util;
22
23 import com.google.common.base.CharMatcher;
24 import org.apache.commons.collections.CollectionUtils;
25 import org.apache.commons.text.StringEscapeUtils;
26 import org.apache.commons.lang3.StringUtils;
27 import org.apache.commons.lang3.math.NumberUtils;
28 import org.apache.commons.text.WordUtils;
29 import org.apache.commons.validator.routines.UrlValidator;
30 import org.jsoup.Jsoup;
31 import org.jsoup.safety.Whitelist;
32
33 import java.util.*;
34 import java.util.regex.Pattern;
35
36 public class ValidationUtils {
37
38         private ValidationUtils() {
39         }
40
41         public static final Integer COMPONENT_NAME_MAX_LENGTH = 1024;
42         public static final Pattern COMPONENT_NAME_PATTERN = Pattern
43                         .compile("^[\\w][\\w \\.\\-\\_\\:\\+]{0," + (COMPONENT_NAME_MAX_LENGTH-1) + "}$");
44         public static final Integer ADDITIONAL_INFORMATION_KEY_MAX_LENGTH = 50;
45         public static final Pattern ADDITIONAL_INFORMATION_KEY_PATTERN = Pattern
46                         .compile("^[\\w\\s\\.\\-\\_]{1," + COMPONENT_NAME_MAX_LENGTH + "}$");
47         public static final Integer RSI_NAME_MAX_LENGTH = 1024;
48         public static final Pattern RSI_NAME_PATTERN = Pattern
49                         .compile("^[\\w \\s\\.\\-\\_\\:\\+]{1," + RSI_NAME_MAX_LENGTH + "}$");
50         public static final Integer COMMENT_MAX_LENGTH = 256;
51
52         public static final Integer ICON_MAX_LENGTH = 25;
53         public static final Pattern ICON_PATTERN = Pattern.compile("^[\\w\\-]{1," + ICON_MAX_LENGTH + "}$");
54         public static final Integer PROJECT_CODE_MAX_LEGTH = 50;
55         public static final Pattern PROJECT_CODE_PATTERN = Pattern.compile("^[\\s\\w_.-]{5,50}$");
56
57         // USER_ID format : aannnX (where a=a-z or A-Z, n=0-9, and X=a-z,A-Z, or 0-9)
58         public static final Integer CONNTACT_ID_MAX_LENGTH = 50;
59
60         public static final Pattern CONTACT_ID_PATTERN = Pattern.compile("^[\\s\\w_.-]{1,50}$");
61         public static final Pattern OCTET_PATTERN = Pattern.compile("%[a-fA-F0-9]{2}");
62         public static final Pattern NONE_UTF8_PATTERN = Pattern.compile("[^\\x00-\\x7F]+");
63         public static final Pattern URL_INVALIDE_PATTERN = Pattern.compile("[,#?&@$<>~^`\\\\\\[\\]{}|\")(*!+=;%]+");// ,#?&@$<>~^`\\[]{}|")(*!
64
65         public static final Pattern ENGLISH_PATTERN = Pattern.compile("^[\\p{Graph}\\x20]+$");
66         public static final Pattern COMMENT_PATTERN = Pattern.compile("^[\\u0000-\\u00BF]{1,1024}$");
67         public static final Pattern SERVICE_METADATA_PATTERN = Pattern.compile("^[\\x20-\\x21\\x23-\\x29\\x2B-\\x2E\\x30-\\x39\\x3B\\x3D\\x40-\\x5B\\x5D-\\x7B\\x7D-\\xFF]{1,256}");
68         public static final Integer COMPONENT_DESCRIPTION_MAX_LENGTH = 1024;
69         public static final Integer SERVICE_TYPE_MAX_LENGTH = 256;
70         public static final Integer SERVICE_ROLE_MAX_LENGTH = 256;
71         public static final Integer SERVICE_FUNCTION_MAX_LENGTH = 256;
72         public static final Integer SERVICE_NAMING_POLICY_MAX_SIZE = 100;
73
74         public static final Integer TAG_MAX_LENGTH = 1024;
75         public static final Integer TAG_LIST_MAX_LENGTH = 1024;
76         public static final Integer VENDOR_NAME_MAX_LENGTH = 60;
77         public static final Pattern VENDOR_NAME_PATTERN = Pattern
78                         .compile("^[\\x20-\\x21\\x23-\\x29\\x2B-\\x2E\\x30-\\x39\\x3B\\x3D\\x40-\\x5B\\x5D-\\x7B\\x7D-\\xFF]+$");
79         public static final Integer VENDOR_RELEASE_MAX_LENGTH = 25;
80         public static final Pattern VENDOR_RELEASE_PATTERN = Pattern
81                         .compile("^[\\x20-\\x21\\x23-\\x29\\x2B-\\x2E\\x30-\\x39\\x3B\\x3D\\x40-\\x5B\\x5D-\\x7B\\x7D-\\xFF]+$");
82         public static final Integer RESOURCE_VENDOR_MODEL_NUMBER_MAX_LENGTH = 65;
83
84         public static final Pattern CLEAN_FILENAME_PATTERN = Pattern.compile("[\\x00-\\x1f\\x80-\\x9f\\x5c/<?>\\*:|\"/]+");
85
86         public static final Pattern DASH_PATTERN = Pattern.compile("[-]+");
87         public static final Pattern UNDERSCORE_PATTERN = Pattern.compile("[_]+");
88         public static final Pattern PLUS_PATTERN = Pattern.compile("[+]+");
89         public static final Pattern SPACE_PATTERN = Pattern.compile("[ ]+");
90         public static final Pattern AMP_PATTERN = Pattern.compile("[&]+");
91         public static final Pattern DOT_PATTERN = Pattern.compile("[\\.]+");
92         public static final Pattern APOST_PATTERN = Pattern.compile("[']+");
93         public static final Pattern HASHTAG_PATTERN = Pattern.compile("[#]+");
94         public static final Pattern EQUAL_PATTERN = Pattern.compile("[=]+");
95         public static final Pattern COLON_PATTERN = Pattern.compile("[:]+");
96         public static final Pattern AT_PATTERN = Pattern.compile("[@]+");
97         public static final Pattern AND_PATTERN = Pattern.compile(" [aA][Nn][Dd] ");
98         private static final Set<String> CATEGORY_CONJUNCTIONS = new HashSet<>(
99                         Arrays.asList("of", "to", "for", "as", "a", "an", "the"));
100
101         public static final Pattern COST_PATTERN = Pattern.compile("^[0-9]{1,5}\\.[0-9]{1,3}$");
102         public static final Pattern ARTIFACT_LABEL_PATTERN = Pattern.compile("^[a-zA-Z0-9 \\-@+]+$");
103         public static final Integer ARTIFACT_LABEL_LENGTH = 255;
104         public static final Pattern ARTIFACT_DISPLAY_NAME_PATTERN = Pattern.compile("^[a-zA-Z0-9][a-zA-Z0-9 &\\.'#=:@_\\-+]+$");
105         public static final Pattern CATEGORY_LABEL_PATTERN = Pattern.compile("^[a-zA-Z0-9][a-zA-Z0-9 &\\.'#=:@_\\-+]+$");
106         public static final Integer CATEGORY_LABEL_MIN_LENGTH = 3;
107         public static final Integer CATEGORY_LABEL_MAX_LENGTH = 25;
108
109         public static final Pattern COMPONENT_NAME_DELIMETER_PATTERN = Pattern.compile("[\\.\\-\\_]+");
110         public static final Pattern COMPONENT_INCTANCE_NAME_DELIMETER_PATTERN = Pattern.compile("[\\.\\-]+");
111         public static final Pattern PRODUCT_NAME_DELIMETER_PATTERN = Pattern.compile("[\\.\\-\\_&=#@':\\[\\]\\+]+");
112         public static final Integer CONSUMER_NAME_MAX_LENGTH = 255;
113
114         public static final Pattern CONSUMER_NAME_PATTERN = Pattern.compile("^[\\w]+[\\w\\.\\-]*$");
115         public static final Integer CONSUMER_SALT_LENGTH = 32;
116         public static final Integer CONSUMER_PASSWORD_LENGTH = 64;
117         public static final Pattern CONSUMER_PASS_SALT_PATTERN = Pattern.compile("^[a-z0-9]+$");
118         public static final Pattern FLOAT_PATTERN = Pattern.compile("^[\\d]+[\\.]{1}[\\d]+$");
119         public static final Pattern CERTIFIED_VERSION_PATTERN = Pattern.compile("^[1-9][0-9]*\\.0$");
120         public static final Pattern MINOR_VERSION_PATTERN = Pattern.compile("^0\\.[1-9][0-9]*$");
121         public static final Pattern TAGS_PATTERN = Pattern.compile("<[^><]*>");
122         public static final Pattern TAG_PATTERN = Pattern.compile("^[\\s\\w_.-]{1,1024}$");
123
124         public static final Integer ARTIFACT_NAME_LENGTH = 255;
125         public static final Integer API_URL_LENGTH = 100;
126         public static final Integer ARTIFACT_DESCRIPTION_MAX_LENGTH = 256;
127
128         public static final Integer PRODUCT_FULL_NAME_MIN_LENGTH = 4;
129         public static final Integer PRODUCT_FULL_NAME_MAX_LENGTH = 100;
130         public static final Integer FORWARDING_PATH_NAME_MAX_LENGTH = 100;
131         public static final Pattern FORWARDING_PATH_NAME_PATTERN = Pattern.compile("^[\\w][\\w \\.\\-\\_\\:\\+]{0," + (FORWARDING_PATH_NAME_MAX_LENGTH-1) + "}$");
132
133         public static final Integer POLICY_MAX_LENGTH = 1024;
134         public static final Pattern POLICY_NAME_PATTERN = Pattern
135                         .compile("^[\\w][\\w \\.\\-\\_\\:\\+]{0," + (POLICY_MAX_LENGTH-1) + "}$");
136
137         public static boolean validateArtifactLabel(String label) {
138                 return ARTIFACT_LABEL_PATTERN.matcher(label).matches();
139         }
140
141         public static boolean validateArtifactDisplayName(String displayName) {
142                 return ARTIFACT_DISPLAY_NAME_PATTERN.matcher(displayName).matches();
143         }
144
145         public static String cleanUpText(String text){
146                 text = removeNoneUtf8Chars(text);
147                 text = normaliseWhitespace(text);
148                 text = stripOctets(text);
149                 text = removeHtmlTagsOnly(text);
150                 return text;
151         }
152
153         public static boolean validateTagPattern(String tag) {
154                 return TAG_PATTERN.matcher(tag).matches();
155         }
156
157         public static boolean validateServiceMetadata(String metadataField) {
158                 return SERVICE_METADATA_PATTERN.matcher(metadataField).matches();
159         }
160
161         public static boolean validateCommentPattern(String comment) {
162                 return COMMENT_PATTERN.matcher(comment).matches();
163         }
164
165         public static boolean validateCategoryDisplayNameFormat(String label) {
166                 boolean res = true;
167                 if (label != null) {
168                         label = label.trim();
169                         res = CATEGORY_LABEL_PATTERN.matcher(label).matches();
170                 }
171                 return res;
172         }
173
174         public static String normalizeCategoryName4Display(String str) {
175                 if (str != null) {
176                         str = str.trim();
177                         str = DASH_PATTERN.matcher(str).replaceAll("-");
178                         str = UNDERSCORE_PATTERN.matcher(str).replaceAll("_");
179                         str = AMP_PATTERN.matcher(str).replaceAll("&");
180                         str = PLUS_PATTERN.matcher(str).replaceAll("+");
181                         str = DOT_PATTERN.matcher(str).replaceAll(".");
182                         str = APOST_PATTERN.matcher(str).replaceAll("'");
183                         str = HASHTAG_PATTERN.matcher(str).replaceAll("#");
184                         str = EQUAL_PATTERN.matcher(str).replaceAll("=");
185                         str = COLON_PATTERN.matcher(str).replaceAll(":");
186                         str = AT_PATTERN.matcher(str).replaceAll("@");
187                         str = normaliseWhitespace(str);
188                         str = AND_PATTERN.matcher(str).replaceAll(" & ");
189
190                         // Case normalizing
191                         StringBuilder sb = new StringBuilder();
192                         String[] split = str.split(" ");
193                         for (int i = 0; i < split.length; i++) {
194                                 String splitted = split[i];
195                                 String lowerCase = splitted.toLowerCase();
196                                 // BANK OF AMERICA --> BANK of AMERICA ("of" is lowercased), but
197                                 // OF BANK OF AMERICA --> OF BANK of AMERICA (first "OF" is not
198                                 // lowercased because it's first word)
199                                 // Agreed with Ella, 26/11/15
200                                 if ((i > 0) && CATEGORY_CONJUNCTIONS.contains(lowerCase)) {
201                                         sb.append(lowerCase);
202                                 } else {
203                                         sb.append(WordUtils.capitalize(splitted));
204                                 }
205                                 sb.append(" ");
206                         }
207                         str = sb.toString().trim();
208                 }
209                 return str;
210         }
211
212         public static String normalizeCategoryName4Uniqueness(String str) {
213                 return str.toLowerCase();
214         }
215
216         public static boolean validateCategoryDisplayNameLength(String label) {
217                 return (label != null && label.length() >= CATEGORY_LABEL_MIN_LENGTH
218                                 && label.length() <= CATEGORY_LABEL_MAX_LENGTH);
219         }
220
221         public static boolean validateProductFullNameLength(String fullName) {
222                 return (fullName != null && fullName.length() >= PRODUCT_FULL_NAME_MIN_LENGTH
223                                 && fullName.length() <= PRODUCT_FULL_NAME_MAX_LENGTH);
224         }
225
226         public static boolean validateArtifactLabelLength(String label) {
227                 return label.length() > 0 && label.length() <= ARTIFACT_LABEL_LENGTH;
228         }
229
230         public static boolean validateResourceInstanceNameLength(String resourceInstanceName) {
231                 return resourceInstanceName.length() <= RSI_NAME_MAX_LENGTH;
232         }
233
234         public static boolean validateResourceInstanceName(String resourceInstanceName) {
235                 return RSI_NAME_PATTERN.matcher(resourceInstanceName).matches();
236         }
237
238         public static boolean validateUrlLength(String url) {
239                 return url.length() <= API_URL_LENGTH;
240         }
241
242         public static boolean validateArtifactNameLength(String artifactName) {
243                 return (artifactName.length() <= ARTIFACT_NAME_LENGTH && artifactName.length() > 0);
244         }
245
246         public static boolean validateComponentNamePattern(String componentName) {
247                 return COMPONENT_NAME_PATTERN.matcher(componentName).matches();
248         }
249
250         public static boolean validateComponentNameLength(String componentName) {
251                 return componentName.length() <= COMPONENT_NAME_MAX_LENGTH;
252         }
253
254         public static boolean validateIcon(String icon) {
255                 return ICON_PATTERN.matcher(icon).matches();
256         }
257
258         public static boolean validateIconLength(String icon) {
259                 return icon.length() <= ICON_MAX_LENGTH;
260         }
261
262         public static boolean validateProjectCode(String projectCode) {
263                 return PROJECT_CODE_PATTERN.matcher(projectCode).matches();
264         }
265
266         public static boolean validateProjectCodeLegth(String projectCode) {
267                 return projectCode.length() <= PROJECT_CODE_MAX_LEGTH;
268         }
269
270         public static boolean validateContactId(String contactId) {
271                 return CONTACT_ID_PATTERN.matcher(contactId).matches();
272         }
273
274         public static boolean validateCost(String cost) {
275                 return COST_PATTERN.matcher(cost).matches();
276         }
277
278         public static String removeHtmlTags(String str) {
279                 return Jsoup.clean(str, Whitelist.none());
280         }
281
282         public static String removeAllTags(String htmlText) {
283
284                 return TAGS_PATTERN.matcher(htmlText).replaceAll("").trim();
285         }
286
287         public static String normaliseWhitespace(String str) {
288                 StringBuilder sb = new StringBuilder(str.length());
289                 appendNormalisedWhitespace(sb, str, false);
290                 return sb.toString();
291         }
292
293         private static void appendNormalisedWhitespace(StringBuilder accum, String string, boolean stripLeading) {
294                 boolean lastWasWhite = false;
295                 boolean reachedNonWhite = false;
296
297                 int len = string.length();
298                 int c;
299                 for (int i = 0; i < len; i+= Character.charCount(c)) {
300                         c = string.codePointAt(i);
301                         if (isWhitespace(c)) {
302                                 if ((stripLeading && !reachedNonWhite) || lastWasWhite)
303                                         continue;
304                                 accum.append(' ');
305                                 lastWasWhite = true;
306                         }
307                         else {
308                                 accum.appendCodePoint(c);
309                                 lastWasWhite = false;
310                                 reachedNonWhite = true;
311                         }
312                 }
313         }
314
315         private static boolean isWhitespace(int c){
316                 return c == ' ';
317         }
318
319         public static String stripOctets(String str) {
320                 return OCTET_PATTERN.matcher(str).replaceAll("");
321         }
322
323         public static String removeNoneUtf8Chars(String input) {
324                 return NONE_UTF8_PATTERN.matcher(input).replaceAll("");
325         }
326
327         public static boolean validateIsEnglish(String input) {
328                 return ENGLISH_PATTERN.matcher(input).matches();
329         }
330
331         public static boolean validateIsAscii(String input) {
332
333                 return CharMatcher.ASCII.matchesAllOf(input);
334         }
335
336         public static String convertHtmlTagsToEntities(String input) {
337                 return StringEscapeUtils.escapeHtml4(input);
338         }
339
340         public static List<String> removeDuplicateFromList(List<String> list) {
341                 Set<String> hs = new LinkedHashSet<>(list);
342                 list.clear();
343                 list.addAll(hs);
344                 return list;
345
346         }
347
348         public static boolean validateTagLength(String tag) {
349                 if (tag != null) {
350                         return tag.length() <= TAG_MAX_LENGTH;
351                 }
352                 return false;
353         }
354
355         public static boolean validateTagListLength(int tagListLength) {
356                 return tagListLength <= TAG_LIST_MAX_LENGTH;
357         }
358
359         public static boolean validateDescriptionLength(String description) {
360                 return description.length() <= COMPONENT_DESCRIPTION_MAX_LENGTH;
361         }
362
363         public static boolean validateStringNotEmpty(String value) {
364                 if ((value == null) || (value.isEmpty())) {
365                         return false;
366                 }
367                 return true;
368         }
369
370         public static boolean validateListNotEmpty(List<?> list) {
371                 if ((list == null) || (list.isEmpty())) {
372                         return false;
373                 }
374                 return true;
375         }
376
377         public static boolean validateVendorName(String vendorName) {
378                 return VENDOR_NAME_PATTERN.matcher(vendorName).matches();
379         }
380
381         public static boolean validateVendorNameLength(String vendorName) {
382                 return vendorName.length() <= VENDOR_NAME_MAX_LENGTH;
383         }
384
385         public static boolean validateResourceVendorModelNumberLength(String resourceVendorModelNumber) {
386                 return resourceVendorModelNumber.length() <= RESOURCE_VENDOR_MODEL_NUMBER_MAX_LENGTH;
387         }
388
389         public static boolean validateVendorRelease(String vendorRelease) {
390                 return VENDOR_RELEASE_PATTERN.matcher(vendorRelease).matches();
391         }
392
393         public static boolean validateVendorReleaseLength(String vendorRelease) {
394                 return vendorRelease.length() <= VENDOR_RELEASE_MAX_LENGTH;
395         }
396
397         public static boolean validateServiceTypeLength(String serviceType) {
398                 return serviceType.length() <= SERVICE_TYPE_MAX_LENGTH;
399         }
400
401         public static boolean validateServiceRoleLength(String serviceRole) {
402                 return serviceRole.length() <= SERVICE_ROLE_MAX_LENGTH;
403         }
404
405         public static boolean validateServiceFunctionLength(String serviceFunction) {
406                 return serviceFunction.length() <= SERVICE_FUNCTION_MAX_LENGTH;
407         }
408
409         public static boolean validateServiceNamingPolicyLength(String namingPolicy) {
410                 return namingPolicy.length() <= SERVICE_NAMING_POLICY_MAX_SIZE;
411         }
412         public static boolean hasBeenCertified(String version) {
413                 return NumberUtils.toDouble(version) >= 1;
414         }
415
416     public static String normaliseComponentName(String name) {
417         String[] split = splitComponentName(name);
418         StringBuilder sb = new StringBuilder();
419         for (String splitElement : split) {
420             sb.append(splitElement);
421         }
422         return sb.toString();
423
424         }
425
426     public static String normalizeComponentInstanceName(String name) {
427         String[] split = splitComponentInstanceName(name);
428         StringBuilder sb = new StringBuilder();
429         for (String splitElement : split) {
430             sb.append(splitElement);
431         }
432         return sb.toString();
433
434         }
435
436         private static String[] splitComponentName(String name) {
437                 String normalizedName = name.toLowerCase();
438                 normalizedName = COMPONENT_NAME_DELIMETER_PATTERN.matcher(normalizedName).replaceAll(" ");
439                 return normalizedName.split(" ");
440         }
441
442         private static String[] splitComponentInstanceName(String name) {
443                 String normalizedName = name.toLowerCase();
444                 normalizedName = COMPONENT_INCTANCE_NAME_DELIMETER_PATTERN.matcher(normalizedName).replaceAll(" ");
445                 return normalizedName.split(" ");
446         }
447
448     public static String convertToSystemName(String name) {
449         String[] split = splitComponentName(name);
450         StringBuilder sb = new StringBuilder();
451         for (String splitElement : split) {
452             String capitalize = WordUtils.capitalize(splitElement);
453             sb.append(capitalize);
454         }
455         return sb.toString();
456     }
457
458         public static String normalizeFileName(String filename) {
459                 return cleanFileName(filename);
460
461         }
462
463         private static String cleanFileName(String str) {
464                 str = CLEAN_FILENAME_PATTERN.matcher(str).replaceAll("");
465                 str = normaliseWhitespace(str);
466                 str = SPACE_PATTERN.matcher(str).replaceAll("-");
467                 str = DASH_PATTERN.matcher(str).replaceAll("-");
468                 str = StringUtils.strip(str, "-_ .");
469
470                 return str;
471         }
472
473         public static boolean validateUrl(String url) {
474
475                 UrlValidator urlValidator = new UrlValidator();
476                 if (!urlValidator.isValid(url)) {
477                         return false;
478                 }
479                 if (NONE_UTF8_PATTERN.matcher(url).find()) {
480                         return false;
481                 }
482
483                 if (URL_INVALIDE_PATTERN.matcher(url).find()) {
484                         return false;
485                 }
486                 return true;
487
488         }
489
490         public static String cleanArtifactDisplayName(String strIn) {
491                 String str = DASH_PATTERN.matcher(strIn).replaceAll("-");
492                 str = UNDERSCORE_PATTERN.matcher(str).replaceAll("_");
493                 str = PLUS_PATTERN.matcher(str).replaceAll("+");
494                 str = normaliseWhitespace(str);
495                 str = str.trim();
496
497                 return str;
498         }
499
500         public static String normalizeArtifactLabel(String strIn) {
501
502                 String str = DASH_PATTERN.matcher(strIn).replaceAll("");
503                 str = UNDERSCORE_PATTERN.matcher(str).replaceAll("");
504                 str = PLUS_PATTERN.matcher(str).replaceAll("");
505                 str = SPACE_PATTERN.matcher(str).replaceAll("");
506                 str = DOT_PATTERN.matcher(str).replaceAll("");
507                 str = str.toLowerCase();
508
509                 return str;
510         }
511
512         public static boolean validateAdditionalInformationKeyName(String str) {
513                 return ADDITIONAL_INFORMATION_KEY_PATTERN.matcher(str).matches();
514         }
515
516         public static String normalizeAdditionalInformation(String str) {
517                 if (str != null) {
518                         str = DASH_PATTERN.matcher(str).replaceAll("-");
519                         str = UNDERSCORE_PATTERN.matcher(str).replaceAll("_");
520                         str = normaliseWhitespace(str);
521                 }
522                 return str;
523         }
524
525         public static boolean validateLength(String str, int length) {
526                 if (str == null) {
527                         return true;
528                 }
529                 return str.length() <= length;
530         }
531
532         public static boolean validateConsumerName(String consumerName) {
533                 return CONSUMER_NAME_PATTERN.matcher(consumerName).matches();
534         }
535
536         public static boolean isUTF8Str(String str) {
537                 if (NONE_UTF8_PATTERN.matcher(str).find()) {
538                         return false;
539                 }
540                 return true;
541         }
542
543         public static boolean validateConsumerPassSalt(String consumerSalt) {
544                 return CONSUMER_PASS_SALT_PATTERN.matcher(consumerSalt).matches();
545         }
546
547         public static boolean isFloatNumber(String number) {
548                 return FLOAT_PATTERN.matcher(number).matches();
549         }
550
551         public static boolean validateCertifiedVersion(String version) {
552                 return (version != null && CERTIFIED_VERSION_PATTERN.matcher(version).matches());
553         }
554
555         public static boolean validateMinorVersion(String version) {
556                 return (version != null && MINOR_VERSION_PATTERN.matcher(version).matches());
557         }
558
559         public static boolean validateCategoryIconNotEmpty(List<String> categoryIcons) {
560                 return CollectionUtils.isEmpty(categoryIcons);
561         }
562
563         public static String normaliseProductName(String name) {
564                 String[] split = splitComponentName(PRODUCT_NAME_DELIMETER_PATTERN, name);
565                 StringBuilder sb = new StringBuilder();
566                 for (String splitElement : split) {
567                         sb.append(splitElement);
568                 }
569                 return sb.toString();
570
571         }
572
573         private static String[] splitComponentName(Pattern pattern, String name) {
574                 String normalizedName = name.toLowerCase();
575                 normalizedName = pattern.matcher(normalizedName).replaceAll(" ");
576                 return normalizedName.split(" ");
577         }
578
579         public static String removeHtmlTagsOnly(String htmlText) {
580                 return HtmlCleaner.stripHtml(htmlText, false);
581         }
582
583         public static boolean validateForwardingPathNamePattern(String forwardingPathName) {
584                 return FORWARDING_PATH_NAME_PATTERN.matcher(forwardingPathName).matches();
585         }
586 }