1 package org.paneris.jal.model;
2
3 import java.text.SimpleDateFormat;
4 import java.util.Enumeration;
5
6 import org.paneris.util.ExtendedHash;
7
8 import org.apache.oro.text.regex.MalformedPatternException;
9 import org.apache.oro.text.regex.Pattern;
10 import org.apache.oro.text.regex.PatternCompiler;
11 import org.apache.oro.text.regex.PatternMatcher;
12 import org.apache.oro.text.regex.Perl5Compiler;
13 import org.apache.oro.text.regex.Perl5Matcher;
14 import org.apache.oro.text.regex.StringSubstitution;
15 import org.apache.oro.text.regex.Util;
16
17
18
19
20
21
22
23
24
25 public final class CSVUtil {
26
27 private static final ExtendedHash escapeHash = new ExtendedHash();
28
29 static {
30 escapeHash.put("\"","\"\"");
31 }
32
33
34
35
36
37
38
39 public static String escapeCSV(String s) {
40 if (s.indexOf("\"") != -1 || s.indexOf(",") != -1) {
41 return "\"" + escape(s, escapeHash) + "\"";
42 } else {
43 return s;
44 }
45 }
46
47
48
49
50
51
52
53
54
55 public static String escape(String s, ExtendedHash h) {
56 PatternCompiler compiler = new Perl5Compiler();
57 PatternMatcher matcher = new Perl5Matcher();
58 String result = "";
59 for (Enumeration en = h.keys() ; en.hasMoreElements() ;) {
60 String regularExpression = (String) en.nextElement();
61 try {
62 Pattern pattern = compiler.compile(regularExpression);
63 result = Util.substitute(matcher,
64 pattern, new StringSubstitution((String) h.get(regularExpression)),
65 s, Util.SUBSTITUTE_ALL);
66 } catch (MalformedPatternException e) {
67 throw new RuntimeException("MalformedPatternException: " +
68 e.toString());
69 }
70 }
71 return result;
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85 public static String getCSVValue(DDField ddfieldParameter,
86 String db,
87 SimpleDateFormat dateFormatter,
88 SimpleDateFormat datetimeFormatter)
89 throws Exception {
90
91 String returnString = "";
92 if (ddfieldParameter.getMetaData().getRelationshipTable().equals("")) {
93
94 if (ddfieldParameter.getMetaData().getType().equals("boolean")) {
95 if ((ddfieldParameter.getValue() != null) && ((Boolean)ddfieldParameter.getValue()).booleanValue()) {
96 returnString += "True";
97 } else {
98 returnString += "False";
99 }
100 return returnString;
101
102
103 } else if (ddfieldParameter.getMetaData().getType().equals("autorandom")) {
104 if (ddfieldParameter.getValue() != null) {
105 returnString += ddfieldParameter.getValue().toString();
106 }
107 return returnString;
108
109
110 } else if (ddfieldParameter.getMetaData().getType().equals("id")) {
111 if (ddfieldParameter.getValue() != null) {
112 returnString += ddfieldParameter.getValue().toString();
113 }
114 return returnString;
115
116
117 } else if (ddfieldParameter.getMetaData().getType().equals("date") ||
118 ddfieldParameter.getMetaData().getType().equals("datedrop")
119 ) {
120 returnString += '"';
121 if (ddfieldParameter.getValue() != null) {
122 returnString += dateFormatter.format((java.util.Date) ddfieldParameter.getValue());
123 }
124 returnString += '"';
125 return returnString;
126
127 } else if (ddfieldParameter.getMetaData().getType().equals("datetime") ||
128 ddfieldParameter.getMetaData().getType().equals("timestamp") ||
129 ddfieldParameter.getMetaData().getType().equals("datetimedrop")
130 ) {
131 returnString += '"';
132 if (ddfieldParameter.getValue() != null) {
133 returnString += datetimeFormatter.format((java.util.Date) ddfieldParameter.getValue());
134 }
135 returnString += '"';
136 return returnString;
137
138
139 } else {
140 if (ddfieldParameter.getValue() != null) {
141 String value = ddfieldParameter.getValue().toString();
142 returnString += escapeCSV(value);
143 }
144 return returnString;
145 }
146 } else {
147 if (ddfieldParameter.getValue() != null) {
148 DDRecord rs = ddfieldParameter.getLookup();
149 DDField field = (DDField) rs.get(ddfieldParameter.getMetaData().getRelationshipField());
150 String value = field.getDisplayValue();
151 if (value.indexOf("\"") != -1) {
152 returnString += escapeCSV(value) ;
153 } else if (value.indexOf(",") != -1) {
154 returnString += escapeCSV(value) ;
155 } else {
156 returnString += value ;
157 }
158 }
159 return returnString;
160 }
161 }
162 }
163