|
Author: adrianc
Date: Mon Apr 23 02:31:53 2012 New Revision: 1329034 URL: http://svn.apache.org/viewvc?rev=1329034&view=rev Log: Added <continue> and <break> elements to Mini-language looping elements. Added: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Break.java (with props) ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Continue.java (with props) Modified: ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/While.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Iterate.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/IterateMap.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Loop.java Modified: ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd?rev=1329034&r1=1329033&r2=1329034&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd (original) +++ ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd Mon Apr 23 02:31:53 2012 @@ -3568,6 +3568,20 @@ under the License. </xs:annotation> </xs:attribute> </xs:attributeGroup> + <xs:element name="break" substitutionGroup="ControlOperations"> + <xs:annotation> + <xs:documentation> + Causes script execution to exit the nearest enclosing loop element. + </xs:documentation> + </xs:annotation> + </xs:element> + <xs:element name="continue" substitutionGroup="ControlOperations"> + <xs:annotation> + <xs:documentation> + Causes script execution to return to the beginning of the nearest enclosing loop element. + </xs:documentation> + </xs:annotation> + </xs:element> <xs:element name="return" substitutionGroup="ControlOperations"> <xs:annotation> <xs:documentation> Modified: ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation%24Factory?rev=1329034&r1=1329033&r2=1329034&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory (original) +++ ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory Mon Apr 23 02:31:53 2012 @@ -67,7 +67,9 @@ org.ofbiz.minilang.method.entityops.Stor org.ofbiz.minilang.method.entityops.TransactionBegin$TransactionBeginFactory org.ofbiz.minilang.method.entityops.TransactionCommit$TransactionCommitFactory org.ofbiz.minilang.method.entityops.TransactionRollback$TransactionRollbackFactory +org.ofbiz.minilang.method.envops.Break$BreakFactory org.ofbiz.minilang.method.envops.ClearField$ClearFieldFactory +org.ofbiz.minilang.method.envops.Continue$ContinueFactory org.ofbiz.minilang.method.envops.FieldToList$FieldToListFactory org.ofbiz.minilang.method.envops.FirstFromList$FirstFromListFactory org.ofbiz.minilang.method.envops.Iterate$IterateFactory Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/While.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/While.java?rev=1329034&r1=1329033&r2=1329034&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/While.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/While.java Mon Apr 23 02:31:53 2012 @@ -27,6 +27,8 @@ import org.ofbiz.minilang.MiniLangExcept import org.ofbiz.minilang.SimpleMethod; import org.ofbiz.minilang.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; +import org.ofbiz.minilang.method.envops.Break.BreakElementException; +import org.ofbiz.minilang.method.envops.Continue.ContinueElementException; import org.w3c.dom.Element; /** @@ -52,9 +54,20 @@ public class While extends MethodOperati // if a sub-op returns false return false and stop, otherwise drop though loop and // return true while (condition.checkCondition(methodContext)) { - boolean runSubOpsResult = SimpleMethod.runSubOps(thenSubOps, methodContext); - if (!runSubOpsResult) { - return false; + try { + for (MethodOperation methodOperation : thenSubOps) { + if (!methodOperation.exec(methodContext)) { + return false; + } + } + } catch (MiniLangException e) { + if (e instanceof BreakElementException) { + break; + } + if (e instanceof ContinueElementException) { + continue; + } + throw e; } } return true; Added: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Break.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Break.java?rev=1329034&view=auto ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Break.java (added) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Break.java Mon Apr 23 02:31:53 2012 @@ -0,0 +1,77 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.ofbiz.minilang.method.envops; + +import org.ofbiz.minilang.MiniLangException; +import org.ofbiz.minilang.SimpleMethod; +import org.ofbiz.minilang.method.MethodContext; +import org.ofbiz.minilang.method.MethodOperation; +import org.w3c.dom.Element; + +/** + * Causes script execution to exit the nearest loop element. + */ +public class Break extends MethodOperation { + + public Break(Element element, SimpleMethod simpleMethod) throws MiniLangException { + super(element, simpleMethod); + } + + @Override + public boolean exec(MethodContext methodContext) throws MiniLangException { + throw new BreakElementException(); + } + + @Override + public String expandedString(MethodContext methodContext) { + return this.rawString(); + } + + @Override + public String rawString() { + return "<break/>"; + } + + @SuppressWarnings("serial") + public class BreakElementException extends MiniLangException { + + public BreakElementException() { + super("<break> element encountered without enclosing loop"); + } + + @Override + public String getMessage() { + StringBuilder sb = new StringBuilder(super.getMessage()); + SimpleMethod method = getSimpleMethod(); + sb.append(" Method = ").append(method.getMethodName()).append(", File = ").append(method.getFromLocation()); + sb.append(", Element = <break>, Line ").append(getLineNumber()); + return sb.toString(); + } + } + + public static final class BreakFactory implements Factory<Break> { + public Break createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException { + return new Break(element, simpleMethod); + } + + public String getName() { + return "break"; + } + } +} Propchange: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Break.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Break.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Rev URL Added: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Continue.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Continue.java?rev=1329034&view=auto ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Continue.java (added) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Continue.java Mon Apr 23 02:31:53 2012 @@ -0,0 +1,77 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.ofbiz.minilang.method.envops; + +import org.ofbiz.minilang.MiniLangException; +import org.ofbiz.minilang.SimpleMethod; +import org.ofbiz.minilang.method.MethodContext; +import org.ofbiz.minilang.method.MethodOperation; +import org.w3c.dom.Element; + +/** + * Causes script execution to return to the beginning of the nearest enclosing loop element. + */ +public class Continue extends MethodOperation { + + public Continue(Element element, SimpleMethod simpleMethod) throws MiniLangException { + super(element, simpleMethod); + } + + @Override + public boolean exec(MethodContext methodContext) throws MiniLangException { + throw new ContinueElementException(); + } + + @Override + public String expandedString(MethodContext methodContext) { + return this.rawString(); + } + + @Override + public String rawString() { + return "<continue/>"; + } + + @SuppressWarnings("serial") + public class ContinueElementException extends MiniLangException { + + public ContinueElementException() { + super("<continue> element encountered without enclosing loop"); + } + + @Override + public String getMessage() { + StringBuilder sb = new StringBuilder(super.getMessage()); + SimpleMethod method = getSimpleMethod(); + sb.append(" Method = ").append(method.getMethodName()).append(", File = ").append(method.getFromLocation()); + sb.append(", Element = <continue>, Line ").append(getLineNumber()); + return sb.toString(); + } + } + + public static final class ContinueFactory implements Factory<Continue> { + public Continue createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException { + return new Continue(element, simpleMethod); + } + + public String getName() { + return "continue"; + } + } +} Propchange: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Continue.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Continue.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Rev URL Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Iterate.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Iterate.java?rev=1329034&r1=1329033&r2=1329034&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Iterate.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Iterate.java Mon Apr 23 02:31:53 2012 @@ -34,6 +34,8 @@ import org.ofbiz.minilang.SimpleMethod; import org.ofbiz.minilang.method.ContextAccessor; import org.ofbiz.minilang.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; +import org.ofbiz.minilang.method.envops.Break.BreakElementException; +import org.ofbiz.minilang.method.envops.Continue.ContinueElementException; import org.w3c.dom.Element; /** @@ -68,9 +70,20 @@ public class Iterate extends MethodOpera try { while ((theEntry = eli.next()) != null) { entryAcsr.put(methodContext, theEntry); - if (!SimpleMethod.runSubOps(subOps, methodContext)) { - // only return here if it returns false, otherwise just carry on - return false; + try { + for (MethodOperation methodOperation : subOps) { + if (!methodOperation.exec(methodContext)) { + return false; + } + } + } catch (MiniLangException e) { + if (e instanceof BreakElementException) { + break; + } + if (e instanceof ContinueElementException) { + continue; + } + throw e; } } } finally { Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/IterateMap.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/IterateMap.java?rev=1329034&r1=1329033&r2=1329034&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/IterateMap.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/IterateMap.java Mon Apr 23 02:31:53 2012 @@ -29,6 +29,8 @@ import org.ofbiz.minilang.SimpleMethod; import org.ofbiz.minilang.method.ContextAccessor; import org.ofbiz.minilang.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; +import org.ofbiz.minilang.method.envops.Break.BreakElementException; +import org.ofbiz.minilang.method.envops.Continue.ContinueElementException; import org.w3c.dom.Element; /** @@ -79,10 +81,20 @@ public class IterateMap extends MethodOp for (Map.Entry<? extends Object, ? extends Object> theEntry : theMap.entrySet()) { keyAcsr.put(methodContext, theEntry.getKey()); valueAcsr.put(methodContext, theEntry.getValue()); - - if (!SimpleMethod.runSubOps(subOps, methodContext)) { - // only return here if it returns false, otherwise just carry on - return false; + try { + for (MethodOperation methodOperation : subOps) { + if (!methodOperation.exec(methodContext)) { + return false; + } + } + } catch (MiniLangException e) { + if (e instanceof BreakElementException) { + break; + } + if (e instanceof ContinueElementException) { + continue; + } + throw e; } } return true; Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Loop.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Loop.java?rev=1329034&r1=1329033&r2=1329034&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Loop.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Loop.java Mon Apr 23 02:31:53 2012 @@ -28,6 +28,8 @@ import org.ofbiz.minilang.SimpleMethod; import org.ofbiz.minilang.method.ContextAccessor; import org.ofbiz.minilang.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; +import org.ofbiz.minilang.method.envops.Break.BreakElementException; +import org.ofbiz.minilang.method.envops.Continue.ContinueElementException; import org.w3c.dom.Element; /** @@ -67,9 +69,20 @@ public class Loop extends MethodOperatio } for (int i = 0; i < count; i++) { fieldAcsr.put(methodContext, i); - if (!SimpleMethod.runSubOps(subOps, methodContext)) { - // only return here if it returns false, otherwise just carry on - return false; + try { + for (MethodOperation methodOperation : subOps) { + if (!methodOperation.exec(methodContext)) { + return false; + } + } + } catch (MiniLangException e) { + if (e instanceof BreakElementException) { + break; + } + if (e instanceof ContinueElementException) { + continue; + } + throw e; } } return true; @@ -86,7 +99,7 @@ public class Loop extends MethodOperatio @Override public String rawString() { - return "<loop count=\"" + this.countStr + "\"/>"; + return "<loop field=\"" + this.fieldAcsr + "\" count=\"" + this.countStr + "\"/>"; } public static final class LoopFactory implements Factory<Loop> { |
| Free forum by Nabble | Edit this page |
