Class SubstPropsHandler
- java.lang.Object
-
- sunlabs.brazil.properties.SubstPropsHandler
-
- All Implemented Interfaces:
Handler
public class SubstPropsHandler extends java.lang.Object implements Handler
Handler that performs value conversions on ${...} substitutions. For any property whose name matches the supplied regular expression, The source value is "converted" based on a token in the regular expression.This Handler is a generalization of the
convert
attribute of theget
tag of theSetTemplate
. Unlike the implementation in theSetTemplate
that implements a small, fixed set of conversions of property values in the context ofget
, this handler allows plug-able conversion filters, and performs the conversions any time ${...} substitutions are resolved, not just in the context of theget
tag.This requires the addition of new syntax in ${...} substitutions to specify the both the conversion (or filter) to apply, and the value to apply it to. This new syntax is configurable using the
match
,key
, andtoken
attributes, but defaults to: ${filter(value)} wherefilter
represents the conversion filter, andvalue
represents the property name whose contents is filtered.Any class that implements the
Convert
interface can be loaded and called to perform filtering. Filters that implement all the options of the<get ... convert=xxx>
conversion options are included.See the examples, below for the details.
- match
- A regular expression that matches a property name that is a candidate
for filtering. This expression should have at least 2 sets of ()'s
in order to gather values for "key" and "token" below. The
default value is
^([a-z]+)\([^)]+\)$
- key
- The regular expression substitution string used to represent the
actual property name to filter. The default is
\\2
- token
- The regular expression substitution string used to represent the
filter name or "token". The default is
\\1
Using the defaults for "match", "key", and "token", a property named "foo" would be represented as
${xxx(foo)}
where "xxx" is the name of the conversion filter. - tokens
- A witespace separated list of filter names or "token"s that map the conversion filters to conversion classes. For each token (e.g. foo), there should be a property of the form "foo.class" which specifies the name of the class that implements the filter, (and implements the Convert interface described below). Any additional properties (e.g. x, y, z) needed to initialize a filter should be present in the properties file as "foo.x, foo.y...".
- [token].code
- The name to match the "token" in the property name. The default is "[token]".
This class contains sample implementations of the
convert
interface. See below for their functions.- Version:
- 1.5
- Author:
- Stephen Uhler
- See Also:
Properties
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
SubstPropsHandler.Convert
Class that maps strings to strings.static class
SubstPropsHandler.Html
HTML escape a value.static class
SubstPropsHandler.LowerCase
Convert a value to lowercase.static class
SubstPropsHandler.Resub
Do a regexp substitution on a value.class
SubstPropsHandler.SubstProps
This class implements a properties object that knows how to extract the "name" and "filter" from a properly constructed name, and to invoke the filter on the value of the encoded name.static class
SubstPropsHandler.Url
URL encode a String.
-
Constructor Summary
Constructors Constructor Description SubstPropsHandler()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
init(Server server, java.lang.String prefix)
Initializes the handler.boolean
respond(Request request)
Responds to an HTTP request.java.lang.String
toString()
-
-
-
Method Detail
-
init
public boolean init(Server server, java.lang.String prefix)
Description copied from interface:Handler
Initializes the handler.- Specified by:
init
in interfaceHandler
- Parameters:
server
- The HTTP server that created thisHandler
. TypicalHandler
s will useServer.props
to obtain run-time configuration information.prefix
- The handlers name. The string thisHandler
may prepend to all of the keys that it uses to extract configuration information fromServer.props
. This is set (by theServer
andChainHandler
) to help avoid configuration parameter namespace collisions.- Returns:
true
if thisHandler
initialized successfully,false
otherwise. Iffalse
is returned, thisHandler
should not be used.
-
respond
public boolean respond(Request request) throws java.io.IOException
Description copied from interface:Handler
Responds to an HTTP request.- Specified by:
respond
in interfaceHandler
- Parameters:
request
- TheRequest
object that represents the HTTP request.- Returns:
true
if the request was handled. A request was handled if a response was supplied to the client, typically by callingRequest.sendResponse()
orRequest.sendError
.- Throws:
java.io.IOException
- if there was an I/O error while sending the response to the client. Typically, in that case, theServer
will (try to) send an error message to the client and then close the client's connection.The
IOException
should not be used to silently ignore problems such as being unable to access some server-side resource (for example getting aFileNotFoundException
due to not being able to open a file). In that case, theHandler
's duty is to turn thatIOException
into a HTTP response indicating, in this case, that a file could not be found.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-