Changeset 834


Ignore:
Timestamp:
2008-11-26 13:13:36 (4 years ago)
Author:
bruno
Message:

Template engine: I found that the code for the OutputBlock?, which is one of the most basic template blocks, was getting a bit big with all the template inheritance things in it. Therefore, splitted that of in another class, InheritBlock?. Before, it was a bit like two different blocks were implemented in one class.

Location:
trunk/universe/kauri-template/src/main/java/org/kauriproject/template
Files:
1 added
3 edited

Legend:

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

    r832 r834  
    283283            } 
    284284 
     285            int blocksPushed = 1; 
    285286            if (silencedLevel > 0) { 
    286287                silencedLevel++; 
     288                blocksPushed = 0; 
     289            } else if (root && attributes.getValue(TemplateBuilder.NAMESPACE_KTL, InheritBlock.INHERIT) != null) { 
     290                InheritBlock inherit = new InheritBlock(elFacade, locator, new SaxElement(uri, 
     291                                localName, name, attributes), templateService); 
     292                pushBlock(inherit); 
    287293            } else { 
    288294                // KTL directives 
    289                 int blocksPushed = 1; 
    290295                if (uri != null && uri.equals(NAMESPACE_KTL)) { 
    291296                    Directive directive = Directive.fromString(localName); 
     
    402407                        // TODO: maybe we should provide an errorblock in this case ? 
    403408                        OutputBlock outblock = new OutputBlock(elFacade, locator, new SaxElement("", 
    404                                 localName, localName, attributes), templateService, root); 
     409                                localName, localName, attributes), root); 
    405410                        pushBlock(outblock); 
    406411                    } 
     
    410415                } else if (outputAllowed()) { // "ordinary" start elements 
    411416                    OutputBlock outblock = new OutputBlock(elFacade, locator, new SaxElement(uri, localName, 
    412                         name, attributes), templateService, root); 
     417                        name, attributes), root); 
    413418                    pushBlock(outblock); 
    414419                } else { 
    415420                    silencedLevel++; 
    416421                } 
    417                 if (silencedLevel == 0) 
    418                     pushCountStack.push(blocksPushed); 
    419             } 
    420  
     422            } 
     423            if (silencedLevel == 0) 
     424                pushCountStack.push(blocksPushed); 
    421425        } 
    422426 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/OutputBlock.java

    r832 r834  
    1717 
    1818import java.util.ArrayList; 
    19 import java.util.LinkedList; 
    2019import java.util.List; 
    21 import java.util.Map; 
    2220 
    2321import org.kauriproject.template.el.ELFacade; 
     
    3836 
    3937    protected ELFacade elFacade; 
    40     protected TemplateService templateService; 
    4138    protected boolean root; 
    4239 
    43     public OutputBlock(ELFacade elFacade, Locator locator, SaxElement saxElement, 
    44             TemplateService templateService, boolean root) { 
     40    public OutputBlock(ELFacade elFacade, Locator locator, SaxElement saxElement, boolean root) { 
    4541        super(locator, saxElement); 
    4642        this.elFacade = elFacade; 
    4743        this.attributes = saxElement.getAttributes(); 
    4844        this.startStep = new StartStep(this, locator); 
    49         this.templateService = templateService; 
    5045        this.root = root; 
    5146    } 
     
    8277        public Step executeAndProceed(ExecutionContext context, TemplateResult result) throws SAXException { 
    8378            AttributesImpl atts = new AttributesImpl(attributes); 
     79            if (context.isSilencing()) { 
     80                return getEndStep().getCompiledNext(); 
     81            } 
     82 
    8483            Expression elExpression; 
    8584            for (int i = 0; i < attributes.getLength(); i++) { 
     
    8887            } 
    8988 
    90             if (root) { 
    91                 // check inheritance 
    92                 String sourceLocation = atts.getValue(TemplateBuilder.NAMESPACE_KTL, INHERIT); 
    93                 // TODO: check all attributes in ktl namespace, log warning if not supported 
    94                 if (sourceLocation != null) { 
    95                     sourceLocation = context.getSourceResolver().toAbsolutePath(sourceLocation, context.getBaseUri()); 
    96                     // inheritance => lookup base 
    97                     CompiledTemplate baseTemplate; 
    98                     try { 
    99                         // ask service to get compiled template for sourcelocation 
    100                         // this may trigger a build of the base template 
    101                         baseTemplate = templateService.buildTemplate(sourceLocation, context); 
    102                     } catch (Exception ex) { 
    103                         throw new RuntimeException("Error generating base template, location " 
    104                                 + getLocation() + " : " + ex); 
    105                     } 
    106  
    107                     // merge inheritance mappings 
    108                     Map<String, List<InheritanceBlock>> inheritanceChain = context.getInheritanceChain(); 
    109                     Map<String, InheritanceBlock> baseRegistry = baseTemplate.getInheritanceRegistry(); 
    110                     for (String block : baseRegistry.keySet()) { 
    111                         if (!inheritanceChain.containsKey(block)) { 
    112                             List<InheritanceBlock> chain = new LinkedList<InheritanceBlock>(); 
    113                             chain.add(baseRegistry.get(block)); 
    114                             inheritanceChain.put(block, chain); 
    115                         } else { 
    116                             inheritanceChain.get(block).add(baseRegistry.get(block)); 
    117                         } 
    118                     } 
    119  
    120                     context.inheritInc(); 
    121                     context.getExecutor().execute(baseTemplate, null, context, result); 
    122                     context.inheritDecr(); 
    123                      
    124                     return getEndStep().getCompiledNext(); 
    125                 } 
    126             } 
    127  
    128             if(context.isSilencing()) { 
    129                 return getEndStep().getCompiledNext(); 
    130             } 
    131              
    13289            SaxElement el = getSaxElement(); 
    13390            result.startElement(el.getUri(), el.getLocalName(), el.getName(), atts); 
     
    13693                // execute the extend blocks before proceding to next 
    13794                for (ExtendBlock extendBlock : context.getExtendList()) { 
    138                     executeBlock(extendBlock, context, result); 
     95                    context.getExecutor().execute(extendBlock.getStartStep(), extendBlock.getEndStep().getCompiledNext(), context, result); 
    13996                } 
    14097            } 
     
    14299            return super.executeAndProceed(context, result); 
    143100        } 
    144  
    145         private void executeBlock(TemplateBlock block, ExecutionContext context, TemplateResult result) 
    146                 throws SAXException { 
    147             Step step = block.getStartStep(); 
    148             Step end = block.getEndStep(); 
    149             while (step != end) { 
    150                 step = step.executeAndProceed(context, result); 
    151             } 
    152             end.executeAndProceed(context, result); 
    153         } 
    154  
    155101    } 
    156102 
  • trunk/universe/kauri-template/src/main/java/org/kauriproject/template/TemplateExecutor.java

    r816 r834  
    44 
    55public interface TemplateExecutor { 
     6    /** 
     7     * Executes steps, starting from the startStep up to, but excluding, the endStep. 
     8     */ 
    69    void execute(Step startStep, Step endStep, ExecutionContext context, TemplateResult result) throws SAXException; 
    710} 
Note: See TracChangeset for help on using the changeset viewer.