Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve variables recursively #148

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
38 changes: 37 additions & 1 deletion src/main/java/org/vafer/jdeb/utils/MapVariableResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,50 @@
*/
public final class MapVariableResolver implements VariableResolver {

private static final String MVN_VAR_PREFIX = "${";
private static final String MVN_VAR_SUFFIX = "}";

private final Map<String, String> map;

public MapVariableResolver( Map<String, String> map ) {
this.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;
}

}
100 changes: 100 additions & 0 deletions src/test/java/org/vafer/jdeb/utils/MapVariableResolverTest.java
Original file line number Diff line number Diff line change
@@ -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<String, String> map = new HashMap<String, String>();
map.put("a", "b");
MapVariableResolver testee = new MapVariableResolver(map);

assertEquals("b", testee.get("a"));
}

public void testWhenValueContainsNotWellFormatedVariable() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("a", "}b${");
MapVariableResolver testee = new MapVariableResolver(map);

assertEquals("}b${", testee.get("a"));
}

public void testWhenValueContainsEmptyVariable() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("a", "b${}");
MapVariableResolver testee = new MapVariableResolver(map);

assertEquals("b${}", testee.get("a"));
}

public void testWhenValueContainsUnknownVariable() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("a", "${b}");
MapVariableResolver testee = new MapVariableResolver(map);

assertEquals("${b}", testee.get("a"));
}

public void testWhenValueContainsKnownVariable() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("a", "${b}");
map.put("b", "c");
MapVariableResolver testee = new MapVariableResolver(map);

assertEquals("c", testee.get("a"));
}

public void testWhenValueContainsKnownVariableAndMore() throws Exception {
Map<String, String> map = new HashMap<String, String>();
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<String, String> map = new HashMap<String, String>();
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<String, String> map = new HashMap<String, String>();
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"));
}
}