re base code
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / util / HtmlCleaner.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 org.apache.commons.lang3.StringEscapeUtils;
24
25 import javax.swing.text.html.HTML;
26 import javax.swing.text.html.HTML.Tag;
27 import java.util.HashSet;
28 import java.util.Set;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32 public class HtmlCleaner {
33
34         private static Set<String> htmlTags = new HashSet<>();
35
36         private static String patternHtmlFullTagStr = "</?\\w+((\\s+\\w+(\\s*=\\s*(?:\".*?\"|'.*?'|[\\^'\">\\s]+))?)+\\s*|\\s*)/?>";
37
38         private static String patternHtmlTagOnlyStr = "</?(\\w+)[^>]*/?>";
39
40         private static Pattern onlyTagPattern = Pattern.compile(patternHtmlTagOnlyStr);
41
42         private static Pattern fullTagPattern = Pattern.compile(patternHtmlFullTagStr);
43
44         static {
45                 Tag[] allTags = HTML.getAllTags();
46                 for (Tag tag : allTags) {
47                         htmlTags.add(tag.toString().toLowerCase());
48                 }
49         }
50
51         public static String stripHtml(String input) {
52
53                 return stripHtml(input, false);
54
55         }
56
57         public static String stripHtml(String input, boolean toEscape) {
58
59                 if (input == null || input.isEmpty()) {
60                         return input;
61                 }
62
63                 Matcher matcher = onlyTagPattern.matcher(input);
64
65                 Set<String> tagsToRemove = new HashSet<>();
66
67                 while (matcher.find()) {
68
69                         int start = matcher.start();
70                         int end = matcher.end();
71
72                         String matchTag = input.substring(start, end);
73
74                         int groupCount = matcher.groupCount();
75
76                         if (groupCount > 0) {
77                                 String tag = matcher.group(1);
78                                 if (tag != null && htmlTags.contains(tag.toLowerCase())) {
79                                         if (!tagsToRemove.contains(matchTag)) {
80                                                 tagsToRemove.add(matchTag);
81                                         }
82                                 }
83                         }
84                 }
85
86                 String stripHtmlStr = removeTagsFromString(tagsToRemove, input);
87
88                 if (stripHtmlStr != null) {
89                         if (toEscape) {
90                                 stripHtmlStr = StringEscapeUtils.escapeHtml4(stripHtmlStr);
91                         }
92                 }
93
94                 return stripHtmlStr;
95
96         }
97
98         private static String removeTagsFromString(Set<String> tagsToRemove, String input) {
99
100                 String stripStr = input;
101                 if (input == null || tagsToRemove.isEmpty()) {
102                         return input;
103                 }
104
105                 for (String tag : tagsToRemove) {
106                         stripStr = stripStr.replaceAll(tag, "");
107                 }
108                 return stripStr;
109         }
110
111 }