Platform hardening for common bundle
[appc.git] / appc-common / src / main / java / org / onap / appc / util / MessageFormatter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.util;
26
27 import org.apache.commons.lang3.StringUtils;
28 import java.util.*;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32
33 public class MessageFormatter {
34     private final static String paramNameRegexGroupName = "paramName";
35     private final static String paramRegex = "\\$\\{(?<paramName>[^}$]+)\\}"; // start with ${ and
36                                                                               // after there is one
37                                                                               // or more characters
38                                                                               // that are not $ and
39                                                                               // not } and ended
40                                                                               // with }
41
42
43     public static String format(String messageTemplate, Map<String, Object> params) {
44         if (StringUtils.isEmpty(messageTemplate))
45             return "";
46         if (params == null || params.isEmpty())
47             return messageTemplate;
48
49         String formattedMessage = messageTemplate;
50         if (formattedMessage.contains("$")) {
51             for (Map.Entry<String, Object> entry : params.entrySet()) {
52                 formattedMessage = formattedMessage.replaceAll("\\$\\{" + entry.getKey() + "\\}",
53                         escapeDollarChar(String.valueOf(entry.getValue())));
54             }
55         }
56
57         return formattedMessage;
58     }
59
60     private static String escapeDollarChar(String msg) {
61         String formatedMsg = msg;
62         if (formatedMsg.contains("$")) {
63             formatedMsg = formatedMsg.replaceAll("\\$", "\\\\\\$");
64
65         }
66         return formatedMsg;
67     }
68
69     public static List<String> getParamsNamesList(String messageTemplate) {
70         List<String> paramsNames = null;
71         if (!StringUtils.isEmpty(messageTemplate)) {
72             paramsNames = new ArrayList<String>();
73             Matcher m = Pattern.compile(paramRegex).matcher(messageTemplate);
74             while (m.find()) {
75                 String paramName = m.group(paramNameRegexGroupName);
76                 paramsNames.add(paramName);
77             }
78         }
79         return paramsNames;
80     }
81
82     public static Set<String> getParamsNamesSet(String messageTemplate) {
83         List<String> paramsNamesList = getParamsNamesList(messageTemplate);
84         Set<String> paramsNamesSet = null;
85         if (paramsNamesList != null && !paramsNamesList.isEmpty()) {
86             paramsNamesSet = new HashSet<String>();
87             for (String paramName : paramsNamesList) {
88                 paramsNamesSet.add(paramName);
89             }
90         }
91         return paramsNamesSet;
92     }
93 }