diff --git a/README.md b/README.md index 6c07f01f9..e98df2551 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +![Archived](https://img.shields.io/badge/Current_Status-archived-blue?style=flat) + [![Build Status](https://secure.travis-ci.org/tcurdt/jdeb.png)](http://travis-ci.org/tcurdt/jdeb) # Debian packages in Java diff --git a/src/main/java/org/vafer/jdeb/utils/MapVariableResolver.java b/src/main/java/org/vafer/jdeb/utils/MapVariableResolver.java index deb6d725b..eb27af59a 100644 --- a/src/main/java/org/vafer/jdeb/utils/MapVariableResolver.java +++ b/src/main/java/org/vafer/jdeb/utils/MapVariableResolver.java @@ -26,6 +26,9 @@ */ public final class MapVariableResolver implements VariableResolver { + private static final String MVN_VAR_PREFIX = "${"; + private static final String MVN_VAR_SUFFIX = "}"; + private final Map map; public MapVariableResolver( Map map ) { @@ -33,7 +36,40 @@ public MapVariableResolver( Map map ) { } public String get( String key ) { - return map.get(key); + String value = map.get(key); + try { + return resolveVariables(value); + } catch (Exception e) { + return value; + } + } + + private String resolveVariables(String value ) { + String transformingValue = value; + int searchVariableFromIndex = 0; + while (!isEmptyOfVariable(transformingValue)) { + int indexOfPrefix = value.indexOf(MVN_VAR_PREFIX, searchVariableFromIndex); + int indexOfSuffix = value.indexOf(MVN_VAR_SUFFIX, searchVariableFromIndex) +1; + searchVariableFromIndex = indexOfSuffix; + + String variable = value.substring(indexOfPrefix, indexOfSuffix); + transformingValue = substituteVariable(transformingValue, variable); + } + return transformingValue; + } + + private String substituteVariable(String transformingValue, String variable) { + return transformingValue.replace(variable, get(stripVariable(variable))); + } + + private String stripVariable(String variable) { + return variable.substring(MVN_VAR_PREFIX.length(), variable.length() -1); + } + + private boolean isEmptyOfVariable(String value) { + int indexOfPrefix = value.indexOf(MVN_VAR_PREFIX); + int indexOfSuffix = value.indexOf(MVN_VAR_SUFFIX); + return value == null || indexOfSuffix <= indexOfPrefix; } } diff --git a/src/test/java/org/vafer/jdeb/utils/MapVariableResolverTest.java b/src/test/java/org/vafer/jdeb/utils/MapVariableResolverTest.java new file mode 100644 index 000000000..cd8a51dc3 --- /dev/null +++ b/src/test/java/org/vafer/jdeb/utils/MapVariableResolverTest.java @@ -0,0 +1,100 @@ +/* ***** BEGIN LICENSE BLOCK ***** +* +* Copyright 2014 The jdeb developers. +* Copyright (C) 2014 Linagora +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package org.vafer.jdeb.utils; + +import java.util.HashMap; +import java.util.Map; + +import junit.framework.TestCase; + +public class MapVariableResolverTest extends TestCase { + + public void testRead() throws Exception { + Map map = new HashMap(); + map.put("a", "b"); + MapVariableResolver testee = new MapVariableResolver(map); + + assertEquals("b", testee.get("a")); + } + + public void testWhenValueContainsNotWellFormatedVariable() throws Exception { + Map map = new HashMap(); + map.put("a", "}b${"); + MapVariableResolver testee = new MapVariableResolver(map); + + assertEquals("}b${", testee.get("a")); + } + + public void testWhenValueContainsEmptyVariable() throws Exception { + Map map = new HashMap(); + map.put("a", "b${}"); + MapVariableResolver testee = new MapVariableResolver(map); + + assertEquals("b${}", testee.get("a")); + } + + public void testWhenValueContainsUnknownVariable() throws Exception { + Map map = new HashMap(); + map.put("a", "${b}"); + MapVariableResolver testee = new MapVariableResolver(map); + + assertEquals("${b}", testee.get("a")); + } + + public void testWhenValueContainsKnownVariable() throws Exception { + Map map = new HashMap(); + map.put("a", "${b}"); + map.put("b", "c"); + MapVariableResolver testee = new MapVariableResolver(map); + + assertEquals("c", testee.get("a")); + } + + public void testWhenValueContainsKnownVariableAndMore() throws Exception { + Map map = new HashMap(); + map.put("a", "abc${b}d${e}f"); + map.put("b", "${c}"); + map.put("c", "Z"); + map.put("e", "X"); + MapVariableResolver testee = new MapVariableResolver(map); + + assertEquals("abcZdXf", testee.get("a")); + } + + public void testWhenValueBeginsKnownVariableAndMore() throws Exception { + Map map = new HashMap(); + map.put("a", "${b}${e}abcdf"); + map.put("b", "${c}"); + map.put("c", "Z"); + map.put("e", "X"); + MapVariableResolver testee = new MapVariableResolver(map); + + assertEquals("ZXabcdf", testee.get("a")); + } + + public void testWhenValueEndsKnownVariableAndMore() throws Exception { + Map map = new HashMap(); + map.put("a", "abcdf${b}${e}"); + map.put("b", "${c}"); + map.put("c", "Z"); + map.put("e", "X"); + MapVariableResolver testee = new MapVariableResolver(map); + + assertEquals("abcdfZX", testee.get("a")); + } +}