Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014 Attila Szegedi, Daniel Dekany, Jonathan Revusky
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   * http://www.apache.org/licenses/LICENSE-2.0
   * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package freemarker.core;
 
 
A #list or #foreach element.
 
 final class IteratorBlock extends TemplateElement {
 
     private final Expression listExp;
     private final String loopVarName;
     private final boolean isForEach;

    

Parameters:
listExp a variable referring to a sequence or collection ("the list" from now on)
loopVarName The name of the variable that will hold the value of the current item when looping through the list.
nestedBlock The nested content to execute if the list wasn't empty; can't be null. If the loop variable was specified in the start tag, this is also what we will iterator over.
 
     IteratorBlock(Expression listExp,
                   String loopVarName,
                   TemplateElement nestedBlock,
                   boolean isForEach
     {
         this. = listExp;
         this. = loopVarName;
         setNestedBlock(nestedBlock);
         this. = isForEach;
     }
 
     void accept(Environment envthrows TemplateExceptionIOException {
         acceptWithResult(env);
     }
     
     boolean acceptWithResult(Environment envthrows TemplateExceptionIOException 
     {
         TemplateModel listValue = .eval(env);
         if (listValue == null) {
             if (env.isClassicCompatible()) {
                 listValue = .
             } else {
                 .assertNonNull(nullenv);
             }
         }
 
         return env.visitIteratorBlock(new IterationContext(listValue));
     }

    

Parameters:
loopVariableName Then name of the loop variable whose context we are looking for, or null if we simply look for the innermost context.
Returns:
The matching context or null if no such context exists.
 
     static IterationContext findEnclosingIterationContext(Environment envString loopVariableName)
             throws _MiscTemplateException {
         ArrayList ctxStack = env.getLocalContextStack();
         if (ctxStack != null) {
             for (int i = ctxStack.size() - 1; i >= 0; i--) {
                 Object ctx = ctxStack.get(i);
                 if (ctx instanceof IterationContext
                         && (loopVariableName == null
                             || loopVariableName.equals(((IterationContextctx).getLoopVariableName()))) {
                     return (IterationContextctx;
                 }
             }
         }
        return null;
    }
    
    protected String dump(boolean canonical) {
        StringBuffer buf = new StringBuffer();
        if (canonicalbuf.append('<');
        buf.append(getNodeTypeSymbol());
        buf.append(' ');
        if () {
            buf.append(_CoreStringUtils.toFTLTopLevelIdentifierReference());
            buf.append(" in ");
            buf.append(.getCanonicalForm());
        }
        else {
            buf.append(.getCanonicalForm());
            if ( != null) {
                buf.append(" as ");
                buf.append(_CoreStringUtils.toFTLTopLevelIdentifierReference());
            }
        }
        if (canonical) {
            buf.append(">");
            if (getNestedBlock() != null) {
                buf.append(getNestedBlock().getCanonicalForm());
            }
            if (!(getParentElement() instanceof ListElseContainer)) {
                buf.append("</");
                buf.append(getNodeTypeSymbol());
                buf.append('>');
            }
        }
        return buf.toString();
    }
    
    int getParameterCount() {
        return  != null ? 2 : 1;
    }
    Object getParameterValue(int idx) {
        switch (idx) {
        case 0:
            return ;
        case 1:
            if ( == nullthrow new IndexOutOfBoundsException();
            return ;
        defaultthrow new IndexOutOfBoundsException();
        }
    }
    ParameterRole getParameterRole(int idx) {
        switch (idx) {
        case 0:
            return .;
        case 1:
            if ( == nullthrow new IndexOutOfBoundsException();
            return .;
        defaultthrow new IndexOutOfBoundsException();
        }
    }    
    
        return  ? "#foreach" : "#list";
    }
    boolean isNestedBlockRepeater() {
        return  != null;
    }

    
Holds the context of a #list (or #forEach) directive.
    class IterationContext implements LocalContext {
        
        private static final String LOOP_STATE_HAS_NEXT = "_has_next"// lenght: 9
        private static final String LOOP_STATE_INDEX = "_index"// length 6
        
        private boolean hasNext;
        private TemplateModel loopVar;
        private int index;
        private boolean alreadyEntered;
        private Collection localVarNames = null;
        
        
If the #list has nested #items, it's null outside the #items.
        private String loopVarName;
        
        private final TemplateModel listValue;
        
        public IterationContext(TemplateModel listValueString loopVariableName) {
            this. = listValue;
            this. = loopVariableName;
        }
        
        boolean accept(Environment envthrows TemplateExceptionIOException {
            return executeNestedBlock(envgetNestedBlock());
        }
        void loopForItemsElement(Environment envTemplateElement nestedBlockString loopVarName)
                    throws NonSequenceOrCollectionExceptionTemplateModelExceptionInvalidReferenceException,
                    TemplateExceptionIOException {
            try {
                if () {
                    throw new _MiscTemplateException(env,
                            "The #items directive was already entered earlier for this listing.");
                }
                 = true;
                this. = loopVarName;
                executeNestedBlock(envnestedBlock);
            } finally {
                this. = null;
            }
        }

        
Executes the given block for the listValue: if loopVarName is non-null, then for each list item once, otherwise once if listValue isn't empty.
        private boolean executeNestedBlock(Environment envTemplateElement nestedBlock)
                throws TemplateModelExceptionTemplateExceptionIOException,
            return executeNestedBlockInner(envnestedBlock);
        }
        private boolean executeNestedBlockInner(Environment envTemplateElement nestedBlock)
                InvalidReferenceException {
            final boolean listNotEmpty;
            if ( instanceof TemplateCollectionModel) {
                final TemplateCollectionModel collModel = (TemplateCollectionModel;
                final TemplateModelIterator iterModel
                        =  == null ? collModel.iterator() : ;
                 = iterModel.hasNext();
                listNotEmpty = ;
                if (listNotEmpty) {
                    if ( != null) {
                        try {
                            while () {
                                 = iterModel.next();
                                 = iterModel.hasNext();
                                if (nestedBlock != null) {
                                    env.visitByHiddingParent(nestedBlock);
                                }
                                ++;
                            }
                        } catch (BreakInstruction.Break br) {
                            // Silently exit loop
                        }
                         = null;
                    } else {
                        // We must reuse this later, because TemplateCollectionModel-s that wrap an Iterator only
                        // allow one iterator() call.
                         = iterModel;
                        if (nestedBlock != null) {
                            env.visitByHiddingParent(nestedBlock);
                        }
                    }
                }
            } else if ( instanceof TemplateSequenceModel) {
                final TemplateSequenceModel seqModel = (TemplateSequenceModel;
                final int size = seqModel.size();
                listNotEmpty = size != 0;
                if (listNotEmpty) {
                    if ( != null) {
                        try {
                            for ( = 0;  < size++) {
                                 = seqModel.get();
                                 = (size >  + 1);
                                if (nestedBlock != null) {
                                    env.visitByHiddingParent(nestedBlock);
                                }
                            }
                        } catch (BreakInstruction.Break br) {
                            // Silently exit loop
                        }
                    } else {
                        if (nestedBlock != null) {
                            env.visitByHiddingParent(nestedBlock);
                        }
                    }
                }
            } else if (env.isClassicCompatible()) {
                listNotEmpty = true;
                if ( != null) {
                     = ;
                     = false;
                }
                try {
                    if (nestedBlock != null) {
                        env.visitByHiddingParent(nestedBlock);
                    }
                } catch (BreakInstruction.Break br) {
                    // Silently exit "loop"
                }
            } else {
                throw new NonSequenceOrCollectionException(
                        env);
            }
            
            return listNotEmpty;
        }
        String getLoopVariableName() {
            return this.;
        }
        public TemplateModel getLocalVariable(String name) {
            String loopVariableName = this.;
            if (loopVariableName != null && name.startsWith(loopVariableName)) {
                switch(name.length() - loopVariableName.length()) {
                    case 0: 
                        return ;
                    case 6: 
                        if(name.endsWith()) {
                            return new SimpleNumber();
                        }
                        break;
                    case 9: 
                        if(name.endsWith()) {
                            return  ? . : .;
                        }
                        break;
                }
            }
            return null;
        }
        
        public Collection getLocalVariableNames() {
            String loopVariableName = this.;
            if (loopVariableName != null) {
                if ( == null) {
                     = new ArrayList(3);
                    .add(loopVariableName);
                    .add(loopVariableName + );
                    .add(loopVariableName + );
                }
                return ;
            } else {
                return .;
            }
        }
        boolean hasNext() {
            return ;
        }
        
        int getIndex() {
            return ;
        }
        
    }
    
New to GrepCode? Check out our FAQ X