From b6246427682d0b40f21755b523ae7c9df0bc9bc2 Mon Sep 17 00:00:00 2001 From: Thomas Hilaire Date: Tue, 4 Mar 2014 15:51:54 +0100 Subject: [PATCH 1/3] Resolve variables recursively --- .../vafer/jdeb/utils/MapVariableResolver.java | 38 +++++- .../jdeb/utils/MapVariableResolverTest.java | 114 ++++++++++++++++++ 2 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/vafer/jdeb/utils/MapVariableResolverTest.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..9d04e5ef7 --- /dev/null +++ b/src/test/java/org/vafer/jdeb/utils/MapVariableResolverTest.java @@ -0,0 +1,114 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * + * Copyright (C) 2014 Linagora + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version, provided you comply + * with the Additional Terms applicable for OBM connector by Linagora + * pursuant to Section 7 of the GNU Affero General Public License, + * subsections (b), (c), and (e), pursuant to which you must notably (i) retain + * the “Message sent thanks to OBM, Free Communication by Linagora” + * signature notice appended to any and all outbound messages + * (notably e-mail and meeting requests), (ii) retain all hypertext links between + * OBM and obm.org, as well as between Linagora and linagora.com, and (iii) refrain + * from infringing Linagora intellectual property rights over its trademarks + * and commercial brands. Other Additional Terms apply, + * see for more details. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License + * for more details. + * + * You should have received a copy of the GNU Affero General Public License + * and its applicable Additional Terms for OBM along with this program. If not, + * see for the GNU Affero General Public License version 3 + * and for the Additional Terms applicable to + * OBM connectors. + * + * ***** END LICENSE BLOCK ***** */ +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")); + } +} From dee2e199af1303a6f793b772f998035f001cd676 Mon Sep 17 00:00:00 2001 From: Thomas Hilaire Date: Tue, 4 Mar 2014 16:44:01 +0100 Subject: [PATCH 2/3] Resolve variables recursively - fix license --- .../jdeb/utils/MapVariableResolverTest.java | 46 +++++++------------ 1 file changed, 16 insertions(+), 30 deletions(-) diff --git a/src/test/java/org/vafer/jdeb/utils/MapVariableResolverTest.java b/src/test/java/org/vafer/jdeb/utils/MapVariableResolverTest.java index 9d04e5ef7..cd8a51dc3 100644 --- a/src/test/java/org/vafer/jdeb/utils/MapVariableResolverTest.java +++ b/src/test/java/org/vafer/jdeb/utils/MapVariableResolverTest.java @@ -1,34 +1,20 @@ /* ***** BEGIN LICENSE BLOCK ***** - * - * Copyright (C) 2014 Linagora - * - * This program is free software: you can redistribute it and/or - * modify it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version, provided you comply - * with the Additional Terms applicable for OBM connector by Linagora - * pursuant to Section 7 of the GNU Affero General Public License, - * subsections (b), (c), and (e), pursuant to which you must notably (i) retain - * the “Message sent thanks to OBM, Free Communication by Linagora” - * signature notice appended to any and all outbound messages - * (notably e-mail and meeting requests), (ii) retain all hypertext links between - * OBM and obm.org, as well as between Linagora and linagora.com, and (iii) refrain - * from infringing Linagora intellectual property rights over its trademarks - * and commercial brands. Other Additional Terms apply, - * see for more details. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License - * for more details. - * - * You should have received a copy of the GNU Affero General Public License - * and its applicable Additional Terms for OBM along with this program. If not, - * see for the GNU Affero General Public License version 3 - * and for the Additional Terms applicable to - * OBM connectors. - * - * ***** END 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; From 5efbf9a3f5fc5a9f4205f398dd5cdeefc70cb97d Mon Sep 17 00:00:00 2001 From: Shivam Chauhan Date: Thu, 25 Feb 2021 18:33:29 +0100 Subject: [PATCH 3/3] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) 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