1 package com.genimen.djeneric.tools.generator.core.nodes; 2 3 import com.genimen.djeneric.repository.DjList; 4 import com.genimen.djeneric.tools.generator.core.DjentelParserEngine; 5 import com.genimen.djeneric.tools.generator.core.ParseException; 6 import com.genimen.djeneric.tools.generator.core.util.ContextObject; 7 import com.genimen.djeneric.tools.generator.core.util.ParseContext; 8 9 public class ForNode extends SetNode 10 { 11 private String _separator = null; 12 private int _modulo = 0; 13 private String _moduloSeparator = null; 14 15 public ForNode(int i) 16 { 17 super(i); 18 } 19 20 public ForNode(DjentelParserEngine p, int i) 21 { 22 super(p, i); 23 } 24 25 public void setModulo(int m) 26 { 27 _modulo = m; 28 } 29 30 public void setModuloSeparator(String sep) 31 { 32 sep = sep.substring(1); 34 sep = sep.substring(0, sep.length() - 1); 35 36 _moduloSeparator = StringNode.translateEscapes(sep); 37 } 38 39 public String getName() 40 { 41 return "for"; 42 } 43 44 public String toString() 45 { 46 return "for(" + getLoopVariable() + " in ..."; 47 } 48 49 public void setLoopVariable(String loopVariable) 50 { 51 setSetName(loopVariable); 52 } 53 54 public String getLoopVariable() 55 { 56 return getSetName(); 57 } 58 59 public void setSeparator(String sep) 60 { 61 sep = sep.substring(1); 63 sep = sep.substring(0, sep.length() - 1); 64 65 _separator = StringNode.translateEscapes(sep); 66 } 67 68 public void setIndent(CalculatedExpressionNode ind) 69 { 70 } 71 72 public void setBody(BodyNode body) 73 { 74 } 75 76 public String indentString(String val, int ind) 77 { 78 if (ind == 0) return val; 79 80 StringBuffer result = new StringBuffer (val); 81 82 if (ind > 0) 83 { 84 for (int x = 0; x < ind; x++) 85 result.insert(0, ' '); 86 int i = 0; 87 while (i < result.length()) 88 { 89 if (i < result.length() - 1 && result.charAt(i) == '\n' && result.charAt(i + 1) != '\n') 90 { 91 for (int x = 0; x < ind; x++) 92 result.insert(i + 1, ' '); 93 i += ind; 94 } 95 i++; 96 } 97 } 98 if (ind < 0) 99 { 100 ind = -ind; 101 for (int d = 0; d < ind; d++) 102 if (result.charAt(0) == ' ') result.deleteCharAt(0); 103 int i = 0; 104 while (i < result.length()) 105 { 106 if (i < result.length() - 1 && result.charAt(i) == '\n' && result.charAt(i + 1) != '\n') 107 { 108 int end = i + ind; 109 if (end > result.length()) end = result.length(); 110 for (int d = i; d < end; d++) 111 if (result.charAt(i + 1) == ' ') result.deleteCharAt(i + 1); 112 } 113 i++; 114 } 115 } 116 return result.toString(); 117 } 118 119 public String evaluate(ParseContext context) throws ParseException 120 { 121 context.mark(); 122 StringBuffer result = new StringBuffer (2000); 123 124 String varName = getLoopVariable(); 125 ValueExpression set = (ValueExpression) getChild(0); 126 127 Object o = set.getValue(context); 128 if (!(o instanceof DjList)) throw new ParseException("Expression is not a set of objects", beginLine, beginColumn); 129 130 DjList lst = (DjList) o; 131 132 if (lst.size() == 0) return ""; 133 134 BodyNode body = null; 135 FilterNode filter = null; 136 IndentNode indent = null; 137 OrderByNode orderBy = null; 138 139 for (int i = 1; i < getChildCount(); i++) 140 { 141 if (getChild(i) instanceof FilterNode) filter = (FilterNode) getChild(i); 142 if (getChild(i) instanceof BodyNode) body = (BodyNode) getChild(i); 143 if (getChild(i) instanceof IndentNode) indent = (IndentNode) getChild(i); 144 if (getChild(i) instanceof OrderByNode) orderBy = (OrderByNode) getChild(i); 145 } 146 147 if (body == null) return ""; 148 lst = processFilter(lst, context, filter); 149 processOrderBy(lst, orderBy); 150 151 int colsToIndent = 0; 152 153 if (indent != null) 154 { 155 o = indent.getValue(context); 156 if (!(o instanceof Integer )) throw new ParseException("Indent must be an integer value", beginLine, beginColumn); 157 colsToIndent = ((Integer ) o).intValue(); 158 } 159 160 ContextObject loopvar = context.getObjectStack().push(varName, lst.getDjenericObjectAt(0)); 161 162 for (int i = 0; i < lst.size(); i++) 163 { 164 loopvar.setObject(lst.getDjenericObjectAt(i)); 165 result.append(indentString(body.evaluate(context), colsToIndent)); 166 167 if (_modulo != 0 && i != lst.size() - 1 && ((i + 1) % _modulo == 0)) result.append(_moduloSeparator); 168 else if (_separator != null && i != lst.size() - 1) result.append(_separator); 169 } 170 171 context.releaseToMark(); 172 return result.toString(); 173 } 174 175 } | Popular Tags |