KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > layout > CharSepLE


1 package jimm.datavision.layout;
2 import jimm.datavision.*;
3 import jimm.datavision.field.*;
4 import java.io.*;
5
6 /**
7  * <code>CharSepLE</code> is a layout engine that outputs text data files.
8  * Output is one line per row of data. Column data is separated by a
9  * user-specified character.
10  *
11  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
12  */

13 public class CharSepLE extends SortedLayoutEngine {
14
15 protected char sepChar;
16 protected boolean first;
17
18 /**
19  * Constructor.
20  *
21  * @param out the output writer
22  * @param sepChar the character used to separate column data
23  */

24 public CharSepLE(PrintWriter out, char sepChar) {
25     super(out);
26     this.sepChar = sepChar;
27 }
28
29 /**
30  * This override handles output of a section.
31  *
32  * @param sect section
33  */

34 protected void doOutputSection(Section sect) {
35     first = true;
36     super.doOutputSection(sect);
37     out.println();
38 }
39
40 /**
41  * This override handles output of a field.
42  *
43  * @param field a field
44  */

45 protected void doOutputField(Field field) {
46     String JavaDoc fieldAsString = field.toString();
47     if (fieldAsString == null)
48     fieldAsString = "";
49
50     if (first)
51     first = false;
52     else
53     out.print(sepChar);
54
55     // Make fieldAsString safe for comma- or tab-separated data
56
out.print(asSafeSepString(fieldAsString));
57 }
58
59 protected void doOutputImage(ImageField image) {
60     doOutputField(image);
61 }
62
63 /**
64  * Ignores line output.
65  *
66  * @param line a line
67  */

68 protected void doOutputLine(Line line) {}
69
70 /**
71  * Return a string that's safe to use in a comma-delimited data file.
72  * Returns a new string with all double quotes replaced by two double
73  * quotes. If there are any double quotes, commas, or newlines in the
74  * string, the returned string will be surrounded by double quotes.
75  *
76  * @param str a string to be used in a comma-delimited data file
77  * @return a new string that's safe for use in a comma-delimited data file
78  */

79 protected String JavaDoc asSafeSepString(String JavaDoc str) {
80     if (str == null)
81     return "";
82
83     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
84     boolean needsToBeQuoted = false;
85     int len = str.length();
86     for (int i = 0; i < len; ++i) {
87     char c = str.charAt(i);
88     switch (c) {
89     case '"':
90         buf.append("\"\"");
91         needsToBeQuoted = true;
92         break;
93     case '\n':
94     case '\r':
95         buf.append(c);
96         needsToBeQuoted = true;
97         break;
98     default:
99         buf.append(c);
100         if (c == sepChar)
101         needsToBeQuoted = true;
102         break;
103     }
104     }
105
106     if (needsToBeQuoted) {
107     buf.insert(0, '"');
108     buf.append('"');
109     }
110     return buf.toString();
111 }
112
113 }
114
Popular Tags