2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.util;
27 import org.apache.commons.lang3.StringUtils;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
33 public class MessageFormatter {
34 private final static String paramNameRegexGroupName = "paramName";
37 * start with ${ and after there is one or more characters that are not $ and not } and ended with }
39 private final static String paramRegex = "\\$\\{(?<paramName>[^}$]+)\\}";
41 public static String format(String messageTemplate, Map<String, Object> params) {
42 if (StringUtils.isEmpty(messageTemplate))
44 if (params == null || params.isEmpty())
45 return messageTemplate;
47 String formattedMessage = messageTemplate;
48 if (formattedMessage.contains("$")) {
49 for (Map.Entry<String, Object> entry : params.entrySet()) {
50 formattedMessage = formattedMessage.replaceAll("\\$\\{" + entry.getKey() + "\\}",
51 escapeDollarChar(String.valueOf(entry.getValue())));
55 return formattedMessage;
58 private static String escapeDollarChar(String msg) {
59 String formatedMsg = msg;
60 if (formatedMsg.contains("$")) {
61 formatedMsg = formatedMsg.replaceAll("\\$", "\\\\\\$");
67 public static List<String> getParamsNamesList(String messageTemplate) {
68 List<String> paramsNames = null;
69 if (!StringUtils.isEmpty(messageTemplate)) {
70 paramsNames = new ArrayList<>();
71 Matcher m = Pattern.compile(paramRegex).matcher(messageTemplate);
73 String paramName = m.group(paramNameRegexGroupName);
74 paramsNames.add(paramName);
80 public static Set<String> getParamsNamesSet(String messageTemplate) {
81 List<String> paramsNamesList = getParamsNamesList(messageTemplate);
82 Set<String> paramsNamesSet = null;
83 if (paramsNamesList != null && !paramsNamesList.isEmpty()) {
84 paramsNamesSet = new HashSet<String>();
85 for (String paramName : paramsNamesList) {
86 paramsNamesSet.add(paramName);
89 return paramsNamesSet;