2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.util;
24 import org.apache.commons.lang3.StringUtils;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
31 public class MessageFormatter {
32 private final static String paramNameRegexGroupName = "paramName";
33 private final static String paramRegex = "\\$\\{(?<paramName>[^}$]+)\\}"; //start with ${ and after there is one or more characters that are not $ and not } and ended with }
36 public static String format(String messageTemplate, Map<String,Object> params) {
37 if (StringUtils.isEmpty(messageTemplate))
39 if (params == null || params.isEmpty())
40 return messageTemplate;
42 String formattedMessage = messageTemplate;
43 if (formattedMessage.contains("$")) {
44 for (Map.Entry<String, Object> entry : params.entrySet()) {
45 formattedMessage = formattedMessage.replaceAll("\\$\\{" + entry.getKey() + "\\}", String.valueOf(entry.getValue()));
48 StringBuilder builder = new StringBuilder(formattedMessage);
49 builder.append("; output params: [");
50 for (Map.Entry<String, Object> entry : params.entrySet()) {
51 builder.append(entry.getKey()).append(":").append(String.valueOf(entry.getValue())).append("; ");
54 formattedMessage = builder.toString();*/
57 return formattedMessage;
60 public static List<String> getParamsNamesList(String messageTemplate) {
61 List<String> paramsNames = null;
62 if(!StringUtils.isEmpty(messageTemplate)){
63 paramsNames = new ArrayList<String>();
64 Matcher m = Pattern.compile(paramRegex).matcher(messageTemplate);
66 String paramName = m.group(paramNameRegexGroupName);
67 paramsNames.add(paramName);
72 public static Set<String> getParamsNamesSet(String messageTemplate) {
73 List<String> paramsNamesList = getParamsNamesList(messageTemplate);
74 Set<String> paramsNamesSet = null;
75 if(paramsNamesList != null && !paramsNamesList.isEmpty()){
76 paramsNamesSet = new HashSet<String>();
77 for(String paramName : paramsNamesList){
78 paramsNamesSet.add(paramName);
81 return paramsNamesSet;