01: /*******************************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: *******************************************************************************/package org.ofbiz.minilang.method.envops;
19:
20: import java.util.List;
21: import java.util.LinkedList;
22:
23: import org.w3c.dom.Element;
24:
25: import org.ofbiz.minilang.method.MethodOperation;
26: import org.ofbiz.minilang.method.ContextAccessor;
27: import org.ofbiz.minilang.method.MethodContext;
28: import org.ofbiz.minilang.SimpleMethod;
29: import org.ofbiz.base.util.Debug;
30:
31: /**
32: * Loop
33: */
34: public class Loop extends MethodOperation {
35:
36: public static final String module = Loop.class.getName();
37: protected List subOps = new LinkedList();
38: protected ContextAccessor fieldAcsr;
39: protected String countStr;
40:
41: public Loop(Element element, SimpleMethod simpleMethod) {
42: super (element, simpleMethod);
43: this .fieldAcsr = new ContextAccessor(element
44: .getAttribute("field"));
45: this .countStr = element.getAttribute("count");
46:
47: SimpleMethod.readOperations(element, subOps, simpleMethod);
48: }
49:
50: public boolean exec(MethodContext methodContext) {
51: String countStrExp = methodContext.expandString(this .countStr);
52: int count = 0;
53: try {
54: Double ctDbl = new Double(countStrExp);
55: if (ctDbl != null) {
56: count = ctDbl.intValue();
57: }
58: } catch (NumberFormatException e) {
59: Debug.logError(e, module);
60: return false;
61: }
62:
63: if (count < 1) {
64: Debug.logWarning(
65: "Count is less than one, not doing nothing: "
66: + rawString(), module);
67: return false;
68: }
69:
70: for (int i = 0; i < count; i++) {
71: fieldAcsr.put(methodContext, new Integer(i));
72: if (!SimpleMethod.runSubOps(subOps, methodContext)) {
73: // only return here if it returns false, otherwise just carry on
74: return false;
75: }
76: }
77:
78: return true;
79: }
80:
81: public String rawString() {
82: return "<loop count=\"" + this .countStr + "\"/>";
83: }
84:
85: public String expandedString(MethodContext methodContext) {
86: return this.rawString();
87: }
88: }
|