Skip to content
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
5 changes: 5 additions & 0 deletions src/gov/nasa/worldwind/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ public static synchronized String getStringValue(String key, String defaultValue
public static synchronized String getStringValue(String key)
{
Object o = getInstance().properties.getProperty(key);

if( o != null && o instanceof String )
{
o = WWUtil.replacePropertyReferences((String)o);
}
return o != null ? o.toString() : null;
}

Expand Down
46 changes: 46 additions & 0 deletions src/gov/nasa/worldwind/util/WWUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package gov.nasa.worldwind.util;

import gov.nasa.worldwind.Configuration;
import gov.nasa.worldwind.avlist.*;
import gov.nasa.worldwind.geom.*;
import gov.nasa.worldwind.geom.coords.UTMCoord;
Expand All @@ -14,6 +15,7 @@
import java.lang.reflect.*;
import java.nio.*;
import java.text.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
Expand All @@ -22,6 +24,9 @@
*/
public class WWUtil
{
static final String JAVA_CONFIG_VARIABLE = "\\$\\{([-a-zA-Z0-9._]+)\\}";

static final Pattern propPattern = Pattern.compile(JAVA_CONFIG_VARIABLE);
/**
* Converts a specified string to an integer value. Returns null if the string cannot be converted.
*
Expand Down Expand Up @@ -1270,4 +1275,45 @@ public static void generateTriStripNormals(FloatBuffer vertices, IntBuffer indic
normals.put(i3 + 2, (float) n3.z);
}
}

/**
* Replace all instances of ${....} with the corresponding
* property defined in the {@link #org.nasa.worldwind.Configuration()}
*
* @param in string containing ${} to be replaced
*
* @return result of in after ${} replacement
*/
public final static String replacePropertyReferences(String in)
{
if(in == null || in.indexOf("${") == -1) return in;
String result = in;

Matcher matcher = propPattern.matcher(result);
StringBuffer buf = new StringBuffer();
while (matcher.find())
{
String replaceStr = matcher.group(1);

//Since the Configuration.getStringValue also uses this routine
//this will resolve any nested properties references
String prop = Configuration.getStringValue(replaceStr);

//if it isn't in our Configuration, check if it is a system property
if (prop == null )
//Make sure we expand any properties references in the property
prop = replacePropertyReferences(System.getProperty(replaceStr));
if( prop == null )
{
Logging.logger().warning(String.format("Failed to find property '%s' for '%s'\n", replaceStr, in));
}
else
{
matcher.appendReplacement(buf, prop);
}
}
matcher.appendTail(buf);
result = buf.toString();
return result;
}
}
3 changes: 2 additions & 1 deletion src/gov/nasa/worldwind/util/WWXML.java
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,8 @@ public static String getText(Element context, String path, XPath xpath)

try
{
return xpath.evaluate(path, context);
String val = xpath.evaluate(path, context);
return WWUtil.replacePropertyReferences(val);
}
catch (XPathExpressionException e)
{
Expand Down
49 changes: 49 additions & 0 deletions test/gov/nasa/worldwind/util/WWUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@

package gov.nasa.worldwind.util;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import gov.nasa.worldwind.Configuration;
import gov.nasa.worldwind.util.WWUtil;
import junit.framework.TestCase;

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.util.*;
import java.util.logging.Handler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.StreamHandler;

/**
* Unit tests for {@link WWUtil}.
Expand Down Expand Up @@ -50,4 +60,43 @@ public void testParseTimeString()
time = WWUtil.parseTimeString("invalid time");
TestCase.assertNull(time);
}

@Test
public void testReplacePropertyReferences()
{
//Test that we can resolve nested property references
//and properties defined at the system level
Configuration.setValue("a", "I am a");
System.setProperty("b", "${a}");
Configuration.setValue("c", "${a}, ${b}");
String expanded = WWUtil.replacePropertyReferences("Prefix_${c}_Suffix");
TestCase.assertEquals("Prefix_I am a, I am a_Suffix", expanded);
}

@Test
public void testReplacePropertyReferencesLogging()
{

//Test that we get a log message when the property doesn't exist
Logger logger = Logging.logger();

SimpleFormatter formatter = new SimpleFormatter();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Handler handler = new StreamHandler(out, formatter);
logger.addHandler(handler);
try
{
WWUtil.replacePropertyReferences("${property.does.not.exist}");
handler.flush();
String logMsg = out.toString();

assertNotNull(logMsg);
assertTrue(logMsg.contains("Failed to find property 'property.does.not.exist' for '${property.does.not.exist}'"));
}
finally
{
logger.removeHandler(handler);
}
}

}