Changeset 693


Ignore:
Timestamp:
2008-10-13 08:59:27 (5 years ago)
Author:
jgou
Message:

KTL: improving template inheritance: allow calling the overridden block from the template (calling 'super')

Location:
trunk/universe/kauri-template/src
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/DefaultTemplateBuilder.java

    r686 r693  
    163163        last.setCompiledNext(endStep); 
    164164        last = endStep; 
     165    } 
     166 
     167    protected TemplateBlock findParentOnStack(Class<? extends TemplateBlock> blockClass) { 
     168        boolean result = false; 
     169        int index = stack.size() - 1; 
     170        TemplateBlock block = null; 
     171        while (!result && index >= 0) { 
     172            block = stack.elementAt(index); 
     173            if (block.getClass().isAssignableFrom(blockClass)) { 
     174                result = true; 
     175            } 
     176            index--; 
     177        } 
     178        if (!result) { 
     179            return null; 
     180        } 
     181        return block; 
    165182    } 
    166183 
     
    329346                    } 
    330347                    inheritanceRegistry.put(inheritblock.getName(), inheritblock); 
     348                } else if (Directive.SUPERBLOCK == directive) { 
     349                    InheritanceBlock parent = (InheritanceBlock) findParentOnStack(InheritanceBlock.class); 
     350                    SuperBlock superblock = new SuperBlock(locator, new SaxElement(uri, localName, name, 
     351                            attributes), parent); 
     352                    if (parent == null) { 
     353                        throw new RuntimeException( 
     354                                "A call to a superBlock is only possible from within an overriding block, location " 
     355                                        + superblock.getStartStep().getLocation()); 
     356                    } 
     357                    pushBlock(superblock); 
    331358                } else { 
    332359                    log.warn("Unsupported KTL directive: " + localName); 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/Directive.java

    r686 r693  
    9999     * block name="blockid" 
    100100     */ 
    101     BLOCK("block"); 
     101    BLOCK("block"), 
     102    /** 
     103     * directive (in block construct) to call the overriden block 
     104     */ 
     105    SUPERBLOCK("superBlock"); 
    102106 
    103107    private final String tagName; 
     
    144148        else if (name.equals(BLOCK.tagName)) 
    145149            return BLOCK; 
     150        else if (name.equals(SUPERBLOCK.tagName)) 
     151            return SUPERBLOCK; 
    146152        else 
    147153            return null;// throw RuntimeException ? 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/ExecutionContext.java

    r686 r693  
    1919import java.util.List; 
    2020import java.util.Map; 
     21import java.util.Stack; 
    2122 
    2223import org.kauriproject.template.el.FunctionRegistry; 
     
    3839    private Map<String, IncludeBlock> includeRegistry; 
    3940    private Map<String, List<InheritanceBlock>> inheritanceChain; 
     41    private Stack<SuperBlock> superBlockStack; 
    4042 
    4143    private boolean inherited = false; 
     
    5456        this.includeRegistry = new HashMap<String, IncludeBlock>(); 
    5557        this.inheritanceChain = new HashMap<String, List<InheritanceBlock>>(); 
     58        this.superBlockStack = new Stack<SuperBlock>(); 
    5659    } 
    5760 
     
    104107    } 
    105108 
     109    public Stack<SuperBlock> getSuperBlockStack() { 
     110        return superBlockStack; 
     111    } 
     112 
    106113} 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/InheritanceBlock.java

    r686 r693  
    8181            // lookup base block and execute baseblock.endstep.next 
    8282            if (context.isInherited()) { 
    83                 // InheritanceBlock block = context.getBaseTemplate().getInheritanceRegistry().get(getName()); 
     83                // check if this block is executed as a superblock and return to the calling block 
     84                if (!context.getSuperBlockStack().isEmpty()) { 
     85                    SuperBlock superBlock = context.getSuperBlockStack().peek(); 
     86                    return superBlock.getEndStep(); 
     87                } 
     88                // otherwise: lookup highest overridden block in chain 
    8489                List<InheritanceBlock> chain = context.getInheritanceChain().get(getName()); 
    8590                if (chain != null && chain.size() > 0) { 
  • trunk/universe/kauri-template/src/test/resources/org/kauriproject/template/inherit_multiple.xml

    r686 r693  
    66    <body> 
    77        <ktl:block name="block1"> 
    8             <p>first block - 2nd level child</p> 
     8            <div> 
     9                <ktl:superBlock /> 
     10                <p>first block - 2nd level child</p> 
     11                <ktl:superBlock /> 
     12            </div> 
    913        </ktl:block> 
    1014        <ktl:block name="block2"> 
     
    1216        </ktl:block> 
    1317        <ktl:block name="child"> 
    14       <p>overriding block defined in parent</p> 
    15     </ktl:block> 
    16     <p>blablabla</p> 
     18            <p>overriding block defined in parent</p> 
     19        </ktl:block> 
     20        <p>blablabla</p> 
    1721    </body> 
    1822</html> 
  • trunk/universe/kauri-template/src/test/resources/org/kauriproject/template/inherit_multiple_result.xml

    r686 r693  
    66    <body> 
    77        <p>leading stuff from base</p> 
     8        <div> 
     9        <p>first block - child</p> 
    810        <p>first block - 2nd level child</p> 
     11        <p>first block - child</p> 
     12        </div> 
    913        <p>second block - 2nd level child</p> 
    1014        <div> 
Note: See TracChangeset for help on using the changeset viewer.