@@ -213,8 +213,40 @@ open class FileTreeNode() {
213213 }
214214 }
215215
216+ /* *
217+ * Resolve all file template file name in tree node.
218+ */
219+ fun resolveFileTemplate (context : VelocityContext ? = null) {
220+ val templates = getAllTemplateMap()
221+ val placeholders = getPlaceholderInherit() ? : return
222+ if (fileTemplates != null && templates.isNotEmpty()) {
223+ templates.forEach { (key, value) ->
224+ val realName = getRealNameInternal(context, key)
225+ val realValue = replacePlaceholder(context, value, placeholders, false )
226+ fileTemplates!! [realName] = realValue
227+ }
228+ }
229+ traversal({ it, _ ->
230+ it.resolveFileTemplate()
231+ })
232+ }
233+
234+ /* *
235+ * Return the most matching file template.
236+ * If the node has template, return it, otherwise return the template in parent node.
237+ */
216238 fun getTemplateFile (): String? {
217- return template ? : getFileTemplateInherit()?.get(name)
239+ if (template != null ) {
240+ return template
241+ }
242+ val tpl = getFileTemplateInherit() ? : return null
243+ val path = getPath()
244+ for (entry in tpl) {
245+ if (entry.key != name && path.endsWith(entry.key)) {
246+ return entry.value
247+ }
248+ }
249+ return tpl[name]
218250 }
219251
220252 fun setTemplate (name : String ) {
@@ -425,8 +457,12 @@ open class FileTreeNode() {
425457 *
426458 * @return The tree graph of node
427459 */
428- fun getTreeGraph (context : VelocityContext ? = null): String {
429- return getNodeGraph(context).toString()
460+ fun getTreeGraph (
461+ context : VelocityContext ? = null,
462+ templateFile : Boolean = false,
463+ resolveName : Boolean = true,
464+ ): String {
465+ return getNodeGraph(context, templateFile = templateFile, resolveName = resolveName).toString()
430466 }
431467
432468 /* *
@@ -457,7 +493,9 @@ open class FileTreeNode() {
457493 private fun getNodeGraph (
458494 context : VelocityContext ? ,
459495 head : Stack <String > = Stack (),
460- str : StringBuilder = StringBuilder ()
496+ str : StringBuilder = StringBuilder (),
497+ templateFile : Boolean = false,
498+ resolveName : Boolean = true,
461499 ): StringBuilder {
462500
463501 head.forEach {
@@ -475,9 +513,12 @@ open class FileTreeNode() {
475513 }
476514 }
477515 )
478- str.append(getRealName(context))
479- if (isDir) {
480- // str.append("\tplaceholder: ").append(placeholders)
516+ val n = if (resolveName) getRealName(context) else name
517+
518+ if (isDir || ! templateFile) {
519+ str.append(n)
520+ } else {
521+ str.append(" $n ${getTemplateFile()} " )
481522 }
482523 str.append(" \n " )
483524
@@ -490,7 +531,7 @@ open class FileTreeNode() {
490531 }
491532 )
492533 realChildren.forEach {
493- str.append(it.getNodeGraph(context, head))
534+ str.append(it.getNodeGraph(context, head, StringBuilder (), templateFile ))
494535 }
495536 head.pop()
496537 }
0 commit comments