1 package jimm.datavision.layout; 2 import jimm.datavision.*; 3 import jimm.datavision.field.*; 4 import java.io.*; 5 6 13 public class CharSepLE extends SortedLayoutEngine { 14 15 protected char sepChar; 16 protected boolean first; 17 18 24 public CharSepLE(PrintWriter out, char sepChar) { 25 super(out); 26 this.sepChar = sepChar; 27 } 28 29 34 protected void doOutputSection(Section sect) { 35 first = true; 36 super.doOutputSection(sect); 37 out.println(); 38 } 39 40 45 protected void doOutputField(Field field) { 46 String fieldAsString = field.toString(); 47 if (fieldAsString == null) 48 fieldAsString = ""; 49 50 if (first) 51 first = false; 52 else 53 out.print(sepChar); 54 55 out.print(asSafeSepString(fieldAsString)); 57 } 58 59 protected void doOutputImage(ImageField image) { 60 doOutputField(image); 61 } 62 63 68 protected void doOutputLine(Line line) {} 69 70 79 protected String asSafeSepString(String str) { 80 if (str == null) 81 return ""; 82 83 StringBuffer buf = new StringBuffer (); 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 |