[AAF-21] Initial code import
[aaf/cadi.git] / core / src / main / java / com / att / cadi / util / Vars.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.cadi.util;\r
25 \r
26 import java.util.List;\r
27 \r
28 public class Vars {\r
29         /**\r
30          * Simplified Conversion based on typical use of getting AT&T style RESTful Error Messages\r
31          * @param text\r
32          * @param vars\r
33          * @return\r
34          */\r
35         public static String convert(final String text, final List<String> vars) {\r
36                 String[] array = new String[vars.size()];\r
37                 StringBuilder sb = new StringBuilder();\r
38                 convert(sb,text,vars.toArray(array));\r
39                 return sb.toString();\r
40         }\r
41         /**\r
42          * Convert a format string with "%s" into AT&T RESTful Error %1 %2 (number) format\r
43          * If "holder" is passed in, it is built with full Message extracted (typically for Logging)\r
44          * @param holder\r
45          * @param text\r
46          * @param vars\r
47          * @return\r
48          */\r
49         public static String convert(final StringBuilder holder, final String text, final String ... vars) {\r
50                 StringBuilder sb = null;\r
51                 int idx,index=0,prev = 0;\r
52                 \r
53                 if(text.contains("%s")) {\r
54                         sb = new StringBuilder();\r
55                 }\r
56                 \r
57                 StringBuilder[] sbs = new StringBuilder[] {sb,holder};\r
58                 boolean replace, clearIndex = false;\r
59                 int c;\r
60                 while((idx=text.indexOf('%',prev))>=0) {\r
61                         replace = false;\r
62                         if(clearIndex) {\r
63                                 index=0;\r
64                         }\r
65                         if(sb!=null) {\r
66                                 sb.append(text,prev,idx);\r
67                         }\r
68                         if(holder!=null) {\r
69                                 holder.append(text,prev,idx);\r
70                         }\r
71                         \r
72                         boolean go = true;\r
73                         while(go) {\r
74                                 if(text.length()>++idx) {\r
75                                         switch(c=text.charAt(idx)) {\r
76                                                 case '0': case '1': case '2': case '3': case '4': \r
77                                                 case '5': case '6': case '7': case '8': case '9':\r
78                                                         index *=10;\r
79                                                         index +=(c-'0');\r
80                                                         clearIndex=replace=true;\r
81                                                         continue;\r
82                                                 case 's':\r
83                                                         ++index;\r
84                                                         replace = true;\r
85                                                         continue;\r
86                                                 default:\r
87                                                         break;\r
88                                         }\r
89                                 }\r
90                                 prev = idx;\r
91                                 go=false;\r
92                                 if(replace) {\r
93                                         if(sb!=null) {\r
94                                                 sb.append('%');\r
95                                                 sb.append(index);\r
96                                         }\r
97                                         if(index<=vars.length) {\r
98                                                 if(holder!=null) {\r
99                                                         holder.append(vars[index-1]);\r
100                                                 }\r
101                                         }\r
102                                 } else {\r
103                                         for(StringBuilder s : sbs) {\r
104                                                 if(s!=null) {\r
105                                                         s.append("%");\r
106                                                 }\r
107                                         }\r
108                                 }\r
109                         }\r
110                 }\r
111                 \r
112                 if(sb!=null) {\r
113                         sb.append(text,prev,text.length());\r
114                 }\r
115                 if(holder!=null) {\r
116                         holder.append(text,prev,text.length());\r
117                 }\r
118 \r
119                 return sb==null?text:sb.toString();\r
120         }\r
121 \r
122 }\r