1 package dinamica; 2 3 import java.text.MessageFormat ; 4 import java.util.ArrayList ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.io.PrintWriter ; 8 import java.net.URLEncoder ; 9 import java.sql.Types ; 10 import javax.servlet.RequestDispatcher ; 11 import javax.servlet.ServletContext ; 12 import javax.servlet.http.*; 13 import java.util.Locale ; 14 import electric.xml.*; 15 16 99 public class TemplateEngine 100 { 101 102 103 private String _template = ""; 104 105 106 107 private HttpServletRequest _req = null; 108 109 110 private ServletContext _ctx = null; 111 112 113 private Locale _locale = null; 114 115 116 private IRowEvent _rowEvent = null; 117 118 119 private String _encoding = null; 120 121 125 public void setEncoding(String encoding) 126 { 127 _encoding = encoding; 128 } 129 130 135 public void setRowEventObject(IRowEvent obj) 136 { 137 _rowEvent = obj; 138 } 139 140 144 public void setLocale(Locale l) throws Throwable  145 { 146 _locale = l; 147 replaceLabels(); 148 } 149 150 154 public void setRequest(HttpServletRequest req) 155 { 156 _req = req; 157 } 158 159 166 public TemplateEngine(ServletContext ctx, HttpServletRequest req, String template) 167 { 168 _template = template; 169 _ctx = ctx; 170 _req = req; 171 172 try { 174 replaceDefaultValues(); 175 if (_req!=null && _ctx!=null) { 176 replaceSessionAttributes(); 177 replaceRequestAttributes(); 178 } 179 } catch (Throwable e) {} 180 181 } 182 183 196 public String getSql(Recordset rs) throws Throwable  197 { 198 199 try 200 { 201 202 if (rs!=null) 203 { 204 205 HashMap flds = rs.getFields(); 206 207 208 Iterator i = flds.values().iterator(); 209 while (i.hasNext()) 210 { 211 212 RecordsetField f = (RecordsetField)i.next(); 213 String fname = f.getName(); 214 Object value = rs.getValue(fname); 215 String marker = "${fld:" + fname + "}"; 216 217 if (value==null) 218 { 219 _template = StringUtil.replace(_template, marker, "NULL"); 220 } 221 else 222 { 223 switch (f.getType()) 224 { 225 case Types.VARCHAR: 226 case Types.CHAR: 227 case Types.LONGVARCHAR: 228 String v = (String )value; 229 v = StringUtil.replace(v,"'","''"); 230 _template = StringUtil.replace(_template, marker, "'" + v + "'"); 231 break; 232 233 case Types.DATE: 234 java.util.Date d = (java.util.Date )value; 235 _template = StringUtil.replace(_template, marker, "{d '" + StringUtil.formatDate(d, "yyyy-MM-dd") + "'}"); 236 break; 237 238 case Types.TIMESTAMP: 239 java.util.Date d1 = (java.util.Date )value; 240 _template = StringUtil.replace(_template, marker, "{ts '" + StringUtil.formatDate(d1, "yyyy-MM-dd HH:mm:ss:SSS") + "'}"); 241 break; 242 243 default: 244 String n = String.valueOf(value); 245 _template = StringUtil.replace(_template, marker, n); 246 break; 247 248 } 249 } 250 251 } 252 } 253 254 255 replaceDefaultValues(); 256 257 258 if (_ctx!=null) 259 { 260 replaceRequestAttributes(); 261 replaceSessionAttributes(); 262 263 ArrayList seqs = getMarkers("seq"); 264 265 266 Iterator is = seqs.iterator(); 267 while (is.hasNext()) 268 { 269 270 Marker m = (Marker)is.next(); 271 String seqType = m.getName(); String seqName = m.getExtraInfo(); String marker = "${seq:" + seqType + "@" + seqName + "}"; 274 275 276 String seqConfigParam = "sequence-" + seqType; 277 String seqExpr = _ctx.getInitParameter(seqConfigParam); 278 279 280 if (seqExpr==null || seqExpr.equals("")) 281 { 282 String args[] = {marker}; 283 String msg = Errors.SEQUENCE_BAD_CONFIGURATION; 284 msg = MessageFormat.format(msg, args); 285 throw new Throwable (msg); 286 } 287 288 289 String value = ""; 290 291 if (seqExpr.indexOf("${seq}")<0) 293 value = StringUtil.replace(seqExpr, "$[seq]", seqName); 294 else 295 value = StringUtil.replace(seqExpr, "${seq}", seqName); 296 298 _template = StringUtil.replace(_template, marker, value); 299 300 } 301 } 302 303 return _template; 304 } 305 catch (Throwable e) 306 { 307 String msg = "[TemplateEngine].\n Template:" + _template + "\n"; 308 String data = ""; 309 if (rs!=null) 310 { 311 data = rs.toString(); 312 System.err.println(msg + data); 313 } 314 throw e; 315 } 316 317 } 318 319 342 public void replaceDefaultValues() throws Throwable  343 { 344 345 if (_template.indexOf("${def:")< 0 ) 347 return; 348 349 String markers[] = { 350 "${def:user}", 351 "${def:date}", 352 "${def:time}", 353 "${def:timestamp}", 354 "${def:host}", 355 "${def:context}", 356 "${def:remoteaddr}", 357 "${def:uri}", 358 "${def:dateDMY}", 359 "${def:dateMDY}", 360 "${def:actionroot}" 361 }; 362 363 String values[] = new String [markers.length]; 364 365 String userid = null; 366 if (_req != null) userid = _req.getRemoteUser(); 367 if (userid == null) userid = ""; 368 369 java.util.Date d = new java.util.Date (); 370 values[0] = userid; 371 values[1] = StringUtil.formatDate(d, "yyyy-MM-dd"); 372 values[2] = StringUtil.formatDate(d, "HH:mm:ss"); 373 values[3] = StringUtil.formatDate(d, "yyyy-MM-dd HH:mm:ss:SSS"); 374 375 if (_req!=null) 376 values[4] = _req.getServerName(); else values[4] = ""; 377 378 if (_req!=null) 379 values[5] = _req.getContextPath(); else values[5] = ""; 380 381 if (_req!=null) 382 values[6] = _req.getRemoteAddr(); else values[6] = ""; 383 384 if (_req!=null) 385 values[7] = _req.getRequestURI(); else values[7] = ""; 386 387 values[8] = StringUtil.formatDate(d, "dd-MM-yyyy"); 388 values[9] = StringUtil.formatDate(d, "MM-dd-yyyy"); 389 390 if (_req!=null) 391 { 392 String path = (String )_req.getAttribute("dinamica.action.path"); 393 path = path.substring(0, path.lastIndexOf("/")); 394 values[10] = path; 395 } 396 else values[10] = ""; 397 398 for (int i=0;i<markers.length;i++) 399 { 400 _template = StringUtil.replace(_template,markers[i],values[i]); 401 } 402 403 } 404 405 408 public String toString() 409 { 410 return _template; 411 } 412 413 419 public ArrayList getMarkers(String prefix) throws Throwable  420 { 421 422 423 if (prefix.length()!=3) 424 { 425 String args[] = {prefix}; 426 String msg = Errors.INVALID_PREFIX; 427 msg = MessageFormat.format(msg, args); 428 throw new Throwable (msg); 429 } 430 431 int pos = 0; 432 ArrayList l = new ArrayList (); 433 434 435 while ( pos >= 0 ) 436 { 437 int pos1 = 0; 438 int pos2 = 0; 439 int newPos = 0; 440 441 442 pos1 = _template.indexOf("${" + prefix + ":", pos); 443 if (pos1>=0) 444 { 445 446 447 newPos = pos1 + 6; 448 pos2 = _template.indexOf("}", newPos); 449 450 if (pos2>0) 451 { 452 453 454 String fld = _template.substring(newPos, pos2); 455 Marker m = new Marker(fld,null,pos1,pos2); 456 457 458 int pos3 = fld.indexOf("@"); 459 if (pos3>0) 460 { 461 462 String name = fld.substring(0, pos3); 463 String extraInfo = fld.substring(pos3 + 1, fld.length()); 464 465 if ( (name.indexOf(" ")>=0) || (name.indexOf("\r")>=0) || (name.indexOf("\n")>=0) || (name.indexOf('\t')>=0) ) 466 { 467 String args[] = {name}; 468 String msg = Errors.INVALID_MARKER; 469 msg = MessageFormat.format(msg, args); 470 throw new Throwable (msg); 471 } 472 473 474 m.setName(name); 475 m.setExtraInfo(extraInfo); 476 } 477 478 l.add(m); 479 } 480 else 481 { 482 throw new Throwable ( Errors.MARKER_UNCLOSED ); 483 } 484 pos = pos2 + 1; 485 } 486 else 487 { 488 pos = -1; 489 } 490 } 491 492 return l; 493 494 } 495 496 504 public void replace(Recordset rs, String nullValueExpr) throws Throwable  505 { 506 507 508 ArrayList flds = getMarkers("fld"); 509 510 511 replace(rs, nullValueExpr, flds); 512 513 } 514 515 527 public void replace(Recordset rs, String nullValueExpr, String repeatSectionTag) throws Throwable  528 { 529 530 dinamica.parser.FastTemplateEngine fte = new dinamica.parser.FastTemplateEngine(); 531 532 String section = null; 533 String repeatTemplate = null; 534 int pos1 = 0; 535 int pos2 = 0; 536 537 String tagStart = "<" + repeatSectionTag + ">"; 538 String tagEnd = "</" + repeatSectionTag + ">"; 539 540 541 pos1 = _template.indexOf(tagStart); 542 if (pos1>=0) 543 { 544 545 546 int newPos = pos1 + tagStart.length(); 547 pos2 = _template.indexOf(tagEnd, newPos); 548 549 if (pos2>0) 550 { 551 552 section = _template.substring(pos1, pos2 + tagEnd.length()); 553 repeatTemplate = _template.substring(newPos, pos2); 554 555 556 StringBuffer buf = new StringBuffer (); 557 558 559 if (rs.getRecordCount()>0) 560 { 561 562 fte.setTemplate(repeatTemplate); 564 ArrayList markers = fte.getMarkers(); 565 566 rs.top(); 568 569 570 while (rs.next()) 571 { 572 573 setValues(fte, markers, rs, nullValueExpr); 574 575 String row = fte.toString(); 577 578 579 if (_rowEvent!=null) 580 { 581 row = _rowEvent.onNewRow( rs, row ); 582 } 583 584 585 buf.append(row); 587 588 } 589 _template = StringUtil.replace(_template, section, buf.toString()); 590 } 591 else 592 { 593 _template = StringUtil.replace(_template, section, ""); 594 } 595 596 } 597 else 598 { 599 String args[] = {repeatSectionTag}; 600 String msg = Errors.REPEAT_TAG_NOT_CLOSED; 601 msg = MessageFormat.format(msg, args); 602 throw new Throwable (msg); 603 } 604 605 } 606 else 607 { 608 String args[] = {repeatSectionTag}; 609 String msg = Errors.REPEAT_TAG_NOT_FOUND; 610 msg = MessageFormat.format(msg, args); 611 throw new Throwable (msg); 612 } 613 614 } 615 616 617 625 void replace(Recordset rs, String nullValueExpr, ArrayList markers) throws Throwable  626 { 627 628 String strValue = null; 629 Object value = null; 630 String toReplace = null; 631 632 633 HashMap rsFlds = rs.getFields(); 634 635 636 ArrayList flds = markers; 637 638 639 Iterator i = flds.iterator(); 640 while (i.hasNext()) 641 { 642 643 String formatPluginName = null; 644 IFormatPlugin fmtObj = null; 645 FormatPluginParser fpp = null; 646 647 648 Marker m = (Marker)i.next(); 649 String fName = m.getName(); 650 String fFormat = m.getExtraInfo(); 651 652 653 boolean found = true; 654 boolean fakeField = false; 655 if (!fName.equals("_rowNumber") && !fName.equals("_rowIndex")) 656 { 657 if (!rsFlds.containsKey(fName)) 658 found = false; 659 } 660 else 661 { 662 fakeField = true; 663 } 664 665 666 667 if (found) 668 { 669 670 String defDateFmt = null; 671 672 673 defDateFmt = _ctx.getInitParameter("def-format-date"); 674 if (defDateFmt==null || defDateFmt.equals("")) 675 defDateFmt = "dd-MM-yyyy"; 676 677 678 if (fFormat!=null) 679 { 680 toReplace = "${fld:" + fName + "@" + fFormat + "}"; 681 682 if (fFormat.startsWith("class:")) 684 { 685 formatPluginName = fFormat.substring(6); 686 fpp = new FormatPluginParser(formatPluginName); 687 fmtObj = (IFormatPlugin)Thread.currentThread().getContextClassLoader().loadClass(fpp.getName()).newInstance(); 688 } 689 } 690 else 691 toReplace = "${fld:" + fName + "}"; 692 693 694 value = rs.getValue(fName); 695 696 if (fmtObj!=null) 698 { 699 strValue = fmtObj.format(fName, rs, _locale, fpp.getArgs()); 700 } 701 else 702 { 703 704 705 if (value==null) 706 { 707 strValue=nullValueExpr; 708 } 709 else 710 { 711 712 713 RecordsetField f = null; 714 if (!fakeField) 715 f = (RecordsetField)rsFlds.get(fName); 716 else 717 f = new RecordsetField(fName,"INTEGER", Types.INTEGER); 718 719 720 721 if (f.getType()!=Types.DATE && f.getType()!=Types.TIMESTAMP ) 722 { 723 724 if (fFormat==null) 725 { 726 strValue = String.valueOf(value); 727 } 728 else 729 { 730 if (f.getType()==Types.VARCHAR || f.getType()==Types.CHAR || f.getType()==Types.CLOB || f.getType()==Types.LONGVARCHAR) 732 { 733 if (fFormat.equals("xml")) 734 { 735 strValue = encodeXML((String )value); 737 } 738 else if (fFormat.equals("html")) 739 { 740 strValue = encodeHTML((String )value); 742 } 743 else if (fFormat.equals("url")) 744 { 745 strValue = URLEncoder.encode((String )value, "UTF-8"); 747 } 748 else 749 { 750 throw new Throwable ("Invalid format mask for the field:" + fName); 751 } 752 753 } 754 else 756 { 757 758 if (_locale==null) 759 strValue = StringUtil.formatNumber(value, fFormat); 760 else 761 strValue = StringUtil.formatNumber(value, fFormat, _locale); 762 763 } 764 } 765 } 766 else 767 { 768 769 if (fFormat==null) 770 fFormat = defDateFmt; 771 if (_locale==null) 772 strValue = StringUtil.formatDate((java.util.Date )value, fFormat); 773 else 774 strValue = StringUtil.formatDate((java.util.Date )value, fFormat, _locale); 775 } 776 777 } 778 779 } 780 781 782 _template = StringUtil.replace(_template,toReplace,strValue); 783 784 } 785 786 } 787 788 } 789 790 791 808 public void replaceLabels() throws Throwable  809 { 810 811 if (_ctx==null) 812 throw new Throwable ("Servlet Context is null - this method can't work without a ServletContext."); 813 814 if (_template.indexOf("${lbl:")< 0 ) 816 return; 817 818 819 ArrayList flds = getMarkers("lbl"); 820 821 822 String xmlData = StringUtil.getResource(_ctx, "/WEB-INF/labels.xml"); 823 824 825 Document doc = new Document(xmlData); 826 Element root = doc.getRoot(); 827 828 829 String language = null; 830 if (_locale==null) 831 language = _ctx.getInitParameter("def-language"); 832 else 833 language = _locale.getLanguage(); 834 835 if (language==null || language.equals("")) 836 throw new Throwable ("Language not defined (Locale or default language may be null)"); 837 838 for (int i=0; i<flds.size();i++) 839 { 840 Marker m = (Marker) flds.get(i); 841 String name = m.getName(); 842 String label = "${" + "lbl:" + name + "}"; 843 _template = StringUtil.replace(_template, label, getLabel(name, root, language)); 844 } 845 846 } 847 848 856 String getLabel(String labelName, Element root, String language) throws Throwable  857 { 858 859 860 Element label = root.getElement( new XPath("label[@id='" + labelName + "']")); 861 if (label==null) 862 throw new Throwable ("Label not found: " + labelName); 863 864 865 866 Element translation = label.getElement( new XPath("value[@language='" + language + "']") ); 867 if (translation==null) 868 throw new Throwable ("Label translation not found for this language code: " + language); 869 870 return translation.getString(); 871 872 } 873 874 881 ArrayList getSegments() throws Throwable  882 { 883 884 ArrayList s = new ArrayList (); 885 886 887 ArrayList l = getMarkers("inc"); 888 889 if (l.size()>0) 890 { 891 int lastPos = 0; 892 for (int i=0; i<l.size();i++) 893 { 894 895 Marker m = (Marker) l.get(i); 896 TemplateSegment seg1 = new TemplateSegment(); 897 seg1.segmentType = "data"; 898 seg1.segmentData = _template.substring(lastPos, m.getPos1()); 899 900 TemplateSegment seg2 = new TemplateSegment(); 901 seg2.segmentType = "inc"; 902 seg2.segmentData = m.getName(); 903 904 lastPos = m.getPos2() + 1; 905 906 s.add(seg1); 907 s.add(seg2); 908 909 } 910 TemplateSegment seg1 = new TemplateSegment(); 911 seg1.segmentType = "data"; 912 seg1.segmentData = _template.substring(lastPos); 913 s.add(seg1); 914 915 } 916 else 917 { 918 919 TemplateSegment seg = new TemplateSegment(); 920 seg.segmentType = "data"; 921 seg.segmentData = _template; 922 s.add(seg); 923 } 924 925 return s; 926 } 927 928 944 public void print(HttpServletResponse res) throws Throwable  945 { 946 947 if (_ctx==null) 948 throw new Throwable ("ServletContext is null - can't print template because the request dispatcher must be obtained from the ServletContext."); 949 950 replaceDefaultValues(); 951 replaceLabels(); 952 replaceRequestAttributes(); 953 replaceSessionAttributes(); 954 955 PrintWriter pw = res.getWriter(); 956 957 if (_template.indexOf("${inc:")>=0) 959 { 960 ArrayList s = getSegments(); 961 for (int i=0; i<s.size();i++) 962 { 963 TemplateSegment t = (TemplateSegment)s.get(i); 964 if (t.segmentType.equals("inc")) 965 { 966 RequestDispatcher rd = _ctx.getRequestDispatcher(t.segmentData); 967 rd.include(_req, res); 968 } 969 else 970 { 971 pw.print(t.segmentData); 972 } 973 } 974 } 975 else 976 { 977 byte body[] = null; 979 if (_encoding!=null) 980 body = _template.getBytes(_encoding); 981 else 982 body = _template.getBytes(); 983 984 res.setContentLength(body.length); 985 pw.print(_template); 986 } 987 988 } 989 990 1002 public void setComboValue(String controlName, String value) throws Throwable  1003 { 1004 int pos1 = 0; 1005 int pos2 = 0; 1006 String combo = ""; 1007 1008 1009 String find = "<select name=\"" + controlName + "\""; 1010 1011 1012 pos1 = _template.indexOf(find); 1013 1014 1015 if (pos1>=0) 1016 { 1017 1018 pos2 = _template.indexOf("</select>", pos1); 1019 if (pos2>0) 1020 { 1021 1022 1023 int newpos2 = pos2 + "</select>".length(); 1024 combo = _template.substring(pos1, newpos2); 1025 1026 1027 find = "<option value=\"" + value + "\""; 1028 String newItem = find + " selected "; 1029 String temp = StringUtil.replace(combo, find, newItem); 1030 1031 1032 _template = StringUtil.replace(_template, combo, temp); 1033 1034 } 1035 else 1036 { 1037 throw new Throwable ("Can't find closing tag for this HTML control: " + controlName); 1038 } 1039 } 1040 else 1041 { 1042 throw new Throwable ("HTML control not found: " + controlName); 1043 } 1044 1045 } 1046 1047 1057 public void setComboValue(String controlName, Recordset rs) throws Throwable  1058 { 1059 if (rs.getRecordCount()==0) 1060 return; 1061 1062 rs.top(); 1063 while (rs.next()) 1064 { 1065 1066 String value = String.valueOf(rs.getValue(controlName)); 1067 setComboValue(controlName, value); 1068 } 1069 } 1070 1071 1072 1083 public void setRadioButton(String controlName, String value) throws Throwable  1084 { 1085 1086 int pos1 = 0; 1087 int pos2 = 0; 1088 String ctrl = ""; 1089 int flag = 0; 1090 int pos = 0; 1091 1092 while (flag >= 0) 1093 { 1094 1095 1096 String find = "<input"; 1097 1098 1099 pos1 = _template.indexOf(find,pos); 1100 1101 1102 if (pos1>=0) 1103 { 1104 flag = 1; 1105 1106 1107 pos2 = _template.indexOf(">", pos1); 1108 if (pos2>0) 1109 { 1110 1111 1112 int newpos2 = pos2 + ">".length(); 1113 ctrl = _template.substring(pos1, newpos2); 1114 1115 1116 find = "name=\"" + controlName + "\""; 1117 int newpos1 = ctrl.indexOf(find); 1118 1119 1120 if (newpos1>=0) 1121 { 1122 1123 1124 find = "value=\"" + value + "\""; 1125 String newItem = find + " checked "; 1126 String temp = StringUtil.replace(ctrl, find, newItem); 1127 1128 1129 if (!temp.equals(ctrl)) 1130 { 1131 _template = StringUtil.replace(_template, ctrl, temp); 1132 return; 1133 } 1134 else 1135 { 1136 ctrl = temp; 1137 } 1138 1139 } 1140 pos = pos1 + ctrl.length(); 1141 } 1142 else 1143 { 1144 throw new Throwable ("'input' Tag is not properly closed for this HTML control: " + controlName); 1145 } 1146 } 1147 else 1148 { 1149 flag = -1; 1150 } 1151 1152 } 1153 1154 } 1155 1156 1166 public void setCheckbox(String controlName, Recordset rs) throws Throwable  1167 { 1168 if (rs.getRecordCount()==0) 1169 return; 1170 1171 rs.top(); 1172 while (rs.next()) 1173 { 1174 1175 String value = String.valueOf(rs.getValue(controlName)); 1176 setRadioButton(controlName, value); 1177 } 1178 } 1179 1180 1181 1186 public void setTemplate(String string) 1187 { 1188 _template = string; 1189 } 1190 1191 1200 public String getTagBody(String tagName) throws Throwable  1201 { 1202 1203 int pos1 = 0; 1204 int pos2 = 0; 1205 1206 String tagStart = "<" + tagName + ">"; 1207 String tagEnd = "</" + tagName + ">"; 1208 1209 1210 pos1 = _template.indexOf(tagStart); 1211 if (pos1>=0) 1212 { 1213 1214 1215 int newPos = pos1 + tagStart.length(); 1216 pos2 = _template.indexOf(tagEnd, newPos); 1217 1218 if (pos2>0) 1219 { 1220 1221 return _template.substring(pos1, pos2 + tagEnd.length()); 1222 } 1223 else 1224 { 1225 String args[] = {tagName}; 1226 String msg = Errors.REPEAT_TAG_NOT_CLOSED; 1227 msg = MessageFormat.format(msg, args); 1228 throw new Throwable (msg); 1229 } 1230 1231 } 1232 else 1233 { 1234 String args[] = {tagName}; 1235 String msg = Errors.REPEAT_TAG_NOT_FOUND; 1236 msg = MessageFormat.format(msg, args); 1237 throw new Throwable (msg); 1238 } 1239 1240 1241 } 1242 1243 1248 public void replaceRequestAttributes() throws Throwable  1249 { 1250 if (_ctx==null) 1251 throw new Throwable ("Servlet Context is null - this method can't work without a ServletContext."); 1252 1253 if (_template.indexOf("${req:")< 0 ) 1255 return; 1256 1257 1258 ArrayList flds = getMarkers("req"); 1259 1260 for (int i=0; i<flds.size();i++) 1261 { 1262 Marker m = (Marker) flds.get(i); 1263 1264 String name = m.getName(); 1265 1266 String fmt = m.getExtraInfo(); 1268 if (fmt==null) 1269 fmt = ""; 1270 else 1271 fmt = "@" + fmt; 1272 String label = "${" + "req:" + name + fmt + "}"; 1273 1274 1278 String value = (String )_req.getAttribute(name); 1279 if (value!=null) 1280 { 1281 if (!fmt.equals("")) 1282 { 1283 if (fmt.equals("@xml")) 1284 value = encodeXML(value); 1285 else if (fmt.equals("@html")) 1286 value = encodeHTML(value); 1287 else if (fmt.equals("@url")) 1288 value = URLEncoder.encode(value, "UTF-8"); 1289 else 1290 throw new Throwable ("Invalid encoding directive for request attribute: " + name); 1291 } 1292 _template = StringUtil.replace(_template, label, value); 1293 } 1294 } 1295 } 1296 1297 1302 public void replaceSessionAttributes() throws Throwable  1303 { 1304 1305 if (_req==null) 1306 throw new Throwable ("Request is null - this method can't work without a Request object."); 1307 1308 if (_template.indexOf("${ses:")< 0 ) 1310 return; 1311 1312 HttpSession session = _req.getSession(true); 1314 1315 1316 ArrayList flds = getMarkers("ses"); 1317 1318 for (int i=0; i<flds.size();i++) 1319 { 1320 Marker m = (Marker) flds.get(i); 1321 String name = m.getName(); 1322 1323 Object obj = session.getAttribute(name); 1325 1326 if (obj==null) 1327 throw new Throwable ("Cannot find Session attribute [" + name + "]; UserID: " + _req.getRemoteUser() + "; Session isNew: " + session.isNew() + "; "); 1328 1329 String value = String.valueOf(obj); 1331 String label = "${" + "ses:" + name + "}"; 1332 _template = StringUtil.replace(_template, label, value); 1333 1334 } 1335 1336 } 1337 1338 1344 public String encodeXML(String input) 1345 { 1346 input = StringUtil.replace(input, "&", "&"); 1347 input = StringUtil.replace(input, "<", "<"); 1348 input = StringUtil.replace(input, ">", ">"); 1349 input = StringUtil.replace(input, "'", "'"); 1350 input = StringUtil.replace(input, "\"", """); 1351 1352 return input; 1353 } 1354 1355 1361 public String encodeHTML(String input) 1362 { 1363 input = StringUtil.replace(input, "&", "&"); 1364 input = StringUtil.replace(input, "<", "<"); 1365 input = StringUtil.replace(input, ">", ">"); 1366 input = StringUtil.replace(input, "\"", """); 1367 1368 return input; 1369 } 1370 1371 1376 public void replace(String toReplace, String newValue) 1377 { 1378 _template = StringUtil.replace(_template, toReplace, newValue); 1379 } 1380 1381 1389 public void setValues(dinamica.parser.FastTemplateEngine fte, ArrayList markers, Recordset rs, String nullExpression) throws Throwable  1390 { 1391 1392 String strValue = null; 1393 Object value = null; 1394 1395 1396 String defDateFmt = null; 1397 defDateFmt = _ctx.getInitParameter("def-format-date"); 1398 if (defDateFmt==null || defDateFmt.equals("")) 1399 defDateFmt = "dd-MM-yyyy"; 1400 1401 1402 1403 HashMap rsFlds = rs.getFields(); 1404 1405 1406 ArrayList flds = markers; 1407 1408 1409 Iterator i = flds.iterator(); 1410 while (i.hasNext()) 1411 { 1412 1413 String formatPluginName = null; 1414 IFormatPlugin fmtObj = null; 1415 FormatPluginParser fpp = null; 1416 1417 1418 dinamica.parser.Marker m = (dinamica.parser.Marker)i.next(); 1419 String fName = m.getColumnName(); 1420 String fFormat = m.getFormat(); 1421 1422 1423 boolean found = true; 1424 boolean fakeField = false; 1425 if (!fName.equals("_rowNumber") && !fName.equals("_rowIndex")) 1426 { 1427 if (!rsFlds.containsKey(fName)) 1428 found = false; 1429 } 1430 else 1431 { 1432 fakeField = true; 1433 } 1434 1435 1436 1437 if (found) 1438 { 1439 1440 if (fFormat!=null) 1441 { 1442 if (fFormat.startsWith("class:")) 1444 { 1445 formatPluginName = fFormat.substring(6); 1446 fpp = new FormatPluginParser(formatPluginName); 1447 fmtObj = (IFormatPlugin)Thread.currentThread().getContextClassLoader().loadClass(fpp.getName()).newInstance(); 1448 } 1449 } 1450 1451 1452 value = rs.getValue(fName); 1453 1454 if (fmtObj!=null) 1455 { 1456 strValue = fmtObj.format(fName, rs, _locale, fpp.getArgs()); 1457 } 1458 else 1459 { 1460 1461 if (value==null) 1462 { 1463 strValue=nullExpression; 1464 } 1465 else 1466 { 1467 1468 1469 1470 RecordsetField f = null; 1471 if (!fakeField) 1472 f = (RecordsetField)rsFlds.get(fName); 1473 else 1474 f = new RecordsetField(fName,"INTEGER", Types.INTEGER); 1475 1476 1477 1478 if (f.getType()!=Types.DATE && f.getType()!=Types.TIMESTAMP ) 1479 { 1480 1481 if (fFormat==null) 1482 { 1483 strValue = String.valueOf(value); 1484 } 1485 else 1486 { 1487 if (f.getType()==Types.VARCHAR || f.getType()==Types.CHAR || f.getType()==Types.CLOB ) 1489 { 1490 if (fFormat.equals("xml")) 1491 { 1492 strValue = encodeXML((String )value); 1494 } 1495 else if (fFormat.equals("url")) 1496 { 1497 strValue = URLEncoder.encode((String )value, "UTF-8"); 1499 } 1500 else 1501 { 1502 throw new Throwable ("Invalid format mask for the field:" + fName); 1503 } 1504 1505 } 1506 else 1508 { 1509 1510 if (_locale==null) 1511 strValue = StringUtil.formatNumber(value, fFormat); 1512 else 1513 strValue = StringUtil.formatNumber(value, fFormat, _locale); 1514 1515 } 1516 } 1517 } 1518 else 1519 { 1520 1521 if (fFormat==null) 1522 fFormat = defDateFmt; 1523 if (_locale==null) 1524 strValue = StringUtil.formatDate((java.util.Date )value, fFormat); 1525 else 1526 strValue = StringUtil.formatDate((java.util.Date )value, fFormat, _locale); 1527 } 1528 1529 } 1530 1531 } 1532 1533 fte.setValue(m.getKey(), strValue); 1535 1536 } 1537 1538 } 1539 1540 } 1541 1542 1547 public void clearFieldMarkers() throws Throwable  1548 { 1549 1550 String toReplace = null; 1551 1552 1553 ArrayList flds = getMarkers("fld"); 1554 1555 1556 Iterator i = flds.iterator(); 1557 while (i.hasNext()) 1558 { 1559 1560 1561 Marker m = (Marker)i.next(); 1562 String fName = m.getName(); 1563 String fFormat = m.getExtraInfo(); 1564 1565 if (fFormat!=null) 1566 toReplace = "${fld:" + fName + "@" + fFormat + "}"; 1567 else 1568 toReplace = "${fld:" + fName + "}"; 1569 1570 1571 _template = StringUtil.replace(_template,toReplace,""); 1572 1573 } 1574 1575 } 1576 1577} 1578
| Popular Tags
|