1 17 package org.eclipse.emf.codegen.jet; 18 19 20 21 24 public class JETCharDataGenerator 25 implements JETGenerator 26 { 27 protected static final String FUNCTION_CALL_BEGIN = "stringBuffer.append("; 28 protected static final String FUNCTION_CALL_END = ");"; 29 protected static final String NEW_LINE_BEGIN = "NL + "; 30 protected static final String NEW_LINE_MIDDLE = "\" + NL + \""; 31 protected static final String NEW_LINE_END = " + NL"; 32 33 protected char[] characters; 34 35 public JETCharDataGenerator(char[] characters) 36 { 37 this.characters = characters; 38 } 39 40 public String generate() 41 { 42 StringBuffer stringBuffer = new StringBuffer (characters.length + 16); 43 stringBuffer.append(FUNCTION_CALL_BEGIN); 44 stringBuffer.append(generateCharData()); 45 stringBuffer.append(FUNCTION_CALL_END); 46 return stringBuffer.toString(); 47 } 48 49 protected String generateCharData() 50 { 51 StringBuffer stringBuffer = new StringBuffer (); 52 StringBuffer stringBufferTail = new StringBuffer (); 53 int limit = characters.length - 1; 54 55 buildEnd: 56 while (limit >= 0) 57 { 58 char ch = characters[limit]; 59 switch (ch) 60 { 61 case '\r': 62 { 63 break; 64 } 65 case '\n': 66 { 67 stringBufferTail.append(NEW_LINE_END); 68 break; 69 } 70 default: 71 { 72 limit++; 73 break buildEnd; 74 } 75 } 76 limit--; 77 } 78 79 if (limit < 0) 80 { 81 if (stringBufferTail.length() == 0) 82 { 83 stringBuffer.append("\"\""); 84 } 85 else 86 { 87 stringBuffer.append(stringBufferTail.toString().substring(3)); 88 } 89 } 90 else 91 { 92 int i = 0; 93 buildFront: 94 for (; i <= limit; i ++ ) 95 { 96 char ch = characters[i]; 97 switch (ch) 98 { 99 case '\r': 100 { 101 break; 102 } 103 case '\n': 104 { 105 stringBuffer.append(NEW_LINE_BEGIN); 106 break; 107 } 108 default: 109 { 110 break buildFront; 111 } 112 } 113 } 114 115 stringBuffer.append('"'); 116 for (; i < limit; i++) 117 { 118 char ch = characters[i]; 119 switch (ch) 120 { 121 case '"': 122 { 123 stringBuffer.append("\\\""); 124 break; 125 } 126 case '\\': 127 { 128 stringBuffer.append("\\\\"); 129 break; 130 } 131 case '\r': 132 { 133 continue; 134 } 135 case '\n': 136 { 137 stringBuffer.append(NEW_LINE_MIDDLE); 138 break; 139 } 140 case '\t': 141 { 142 stringBuffer.append("\\t"); 143 break; 144 } 145 default: 146 { 147 stringBuffer.append(ch); 148 } 149 } 150 } 151 stringBuffer.append('"'); 152 stringBuffer.append(stringBufferTail.toString()); 153 } 154 return stringBuffer.toString(); 155 } 156 } 157 | Popular Tags |