Java Reference
In-Depth Information
Suppose we wanted to simply output a comma-separated list of Topic names
instead of the details of each Topic element. We could use the SEPARATOR fea-
ture of «EXPAND» to accomplish this, as shown in the following example. A nice
feature of SEPARATOR is that it places a comma (or specified separator) between
each element, with no trailing separator at the end of the list. Note the judicious
use of the hyphenated closing guillemet ( ), to avoid whitespace in the output.
« DEFINE Main FOR Map»
« FILE "topics.csv"-»
« EXPAND topicList FOREACH elements.typeSelect(Topic) SEPARATOR ","»
« ENDFILE »
« ENDDEFINE »
« DEFINE topicList FOR Topic-»
« this .name-»
« ENDDEFINE »
This is a sample output of the Topic list:
A Topic,A Subtopic,Another Topic,Another Subtopic,A SubSubtopic
Polymorphism
Xpand templates include polymorphism support for metaclasses declared in
«DEFINE» blocks. If a metaclass has two subclasses and each has its own
«DEFINE» block, template execution invokes the proper «DEFINE» to match the
instance.
Let's refactor our mindmap2csv template to take advantage of this capabil-
ity. We can do this in two steps, just to illustrate more completely. First, we mod-
ify the template as shown here:
« IMPORT mindmap»
« DEFINE Main FOR Map»
« EXPAND csvFile(title) FOR elements.typeSelect(Topic)»
« EXPAND csvFile(title) FOR elements.typeSelect(Relationship)»
« ENDDEFINE »
« DEFINE csvFile(String title) FOR List[mindmap::MapElement]»
« ENDDEFINE »
« DEFINE csvFile(String title) FOR List[mindmap::Topic]»
« FILE title + "-topics.csv"-»
« FOREACH this AS topic-»
«topic.name»,«topic.start»,«topic.end»
Search WWH ::




Custom Search