|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.common.util; |
| 21 | + |
| 22 | +import java.util.Map; |
| 23 | +import java.util.regex.Matcher; |
| 24 | +import java.util.regex.Pattern; |
| 25 | + |
| 26 | +/** |
| 27 | + * A formatter that allows named placeholders e.g. "%(param)" to be replaced. |
| 28 | + */ |
| 29 | +public class NamedFormatter { |
| 30 | + private static final Pattern PARAM_REGEX = Pattern |
| 31 | + .compile( |
| 32 | + // Match either any backlash-escaped characters, or a "%(param)" pattern. |
| 33 | + // COMMENTS is specified to allow whitespace in this pattern, for clarity |
| 34 | + "\\\\(.) | (% \\( ([^)]+) \\) )", |
| 35 | + Pattern.COMMENTS |
| 36 | + ); |
| 37 | + |
| 38 | + private NamedFormatter() {} |
| 39 | + |
| 40 | + /** |
| 41 | + * Replaces named parameters of the form <code>%(param)</code> in format strings. For example: |
| 42 | + * |
| 43 | + * <ul> |
| 44 | + * <li><code>NamedFormatter.format("Hello, %(name)!", Map.of("name", "world"))</code> → <code>"Hello, world!"</code></li> |
| 45 | + * <li><code>NamedFormatter.format("Hello, \%(name)!", Map.of("name", "world"))</code> → <code>"Hello, %(world)!"</code></li> |
| 46 | + * <li><code>NamedFormatter.format("Hello, %(oops)!", Map.of("name", "world"))</code> → {@link IllegalArgumentException}</li> |
| 47 | + * </ul> |
| 48 | + * |
| 49 | + * @param fmt The format string. Any <code>%(param)</code> is replaced by its corresponding value in the <code>values</code> map. |
| 50 | + * Parameter patterns can be escaped by prefixing with a backslash. |
| 51 | + * @param values a map of parameter names to values. |
| 52 | + * @return The formatted string. |
| 53 | + * @throws IllegalArgumentException if a parameter is found in the format string with no corresponding value |
| 54 | + */ |
| 55 | + public static String format(String fmt, Map<String, Object> values) { |
| 56 | + final Matcher matcher = PARAM_REGEX.matcher(fmt); |
| 57 | + |
| 58 | + boolean result = matcher.find(); |
| 59 | + |
| 60 | + if (result) { |
| 61 | + final StringBuffer sb = new StringBuffer(); |
| 62 | + do { |
| 63 | + String replacement; |
| 64 | + |
| 65 | + // Escaped characters are unchanged |
| 66 | + if (matcher.group(1) != null) { |
| 67 | + replacement = matcher.group(1); |
| 68 | + } else { |
| 69 | + final String paramName = matcher.group(3); |
| 70 | + if (values.containsKey(paramName) == true) { |
| 71 | + replacement = values.get(paramName).toString(); |
| 72 | + } else { |
| 73 | + throw new IllegalArgumentException("No parameter value for %(" + paramName + ")"); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + matcher.appendReplacement(sb, replacement); |
| 78 | + result = matcher.find(); |
| 79 | + } while (result); |
| 80 | + |
| 81 | + matcher.appendTail(sb); |
| 82 | + return sb.toString(); |
| 83 | + } |
| 84 | + |
| 85 | + return fmt; |
| 86 | + } |
| 87 | +} |
0 commit comments