2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.cadi.util;
24 import java.util.List;
28 * Simplified Conversion based on typical use of getting AT&T style RESTful Error Messages
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));
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)
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;
51 if (text.contains("%s")) {
52 sb = new StringBuilder();
55 StringBuilder[] sbs = new StringBuilder[] {sb,holder};
56 boolean replace, clearIndex = false;
58 while ((idx=text.indexOf('%',prev))>=0) {
64 sb.append(text,prev,idx);
67 holder.append(text,prev,idx);
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':
78 clearIndex=replace=true;
95 if (index<=vars.length) {
97 holder.append(vars[index-1]);
101 for (StringBuilder s : sbs) {
111 sb.append(text,prev,text.length());
114 holder.append(text,prev,text.length());
117 return sb==null?text:sb.toString();