001/* 002 * Copyright 2023 the original author or authors. 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package de.cuioss.tools.formatting.template.token; 017 018import static de.cuioss.tools.base.Preconditions.checkArgument; 019import static java.util.Objects.requireNonNull; 020 021import java.io.Serializable; 022import java.util.Arrays; 023import java.util.List; 024import java.util.Map; 025 026import de.cuioss.tools.formatting.template.FormatterSupport; 027import lombok.EqualsAndHashCode; 028import lombok.ToString; 029 030/** 031 * String Action Token store template and attribute name, and replace attribute 032 * with his value inside the template on execution of 033 * {@linkplain #substituteAttribute(FormatterSupport)} 034 * 035 * @author Eugen Fischer 036 */ 037@ToString 038@EqualsAndHashCode 039public class ActionToken implements Token { 040 041 private static final long serialVersionUID = -6329721490557755853L; 042 043 private final String before; 044 045 private final String attribute; 046 047 private final String after; 048 049 /** 050 * @param template 051 * @param token 052 */ 053 public ActionToken(final String template, final String token) { 054 checkArgument(template.contains(token), "'" + template + " must contain '" + token + "'"); 055 final List<String> splitted = Arrays.asList(template.split(token)); 056 before = extractSurrounding(splitted, 0); 057 attribute = token; 058 after = extractSurrounding(splitted, 1); 059 } 060 061 private static String extractSurrounding(final List<String> splitted, final int index) { 062 var result = ""; 063 if (!splitted.isEmpty() && splitted.size() > index) { 064 result = splitted.get(index); 065 } 066 return result; 067 } 068 069 @Override 070 public String substituteAttribute(final FormatterSupport content) { 071 requireNonNull(content, "Content must not be null. "); 072 final Map<String, Serializable> attributeValues = requireNonNull(content.getAvailablePropertyValues(), 073 "AvailablePropertyValues must not be null. "); 074 final var result = new StringBuilder(); 075 if (attributeValues.containsKey(attribute)) { 076 if (attributeValues.size() > 1) { 077 result.append(before).append(attributeValues.get(attribute)).append(after); 078 } else { 079 // special case : if only one value exists no stored before + after are needed 080 result.append(attributeValues.get(attribute).toString()); 081 } 082 } 083 return result.toString(); 084 } 085 086 @Override 087 public boolean isStringToken() { 088 return false; 089 } 090 091}