org.writersforge.catalan.templates.generic
Class GenericOp

java.lang.Object
  extended byorg.writersforge.catalan.templates.generic.GenericOp
Direct Known Subclasses:
CommandOp, ContentOp

public abstract class GenericOp
extends java.lang.Object

Base class for all template ops. Provides a simple Datum wrapper, plus the abstract resolve() method which all ops must implement, plus a static factory method, createOp(), to make it easier to build the correct op class from an arbitrary Datum.

Author:
jsheets

Constructor Summary
GenericOp(org.writersforge.bellows.Datum op, StyleMap styleMap)
          Creates a new instance of GenericOp.
GenericOp(org.writersforge.bellows.Datum op, StyleMap styleMap, StyleChain chain)
          Creates a new instance of GenericOp with an optional StyleChain argument.
 
Method Summary
static GenericOp createOp(org.writersforge.bellows.Datum op, StyleMap styleMap)
          Factory method for creating new ops.
static GenericOp createOp(org.writersforge.bellows.Datum op, StyleMap styleMap, StyleChain chain)
          Factory method for creating new ops initialized with an optional StyleChain fallback.
protected  StyleChain getChain()
          Returns the StyleChain for this op.
 org.writersforge.bellows.Datum getDatum()
          Returns the op itself.
 java.lang.Object getDatumProperty(java.lang.String property)
          Returns a property value from the encapsulated Datum op.
protected  org.writersforge.bellows.Datum getOp()
          Returns the encapsulated Datum op.
 java.lang.String getOpType()
          Returns the element name of this op.
protected  java.lang.String getParameter(char param)
          Returns the value of a numbered parameter; used for parameterized queries.
protected  StyleMap getStyleMap()
          Returns the StyleMap for this op.
abstract  java.util.List resolve(org.writersforge.bellows.Datum xmlNode)
          Runs a Datum XML node through the op and returns a List of LayoutArea objects.
protected  java.lang.String resolveParameter(char param, org.writersforge.bellows.Datum dataRoot)
          Attempts to resolve a parameter against a Datum.
protected  java.lang.String resolvePath(java.lang.String path, org.writersforge.bellows.Datum dataRoot)
          Attempts to resolve a parameterized query path.
protected  org.writersforge.bellows.Datum[] runQuery(java.lang.String query, org.writersforge.bellows.Datum dataRoot)
          Performs a query on the supplied Datum tree, resolving parameterized variables and managing absolute and relative paths.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericOp

public GenericOp(org.writersforge.bellows.Datum op,
                 StyleMap styleMap)
Creates a new instance of GenericOp. If the styleMap parameter is null, uses DefaultStyle for the StyleMap.

Parameters:
op - the XML template op to wrap
styleMap - the StyleMap to lookup named styles in

GenericOp

public GenericOp(org.writersforge.bellows.Datum op,
                 StyleMap styleMap,
                 StyleChain chain)
Creates a new instance of GenericOp with an optional StyleChain argument. If the styleMap parameter is null, uses DefaultStyle for the StyleMap. If chain is non-null, the new GenericOp will copy the chain into itself and use it as a fallback.

Parameters:
op - the XML template op to wrap
styleMap - the StyleMap to lookup named styles in
chain - optional fallback StyleChain
Method Detail

resolve

public abstract java.util.List resolve(org.writersforge.bellows.Datum xmlNode)
Runs a Datum XML node through the op and returns a List of LayoutArea objects. The caller may pass in a fallback style

Parameters:
xmlNode - the input data node to run through the op
Returns:
a List of LayoutArea objects.

getOp

protected org.writersforge.bellows.Datum getOp()
Returns the encapsulated Datum op.

Returns:
the Datum up

getOpType

public java.lang.String getOpType()
Returns the element name of this op. For example, if the op is <text/> in the template XML, the op type is "text".

Returns:
the op type

getDatumProperty

public java.lang.Object getDatumProperty(java.lang.String property)
Returns a property value from the encapsulated Datum op.

Parameters:
property - the property name in the Datum
Returns:
the raw property value

getDatum

public org.writersforge.bellows.Datum getDatum()
Returns the op itself.

Returns:
the Datum op

getStyleMap

protected StyleMap getStyleMap()
Returns the StyleMap for this op.

Returns:
the StyleMap

getChain

protected StyleChain getChain()
Returns the StyleChain for this op.

Returns:
the StyleChain

getParameter

protected java.lang.String getParameter(char param)
Returns the value of a numbered parameter; used for parameterized queries. The index determines the property name, which is always the prefix "param" followed by the index, for example, "param1", or "param5". If the parameter property does not exists, returns a null, not an empty string.

Parameters:
param - the one-character suffix for the parameter attribute
Returns:
the value of the parameter attribute, or null if not found

resolveParameter

protected java.lang.String resolveParameter(char param,
                                            org.writersforge.bellows.Datum dataRoot)
Attempts to resolve a parameter against a Datum. First looks up the query with getParameter(), then runs the query against the dataRoot, converting the results into String form.

Parameters:
param - the one-character suffix for the parameter attribute
dataRoot - the target data for the query
Returns:
the results of the query in String form

resolvePath

protected java.lang.String resolvePath(java.lang.String path,
                                       org.writersforge.bellows.Datum dataRoot)
Attempts to resolve a parameterized query path. Examines the query for single-character parameters prefixed with "$", for example "$1", "$7", and "$d". The parameter name should correspond to an attribute in the op prefixed with "param". Thus, "$1" in the query path corresponds to an op property of "param1", and a "$d" corresponds to "paramd". The contents of the "param" property are treated as another query path and passed to resolveParameter(). This makes it possible to build queries at runtime, based on the results of other queries.

Parameters:
path - a parameterized query path
dataRoot - the target data for the query
Returns:
the results of the query with parameters replaced

runQuery

protected org.writersforge.bellows.Datum[] runQuery(java.lang.String query,
                                                    org.writersforge.bellows.Datum dataRoot)
Performs a query on the supplied Datum tree, resolving parameterized variables and managing absolute and relative paths. All ops should call this method to do their queries -- directly calling DatumQuery.run() will lose these benefits.

Parameters:
query - a parameterized Bellows query path
dataRoot - the current context for relative searches
Returns:
an array of Datum objects that match the query
See Also:
resolvePath

createOp

public static GenericOp createOp(org.writersforge.bellows.Datum op,
                                 StyleMap styleMap)
Factory method for creating new ops. Creates a new GenericOp sub-class, depending on the contents of the Datum input. The 'type' property of the Datum is the most important factor, but other properties of the Datum may play a part also. The StyleMap should be passed from op to op.

Parameters:
op - the Datum object to wrap
styleMap - the style map for creating LayoutArea objects
Returns:
a newly created GenericOp sub-class

createOp

public static GenericOp createOp(org.writersforge.bellows.Datum op,
                                 StyleMap styleMap,
                                 StyleChain chain)
Factory method for creating new ops initialized with an optional StyleChain fallback. Creates a new GenericOp sub-class, depending on the contents of the Datum input. The 'type' property of the Datum is the most important factor, but other properties of the Datum may play a part also. The StyleMap should be passed from op to op. The StyleChain argument can be null.

Parameters:
op - the Datum object to wrap
styleMap - the style map for creating LayoutArea objects
chain - optional fallback StyleChain
Returns:
a newly created GenericOp sub-class