📄 basicfunctions.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: BasicFunctions.java,v 1.13 2005/07/28 16:33:09 jmettraux Exp $ *///// BasicFunctions.java//// john.mettraux@openwfe.org//// generated with // jtmpl 1.1.01 2004/05/19 (john.mettraux@openwfe.org)//package openwfe.org.engine.impl.functions;import java.text.ParseException;import openwfe.org.time.Time;import openwfe.org.engine.workitem.InFlowWorkItem;import openwfe.org.engine.expressions.FlowExpression;/** * Functions are static methods accepting a workitem and an array of string * as arguments, and they return a String. The main functions for OpenWFE are * implemented here. * * <p><font size=2>CVS Info : * <br>$Author: jmettraux $ * <br>$Id: BasicFunctions.java,v 1.13 2005/07/28 16:33:09 jmettraux Exp $ </font> * * @author john.mettraux@openwfe.org */public abstract class BasicFunctions{ private final static org.apache.log4j.Logger log = org.apache.log4j.Logger .getLogger(BasicFunctions.class.getName()); // // CONSTANTS & co // // static methods (FUNCTIONS) /** * This function will return the current time as an ISO full date. */ public static String now (final FlowExpression fe, final InFlowWorkItem wi, final String[] args) { return Time.toIsoDate(); } /** * This function will return the current date as an ISO date. */ public static String today (final FlowExpression fe, final InFlowWorkItem wi, final String[] args) { final String sNow = Time.toIsoDate(); final int i = sNow.indexOf(" "); return sNow.substring(0, i); } /** * Returns the count of attributes (field) the workitem has. * fieldCount() and attributeCount() are identical functions. */ public static String attributeCount (final FlowExpression fe, final InFlowWorkItem wi, final String[] args) { return ""+wi.getAttributes().size(); } /** * Returns the count of attributes (field) the workitem has. * fieldCount() and attributeCount() are identical functions. */ public static String fieldCount (final FlowExpression fe, final InFlowWorkItem wi, final String[] args) { return ""+wi.getAttributes().size(); } /** * Returns the sum of all of the numeric values given as 'args'. */ public static String sum (final FlowExpression fe, final InFlowWorkItem wi, final String[] args) { if (args == null || args.length < 1) { log.debug("sum() no args provided, returning '0'"); return "0"; } float result = 0; for (int i=0; i<args.length; i++) { log.debug("sum() args["+i+"] is >"+args[i]+"<"); try { result += Float.parseFloat(args[i]); } catch (final NumberFormatException nfe) { // ignore } } return ""+result; } /** * This is an implementation of the lisp 'car' function, given a list as * argument, will return the first element (see also 'cdr'). * So car('a, b, d, e') will yield 'a'.<br> * If a second argument is passed (the first being the list), this second * argument will be used as the list separator (instead of the * default ',').<br> * The separator regex may also be set as a variable "__list_separator__". */ public static String car (final FlowExpression fe, final InFlowWorkItem wi, final String[] args) { final StringList sl = new StringList(fe, wi, args); return sl.car(); } /** * This is the implementation of the 'cdr' lisp function : it will return * the list passed as first arg without its head element. * The second arg may be an alternate list separator, or else the list * separator may be set at flow definition level by setting the * "__list_separator__" variable to the appropriate string value. */ public static String cdr (final FlowExpression fe, final InFlowWorkItem wi, final String[] args) { final StringList sl = new StringList(fe, wi, args); return sl.cdr(); } /** * This function follows car() and cdr(), it will return the nth element * of a list or '' if the list is empty or n is bigger than the list * size. * The first arg is the 'n', the second one is the list itself.<br> * So <pre>elt('2', 'a, b, c, d')</pre> will return 'c'. */ public static String elt (final FlowExpression fe, final InFlowWorkItem wi, final String[] args) { if (args == null || args.length < 2) return ""; final int index = Integer.parseInt(args[0]); final StringList sl = new StringList(fe, wi, args, 1, 2); // sepIndex is set to 2 return sl.elt(index); } /** * This function takes a list as input and returns a the same list but with * a different ordering. */ public static String shuffle (final FlowExpression fe, final InFlowWorkItem wi, final String[] args) { final StringList sl = new StringList(fe, wi, args); return sl.toString(); } /** * This function returns the given list reversed. */ public static String reverse (final FlowExpression fe, final InFlowWorkItem wi, final String[] args) { final StringList sl = new StringList(fe, wi, args); sl.reverse(); return sl.toString(); } /** * This function sorts the given list. */ public static String sort (final FlowExpression fe, final InFlowWorkItem wi, final String[] args) { final StringList sl = new StringList(fe, wi, args); sl.sort(); log.debug("sort() result is >"+sl.toString()+"<"); return sl.toString(); } /** * An alias for timeAdd. */ public static String tadd (final FlowExpression fe, final InFlowWorkItem wi, final String[] args) { return timeAdd(fe, wi, args); } /** * This function takes two arguments : an iso date and a time duration, * it then returns the date + the time duration. */ public static String timeAdd (final FlowExpression fe, final InFlowWorkItem wi, final String[] args) { if (args.length < 2) { throw new IllegalArgumentException ("timeAdd() awaits 2 args."); } final String sDate = args[0]; String sDuration = args[1]; int dmod = +1; if (sDuration.startsWith("-")) { sDuration = sDuration.substring(1); dmod = -1; } long date = 0; try { date = Time.fromIsoDate(sDate); } catch (final ParseException pe) { throw new IllegalArgumentException ("Failed to parse date '"+sDate+"'"); } final long duration = dmod * Time.parseTimeString(sDuration); return Time.toIsoDate(date + duration); } // // RANDOM functions // /** * Returns a random long */ public static String rlong (final FlowExpression fe, final InFlowWorkItem wi, final String[] args) { final java.util.Random gen = new java.util.Random(System.currentTimeMillis()); return ""+gen.nextLong(); } /** * Returns a random int */ public static String rint (final FlowExpression fe, final InFlowWorkItem wi, final String[] args) { final java.util.Random gen = new java.util.Random(System.currentTimeMillis()); return ""+gen.nextInt(); } /** * Returns a random double, so that 0.0 <= d < 1 */ public static String rdouble (final FlowExpression fe, final InFlowWorkItem wi, final String[] args) { final java.util.Random gen = new java.util.Random(System.currentTimeMillis()); return ""+gen.nextDouble(); } // // some debugging functions /** * Returns the args wrapped insed > < signs, thus "'a', 'b'" * returns "|a| |b|". */ public static String debugargs (final FlowExpression fe, final InFlowWorkItem wi, final String[] args) { final StringBuffer sb = new StringBuffer(); for (int i=0; i<args.length; i++) { sb .append("|") .append(args[i]) .append("| "); } final String result = sb.toString().trim(); log.debug("debugargs() result >"+result+"<"); return result; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -