1 package com.teamkonzept.lib.templates; 2 3 import java.io.*; 4 import java.util.*; 5 import java.text.*; 6 7 import com.oroinc.text.regex.*; 8 9 import com.teamkonzept.lib.*; 10 import org.apache.log4j.Category; 11 12 17 public class TKListTag extends TKLabelTag 18 { 19 20 private final static String DIRECTION = "DIRECTION"; 21 22 private final static String UP = "UP"; 23 private final static String DOWN = "DOWN"; 24 25 26 private final static String TYPE = "TYPE"; 27 28 29 private final static String NUMBER = "NUMBER"; 30 31 32 private final static String DATE = "DATE"; 33 34 35 private final static String STRING = "STRING"; 36 37 38 private final static String FIELD = "FIELD"; 39 40 41 private static final String START = "START"; 42 43 44 private static final String END = "END"; 45 46 47 private static final String CONTROL = "CONTROL"; 48 49 50 public TKTemplateSyntax listText; 51 52 public static final int TAG_TYPE = TKIfTag.TAG_TYPE+1; 53 54 55 58 static DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.SHORT); 59 60 static DateFormat DATE_TIME_FORMAT = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); 61 62 63 64 65 66 public String paramsString = null; 67 68 69 private String direction = UP; 70 private String field = null; 71 private String type = STRING; 72 private String control = null; 73 private int start = 0; 74 private int end = Integer.MAX_VALUE; 75 76 private boolean legacy = false; 78 79 private final static Category CAT = Category.getInstance(TKListTag.class); 80 81 89 public TKListTag( TKTemplateSyntax parent, String def, PatternMatcherInput matcherInput, boolean hasSubTags ) throws TKTemplateSyntaxException 90 { 91 super( parent, def, hasSubTags ); 92 if( hasSubTags ) paramsString = def; 93 else label = getParams (def,false,null); 94 try { 95 listText = parent.newChild( matcherInput, parent.getSource() ); 96 } 97 catch (ArrayIndexOutOfBoundsException e) { throw new TKTemplateSyntaxException(parent.getSource(),"NOEND","LIST", def); 99 } 100 checkError(listText,"LIST", def); 101 } 102 103 107 protected void makeParam (StringBuffer name, StringBuffer value, boolean hasSubTags, TKTemplateData td) 108 throws TKTemplateSyntaxException 109 { 110 String valString = value == null ? "" : value.toString(); 111 if (hasSubTags) 112 { 113 TKTemplateSyntax syntax = parent.newChild (valString,parent.getSource(),true); 114 115 if (td != null) 116 { 117 TKTemplateData subTd = new TKTemplateData (td); 118 td = subTd; 119 td.setLocalVariable("TEMPLATE_SYNTAX_DEFAULT_ENCODING",TKAnsiConverter.CONV_ID); 120 } 121 valString = syntax.apply (td); 122 } 123 String sName = name.toString().toUpperCase().trim(); 124 if (sName.equals(DIRECTION)) 125 { 126 if (valString.toUpperCase().equals(DOWN)) 127 direction = DOWN; 128 else if (valString.toUpperCase().equals(UP)) 129 direction = UP; 130 else 131 throw new TKTemplateSyntaxException(parent.getSource(),"Ungueltiger Value fuer Direction : " , "LIST", valString); 132 133 } 134 else if (sName.equals(TYPE)) 135 { 136 if (valString.toUpperCase().equals(STRING)) 137 type = STRING; 138 else if (valString.toUpperCase().equals(NUMBER)) 139 type = NUMBER; 140 else if (valString.toUpperCase().equals(DATE)) 141 type = DATE; 142 else 143 throw new TKTemplateSyntaxException(parent.getSource(),"Ungueltiger Value fuer Type : " , "LIST", valString); 144 } 145 else if (sName.equals(FIELD)) 146 { 147 field = valString; 148 } 149 else if (sName.equals(END)) 150 { 151 end = toInt(valString); 152 } 153 else if (sName.equals(START)) 154 { 155 start = toInt(valString); 156 } 157 else if (sName.equals(CONTROL)) 158 { 159 control = valString; 160 } 161 else 162 { 163 throw new TKTemplateSyntaxException(parent.getSource(), "Ungueltiger Listparameter : " , "LIST", sName); 164 } 165 } 166 167 private int toInt(String in) throws TKTemplateSyntaxException 168 { 169 try 170 { 171 return Integer.parseInt(in); 172 } 173 catch (NumberFormatException e) 174 { 175 throw new TKTemplateSyntaxException(parent.getSource(), "Ungueltiger Listparameter : " , "LIST", in); 176 } 177 } 178 179 protected String getParams( String def, boolean hasSubTags, TKTemplateData td) throws TKTemplateSyntaxException 180 { 181 int pos = def.indexOf(':'); 182 if (pos < 0) 183 { 184 if (hasSubTags) return parent.newChild(def,parent.getSource(),hasSubTags).apply (td); 185 else return def; 186 } 187 int legacyPos = def.indexOf('='); 188 if (legacyPos < 0) 189 { 190 legacy = true; 192 return getRealLabel( td ); 193 } 194 String label = def.substring( 0, pos++ ); 195 if (hasSubTags) label = parent.newChild(label,parent.getSource(),hasSubTags).apply (td); 196 197 StringBuffer name = null; 198 StringBuffer value = null; 199 boolean inName = true; 200 201 while (pos < def.length()) { 202 203 char chr = def.charAt (pos++); 204 205 if (chr == ';') { 206 207 if (name != null) makeParam (name, value, hasSubTags, td); 208 inName = true; 209 name = null; 210 value = null; 211 continue; 212 } 213 214 if (chr == '=') { inName = false; continue; } 215 216 if ((chr == '\\') && (pos < def.length())) chr = def.charAt (pos++); 217 218 (inName ? (name == null ? (name = new StringBuffer ()) : name) : 219 (value == null ? (value = new StringBuffer ()) : value)) .append (chr); 220 } 221 222 if (name != null) makeParam (name, value, hasSubTags, td); 223 224 return label; 225 } 226 227 234 public String apply( TKTemplateData td ) throws TKTemplateSyntaxException 235 { 236 StringWriter wr = new StringWriter(); 237 try 238 { 239 apply(td, wr); 240 } 241 catch (Exception e) 242 { 243 CAT.error("apply", e); 244 } 245 return wr.getBuffer().toString(); 246 } 247 248 255 public void apply(TKTemplateData td, Writer writer) 256 throws TKTemplateSyntaxException, IOException 257 { 258 String label = getRealLabel( td ); 259 if (paramsString != null && !legacy) label = getParams(paramsString,true,td); 261 if (legacy) 262 { 263 int pos = label.indexOf(':'); 264 if (pos >= 0) 265 { 266 control = label.substring (pos+1,label.length()); 267 label = label.substring (0,pos); 268 } 269 } 270 if( td.getListIterator() == null ) 272 { 273 return; 274 } 275 276 int count = 0; 277 boolean cont = true; 278 LinkedList set = new LinkedList(); 279 ComparableKey comp = new ComparableKey(); 280 StringBuffer result = new StringBuffer (); 281 if (field != null) 282 { 283 boolean loop = true; 284 while (loop) 285 { 286 TKTemplate template = listText.getNewTemplate(); 287 template.setListIterator( td.getListIterator() ); 288 289 template.td.parent = td; loop = td.getListIterator().apply( template, count++, label ); 291 if (loop) 292 { 293 int index = 0; 295 boolean greater =true; 296 while (greater) 297 { 298 if (set.size() == index) 299 { 300 set.add(template); 301 greater = false; 302 } 303 else 304 { 305 TKTemplate temp = (TKTemplate)set.get(index); 306 if (comp.compare(temp, template) <= 0) 307 { 308 set.add(index, template); 309 greater = false; 310 } 311 } 312 index++; 313 } 314 } 316 } 317 Iterator iterator = set.iterator(); 318 319 count = 0; 320 boolean noCont = true; 321 while (iterator.hasNext() && noCont) 322 { 323 TKTemplate temp = (TKTemplate)iterator.next(); 324 if (count >= start && count < end) 325 { 326 temp.td.setMergedApply(td ); 327 result.append( listText.apply( temp.td ) ); 328 String controlVal = control == null ? null :(String ) temp.td.getVariable(control); 329 if (controlVal != null && controlVal.trim().equals("0")) 330 noCont = false; 331 } 332 count++; 333 } 334 } 335 else 336 { 337 while( cont ) 338 { 339 TKTemplate template = listText.getNewTemplate(); 340 template.setListIterator( td.getListIterator() ); 341 template.td.parent = td; 342 cont = (td.getListIterator().apply( template, count++, label ) ); 343 if( cont ) 344 { 345 template.td.setMergedApply( td ); String chunk = listText.apply( template.td ); 347 348 String controlVal = control == null ? null : 349 (String ) template.td.getVariable(control); 350 if (controlVal != null && controlVal.trim().equals("-1")) 351 { 352 cont = false; 353 } 354 else if (controlVal != null && controlVal.trim().equals("0")) 355 { 356 cont = false; 357 result.append(chunk); 358 } 359 else 360 { 361 result.append(chunk); 362 } 363 } 364 template = null; 365 } 366 } 367 writer.write(result.toString()); 368 } 369 370 class ComparableKey implements Comparator 371 { 372 ComparableKey() 373 { 374 } 375 376 379 public int compare(Object o1, Object o2) 380 { 381 TKTemplate t1 = (TKTemplate)o1; 382 TKTemplate t2 = (TKTemplate)o2; 383 Object value1 = t1.td.getVariable(field); 384 Object value2 = t2.td.getVariable(field); 385 try 386 { 387 if (value1 == null && value2 != null) 388 return direction.equals(UP) ? -1:1; 389 if (value1 == null && value2 == null) 390 return 0; 391 if (value1 != null && value2 == null) 392 return direction.equals(UP) ? 1:-1; 393 if (value1 != null && value2 != null) 394 { 395 if (type.equals(STRING)) 396 { 397 if (direction.equals(UP)) 398 return ((String )value1).compareTo((String )value2); 399 else 400 return ((String )value2).compareTo((String )value1); 401 } 402 else if (type.equals(NUMBER)) 403 { 404 Integer i1 = new Integer (value1.toString()); 405 Integer i2 = new Integer (value2.toString()); 406 if (direction.equals(UP)) 407 return i1.intValue() - i2.intValue(); 408 else 409 return i2.intValue() - i1.intValue(); 410 } 411 else if (type.equals(DATE)) 412 { 413 Date d1; 414 try 415 { 416 d1 = TKListTag.DATE_TIME_FORMAT.parse((String )value1); 417 } 418 catch (Exception parseError) 419 { 420 d1 = TKListTag.DATE_FORMAT.parse((String )value1); 422 } 423 Date d2; 424 try 425 { 426 d2 = TKListTag.DATE_TIME_FORMAT.parse((String )value2); 427 } 428 catch (Exception parseError) 429 { 430 d2 = TKListTag.DATE_FORMAT.parse((String )value2); 432 } 433 if (d1.before(d2)) 434 return direction.equals(UP) ? -1:1; 435 else if (d1.after(d2)) 436 return direction.equals(UP) ? 1:-1; 437 return 0; 438 } 439 } 440 } 441 catch (Exception e) 442 { 443 CAT.error("ComparableList : " ,e); 444 } 445 return 0; 446 } 447 } 448 } 449 450 451 | Popular Tags |