1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.aaf.cadi.util;
\r
25 import java.util.List;
\r
29 * Simplified Conversion based on typical use of getting AT&T style RESTful Error Messages
\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
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
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
52 if(text.contains("%s")) {
\r
53 sb = new StringBuilder();
\r
56 StringBuilder[] sbs = new StringBuilder[] {sb,holder};
\r
57 boolean replace, clearIndex = false;
\r
59 while((idx=text.indexOf('%',prev))>=0) {
\r
65 sb.append(text,prev,idx);
\r
68 holder.append(text,prev,idx);
\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
79 clearIndex=replace=true;
\r
96 if(index<=vars.length) {
\r
98 holder.append(vars[index-1]);
\r
102 for(StringBuilder s : sbs) {
\r
112 sb.append(text,prev,text.length());
\r
115 holder.append(text,prev,text.length());
\r
118 return sb==null?text:sb.toString();
\r