The following example is from the real templates included in DocFlex/Javadoc.
The task was the following.
For an element representing a Java interface, we need to obtain all Java classes implementing that interface.
The list of interfaces implemented by a class includes:
Using an element map provides a necessary solution. Here's how this can be done.
The element map is created with the following call:
prepareElementMap (
"all-known-implementing-classes",
findElementsByLPath (mainContext.rootElement,
"classes^::ClassDoc[!getAttrBooleanValue('isInterface')]"
),
FlexQuery (getElementIds (
findElementsByLRules (
Array (
LocationRule ("* -> interfaces^::ClassDoc", true),
LocationRule ("* -> superclass^::ClassDoc", true)
),
"ClassDoc",
FlexQuery (getAttrBooleanValue("isInterface"))
)
)),
)
Here's the explanation for all parameters:
(1) |
This specifies the map identifier. It's just a string
since that map is the only one for the whole generation session.
|
(2) |
This actually provides an enumeration of all classes in the project.
|
(3) |
This is a keys subquery. It returns an array of the unique identifiers
of all interfaces implemented by a given class. Those identifiers will be the
hash keys associated with the given class element in the map.
|
The prepareElementMap()
function call shown above may be specified
at any location within templates before the point where the element map may be actually used.
Best of all, probably, this may be somewhere within the initialization expression
of the main template
(see Template Properties Dialog | Processing | Init/Finish | Template Init Expression).
In any case, the element map will be created only once.
See prepareElementMap()
function description for details.
Now, let's look how that element map may be used.
Let's suppose, the interested interface, for which we want to obtain the list of
all classes implementing it, is presented by the interface
element (e.g. this may be a local variable or the generator's current element).
Then, the following call will return an enumeration of the classes we need:
The first parameter is the element map identifier. The second parameter specifies the hash key. According to how that particular element map was created, the hash keys are the unique identifiers of the elements representing the interfaces.findElementsByKey ( "all-known-implementing-classes", interface.id )
See Also:
GOMElement.id, findElementsByLPath(), findElementsByLRules(), FlexQuery()