reorder the modifier
[appc.git] / appc-core / appc-common-bundle / src / main / java / org / onap / appc / util / MessageFormatter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.util;
25
26 import org.apache.commons.lang3.StringUtils;
27 import java.util.*;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30
31
32 public class MessageFormatter {
33     private static final String paramNameRegexGroupName = "paramName";
34
35     /**
36      * start with ${ and after there is one or more characters that are not $ and not } and ended with }
37      */
38     private static final String paramRegex = "\\$\\{(?<paramName>[^}$]+)\\}";
39
40     public static String format(String messageTemplate, Map<String, Object> params) {
41         if (StringUtils.isEmpty(messageTemplate))
42             return "";
43         if (params == null || params.isEmpty())
44             return messageTemplate;
45
46         String formattedMessage = messageTemplate;
47         if (formattedMessage.contains("$")) {
48             for (Map.Entry<String, Object> entry : params.entrySet()) {
49                 formattedMessage = formattedMessage.replaceAll("\\$\\{" + entry.getKey() + "\\}",
50                         escapeDollarChar(String.valueOf(entry.getValue())));
51             }
52         }
53
54         return formattedMessage;
55     }
56
57     private static String escapeDollarChar(String msg) {
58         String formatedMsg = msg;
59         if (formatedMsg.contains("$")) {
60             formatedMsg = formatedMsg.replaceAll("\\$", "\\\\\\$");
61
62         }
63         return formatedMsg;
64     }
65
66     public static List<String> getParamsNamesList(String messageTemplate) {
67         List<String> paramsNames = null;
68         if (!StringUtils.isEmpty(messageTemplate)) {
69             paramsNames = new ArrayList<>();
70             Matcher m = Pattern.compile(paramRegex).matcher(messageTemplate);
71             while (m.find()) {
72                 String paramName = m.group(paramNameRegexGroupName);
73                 paramsNames.add(paramName);
74             }
75         }
76         return paramsNames;
77     }
78
79     public static Set<String> getParamsNamesSet(String messageTemplate) {
80         List<String> paramsNamesList = getParamsNamesList(messageTemplate);
81         Set<String> paramsNamesSet = null;
82         if (paramsNamesList != null && !paramsNamesList.isEmpty()) {
83             paramsNamesSet = new HashSet<String>();
84             for (String paramName : paramsNamesList) {
85                 paramsNamesSet.add(paramName);
86             }
87         }
88         return paramsNamesSet;
89     }
90 }