dfh.treepath
Class FunctionalForester<N>

java.lang.Object
  extended by dfh.treepath.Forester<N>
      extended by dfh.treepath.FunctionalForester<N>
Type Parameters:
N - the type of node in the tree
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
MatchPath

public abstract class FunctionalForester<N>
extends Forester<N>

A generic Forester filled with useful attributes (these are the functions). Basically, this Forester holds a bunch of functions, mostly copied from XPath. If your Forester extends this class, you get all these functions as well.

To reduce the incidence of namespace collisions, all the attributes in this class have namespace prefixes and indicate their category of function:

s
String function
m
mathematical function
u
utility function

Author:
David F. Houghton - May 7, 2012
See Also:
Serialized Form

Field Summary
 
Fields inherited from class dfh.treepath.Forester
attributeCache, attributes, pathMt
 
Constructor Summary
FunctionalForester(NodeTest<N>... nodeTest)
          Constructs a funtional forester that ignores the specified node types.
 
Method Summary
protected  Number abs(N n, Collection<N> c, Index<N> i, Number num)
          Calls Math.abs(int) or Math.abs(double), attempting to preserve the precision of the argument.
protected  Number ceil(N n, Collection<N> c, Index<N> i, Number num)
          Calls Math.ceil(double).
protected  Integer compare(N n, Collection<N> c, Index<N> i, String s1, String s2)
          Calls String.compareTo(String)
protected  String concatenate(N n, Collection<N> c, Index<N> i, Object... parts)
          Joins together the stringifications of a list of items in a new string.
protected  boolean contains(N n, Collection<N> c, Index<N> i, String s, String pattern)
          Calls String.indexOf(int) and returns whether the value is greater than -1.
protected  Boolean defined(N n, Collection<N> c, Index<N> i, Object o)
          A boolean attribute that is true if the object parameter is not null.
protected  boolean endsWith(N n, Collection<N> c, Index<N> i, String s, String pattern)
          Calls String.endsWith(String).
protected  boolean find(N n, Collection<N> c, Index<N> i, String s, String pattern)
          Determines whether the pattern matches the input string anywhere.
protected  Number floor(N n, Collection<N> c, Index<N> i, Number num)
          Calls Math.floor(double).
protected  int index(N n, Collection<N> c, Index<N> i, String s, String pattern)
          Calls String.indexOf(String).
protected  Integer integralPortion(N n, Collection<N> c, Index<N> i, Number num)
          Calls Number.intValue().
protected  String join(N n, Collection<N> c, Index<N> i, Object separator, Object... items)
          Returns the stringification of a list of items, separating each pair of items with the specified separator.
protected  Integer length(N n, Collection<N> c, Index<N> i, String s)
          Calls String.length().
protected  boolean lookingAt(N n, Collection<N> c, Index<N> i, String s, String pattern)
          Determines whether the pattern matches the beginning of the input string.
protected  String lowercase(N n, Collection<N> c, Index<N> i, String s)
          Calls String.toLowerCase().
protected  boolean matches(N n, Collection<N> c, Index<N> i, String s, String pattern)
          Determines whether the pattern matches the entirety of the input string.
protected  Number max(N n, Collection<N> c, Index<N> i, Number... nums)
          Returns the maximum value in a list of numbers.
protected  Long millis(N n, Collection<N> c, Index<N> i)
          Returns System.currentTimeMillis().
protected  Number min(N n, Collection<N> c, Index<N> i, Number... nums)
          Returns the minimum value in a list of numbers.
protected  String normalizeWhitespace(N n, Collection<N> c, Index<N> i, String s)
          Calls String.trim() and then replaces all internal whitespace with a single space.
protected  Double product(N n, Collection<N> c, Index<N> i, Number... nums)
          Multiplies the numbers in a list, returning the product as a double.
protected  String replace(N n, Collection<N> c, Index<N> i, String s, String pattern, String replacement)
          Calls String#replace(String, String).
protected  String replaceAll(N n, Collection<N> c, Index<N> i, String s, String pattern, String replacement)
          Calls String.replaceAll(String, String)
protected  String replaceFirst(N n, Collection<N> c, Index<N> i, String s, String pattern, String replacement)
          Calls String.replaceFirst(String, String)
protected  Number round(N n, Collection<N> c, Index<N> i, Number num)
          Calls Math.round(double).
protected  boolean startsWith(N n, Collection<N> c, Index<N> i, String s, String pattern)
          Calls String.startsWith(String).
protected  String substring(N n, Collection<N> c, Index<N> i, String s, Integer... offsets)
          Calls String.substring(int, int) or String.substring(int) , depending on how many integer parameters are provided.
protected  Double sum(N n, Collection<N> c, Index<N> i, Number... nums)
          Sums the numbers in a list, returning the sum as a double.
protected  String trim(N n, Collection<N> c, Index<N> i, String s)
          Calls String.trim().
protected  String ucFirst(N n, Collection<N> c, Index<N> i, String s)
          Capitalizes the first letter in the string.
protected  String uppercase(N n, Collection<N> c, Index<N> i, String s)
          Calls String.toUpperCase().
 
Methods inherited from class dfh.treepath.Forester
ancestors, attribToBoolean, attribute, attribute, attributes, axis, children, children, closest, depth, descendants, echo, False, following, followingSiblings, getAttributes, getLoggingStream, hasTag, height, id, index, index, init, isLeaf, isRoot, kids, leaves, log, matchesTag, mixin, Null, parent, path, pick, preceding, precedingSiblings, root, setLoggingStream, siblings, siblings, siblingsOrSelf, size, This, True, tsize, uid, width
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FunctionalForester

public FunctionalForester(NodeTest<N>... nodeTest)
Constructs a funtional forester that ignores the specified node types.

Parameters:
nodeTest - nodes to ignore
Method Detail

matches

@Attribute(value="s:matches",
           description="whether the string parameter matches a pattern")
protected boolean matches(N n,
                                                    Collection<N> c,
                                                    Index<N> i,
                                                    String s,
                                                    String pattern)
Determines whether the pattern matches the entirety of the input string. This method calls Pattern.matches(String, CharSequence). If you have to match a particular pattern often, it is likely more efficient to write your own attribute that calls a pre-compiled Pattern.

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
s - string matched against
pattern - pattern to match
Returns:
whether the pattern matched

startsWith

@Attribute(value="s:starts-with",
           description="whether the string parameters as a particular prefix")
protected boolean startsWith(N n,
                                                       Collection<N> c,
                                                       Index<N> i,
                                                       String s,
                                                       String pattern)
Calls String.startsWith(String).

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
s - string to search in
pattern - prefix
Returns:
whether the string has the given prefix

endsWith

@Attribute(value="s:ends-with",
           description="whether the string parameter has a particular suffix")
protected boolean endsWith(N n,
                                                     Collection<N> c,
                                                     Index<N> i,
                                                     String s,
                                                     String pattern)
Calls String.endsWith(String).

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
s - string to search in
pattern - suffix
Returns:
whether the string has the given suffix

contains

@Attribute(value="s:contains",
           description="whether the string contains a particular infix")
protected boolean contains(N n,
                                                     Collection<N> c,
                                                     Index<N> i,
                                                     String s,
                                                     String pattern)
Calls String.indexOf(int) and returns whether the value is greater than -1.

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
s - string to search in
pattern - infix
Returns:
whether the string contains the given infix

index

@Attribute(value="s:index",
           description="the index of an infix in the string")
protected int index(N n,
                                              Collection<N> c,
                                              Index<N> i,
                                              String s,
                                              String pattern)
Calls String.indexOf(String).

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
s - string to search in
pattern - infix
Returns:
index of first character of infix in string

concatenate

@Attribute(value="s:concat",
           description="string concatenating items")
protected String concatenate(N n,
                                                       Collection<N> c,
                                                       Index<N> i,
                                                       Object... parts)
Joins together the stringifications of a list of items in a new string.

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
parts - items to concatenate
Returns:
concatenation of the stringification of an arbitrary list of objects

max

@Attribute(value="m:max",
           description="maximum value")
protected Number max(N n,
                                               Collection<N> c,
                                               Index<N> i,
                                               Number... nums)
Returns the maximum value in a list of numbers.

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
nums - a list of numbers
Returns:
the maximum value in the list

min

@Attribute(value="m:min",
           description="minimum value")
protected Number min(N n,
                                               Collection<N> c,
                                               Index<N> i,
                                               Number... nums)
Returns the minimum value in a list of numbers.

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
nums - a list of numbers
Returns:
the minimum value

sum

@Attribute(value="m:sum",
           description="sum of values")
protected Double sum(N n,
                                               Collection<N> c,
                                               Index<N> i,
                                               Number... nums)
Sums the numbers in a list, returning the sum as a double.

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
nums - a list of numbers
Returns:
the sum of values in the list represented as a double

product

@Attribute(value="m:prod",
           description="product of values")
protected Double product(N n,
                                                   Collection<N> c,
                                                   Index<N> i,
                                                   Number... nums)
Multiplies the numbers in a list, returning the product as a double. An empty list returns 0.

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
nums - a list of numbers
Returns:
the product

millis

@Attribute(value="u:millis",
           description="current time in milliseconds")
protected Long millis(N n,
                                                Collection<N> c,
                                                Index<N> i)
Returns System.currentTimeMillis(). Potentially useful in debugging.

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
Returns:
System.currentTimeMillis()

replaceFirst

@Attribute(value="s:replace-first",
           description="replace first occurrence of pattern")
protected String replaceFirst(N n,
                                                        Collection<N> c,
                                                        Index<N> i,
                                                        String s,
                                                        String pattern,
                                                        String replacement)
Calls String.replaceFirst(String, String)

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
pattern - regular expression
replacement - replacement pattern
Returns:
transformed string

replaceAll

@Attribute(value="s:replace-all",
           description="replace all occurrences of pattern")
protected String replaceAll(N n,
                                                      Collection<N> c,
                                                      Index<N> i,
                                                      String s,
                                                      String pattern,
                                                      String replacement)
Calls String.replaceAll(String, String)

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
pattern - regular expression
replacement - replacement pattern
Returns:
transformed string

replace

@Attribute(value="s:replace",
           description="replace all occurrences of infix")
protected String replace(N n,
                                                   Collection<N> c,
                                                   Index<N> i,
                                                   String s,
                                                   String pattern,
                                                   String replacement)
Calls String#replace(String, String). This is more efficient than replaceAll(Object, Collection, Index, String, String, String) because it doesn't involve any regular expression compilation.

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
pattern - infix
replacement - replacement infix
Returns:
transformed string

compare

@Attribute(value="s:cmp",
           description="compare string order")
protected Integer compare(N n,
                                                    Collection<N> c,
                                                    Index<N> i,
                                                    String s1,
                                                    String s2)
Calls String.compareTo(String)

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
s1 - first string
s2 - second string
Returns:
integer representing the alphabetical ordering of the two strings

substring

@Attribute(value="s:substr",
           description="select substring")
protected String substring(N n,
                                                     Collection<N> c,
                                                     Index<N> i,
                                                     String s,
                                                     Integer... offsets)
Calls String.substring(int, int) or String.substring(int) , depending on how many integer parameters are provided.

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
s - string to obtain a substring from
offsets - one or two character offsets
Returns:
a substring

length

@Attribute(value="s:len",
           description="string length")
protected Integer length(N n,
                                                   Collection<N> c,
                                                   Index<N> i,
                                                   String s)
Calls String.length().

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
s - string to measure
Returns:
the length of the string parameter

uppercase

@Attribute(value="s:uc",
           description="uppercase")
protected String uppercase(N n,
                                                     Collection<N> c,
                                                     Index<N> i,
                                                     String s)
Calls String.toUpperCase().

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
s - string to transform
Returns:
string in uppercase

lowercase

@Attribute(value="s:lc",
           description="lowercase")
protected String lowercase(N n,
                                                     Collection<N> c,
                                                     Index<N> i,
                                                     String s)
Calls String.toLowerCase().

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
s - string to transform
Returns:
string in lowercase

ucFirst

@Attribute(value="s:ucfirst",
           description="capitalize only first letter")
protected String ucFirst(N n,
                                                   Collection<N> c,
                                                   Index<N> i,
                                                   String s)
Capitalizes the first letter in the string.

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
s - string to transform
Returns:
initial uppercase string

trim

@Attribute(value="s:trim",
           description="trim whitespace")
protected String trim(N n,
                                                Collection<N> c,
                                                Index<N> i,
                                                String s)
Calls String.trim().

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
s - string to transform
Returns:
trim without marginal whitespace

normalizeWhitespace

@Attribute(value="s:nspace",
           description="normalize whitespace")
protected String normalizeWhitespace(N n,
                                                               Collection<N> c,
                                                               Index<N> i,
                                                               String s)
Calls String.trim() and then replaces all internal whitespace with a single space.

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
s - string to transform
Returns:
trim without marginal whitespace and with internal whitespace normalized to a single space

join

@Attribute(value="s:join",
           description="concatenate items with separator")
protected String join(N n,
                                                Collection<N> c,
                                                Index<N> i,
                                                Object separator,
                                                Object... items)
Returns the stringification of a list of items, separating each pair of items with the specified separator.

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
separator - object whose stringification should be placed between any two items
items - items to join
Returns:
trim without marginal whitespace

abs

@Attribute(value="m:abs",
           description="absolute value")
protected Number abs(N n,
                                               Collection<N> c,
                                               Index<N> i,
                                               Number num)
Calls Math.abs(int) or Math.abs(double), attempting to preserve the precision of the argument.

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
num - number to obtain the absolute value of
Returns:
absolute value of number

ceil

@Attribute(value="m:ceil",
           description="round up")
protected Number ceil(N n,
                                                Collection<N> c,
                                                Index<N> i,
                                                Number num)
Calls Math.ceil(double).

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
num - number to obtain the ceiling value of
Returns:
ceiling of num

integralPortion

@Attribute(value="m:int",
           description="integral portion")
protected Integer integralPortion(N n,
                                                            Collection<N> c,
                                                            Index<N> i,
                                                            Number num)
Calls Number.intValue().

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
num - number to obtain the integral portion of
Returns:
integral portion of num

floor

@Attribute(value="m:floor",
           description="round down")
protected Number floor(N n,
                                                 Collection<N> c,
                                                 Index<N> i,
                                                 Number num)
Calls Math.floor(double).

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
num - number to obtain the floor value of
Returns:
floor of num

round

@Attribute(value="m:round",
           description="round to nearest whole number")
protected Number round(N n,
                                                 Collection<N> c,
                                                 Index<N> i,
                                                 Number num)
Calls Math.round(double).

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
num - number to round
Returns:
num rounded to the nearest whole number

lookingAt

@Attribute(value="s:looking-at",
           description="match prefix")
protected boolean lookingAt(N n,
                                                      Collection<N> c,
                                                      Index<N> i,
                                                      String s,
                                                      String pattern)
Determines whether the pattern matches the beginning of the input string. This method calls Matcher.lookingAt(). If you have to match a particular pattern often, it is likely more efficient to write your own attribute that uses a pre-compiled Pattern.

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
s - string matched against
pattern - pattern to match
Returns:
whether the pattern matched

find

@Attribute(value="s:find",
           description="look for pattern in string")
protected boolean find(N n,
                                                 Collection<N> c,
                                                 Index<N> i,
                                                 String s,
                                                 String pattern)
Determines whether the pattern matches the input string anywhere. This method calls Matcher.find(). If you have to match a particular pattern often, it is likely more efficient to write your own attribute that uses a pre-compiled Pattern.

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index; required for method signature but ignored
s - string matched against
pattern - pattern to match
Returns:
whether the pattern matched

defined

@Attribute(value="u:def",
           description="whether the parameter value is non-null")
protected final Boolean defined(N n,
                                                          Collection<N> c,
                                                          Index<N> i,
                                                          Object o)
A boolean attribute that is true if the object parameter is not null.

Parameters:
n - context node; required for method signature but ignored
c - collection of which context node is a member; required for method signature but ignored
i - tree index
o - value to test
Returns:
whether o isn't null