/*---------------------------------------------------------------------------*\
$Id: ec36caa787736c50ac9395df743dbfcf7ce28cd9 $
---------------------------------------------------------------------------
This software is released under a BSD-style license:
Copyright (c) 2004-2007 Brian M. Clapper. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. The end-user documentation included with the redistribution, if any,
must include the following acknowlegement:
"This product includes software developed by Brian M. Clapper
(bmc@clapper.org, http://www.clapper.org/bmc/). That software is
copyright (c) 2004-2007 Brian M. Clapper."
Alternately, this acknowlegement may appear in the software itself,
if wherever such third-party acknowlegements normally appear.
3. Neither the names "clapper.org", "clapper.org Java Utility Library",
nor any of the names of the project contributors may be used to
endorse or promote products derived from this software without prior
written permission. For written permission, please contact
bmc@clapper.org.
4. Products derived from this software may not be called "clapper.org
Java Utility Library", nor may "clapper.org" appear in their names
without prior written permission of Brian M. Clapper.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
NO EVENT SHALL BRIAN M. CLAPPER BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\*---------------------------------------------------------------------------*/
package org.clapper.util.text;
/**
* The WindowsCmdVariableSubstituter class implements the
* VariableSubstituter interface and provides an inline
* variable substitution capability using a syntax that's reminiscent of
* the Microsoft Windows cmd.exe command interpreter.
* This syntax assumes that variable references are surrounded by "%"
* characters. For example, given the string (variables in
* bold):
*
*
*
* file:%user.home%/profiles/%PLATFORM%/config.txt
*
*
*
* and the variable values:
*
*
* user.home=/home/bmc
* PLATFORM=freebsd
*
*
* a WindowsCmdVariableSubstituter will produce the result
* string (substitutions noted in bold):
*
*
* file:/home/bmc/profiles/freebsd/config.txt
*
*
* Notes and Caveats
*
*
* - To include a literal "%" character in a string, double it.
*
*
- If a variable doesn't have a value, its reference is replaced
* by an empty string.
*
*
- As with all VariableSubstituter classes, an instance
* of the WindowsCmdVariableSubstituter class enforces
* its own variable syntax but defers actual variable value
* resolution to a separate VariableDereferencer object.
* One consequence of that approach is that variable names may be
* case-sensitive or case-insensitive, depending on how the supplied
* VariableDereferencer object interprets variable names.
*
*
* It's also possible to configure a WindowsCmdVariableSubstituter
* to throw a {@link UndefinedVariableException}, rather than substituting
* a blank, if a variable is undefined and a default value is not specified.
* See the {@link #setAbortOnUndefinedVariable} method.
*
* @see UnixShellVariableSubstituter
* @see VariableDereferencer
* @see VariableSubstituter
* @see java.lang.String
*
* @version $Revision$
*
* @author Copyright © 2004-2007 Brian M. Clapper
*/
public class WindowsCmdVariableSubstituter
extends AbstractVariableSubstituter
{
/*----------------------------------------------------------------------*\
Constructor
\*----------------------------------------------------------------------*/
/**
* Default constructor.
*/
public WindowsCmdVariableSubstituter()
{
super();
}
/*----------------------------------------------------------------------*\
Public Methods
\*----------------------------------------------------------------------*/
/**
* Substitute all variable references in the supplied string, using
* a Unix Bourne Shell-style variable syntax. This method uses a
* supplied VariableDereferencer object to resolve variable
* values. Note that this method throws no exceptions. Syntax errors in
* the variable references are silently ignored. Variables that have no
* value are substituted as the empty string. If the
* nameChecker parameter is not null, this method calls its
* {@link VariableNameChecker#legalVariableCharacter(char)} method to
* determine whether a given character is a legal part of a variable
* name. If nameChecker is null, then this method assumes
* that variable names may consist solely of alphanumeric characters and
* underscores.
*
* @param s the string containing possible variable references
* @param deref the VariableDereferencer object
* to use to resolve the variables' values.
* @param nameChecker the VariableNameChecker object to be
* used to check for legal variable name characters,
* or null
* @param context an optional context object, passed through
* unmodified to the deref object's
* {@link VariableDereferencer#getVariableValue}
* method. This object can be anything at all (and,
* in fact, may be null if you don't care.) It's
* primarily useful for passing context information
* from the caller to the
* VariableDereferencer.
*
* @return The (possibly) expanded string.
*
* @throws VariableSubstitutionException substitution error
*
* @see #substitute(String,VariableDereferencer,Object)
* @see VariableDereferencer#getVariableValue(String,Object)
*/
public String substitute( String s,
final VariableDereferencer deref,
final VariableNameChecker nameChecker,
final Object context)
throws VariableSyntaxException,
UndefinedVariableException,
VariableSubstitutionException
{
if (s != null)
s = doSubstitution(s, context, nameChecker, deref);
return s;
}
/*----------------------------------------------------------------------*\
Private Methods
\*----------------------------------------------------------------------*/
/**
* Worker routine called by substitute() to perform the actual
* substitution on a non-null string.
*
* @param s the string containing possible variable references
* @param deref the VariableDereferencer object
* to use to resolve the variables' values.
* @param nameChecker the VariableNameChecker object to be
* used to check for legal variable name characters,
* or null
* @param context an optional context object, passed through
* unmodified to the deref object's
* {@link VariableDereferencer#getVariableValue}
* method. This object can be anything at all (and,
* in fact, may be null if you don't care.) It's
* primarily useful for passing context information
* from the caller to the
* VariableDereferencer.
*
* @return The (possibly) expanded string.
*
* @throws VariableSubstitutionException substitution error
*/
private String doSubstitution(final String s,
final Object context,
VariableNameChecker nameChecker,
final VariableDereferencer deref)
throws VariableSubstitutionException
{
StringBuffer result = new StringBuffer();
int len = s.length();
char prev = '\0';
StringBuffer var = new StringBuffer();
boolean inVar = false;
boolean syntaxError = false;
char ch[];
if (nameChecker == null)
nameChecker = this;
ch = s.toCharArray();
for (int i = 0; i < len; i++)
{
char c = ch[i];
if (c == '%')
{
if (inVar)
{
if (prev == '%')
{
// Doubled "%". Insert one literal "%".
inVar = false;
result.append('%');
}
else
{
// End of variable reference. If the variable name
// is syntactically incorrect, just store the
// entire original sequence in the result string.
String varName = var.toString();
if (syntaxError)
{
result.append('%' + varName + '%');
}
else
{
String value = deref.getVariableValue(varName,
context);
if (((value == null) || (value.length() == 0))
&&
(getAbortOnUndefinedVariable() == true))
{
throw new UndefinedVariableException
("Variable \"" + varName +
"\" is not defined.");
}
result.append(value == null ? "" : value);
}
var.setLength(0);
inVar = false;
syntaxError = false;
prev = '\0'; // prevent match on trailing "%"
}
}
else
{
// Possible start of a new variable.
inVar = true;
prev = c;
}
}
else
{
// Not a '%'
if (inVar)
{
var.append(c);
if (! nameChecker.legalVariableCharacter(c))
syntaxError = true;
}
else
{
result.append(c);
}
prev = c;
}
}
if (inVar)
{
// Never saw the trailing "%" for the last variable reference.
// Transfer the characters buffered in 'var' into the result,
// without modification.
result.append('%');
result.append(var.toString());
syntaxError = true;
}
if (syntaxError && (getAbortOnSyntaxError() == true))
{
throw new VariableSyntaxException
("Syntactically incorrect reference to variable \"" +
var.toString() + "\"");
}
return result.toString();
}
}
Ads help maintain this website.