From 33f34b6346bc6ef0304e43fd402993e6e7cc403c Mon Sep 17 00:00:00 2001 From: zapata Date: Sat, 5 Oct 2002 01:45:15 +0000 Subject: [PATCH] - general maintenance - added a Loop ProducerNode for loop constructions - added a routine to work with sublists in producers --- source/config.properties-dist | 14 ++++ .../producer/EvaluatedAssignmentProducerNode.java | 2 +- .../producer/ExpandedAssignmentProducerNode.java | 2 +- source/mir/producer/LoopProducerNode.java | 69 +++++++++++++++++ .../reader/DefaultProducerNodeBuilders.java | 52 ++++++++++++- .../mir/producer/reader/ProducerConfigReader.java | 28 +++++-- .../mir/producer/reader/ScriptedProducerNode.java | 30 ++++++-- .../reader/ScriptedProducerNodeDefinition.java | 34 +++++++-- .../producer/reader/ScriptedProducerNodeTool.java | 9 +-- source/mir/util/GeneratorListFunctions.java | 88 ++++++++++++++++++++++ source/mir/util/GeneratorStringFunctions.java | 21 +----- source/mir/util/ParameterExpander.java | 7 +- source/mir/util/SimpleParser.java | 4 - source/mir/util/StringRoutines.java | 15 ++++ source/mir/util/SubsetIterator.java | 83 ++++++++++++++++++++ .../basic/MirBasicProducerAssistantLocalizer.java | 2 + .../localizer/basic/MirBasicProducerLocalizer.java | 1 + 17 files changed, 406 insertions(+), 55 deletions(-) create mode 100755 source/mir/producer/LoopProducerNode.java create mode 100755 source/mir/util/GeneratorListFunctions.java create mode 100755 source/mir/util/SubsetIterator.java diff --git a/source/config.properties-dist b/source/config.properties-dist index 8f6bdaef..e69e653f 100755 --- a/source/config.properties-dist +++ b/source/config.properties-dist @@ -355,3 +355,17 @@ ServletModule.Language.Logfile=log/mir.log ServletModule.Language.ConfirmTemplate=admin/confirm.template ServletModule.Language.ObjektTemplate=admin/language.template ServletModule.Language.ListTemplate=admin/languagelist.template + +# +# producer-related configs below +# + +Producer.ExtLinkName=extlink.gif +Producer.IntLinkName=intlink.gif +Producer.MailLinkName=maillink.gif +Producer.Logfile=log/producer.log + +Producer.Content.Template=producer/content.template +Producer.Content.Batchsize=10 + + diff --git a/source/mir/producer/EvaluatedAssignmentProducerNode.java b/source/mir/producer/EvaluatedAssignmentProducerNode.java index 3d0c8d56..a9fb3885 100755 --- a/source/mir/producer/EvaluatedAssignmentProducerNode.java +++ b/source/mir/producer/EvaluatedAssignmentProducerNode.java @@ -49,7 +49,7 @@ public class EvaluatedAssignmentProducerNode implements ProducerNode { try { ParameterExpander.setValueForKey( aValueMap, - key, + ParameterExpander.expandExpression( aValueMap, key ), ParameterExpander.evaluateExpression( aValueMap, value )); } diff --git a/source/mir/producer/ExpandedAssignmentProducerNode.java b/source/mir/producer/ExpandedAssignmentProducerNode.java index dc3e1127..fee5b005 100755 --- a/source/mir/producer/ExpandedAssignmentProducerNode.java +++ b/source/mir/producer/ExpandedAssignmentProducerNode.java @@ -49,7 +49,7 @@ public class ExpandedAssignmentProducerNode implements ProducerNode { try { ParameterExpander.setValueForKey( aValueMap, - key, + ParameterExpander.expandExpression( aValueMap, key ), ParameterExpander.expandExpression( aValueMap, value )); } diff --git a/source/mir/producer/LoopProducerNode.java b/source/mir/producer/LoopProducerNode.java new file mode 100755 index 00000000..2e40b89b --- /dev/null +++ b/source/mir/producer/LoopProducerNode.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2001, 2002 The Mir-coders group + * + * This file is part of Mir. + * + * Mir is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Mir is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Mir; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * In addition, as a special exception, The Mir-coders gives permission to link + * the code of this program with the com.oreilly.servlet library, any library + * licensed under the Apache Software License, The Sun (tm) Java Advanced + * Imaging library (JAI), The Sun JIMI library (or with modified versions of + * the above that use the same license as the above), and distribute linked + * combinations including the two. You must obey the GNU General Public + * License in all respects for all of the code used other than the above + * mentioned libraries. If you modify this file, you may extend this exception + * to your version of the file, but you are not obligated to do so. If you do + * not wish to do so, delete this exception statement from your version. + */ + +package mir.producer; + +import java.util.*; +import java.io.*; +import mir.util.*; + + +public class LoopProducerNode extends ProducerNodeDecorator { + private String condition; + private String limit; + + public LoopProducerNode( + String aCondition, + String aLimit, + ProducerNode aSubNode) { + super(aSubNode); + + limit = aLimit; + condition = aCondition; + } + + public void produce(Map aValueMap, String aVerb, PrintWriter aLogger) throws ProducerFailure { + int loopNr; + int maxNrLoops; + + try { + loopNr = 0; + maxNrLoops = ParameterExpander.evaluateIntegerExpressionWithDefault(aValueMap, limit, 1000); + while (ParameterExpander.evaluateBooleanExpression(aValueMap, condition) && (loopNr default value + private Map integerParameters; // name -> default value + private Map stringParameters; // name -> default value private Set nodeParameters; private ProducerNode body; private String name; @@ -46,13 +47,18 @@ public class ScriptedProducerNodeDefinition { public ScriptedProducerNodeDefinition(String aName) { name = aName; - parameters = new HashMap(); + integerParameters = new HashMap(); + stringParameters = new HashMap(); nodeParameters = new HashSet(); body = new CompositeProducerNode(); } - public void addParameter(String aName, String aDefaultValue) { - parameters.put(aName, aDefaultValue); + public void addStringParameter(String aName, String aDefaultValue) { + stringParameters.put(aName, aDefaultValue); + } + + public void addIntegerParameter(String aName, String aDefaultValue) { + integerParameters.put(aName, aDefaultValue); } public void addNodeParameter(String aName) { @@ -63,8 +69,12 @@ public class ScriptedProducerNodeDefinition { body = aBody; } - protected Map getParameters() { - return parameters; + protected Map getStringParameters() { + return stringParameters; + } + + protected Map getIntegerParameters() { + return integerParameters; } protected Set getNodeParameters() { @@ -89,7 +99,17 @@ public class ScriptedProducerNodeDefinition { public Set getAttributesSelection(boolean aRequired) { Set result = new HashSet(); - Iterator i = parameters.entrySet().iterator(); + Iterator i = stringParameters.entrySet().iterator(); + + while (i.hasNext()) { + Map.Entry entry = (Map.Entry) i.next(); + + if ((entry.getValue() == null) == aRequired ) { + result.add(entry.getKey()); + } + } + + i = integerParameters.entrySet().iterator(); while (i.hasNext()) { Map.Entry entry = (Map.Entry) i.next(); diff --git a/source/mir/producer/reader/ScriptedProducerNodeTool.java b/source/mir/producer/reader/ScriptedProducerNodeTool.java index 56710b46..e8a2251f 100755 --- a/source/mir/producer/reader/ScriptedProducerNodeTool.java +++ b/source/mir/producer/reader/ScriptedProducerNodeTool.java @@ -60,17 +60,14 @@ public class ScriptedProducerNodeTool { runtimeStack.pop(); } - public static Map saveMapValues(Map aMap, Set aKeys) { - Map result = new HashMap(); + public static void saveMapValues(Map aDestination, Map aSource, Set aKeys) { Iterator i = aKeys.iterator(); while (i.hasNext()) { Object key = i.next(); - if (aMap.containsKey(key)) - result.put(key, aMap.get(key)); + if (aSource.containsKey(key)) + aDestination.put(key, aSource.get(key)); } - - return result; } public static void restoreMapValues(Map aMap, Set aKeys, Map aSavedValues) { diff --git a/source/mir/util/GeneratorListFunctions.java b/source/mir/util/GeneratorListFunctions.java new file mode 100755 index 00000000..19726157 --- /dev/null +++ b/source/mir/util/GeneratorListFunctions.java @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2001, 2002 The Mir-coders group + * + * This file is part of Mir. + * + * Mir is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Mir is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Mir; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * In addition, as a special exception, The Mir-coders gives permission to link + * the code of this program with the com.oreilly.servlet library, any library + * licensed under the Apache Software License, The Sun (tm) Java Advanced + * Imaging library (JAI), The Sun JIMI library (or with modified versions of + * the above that use the same license as the above), and distribute linked + * combinations including the two. You must obey the GNU General Public + * License in all respects for all of the code used other than the above + * mentioned libraries. If you modify this file, you may extend this exception + * to your version of the file, but you are not obligated to do so. If you do + * not wish to do so, delete this exception statement from your version. + */ + +package mir.util; + +import java.util.*; +import java.net.*; + +//import mir.misc.*; +import mir.generator.*; + +public class GeneratorListFunctions { + private GeneratorListFunctions() {} + + public static class subListFunction implements Generator.GeneratorFunction { + public Object perform(List aParameters) throws GeneratorExc, GeneratorFailure { + try { + int skip; + int maxSize; + + if (aParameters.size()>3 || aParameters.size()<2) + throw new GeneratorExc("iteratorSubsetFunction: 2 or 3 parameters expected"); + if (aParameters.get(0)==null) + return ""; + + if (!(aParameters.get(0) instanceof RewindableIterator) && !(aParameters.get(0) instanceof List)) + throw new GeneratorExc("iteratorSubsetFunction: first parameter must be a RewindableIterator (not a "+aParameters.get(0).getClass().getName()+")"); + + skip = StringRoutines.interpretAsInteger(aParameters.get(1)); + if (aParameters.size()>=2) + maxSize = StringRoutines.interpretAsInteger(aParameters.get(2)); + else + maxSize = -1; + + + if ((aParameters.get(0) instanceof RewindableIterator)) + return new SubsetIterator((RewindableIterator) aParameters.get(0), skip, maxSize); + else { + List list = (List) aParameters.get(0); + + if (skip>=list.size()) + return new Vector(); + if (maxSize<0 || (skip+maxSize)>=list.size()) + return list.subList(skip, list.size()); + else + return list.subList(skip, skip+maxSize); + } + } + catch (GeneratorExc e) { + throw e; + } + catch (GeneratorFailure e) { + throw e; + } + catch (Throwable e) { + throw new GeneratorFailure(e); + } + }; + } +} diff --git a/source/mir/util/GeneratorStringFunctions.java b/source/mir/util/GeneratorStringFunctions.java index 9aed0062..66f7533a 100755 --- a/source/mir/util/GeneratorStringFunctions.java +++ b/source/mir/util/GeneratorStringFunctions.java @@ -38,21 +38,6 @@ public class GeneratorStringFunctions { private GeneratorStringFunctions() {} - public static int interpretAsInteger(Object aValue) throws GeneratorExc { - if (aValue instanceof Integer) - return ((Integer) aValue).intValue(); - - if (aValue instanceof String) - try { - return Integer.parseInt((String) aValue); - } - catch (Throwable t) { - throw new GeneratorExc("Integer expected, "+aValue+" found"); - } - - throw new GeneratorExc("Integer expected, "+aValue+" found"); - } - public static String interpretAsString(Object aValue) throws GeneratorExc { if (aValue instanceof String) return (String) aValue; @@ -74,12 +59,12 @@ public class GeneratorStringFunctions { if (aParameters.size()==3) { return interpretAsString(aParameters.get(0)).substring( - interpretAsInteger(aParameters.get(1)), - interpretAsInteger(aParameters.get(2))); + StringRoutines.interpretAsInteger(aParameters.get(1)), + StringRoutines.interpretAsInteger(aParameters.get(2))); } else { return interpretAsString(aParameters.get(0)).substring( - interpretAsInteger(aParameters.get(1))); + StringRoutines.interpretAsInteger(aParameters.get(1))); } } catch (GeneratorExc e) { diff --git a/source/mir/util/ParameterExpander.java b/source/mir/util/ParameterExpander.java index 5a752d27..be7367b2 100755 --- a/source/mir/util/ParameterExpander.java +++ b/source/mir/util/ParameterExpander.java @@ -580,8 +580,10 @@ public class ParameterExpander { List parameters; do { - token = scanner.scan(); + token = scanner.peek(); + if (token instanceof LeftSquareBraceToken) { + scanner.scan(); qualifier = parseUntil(MAX_OPERATOR_LEVEL); token = scanner.scan(); if (!(token instanceof RightSquareBraceToken)) @@ -595,6 +597,7 @@ public class ParameterExpander { } } else if (token instanceof IdentifierToken) { + scanner.scan(); qualifier = ((IdentifierToken) token).getName(); if (currentValue instanceof Map) { @@ -653,7 +656,7 @@ public class ParameterExpander { value = parseUntil(unaryOperatorLevel(token)); value = expandOperatorExpression(token, value); } - else if (token instanceof IdentifierToken) { + else if (token instanceof IdentifierToken || token instanceof LeftSquareBraceToken) { value = parseVariable(); } else if (token instanceof LiteralToken) { diff --git a/source/mir/util/SimpleParser.java b/source/mir/util/SimpleParser.java index 8c4a5cb7..f8160f11 100755 --- a/source/mir/util/SimpleParser.java +++ b/source/mir/util/SimpleParser.java @@ -43,8 +43,6 @@ public class SimpleParser { public SimpleParser(String aData) { data=aData; position=0; - - System.out.println("Will parse: "+aData); } public boolean parses(RE aRegularExpression) throws SimpleParserExc { @@ -107,8 +105,6 @@ public class SimpleParser { public String parse(String anExpression, String aMessage) throws SimpleParserExc, SimpleParserFailure { try { - System.out.println("Expression: "+anExpression); - return parse(new RE(anExpression), aMessage); } catch (SimpleParserExc e) { diff --git a/source/mir/util/StringRoutines.java b/source/mir/util/StringRoutines.java index 7907ac24..0edc27fc 100755 --- a/source/mir/util/StringRoutines.java +++ b/source/mir/util/StringRoutines.java @@ -55,6 +55,21 @@ public class StringRoutines { return result; } + public static int interpretAsInteger(Object aValue) throws Exception { + if (aValue instanceof Integer) + return ((Integer) aValue).intValue(); + + if (aValue instanceof String) + try { + return Integer.parseInt((String) aValue); + } + catch (Throwable t) { + throw new Exception("Integer expected, "+aValue+" found"); + } + + throw new Exception("Integer expected, "+aValue+" found"); + } + public static String performRegularExpressionReplacement(String aSource, String aSearchExpression, String aReplacement) throws Exception { diff --git a/source/mir/util/SubsetIterator.java b/source/mir/util/SubsetIterator.java new file mode 100755 index 00000000..b1204afd --- /dev/null +++ b/source/mir/util/SubsetIterator.java @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2001, 2002 The Mir-coders group + * + * This file is part of Mir. + * + * Mir is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Mir is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Mir; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * In addition, as a special exception, The Mir-coders gives permission to link + * the code of this program with the com.oreilly.servlet library, any library + * licensed under the Apache Software License, The Sun (tm) Java Advanced + * Imaging library (JAI), The Sun JIMI library (or with modified versions of + * the above that use the same license as the above), and distribute linked + * combinations including the two. You must obey the GNU General Public + * License in all respects for all of the code used other than the above + * mentioned libraries. If you modify this file, you may extend this exception + * to your version of the file, but you are not obligated to do so. If you do + * not wish to do so, delete this exception statement from your version. + */ + +package mir.util; + +import java.util.*; + +import java.util.*; +import mir.storage.*; +import mir.util.*; +import mir.entity.*; + +public class SubsetIterator implements RewindableIterator { + private RewindableIterator master; + private List cachedItems; + private int position; + private int skip; + private int maxLength; + + public SubsetIterator(RewindableIterator anIterator, int aSkip, int aMaxLength) { + master = anIterator; + position = 0; + skip = aSkip; + maxLength = aMaxLength; + } + + public boolean hasNext() { + while (position