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