View Javadoc

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   * <p> 
18   * Output format (CSV) dependant formatting.
19   * </p>
20   *
21   * @author  timp At paneris.org
22   * @version 0.0.1, 06/10/1999
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       * Do basic escaping of CSV text
35       *
36       * @param input     a String to escape
37       * @return          the escaped string
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       * Do general purpose escaping 
50       *
51       * @param input          a String to escape
52       * @param input          a hash of escapees and replacements
53       * @return               the escaped string
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       * Formats a DDField to CSV format
78       *
79       * @param ddfieldParameter    a DDField which we want to format
80       * @param db                  name of the database to query
81       * @param dateFormatter       format for dates
82       * @param dateTimeFormatter   format for date and times
83       * @return                    a CSV formatted field
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              // boolean
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             // autorandom
103             } else if (ddfieldParameter.getMetaData().getType().equals("autorandom")) {
104                 if (ddfieldParameter.getValue() != null) {
105                     returnString += ddfieldParameter.getValue().toString();
106                 }
107                 return returnString;
108 
109             // id
110             } else if (ddfieldParameter.getMetaData().getType().equals("id")) {
111                 if (ddfieldParameter.getValue() != null) {
112                     returnString += ddfieldParameter.getValue().toString();
113                 }
114                 return returnString;
115 
116             // date
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             // datetime
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             // text, password, uploadurl, textarea and preformattedtextarea
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