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