public class TemplateWorker.Template
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
ATTR_NAME
Names a given template.
|
private java.util.Map<java.lang.String,Aast> |
depot
Temporary storage for ASTs that are created but not yet attached.
|
private java.util.Set<java.lang.String> |
libraries
Caches the list of loaded XML libraries (to avoid reloading).
|
private java.util.Map<java.lang.String,org.w3c.dom.Node> |
templates
Caches the list of loaded AST templates.
|
Constructor and Description |
---|
Template() |
Modifier and Type | Method and Description |
---|---|
boolean |
addToDepot(java.lang.String name,
Aast ast)
Adds or replaces the given AST in the depot using the specified name
as a key by which the AST can be retrieved later.
|
void |
attach(Aast parent,
Aast child)
Grafts the child to the parent as the last childe and registers
the subtree (rooted at the child) with the resolver.
|
void |
attach(Aast parent,
Aast child,
int index)
Grafts the child to the parent at the specified index and registers
the subtree (rooted at the child) with the resolver.
|
Aast |
getFromDepot(java.lang.String name)
Returns the AST found in the depot by the given name or
null if no such AST exists. |
Aast |
graft(java.lang.String name,
Aast source,
Aast parent,
java.lang.Object[] replacements)
Lookup a named template, process any attribute text replacements
(if specified), attach the new AST subtree as the last child of the
specified parent and, if any of the AST nodes has a
PEERNODE attribute AND the source
parameter is not null , cross-reference that node with
source , using the respective 'peerid' values. |
Aast |
graft(java.lang.String name,
Aast source,
long parentid,
java.lang.Object[] replacements)
Lookup a named template, process any attribute text replacements
(if specified), attach the new AST subtree as the last child of the
specified parent and, if any of the AST nodes has a
PEERNODE attribute AND the source
parameter is not null , cross-reference that node with
source , using the respective 'peerid' values. |
Aast |
graftAt(java.lang.String name,
Aast source,
Aast parent,
int index,
java.lang.Object[] replacements)
Lookup a named template, process any attribute text replacements
(if specified), attach the new AST subtree as a child of the
specified parent AT THE SPECIFIED INDEX and, if any of the AST nodes
has a
PEERNODE attribute AND the source
parameter is not null , cross-reference that node with
source , using the respective 'peerid' values. |
Aast |
graftAt(java.lang.String name,
Aast source,
long parentid,
int index,
java.lang.Object[] replacements)
Lookup a named template, process any attribute text replacements
(if specified), attach the new AST subtree as a child of the
specified parent AT THE SPECIFIED INDEX and, if any of the AST nodes
has a
PEERNODE attribute AND the source
parameter is not null , cross-reference that node with
source , using the respective 'peerid' values. |
boolean |
isInDepot(java.lang.String name)
Detects if an AST exists in the depot under the given name.
|
void |
load(java.lang.String name)
Load a named template library from an XML file.
|
private void |
processReplacements(org.w3c.dom.Node tpl,
java.util.Map replacements)
Recursively sweep all attribute values of this node and its subtree,
based on a map of key/value pairs.
|
Aast |
removeFromDepot(java.lang.String name)
Returns the AST found in the depot by the given name, removing it
first.
|
private void |
zeroAstIds(org.w3c.dom.Element tpl)
As a safety measure, set all AST IDs to 0, so they will have appropriate, available IDs
applied when the template is grafted to a parent AST.
|
private static final java.lang.String ATTR_NAME
private java.util.Map<java.lang.String,org.w3c.dom.Node> templates
private java.util.Set<java.lang.String> libraries
private java.util.Map<java.lang.String,Aast> depot
public void load(java.lang.String name) throws ConfigurationException
<name>.<tpl>
. it must be located somewhere
in the path defined by the global configuration parameter
patpath
.name
- Root name of the XML library containing AST templates.ConfigurationException
- if any error occurs loading the template library.public Aast graft(java.lang.String name, Aast source, long parentid, java.lang.Object[] replacements) throws AstException
PEERNODE
attribute AND the source
parameter is not null
, cross-reference that node with
source
, using the respective 'peerid' values. Any
specified replacement key found in the form ${key}
will be replaced with the matching value. The object array must
be made as sequential pairs of key string and matching value string.name
- The name of the template to use. This template must have
been previously loaded using load(java.lang.String)
.source
- The source node to annotate if the PEERNODE
annotation exists in the AST subtree. May be
null
.parentid
- The ID of the target AST to be used as a parent for this
subtree. If null
no attachment occurs but the
tree is still created.replacements
- An array of key/value string pairs where any keys found
in the template node's attribute values will be replaced
with the associated values.AstException
- if there is any problem creating or attaching the template.public Aast graftAt(java.lang.String name, Aast source, long parentid, int index, java.lang.Object[] replacements) throws AstException
PEERNODE
attribute AND the source
parameter is not null
, cross-reference that node with
source
, using the respective 'peerid' values. Any
specified replacement key found in the form ${key}
will be replaced with the matching value. The object array must
be made as sequential pairs of key string and matching value string.name
- The name of the template to use. This template must have
been previously loaded using load(java.lang.String)
.source
- The source node to annotate if the PEERNODE
annotation exists in the AST subtree. May be
null
.parentid
- The ID of the target AST to be used as a parent for this
subtree. If null
no attachment occurs but the
tree is still created.index
- 0 to insert as the first child, -1 to insert as the last
child or a 0-based index to specify an exact child index
at which to insert the new child. All current children at
that index or at a greater index, are moved right. If the
index is greater than the current number of children, the
new child will be added at the end.replacements
- An array of key/value string pairs where any keys found
in the template node's attribute values will be replaced
with the associated values.AstException
- if there is any problem creating or attaching the template.public Aast graft(java.lang.String name, Aast source, Aast parent, java.lang.Object[] replacements) throws AstException
PEERNODE
attribute AND the source
parameter is not null
, cross-reference that node with
source
, using the respective 'peerid' values. Any
specified replacement key found in the form ${key}
will be replaced with the matching value. The object array must
be made as sequential pairs of key string and matching value string.name
- The name of the template to use. This template must have
been previously loaded using load(java.lang.String)
.source
- The source node to annotate if the PEERNODE
annotation exists in the AST subtree. May be
null
.parent
- The target AST to be used as a parent for this subtree.
If null
no attachment occurs but the tree
is still created.replacements
- An array of key/value string pairs where any keys found
in the template node's attribute values will be replaced
with the associated values.AstException
- if there is any problem creating or attaching the template.public Aast graftAt(java.lang.String name, Aast source, Aast parent, int index, java.lang.Object[] replacements) throws AstException
PEERNODE
attribute AND the source
parameter is not null
, cross-reference that node with
source
, using the respective 'peerid' values. Any
specified replacement key found in the form ${key}
will be replaced with the matching value. The object array must
be made as sequential pairs of key string and matching value string.name
- The name of the template to use. This template must have
been previously loaded using load(java.lang.String)
.source
- The source node to annotate if the PEERNODE
annotation exists in the AST subtree. May be
null
.parent
- The target AST to be used as a parent for this subtree.
If null
no attachment occurs but the tree
is still created.index
- 0 to insert as the first child, -1 to insert as the last
child or a 0-based index to specify an exact child index
at which to insert the new child. All current children at
that index or at a greater index, are moved right. If the
index is greater than the current number of children, the
new child will be added at the end.replacements
- An array of key/value string pairs where any keys found
in the template node's attribute values will be replaced
with the associated values.AstException
- if there is any problem creating or attaching the template.public void attach(Aast parent, Aast child) throws AstException
parent
- The node at which to graft the child subtree. If
null
this method immediately returns.child
- The root of the subtree to be grafted.AstException
- if there is any problem grafting the child to the parent.public void attach(Aast parent, Aast child, int index) throws AstException
parent
- The node at which to graft the child subtree. If
null
this method immediately returns.child
- The root of the subtree to be grafted.index
- 0 to insert as the first child, -1 to insert as the last
child or a 0-based index to specify an exact child index
at which to insert the new child. All current children at
that index or at a greater index, are moved right. If the
index is greater than the current number of children, the
new child will be added at the end.AstException
- if there is any problem grafting the child to the parent.public boolean addToDepot(java.lang.String name, Aast ast)
name
- The name to use as a key for the depot.ast
- The AST to add to the depot.true
.public Aast removeFromDepot(java.lang.String name)
null
is returned.name
- The name to use as a key for the depot lookup.null
if no such AST exists.public Aast getFromDepot(java.lang.String name)
null
if no such AST exists.name
- The name to use as a key for the depot lookup.null
if no such AST exists.public boolean isInDepot(java.lang.String name)
name
- The name to use as a key for the depot lookup.private void processReplacements(org.w3c.dom.Node tpl, java.util.Map replacements)
${key}
in any attribute value. Once the
current node's attributes are processed, all child nodes are visited
and this method is called recursively for each child.tpl
- The node whose attributes must be processed.replacements
- A collection of key/value pairs.private void zeroAstIds(org.w3c.dom.Element tpl)
tpl
- Java template XML element, whose descendant elements represent ASTs.