You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ad...@apache.org on 2012/05/05 22:53:14 UTC
svn commit: r1334502 - in /ofbiz/trunk/framework/minilang:
dtd/simple-methods-v2.xsd src/org/ofbiz/minilang/method/envops/Iterate.java
Author: adrianc
Date: Sat May 5 20:53:13 2012
New Revision: 1334502
URL: http://svn.apache.org/viewvc?rev=1334502&view=rev
Log:
Overhauled Mini-language <iterate> element.
Modified:
ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd
ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Iterate.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=1334502&r1=1334501&r2=1334502&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd (original)
+++ ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd Sat May 5 20:53:13 2012
@@ -3409,34 +3409,32 @@ under the License.
<xs:element name="iterate" substitutionGroup="ControlOperations">
<xs:annotation>
<xs:documentation>
- The operations contained by the iterate tag will be executed for each of the entries in the list,
- and will make the current entry available in the method environment by the entry-name specified.
- This tag can contain any of the simple-method operations, including the conditional/if operations.
-
- Any simple-method operation can be nested under the iterate tag.
+ The operations contained in the iterate element will be executed for each of the entries in the list,
+ and will make the current entry available in the environment by the name specified.
</xs:documentation>
</xs:annotation>
<xs:complexType>
- <xs:group minOccurs="0" maxOccurs="unbounded" ref="AllOperations"/>
- <xs:attributeGroup ref="attlist.iterate"/>
+ <xs:group minOccurs="0" maxOccurs="unbounded" ref="AllOperations" />
+ <xs:attribute type="xs:string" name="entry" use="required">
+ <xs:annotation>
+ <xs:documentation>
+ The name of the environment field that will contain each entry in the list.
+ <br/><br/>
+ Required. Attribute type: expression.
+ </xs:documentation>
+ </xs:annotation>
+ </xs:attribute>
+ <xs:attribute type="xs:string" name="list" use="required">
+ <xs:annotation>
+ <xs:documentation>
+ The name of the environment field that contains the list to iterate over.
+ <br/><br/>
+ Required. Attribute type: expression.
+ </xs:documentation>
+ </xs:annotation>
+ </xs:attribute>
</xs:complexType>
</xs:element>
- <xs:attributeGroup name="attlist.iterate">
- <xs:attribute type="xs:string" name="entry" use="required">
- <xs:annotation>
- <xs:documentation>
- The name of the method environment field that will contain each entry as we iterate through the list.
- </xs:documentation>
- </xs:annotation>
- </xs:attribute>
- <xs:attribute type="xs:string" name="list" use="required">
- <xs:annotation>
- <xs:documentation>
- The name of the method environment field that contains the list to iterate over.
- </xs:documentation>
- </xs:annotation>
- </xs:attribute>
- </xs:attributeGroup>
<xs:element name="iterate-map" substitutionGroup="ControlOperations">
<xs:annotation>
<xs:documentation>
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=1334502&r1=1334501&r2=1334502&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 Sat May 5 20:53:13 2012
@@ -25,12 +25,15 @@ import java.util.List;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilGenerics;
+import org.ofbiz.base.util.collections.FlexibleMapAccessor;
+import org.ofbiz.base.util.string.FlexibleStringExpander;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.util.EntityListIterator;
import org.ofbiz.minilang.MiniLangException;
+import org.ofbiz.minilang.MiniLangRuntimeException;
+import org.ofbiz.minilang.MiniLangValidate;
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;
@@ -40,35 +43,41 @@ import org.w3c.dom.Element;
/**
* Process sub-operations for each entry in the list
*/
-public class Iterate extends MethodOperation {
+public final class Iterate extends MethodOperation {
public static final String module = Iterate.class.getName();
- protected ContextAccessor<Object> entryAcsr;
- protected ContextAccessor<Object> listAcsr;
- protected List<MethodOperation> subOps;
+ private final FlexibleMapAccessor<Object> entryFma;
+ private final FlexibleMapAccessor<Object> listFma;
+ private final List<MethodOperation> subOps;
public Iterate(Element element, SimpleMethod simpleMethod) throws MiniLangException {
super(element, simpleMethod);
- this.entryAcsr = new ContextAccessor<Object>(element.getAttribute("entry"), element.getAttribute("entry-name"));
- this.listAcsr = new ContextAccessor<Object>(element.getAttribute("list"), element.getAttribute("list-name"));
+ if (MiniLangValidate.validationOn()) {
+ MiniLangValidate.attributeNames(simpleMethod, element, "entry", "list");
+ MiniLangValidate.expressionAttributes(simpleMethod, element, "entry", "list");
+ MiniLangValidate.requiredAttributes(simpleMethod, element, "entry", "list");
+ }
+ this.entryFma = FlexibleMapAccessor.getInstance(element.getAttribute("entry"));
+ this.listFma = FlexibleMapAccessor.getInstance(element.getAttribute("list"));
this.subOps = Collections.unmodifiableList(SimpleMethod.readOperations(element, simpleMethod));
}
@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
- if (listAcsr.isEmpty()) {
- Debug.logWarning("No list-name specified in iterate tag, doing nothing: " + rawString(), module);
+ if (listFma.isEmpty()) {
+ if (Debug.verboseOn())
+ Debug.logVerbose("Collection not found, doing nothing: " + this, module);
return true;
}
- Object oldEntryValue = entryAcsr.get(methodContext);
- Object objList = listAcsr.get(methodContext);
+ Object oldEntryValue = entryFma.get(methodContext.getEnvMap());
+ Object objList = listFma.get(methodContext.getEnvMap());
if (objList instanceof EntityListIterator) {
EntityListIterator eli = (EntityListIterator) objList;
GenericValue theEntry;
try {
while ((theEntry = eli.next()) != null) {
- entryAcsr.put(methodContext, theEntry);
+ entryFma.put(methodContext.getEnvMap(), theEntry);
try {
for (MethodOperation methodOperation : subOps) {
if (!methodOperation.exec(methodContext)) {
@@ -86,31 +95,21 @@ public class Iterate extends MethodOpera
}
}
} finally {
- // close the iterator
try {
eli.close();
} catch (GenericEntityException e) {
- Debug.logError(e, module);
- String errMsg = "ERROR: Error closing entityListIterator in " + simpleMethod.getShortDescription() + " [" + e.getMessage() + "]: " + rawString();
- if (methodContext.getMethodType() == MethodContext.EVENT) {
- methodContext.putEnv(simpleMethod.getEventErrorMessageName(), errMsg);
- methodContext.putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode());
- } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
- methodContext.putEnv(simpleMethod.getServiceErrorMessageName(), errMsg);
- methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode());
- }
- return false;
+ throw new MiniLangRuntimeException("Error closing entityListIterator: " + e.getMessage(), this);
}
}
} else if (objList instanceof Collection<?>) {
Collection<Object> theCollection = UtilGenerics.checkCollection(objList);
if (theCollection.size() == 0) {
if (Debug.verboseOn())
- Debug.logVerbose("Collection with name " + listAcsr + " has zero entries, doing nothing: " + rawString(), module);
+ Debug.logVerbose("Collection has zero entries, doing nothing: " + this, module);
return true;
}
for (Object theEntry : theCollection) {
- entryAcsr.put(methodContext, theEntry);
+ entryFma.put(methodContext.getEnvMap(), theEntry);
try {
for (MethodOperation methodOperation : subOps) {
if (!methodOperation.exec(methodContext)) {
@@ -131,12 +130,12 @@ public class Iterate extends MethodOpera
Iterator<Object> theIterator = UtilGenerics.cast(objList);
if (!theIterator.hasNext()) {
if (Debug.verboseOn())
- Debug.logVerbose("List with name " + listAcsr + " has no more entries, doing nothing: " + rawString(), module);
+ Debug.logVerbose("Iterator has zero entries, doing nothing: " + this, module);
return true;
}
while (theIterator.hasNext()) {
Object theEntry = theIterator.next();
- entryAcsr.put(methodContext, theEntry);
+ entryFma.put(methodContext.getEnvMap(), theEntry);
try {
for (MethodOperation methodOperation : subOps) {
if (!methodOperation.exec(methodContext)) {
@@ -154,18 +153,17 @@ public class Iterate extends MethodOpera
}
}
} else {
- if (Debug.infoOn())
- Debug.logInfo("List not found with name " + listAcsr + ", doing nothing: " + rawString(), module);
+ if (Debug.verboseOn())
+ Debug.logVerbose("Cannot iterate over a " + objList.getClass().getName() + ", doing nothing: " + this, module);
return true;
}
- entryAcsr.put(methodContext, oldEntryValue);
+ entryFma.put(methodContext.getEnvMap(), oldEntryValue);
return true;
}
@Override
public String expandedString(MethodContext methodContext) {
- // TODO: something more than a stub/dummy
- return this.rawString();
+ return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap());
}
public List<MethodOperation> getSubOps() {
@@ -174,8 +172,20 @@ public class Iterate extends MethodOpera
@Override
public String rawString() {
- // TODO: something more than the empty tag
- return "<iterate list-name=\"" + this.listAcsr + "\" entry-name=\"" + this.entryAcsr + "\"/>";
+ return toString();
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder("<iterate ");
+ if (!this.entryFma.isEmpty()) {
+ sb.append("entry=\"").append(this.entryFma).append("\" ");
+ }
+ if (!this.listFma.isEmpty()) {
+ sb.append("list=\"").append(this.listFma).append("\" ");
+ }
+ sb.append("/>");
+ return sb.toString();
}
public static final class IterateFactory implements Factory<Iterate> {