1 package com.instantbank.lettertemplate.editor.web;
2
3 import java.io.File;
4 import java.io.PrintWriter;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.io.BufferedOutputStream;
8 import java.io.FileOutputStream;
9 import java.io.FileNotFoundException;
10 import java.io.Reader;
11 import java.io.BufferedReader;
12 import java.io.StringReader;
13 import java.io.StringWriter;
14 import java.io.ByteArrayInputStream;
15 import java.io.ByteArrayOutputStream;
16
17 import java.util.ArrayList;
18 import java.util.HashSet;
19 import java.util.Set;
20 import java.util.Hashtable;
21 import java.util.Iterator;
22 import java.util.Set;
23 import java.util.Enumeration;
24 import java.util.Locale;
25 import java.util.Calendar;
26 import java.util.Date;
27 import java.text.SimpleDateFormat;
28
29 import javax.xml.transform.Templates;
30 import javax.xml.transform.TransformerConfigurationException;
31 import javax.xml.transform.TransformerFactory;
32 import javax.xml.transform.Result;
33
34 import javax.xml.transform.sax.SAXSource;
35 import javax.xml.transform.sax.SAXTransformerFactory;
36 import javax.xml.transform.sax.TransformerHandler;
37 import javax.xml.transform.sax.SAXResult;
38 import javax.xml.transform.sax.TemplatesHandler;
39
40 import javax.xml.transform.stream.StreamResult;
41
42 import org.apache.fop.apps.Driver;
43 import org.apache.fop.apps.FOPException;
44
45 import org.apache.log.LogTarget;
46 import org.apache.log.Hierarchy;
47
48 import org.apache.log.format.PatternFormatter;
49
50 import org.apache.log.output.io.StreamTarget;
51
52 import org.apache.log4j.Category;
53
54 import org.xml.sax.SAXException;
55 import org.xml.sax.XMLReader;
56 import org.xml.sax.InputSource;
57
58 import org.xml.sax.helpers.XMLReaderFactory;
59
60 import com.instantbank.common.utilcomponents.Debug;
61 import com.instantbank.common.utilcomponents.CommonUtil;
62 import com.instantbank.common.utilcomponents.JNDINames;
63 import com.instantbank.common.utilcomponents.LetterTemplateExceptionMessage;
64 import com.instantbank.common.utilcomponents.LetterTemplateGlobals;
65 import com.instantbank.common.utilcomponents.DAOException;
66
67 import com.instantbank.component.lettertemplate.util.VariablesFormat;
68 import com.instantbank.component.lettertemplate.ejb.LetterTemplate;
69 import com.instantbank.component.job.util.Field;
70
71 import com.instantbank.lettertemplate.control.LetterTemplateEventException;
72 import com.instantbank.lettertemplate.editor.util.LetterOp;
73
74 import ru.novosoft.rtf2fo.RTFReader;
75 import ru.novosoft.rtf2fo.FOBuilder;
76
77
85 public class TemplateTransformer {
86
87
90 public static final String IMGMARK = "ef\'\\";
91
92
97 public static final String VARMARK = "ff\'\\";
98
99
102 public static final char CHARIMGMARK = '\u00FE';
103
104
107 public static final String openRtfVarTag
108 = "{\\field{\\*\\fldinst { DOCVARIABLE ";
109
110
113 public static final String closeRtfVarTag
114 = " \\\\* MERGEFORMAT }}{\\fldrslt {none}}}";
115
116
120 public static final String FO_EXT = ".fo";
121
122
126 public static final String IMAGES_DIR = ".images";
127
128
131 public static final int RENDER_PDF = Driver.RENDER_PDF;
132
133
136 public static final int RENDER_TXT = Driver.RENDER_TXT;
137
138
141 String foTemplate;
142
143
146 private String foFileName;
147
148
151 protected String workDir;
152
153
156 private int renderMode = RENDER_PDF;
157
158
161 private static Debug debug;
162
163
164
165
169 private XMLReader reader;
170
171
175 private Templates xslTemplates;
176
177
180 private SAXTransformerFactory saxFactory;
181
182
185 private VariablesFormat varFormatter;
186
187
191 private ArrayList[] theImages;
192
193
196 private ArrayList[] theVars;
197
198
201 private String[] varsOffsetType;
202
203
206 private Hashtable hashFields;
207
208
211 private Hashtable calendars;
212
213
216 private LetterTemplate letejb;
217
218
221 private static String rtf2foconfdir;
222
225 private static Category cat;
226
227
230 private static SimpleDateFormat dateFormater;
231
232
235 org.apache.log.Logger foplog;
236
237 static {
238 debug = new Debug();
239 debug.setDebugginOn(true);
240 debug.setPreMessage("** TemplateTransformer: ");
241
242 dateFormater =
243 new SimpleDateFormat(
244 LetterTemplateGlobals.DATE_PARSING_PATTERN, Locale.US);
245
246 rtf2foconfdir =
247 CommonUtil.getApplicationProperty(JNDINames.RTF2FO_CONFIG_PATH);
248
249 System.setProperty("rtf2fo.home", "file:/" + rtf2foconfdir);
250 System.setProperty("log4j.configuration",
251 "file:/" + rtf2foconfdir + "/conf/defaultLog.properties");
252
253 cat = Category.getInstance(TemplateTransformer.class.getName());
254
255 }
256
257
258
270 public TemplateTransformer(String tmpDir, String foTemplate,
271 ArrayList[] theImages, ArrayList[] theVars,
272 String foFileName, LetterTemplate letejb)
273 throws LetterTemplateEventException {
274
275 debug.println("Rtf2FoConfdir = " + rtf2foconfdir);
276 workDir = tmpDir;
277
278 varFormatter = new VariablesFormat();
279
280 this.foTemplate = foTemplate;
281 this.foFileName = foFileName;
282 this.theImages = theImages;
283 this.theVars = theVars;
284 this.letejb = letejb;
285
286 initFopLogger();
287
288 initfiller();
289
290
291 this.toFoFile();
292 try {
293 this.hashFields = letejb.loadFields();
294 initOffsetType(theVars, hashFields);
295 }
296 catch(Exception ex) {
297 debug.println(CommonUtil.stackTraceToString(ex));
298 throw new LetterTemplateEventException
299 ("Field information can not be loaded");
300 }
301
302 try {
303 this.calendars = letejb.loadCalendars();
304 }
305 catch(Exception ex) {
306 debug.println(CommonUtil.stackTraceToString(ex));
307 throw new LetterTemplateEventException
308 ("Calendar information can not be loaded");
309 }
310 }
311
312
313
324 public TemplateTransformer(ArrayList[] theVars, Hashtable hashFields)
325 throws LetterTemplateEventException {
326
327 this.theVars = theVars;
328 this.hashFields = hashFields;
329 initOffsetType(theVars, hashFields);
330
331 }
332
333
334
337 private void initFopLogger() {
338 Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
339
340 PatternFormatter formatter = new PatternFormatter(
341 "[%{priority}]: %{message}\n%{throwable}");
342 LogTarget target = null;
343 try {
344 target = new StreamTarget(
345 new FileOutputStream(workDir + "/fop.log"), formatter);
346 }
347 catch(Exception ex) {
348 debug.println("fop logger file couln't be opened");
349 }
350 hierarchy.setDefaultLogTarget(target);
351 foplog = hierarchy.getLoggerFor("fop");
352 foplog.setPriority(org.apache.log.Priority.DEBUG);
353 debug.println("Everything was right initiating FopLogger");
354 }
355
356
357
367 private void initOffsetType(ArrayList[] theVars, Hashtable hashFields) {
368 varsOffsetType = new String[theVars.length];
369 for(int i = 0; i < theVars.length; i++) {
370 Long varCode = (Long)theVars[i].get(0);
371 Field field = (Field)hashFields.get(varCode);
372 varsOffsetType[i] = field.getOffsetType();
373 }
374 }
375
376
377
385 public Set offsetConsistency() {
386 Set res = new HashSet();
387 for(int i = 0; i < theVars.length; i++) {
388 if(varsOffsetType[i].equals(LetterTemplateGlobals.NO_OFFSET)
389 && nonullOffset((ArrayList)theVars[i].get(1))) {
390 res.add(theVars[i].get(0));
391 }
392 }
393 return res;
394 }
395
396
397
403 private static boolean nonullOffset(ArrayList fmtDelta) {
404 Iterator it = fmtDelta.iterator();
405 while(it.hasNext()) {
406 Long[] fd = (Long[])it.next();
407 if(fd[1].longValue() != 0) {
408 return true;
409 }
410 }
411 return false;
412 }
413
414
415
420 public Hashtable getHashFields() {
421 return hashFields;
422 }
423
424
425
428 private void initfiller() {
429 try {
430 TransformerFactory transformerFactory = TransformerFactory.newInstance();
431 if(transformerFactory.getFeature(SAXSource.FEATURE)
432 && transformerFactory.getFeature(SAXResult.FEATURE)) {
433 saxFactory =
434 ((SAXTransformerFactory)transformerFactory);
435
436 TemplatesHandler templatesHandler = saxFactory.newTemplatesHandler();
437
438 reader = XMLReaderFactory.createXMLReader();
439 reader.setContentHandler(templatesHandler);
440
441
442 reader.parse(
443 new InputSource(
444 new ByteArrayInputStream(LetterTemplateGlobals.fillSheet.getBytes())));
445
446
447 xslTemplates = templatesHandler.getTemplates();
448 }
449 }
450 catch(TransformerConfigurationException tex) {
451 cat.debug(tex.getMessage());
452 }
453 catch(SAXException sex) {
454 cat.debug(CommonUtil.stackTraceToString(sex));
455 }
456 catch(IOException iox) {
457 cat.debug(CommonUtil.stackTraceToString(iox));
458 }
459 }
460
461
462
472 public static String rtftoFoTemplate(String data) {
473
474 ru.novosoft.rtf2fo.Options opt = new ru.novosoft.rtf2fo.Options();
475
476
477 opt.generateFields = true;
478
479
480 opt.noTextBoxes = true;
481 opt.noUseContentTabs = true;
482 opt.useMasterName = false;
483 opt.generateSimpleFontFamily = true;
484
485 try {
486 StringWriter writer = new StringWriter();
487 Reader reader = new BufferedReader(new StringReader(data));
488 RTFReader rtfReader = new RTFReader(reader, null, null);
489 FOBuilder builder = new FOBuilder(rtfReader, writer, opt);
490
491 rtfReader.setControlHandler(builder);
492 rtfReader.setCharacterHandler(builder);
493 rtfReader.process();
494
495 writer.flush();
496 writer.close();
497 return writer.toString();
498 }
499 catch(IOException iox) {
500 debug.println("Problems in toFoTemplate");
501 debug.println(CommonUtil.stackTraceToString(iox));
502 return null;
503 }
504 }
505
506
507
521 public static String substituteVariableTags(String rtf, ArrayList[] theVariables)
522 throws LetterTemplateEventException {
523
524 StringBuffer buff = new StringBuffer(rtf);
525 buff.reverse();
526 String search = buff.toString();
527
528 ArrayList var;
529 int iSch = search.indexOf(VARMARK);
530 int gap = 0;
531 int pos = iSch + gap;
532
533 String varTag;
534
535 String varName;
536 Long varCode;
537 Long fmtCode;
538
539 int varIdx = theVariables.length - 1;
540 while(iSch >= 0) {
541 pos = iSch + gap;
542 if(iSch + 4 < search.length() && search.charAt(iSch + 4) == '\\') {
543 iSch += 5;
544 }
545 else {
546 if(varIdx < 0) {
547 throw
548 new LetterTemplateEventException(
549 LetterTemplateExceptionMessage.EXTRA_VAR_POSITION);
550 }
551 var = theVariables[varIdx--];
552 varName = buildVarName(var);
553 varTag = rtfVariableTag(varName);
554 buff.replace(pos, pos + 4, varTag);
555 iSch += 4;
556 gap += varTag.length() - 4;
557 }
558 iSch = search.indexOf(VARMARK, iSch);
559
560 }
561
562 if(varIdx >= 0) {
563 var = theVariables[varIdx--];
564 varCode = (Long)var.get(1);
565 debug.println("Missing pos for var = " + varCode);
566 throw
567 new LetterTemplateEventException(
568 LetterTemplateExceptionMessage.VAR_POSITION_MISSING);
569 }
570
571 buff.reverse();
572 return buff.toString();
573 }
574
575
576 private static String buildVarName(ArrayList var) {
577 return buildVarName((Long)var.get(1), (Long)var.get(2), (Long)var.get(3));
578 }
579
580
581 private static String buildVarName(Long varCode, Long fmtCode, Long delta) {
582 String offset = "";
583 long off = delta.longValue();
584 if(off > 0) {
585 offset = "_p" + off;
586 }
587 else if(off < 0) {
588 offset = "_m" + (-off);
589 }
590
591 return "f" + fmtCode + offset + "_v" + varCode;
592 }
593
594
595
602 private static String rtfVariableTag(String varName) {
603 String res = openRtfVarTag + varName + closeRtfVarTag;
604 StringBuffer buff = new StringBuffer(res);
605 buff.reverse();
606 return buff.toString();
607 }
608
609
610
619 public static String actualFontTable(String fontTable) {
620 StringBuffer buff = new StringBuffer(fontTable);
621 debug.println("fonttable = " + fontTable);
622 int fnilPos;
623 int semiPos;
624 int gap;
625 int bufPos;
626 int width;
627 try {
628 for(fnilPos = fontTable.indexOf("\\fnil"), gap = 0;
629 fnilPos >= 0;
630 fnilPos = fontTable.indexOf("\\fnil", semiPos)) {
631
632 bufPos = fnilPos + gap;
633 semiPos = fontTable.indexOf(";", fnilPos);
634 width = semiPos - fnilPos;
635 int whitePos = fontTable.indexOf(' ', fnilPos);
636 String family = actualFamily(fontTable.substring(whitePos + 1, semiPos));
637 debug.println("newFamily = " + family);
638 buff.replace(bufPos, bufPos + width, family);
639 debug.println("table buffer = " + buff.toString());
640 gap = gap + family.length() - width;
641 }
642 return buff.toString();
643 }
644 catch(Throwable tex) {
645 debug.println("Problems converting fontable: " + fontTable);
646 debug.println(CommonUtil.stackTraceToString(tex));
647 }
648
649 return fontTable;
650 }
651
652
653
660 public static String actualFamily(String fontName) {
661 int i;
662 for(i = 0; i < LetterOp.FONTNAME.length; i++) {
663 if(LetterOp.FONTNAME[i].equals(fontName)) {
664 break;
665 }
666 }
667
668 if(i >= LetterOp.FONTNAME.length) {
669 return "\\fnil " + fontName;
670 }
671
672 return LetterOp.FONTFAMILY[i] + " " + LetterOp.FONTALTNAME[i];
673 }
674
675
676
687 public static ArrayList[] setOfVariables(ArrayList allVars) {
688
689 if(allVars.size() == 0) {
690 return new ArrayList[0];
691 }
692
693
694 Set set = new HashSet();
695 Iterator it = allVars.iterator();
696 while(it.hasNext()) {
697 ArrayList slot = (ArrayList)it.next();
698 ArrayList setSlot = new ArrayList(3);
699 setSlot.add((Long)slot.get(1));
700 setSlot.add((Long)slot.get(2));
701 setSlot.add((Long)slot.get(3));
702 set.add(setSlot);
703 }
704
705
706
707
708
709
710
711 Hashtable ht = new Hashtable();
712 it = set.iterator();
713 while(it.hasNext()) {
714 ArrayList setSlot = (ArrayList)it.next();
715 Long varCode = (Long)setSlot.get(0);
716 Long[] fmtDelta = new Long[]{(Long)setSlot.get(1), (Long)setSlot.get(2)};
717 ArrayList formats = (ArrayList)ht.get(varCode);
718 if(formats == null) {
719 formats = new ArrayList(1);
720 formats.add(fmtDelta);
721 ht.put(varCode, formats);
722 }
723 else {
724 formats.add(fmtDelta);
725 }
726 }
727
728
729 ArrayList tmp = new ArrayList();
730 Enumeration e = ht.keys();
731 int k = 0;
732 while(e.hasMoreElements()) {
733 ArrayList slot = new ArrayList(2);
734 Long varCode = (Long)e.nextElement();
735 slot.add(0, varCode);
736 slot.add(1, ht.get(varCode));
737
738
739 int m = 0;
740 while(m < tmp.size()) {
741 long mCode = ((Long)((ArrayList)tmp.get(m)).get(0)).longValue();
742 if(varCode.longValue() < mCode) {
743 tmp.add(m, slot);
744 break;
745 }
746 m++;
747 }
748 if(m == tmp.size()) {
749 tmp.add(slot);
750 }
751 }
752
753
754 ArrayList res[] = new ArrayList[tmp.size()];
755 for(int l = 0; l < res.length; l++) {
756 res[l] = (ArrayList)tmp.get(l);
757 }
758 return res;
759 }
760
761
762
767 public static void printSetOfVars(ArrayList[] set) {
768 debug.println("SetOfVars:");
769 StringBuffer sb;
770 for(int i = 0; i < set.length; i++) {
771 sb = new StringBuffer();
772 ArrayList setSlot = (ArrayList)set[i];
773 Long code = (Long)setSlot.get(0);
774 ArrayList fmtDelta = (ArrayList)setSlot.get(1);
775 Iterator ik = fmtDelta.iterator();
776 sb.append(code.toString() + " => [");
777 while(ik.hasNext()) {
778 Long[] fmtOff = (Long[])ik.next();
779 sb.append("(" + fmtOff[0] + "," + fmtOff[1] + ")");
780 }
781 sb.append(" ]");
782 debug.println(sb.toString());
783 }
784 }
785
786
787
793 public static void printAllVars(ArrayList allVars) {
794 if(allVars == null) {
795 return;
796 }
797 StringBuffer sb = new StringBuffer(25);
798 for(int i = 0; i < allVars.size(); i++) {
799 ArrayList slot = (ArrayList)allVars.get(i);
800 sb.append("AllVars = (");
801 sb.append((Long)slot.get(0));
802 sb.append(", ");
803 sb.append((Long)slot.get(1));
804 sb.append(", ");
805 sb.append((Long)slot.get(2));
806 sb.append(", ");
807 sb.append((Long)slot.get(3));
808 sb.append(')');
809 debug.println(sb.toString());
810 }
811 }
812
813
814
821 public static void printRawVars(Long[] varCodes, String[] varValues) {
822 debug.println("Raw Var Values:");
823 int length = varCodes.length;
824 if(length > varValues.length) {
825 length = varValues.length;
826 debug.println("varCodes longer that varValues!!");
827 }
828 else if(length < varValues.length) {
829 debug.println("varCodes shorter that varValues!!");
830 }
831
832 for(int i = 0; i < length; i++) {
833 debug.println("var(" + varCodes[i] + ") = " + varValues[i]);
834 }
835 }
836
837
838
845 public static Long[] varCodesFromSet(ArrayList[] set) {
846
847 Long res[] = new Long[set.length];
848 for(int i = 0; i < set.length; i++) {
849 res[i] = (Long)set[i].get(0);
850 }
851
852 return res;
853 }
854
855
856
865 public String toFoFile() throws LetterTemplateEventException {
866
867 String unfilledFo = foTemplate;
868
869
870 debug.println("before: template.hasImages() ?");
871 if(theImages.length > 0) {
872 debug.println("template has images");
873 unfilledFo = substituteImages(workDir, unfilledFo);
874 if(unfilledFo == null) {
875 throw
876 new LetterTemplateEventException(
877 LetterTemplateExceptionMessage.PROBLEMS_SUBSTITUTING_IMAGES);
878 }
879 }
880
881 try {
882 FileOutputStream out =
883 new FileOutputStream(new File(workDir, foFileName + FO_EXT));
884 out.write(unfilledFo.getBytes());
885 out.flush();
886 out.close();
887 return foFileName + FO_EXT;
888 }
889 catch(IOException ex) {
890 debug.println("Problems recording a fo-template");
891 return null;
892 }
893
894 }
895
896
897
912 public String substituteImages(String basePath, String unfilledFo)
913 throws LetterTemplateEventException {
914 File imageDir;
915
916 StringWriter writer = new StringWriter();
917
918 imageDir = new File(basePath, foFileName + IMAGES_DIR);
919 if(!imageDir.mkdir()) {
920 return null;
921 }
922
923 try {
924 int i = 0;
925 int c;
926
927 int imgIdx = 0;
928 int numpict = 0;
929 ArrayList slot;
930 byte[] rawImage;
931 while(i < unfilledFo.length()) {
932 c = unfilledFo.charAt(i);
933 if(c != CHARIMGMARK) {
934 writer.write(c);
935 }
936 else {
937 if(imgIdx >= theImages.length) {
938 throw new
939 LetterTemplateEventException(
940 LetterTemplateExceptionMessage.EXTRA_IMAGE_POSITION);
941 }
942 slot = theImages[imgIdx++];
943 rawImage = (byte[])slot.get(1);
944 numpict++;
945 String imgName = "pict" + numpict + ".jpeg";
946 File fimg = new File(imageDir, imgName);
947 BufferedOutputStream outimg =
948 new BufferedOutputStream(new FileOutputStream(fimg));
949 outimg.write(rawImage);
950 outimg.flush();
951 outimg.close();
952
953 String imgRef = imageReference(foFileName + IMAGES_DIR, imgName);
954 writer.write(imgRef);
955 }
956 i++;
957 }
958 if(imgIdx < theImages.length) {
959 debug.println("Mmmm ... seems like a malformed template.");
960 throw
961 new LetterTemplateEventException(LetterTemplateExceptionMessage.IMAGE_POSITION_MISSING);
962 }
963
964 writer.flush();
965 writer.close();
966 return writer.toString();
967 }
968 catch(Exception ex) {
969 debug.println("In substituteImages");
970 debug.println(CommonUtil.stackTraceToString(ex));
971 return null;
972 }
973 }
974
975
976
985 private String imageReference(String imageDir, String imageFile) {
986 String open = "<fo:inline><fo:external-graphic src=";
987 String close = "/></fo:inline>";
988 String fileUrl = "file:///" + workDir + "/" + imageDir + "/" + imageFile;
989 String ref = "\"" + fileUrl + "\"";
990 return open + ref + close;
991 }
992
993
994
995
996
1003 public void toFile(String fileName, String data) {
1004 try {
1005 File dir = new File(workDir);
1006 PrintWriter fout =
1007 new PrintWriter(
1008 new FileWriter(new File(dir, fileName)));
1009
1010 fout.println(data);
1011 fout.close();
1012 }
1013 catch(IOException ex) {
1014 debug.println("in toFile");
1015 debug.println(CommonUtil.stackTraceToString(ex));
1016 }
1017 }
1018
1019
1020
1027 public ByteArrayOutputStream transform(String[] varValues)
1028 throws LetterTemplateEventException {
1029 String xmlVarValues = buildXmlData(theVars, varsOffsetType, varValues);
1030 String unfilledFoBaseURI = "file:///" + workDir + "/";
1031 String foFilled =
1032 fillVarValues(unfilledFoBaseURI, foFileName + FO_EXT, xmlVarValues);
1033
1034 if(foFilled == null) {
1035 throw
1036 new LetterTemplateEventException(
1037 LetterTemplateExceptionMessage.PROBLEMS_FILLING_VARIABLES);
1038 }
1039 return toLetter(foFilled);
1040 }
1041
1042
1043
1062
1063 public String buildXmlData(ArrayList[] setOfVars, String[] varsOffType,
1064 String[] varValues) {
1065 String open = "<varvalues>";
1066 String close = "</varvalues>";
1067 StringBuffer res = new StringBuffer(open);
1068 for(int i = 0; i < setOfVars.length; i++) {
1069 Long varCode = (Long)setOfVars[i].get(0);
1070 String offType = varsOffType[i];
1071 ArrayList slot = (ArrayList)setOfVars[i].get(1);
1072 String varValue = varValues[i];
1073 for(int k = 0; k < slot.size(); k++) {
1074 Long fmtCode = ((Long[])slot.get(k))[0];
1075 Long delta = ((Long[])slot.get(k))[1];
1076
1077
1078 String varName = buildVarName(varCode, fmtCode, delta);
1079 String auxVarValue = varValue;
1080 if(delta.longValue() != 0) {
1081 auxVarValue = applyDelta(varValue, delta, offType);
1082 }
1083
1084 res.append('<');
1085 res.append(varName);
1086 res.append('>');
1087 String value = varFormatter.format(auxVarValue, fmtCode);
1088 res.append(value);
1089 res.append('<');
1090 res.append('/');
1091 res.append(varName);
1092 res.append('>');
1093 res.append('\n');
1094 }
1095 }
1096 res.append(close);
1097 return res.toString();
1098 }
1099
1100
1101
1119
1120 public String fillVarValues(String baseUri, String foName, String xmlVarData) {
1121 try {
1122 TransformerHandler handler = saxFactory.newTransformerHandler(xslTemplates);
1123
1124 handler.getTransformer().setParameter("template", foName);
1125 handler.getTransformer().setParameter("base", baseUri);
1126
1127
1128
1129
1130 StringWriter writer = new StringWriter();
1131 Result result = new StreamResult(writer);
1132 handler.setResult(result);
1133
1134 reader.setContentHandler(handler);
1135 reader.parse(
1136 new InputSource(new ByteArrayInputStream(xmlVarData.getBytes())));
1137
1138 writer.flush();
1139 writer.close();
1140
1141 return writer.toString();
1142 }
1143 catch(Exception ex) {
1144 return null;
1145 }
1146
1147 }
1148
1149
1150
1153 public void cleanfiles() {
1154 File foFile = new File(workDir, foFileName + FO_EXT);
1155 if(foFile.delete()) {
1156 debug.println(foFileName + FO_EXT + " deleted");
1157 }
1158
1159 if(theImages.length == 0) {
1160 return;
1161 }
1162
1163 File imagDir = new File(workDir, foFileName + IMAGES_DIR);
1164
1165 String[] imgFiles = imagDir.list();
1166
1167 for(int i = 0; i < imgFiles.length; i++) {
1168 debug.println("image = " + imgFiles[i]);
1169 File image = new File(imagDir, imgFiles[i]);
1170
1171 if(image.delete()) {
1172 debug.println(imgFiles[i] + " erased");
1173 }
1174 else {
1175 debug.println(imgFiles[i] + " not erased");
1176 }
1177 }
1178
1179 if(imagDir.delete()) {
1180 cat.info(foFileName + IMAGES_DIR + " erased");
1181 }
1182 }
1183
1184
1185
1191 public void setRenderMode(int mode) {
1192 this.renderMode = mode;
1193 }
1194
1195
1196
1205 public ByteArrayOutputStream toLetter(String filledTemplate) {
1206
1207 try {
1208 InputSource in = new InputSource(new StringReader(filledTemplate));
1209 ByteArrayOutputStream bout = new ByteArrayOutputStream();
1210
1211 Driver driver = new Driver(in, bout);
1212 driver.setLogger(foplog);
1213 driver.setRenderer(renderMode);
1214 driver.run();
1215 bout.close();
1216 return bout;
1217 }
1218 catch(FileNotFoundException fex) {
1219 cat.debug(CommonUtil.stackTraceToString(fex));
1220 }
1221 catch(IOException iox) {
1222 cat.debug(CommonUtil.stackTraceToString(iox));
1223 }
1224 catch(FOPException fopx) {
1225 cat.debug(CommonUtil.stackTraceToString(fopx));
1226 }
1227 return null;
1228 }
1229
1230
1231
1241 public String applyDelta(String strDate, Long delta, String offType) {
1242 try {
1243 Date d = dateFormater.parse(strDate);
1244 if(offType.equals(LetterTemplateGlobals.CHRONO_DAYS_OFFSET)) {
1245 Calendar cal = Calendar.getInstance(Locale.US);
1246 cal.setTime(d);
1247 cal.add(Calendar.DAY_OF_MONTH, delta.intValue());
1248 return dateFormater.format(cal.getTime());
1249 }
1250 else {
1251 Date lbDate = CommonUtil.workableOffset(d, delta.intValue(), calendars);
1252 if(lbDate != null) {
1253 return dateFormater.format(lbDate);
1254 }
1255 else {
1256 throw new
1257 Exception("There were no calendar to calculate the workable offset");
1258 }
1259 }
1260 }
1261 catch(Exception ex) {
1262 return LetterTemplateGlobals.BAD_FORMATED_DATE;
1263 }
1264 }
1265
1266 }
1267
1268