Skip to content

Instantly share code, notes, and snippets.

Created July 10, 2012 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3081986 to your computer and use it in GitHub Desktop.
Save anonymous/3081986 to your computer and use it in GitHub Desktop.
$ groovy -version
Groovy Version: 1.8.6 JVM: 1.6.0_33 Vendor: Apple Inc. OS: Mac OS X
$ ls
CharacterMap.groovy bar.txt
$ groovy -Dlocation=bar.txt CharacterMap.groovy foo
Jul 10, 2012 1:15:31 AM java.lang.Class constructor
WARNING: Server configuration groovy file not configured with system property location
foo
$ cat CharacterMap.groovy
import static java.util.logging.Logger.*
import static java.util.logging.Level.*
class CharacterMap {
public static final String CHARACTER_CONFIGURATION = 'location'
static Map<String,String> characterMap = null
public static String ASCIIize(String input)
{
if (!characterMap)
{
def property = System.getProperty( CHARACTER_CONFIGURATION )
ConfigObject config = null
if( property ) {
ConfigSlurper cs = new ConfigSlurper()
File file = new File( property )
URL location = file.toURL()
config = cs.parse(location)
}
if (config)
{
characterMap = config.characters
}
else
{
getAnonymousLogger().logp WARNING, getClass().name, 'constructor',
"Server configuration groovy file not configured with system property ${CHARACTER_CONFIGURATION}"
return input
}
}
for (String key: characterMap.keySet())
{
if (input.contains(key))
{
input = input.replace(key, characterMap.get(key))
}
}
return input
}
public static void main(String[] args)
{
print ASCIIize(args[0])
}
}
$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment