📄 iterator.java
字号:
/* * Copyright (c) 2005, John Mettraux, OpenWFE.org * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * . Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * . Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * . Neither the name of the "OpenWFE" nor the names of its contributors may be * used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * $Id: Iterator.java,v 1.18 2005/07/26 20:54:47 jmettraux Exp $ *///// Iterator.java//// jmettraux@openwfe.org//// generated with // jtmpl 1.1.00 16.08.2003 John Mettraux (jmettraux@openwfe.org)//package openwfe.org.engine.expressions;import openwfe.org.OpenWfeException;import openwfe.org.engine.Definitions;import openwfe.org.engine.launch.Launcher;import openwfe.org.engine.workitem.InFlowWorkItem;import openwfe.org.engine.workitem.StringAttribute;import openwfe.org.engine.expressions.raw.RawExpression;import openwfe.org.engine.expressions.sync.SynchableExpression;/** * An iterator will iterate for IteratorExpression and * ConcurrentIteratorExpression, it does the job of keeping track * of the loops for them. * * <p><font size=2>CVS Info : * <br>$Author: jmettraux $ * <br>$Id: Iterator.java,v 1.18 2005/07/26 20:54:47 jmettraux Exp $ </font> * * @author jmettraux@openwfe.org */public class Iterator implements java.io.Serializable{ static final long serialVersionUID = -2532616767097005828L; private final static org.apache.log4j.Logger log = org.apache.log4j.Logger .getLogger(Iterator.class.getName()); // // CONSTANTS & co /** * You may determine on what to iterate with<br> * <ul> * <li>on-value</li> * <li>on-field-value</li> * <li>on-variable-value</li> * </ul> * <br> * That's why we have this prefix. */ public final static String VALUE_PREFIX = "on-"; /** * if no TO_FIELD and no TO_VARIABLE is provided, the current iteration * value will be stored in the variable named here. */ public final static String DEFAULT_TO_VARIABLE = "__iteration_value__"; /** * Use this parameter to set the regex used to split the value string into * the different values. * By default it is set to ", *" */ public final static String VALUE_SEPARATOR = "value-separator"; /** * As already stated, this default separator is ", *" */ public final static String DEFAULT_VALUE_SEPARATOR = ", *"; // // FIELDS private int iterationPosition = -1; private java.util.List iterationList = null; private FlowExpressionId templateId = null; // // CONSTRUCTORS /** * This constructor is necessary in order to xml-deserialize * the Iterator out of the pool (it is embedded in an IteratorExpression * though) */ public Iterator () { super(); } public Iterator (final FlowExpression iteratingExpression, final InFlowWorkItem wi) throws ApplyException { super(); prepareIterationList(iteratingExpression, wi); } // // BEAN METHODS public int getIterationPosition () { return this.iterationPosition; } public java.util.List getIterationList () { return this.iterationList; } public FlowExpressionId getTemplateId () { return this.templateId; } public void setIterationPosition (int i) { this.iterationPosition = i; } public void setIterationList (java.util.List l) { this.iterationList = l; } public void setTemplateId (FlowExpressionId fei) { this.templateId = fei; } // // METHODS /** * Returns the current value for the iteration. */ public Object currentIterationValue () { try { return this.iterationList.get(this.iterationPosition); } catch (ArrayIndexOutOfBoundsException e) { } return null; } /** * Will return true if a next iteration step is possible. */ public boolean hasNext () { if (this.iterationList == null || this.iterationList.size() < 1) return false; boolean result = (this.iterationPosition < this.iterationList.size()-1); log.debug("hasNext() ? "+result); return result; } /** * For the given iteratingExpression and workitem, will step to * the next iteration. */ public void next (final OneChildExpression iteratingExpression, final InFlowWorkItem wi) throws OpenWfeException { if (this.templateId == null) this.templateId = iteratingExpression.getChildExpressionId(); iteratingExpression.tag(wi); log.debug("next() tagged workitem"); //final String sIteration = ""+this.iterationPosition+"."; this.iterationPosition++; log.debug("next() iterationPosition : "+this.iterationPosition); log.debug("next() currentIterationValue >"+currentIterationValue()+"<"); // // launch scope try { // // prepare workitem (set field if needed) // ValueUtils.determineAndSetTarget ("to-", // 'to-field' or 'to-variable' iteratingExpression, wi, currentIterationValue()); final Launcher launcher = Definitions .getLauncher(iteratingExpression.context()); final FlowExpressionId subRootExpressionId = launcher.launchSub (wi, iteratingExpression.getId(), iteratingExpression.getChildExpressionId()); // // prepare child expression (set variable if needed) // ValueUtils.determineAndSetTarget ("to-", // 'to-field' or 'to-variable' iteratingExpression, subRootExpressionId, wi, currentIterationValue()); /* if (iteratingExpression instanceof SynchableExpression) // // in the case of a synchable expression, this iterator // takes care of registering the scope root expression // as a (awaited) child { ((SynchableExpression)iteratingExpression) .getSyncExpression().addChild(subRootExpressionId); log.debug ("next() added child to sync expression : "+ subRootExpressionId); } */ } catch (OpenWfeException owe) { throw new ApplyException ("Failed to apply next scope", owe); } // // if last iteration, remove template if ( ! this.hasNext()) { iteratingExpression.getExpressionPool() .removeExpression(this.templateId); log.debug("next() removed template "+this.templateId); } } // // other METHODS protected void prepareIterationList (final FlowExpression iteratingExpression, final InFlowWorkItem wi) throws ApplyException { // // determine on what we will iterate Object val = null; try { val = ValueUtils .determineValue(VALUE_PREFIX, iteratingExpression, wi); } catch (ValueException ve) { throw new ApplyException ("Failed to determine value to iterate on", ve); } // // determine value separator regex String separatorRegex = (String)iteratingExpression.getAttributes() .get(VALUE_SEPARATOR); if (separatorRegex == null) separatorRegex = DEFAULT_VALUE_SEPARATOR; // // turn the value into a list if (val == null) { this.iterationList = null; } else if (val instanceof java.util.List) { this.iterationList = (java.util.List)val; } else if (val instanceof String || val instanceof StringAttribute) { String[] ss = val.toString().split(separatorRegex); this.iterationList = new java.util.ArrayList(ss.length); for (int i=0; i<ss.length; i++) this.iterationList.add(ss[i].trim()); } else { throw new ApplyException ("value of class '"+val.getClass().getName()+ "' is not suitable for iteration"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -