Change nexus values to properties
[appc.git] / app-c / appc / appc-common / src / main / java / org / openecomp / appc / util / MessageFormatter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.openecomp.appc.util;
23
24 import org.apache.commons.lang3.StringUtils;
25
26 import java.util.*;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
30
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 }
34
35
36     public static  String format(String messageTemplate, Map<String,Object> params) {
37         if (StringUtils.isEmpty(messageTemplate))
38             return "";
39         if (params == null || params.isEmpty())
40             return messageTemplate;
41
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()));
46             }
47 /*        } else {
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("; ");
52             }
53             builder.append("]");
54             formattedMessage = builder.toString();*/
55         }
56
57         return formattedMessage;
58     }
59
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);
65             while (m.find()) {
66                 String paramName = m.group(paramNameRegexGroupName);
67                 paramsNames.add(paramName);
68             }
69         }
70         return paramsNames;
71     }
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);
79             }
80         }
81         return paramsNamesSet;
82     }
83 }