1 16 package org.apache.cocoon.components.language.markup.xsp; 17 18 25 public class XSLTExtension { 26 27 31 public static String escapeJavaString(String string) { 32 char chr[] = string.toCharArray(); 33 StringBuffer buffer = new StringBuffer (); 34 35 for (int i = 0; i < chr.length; i++) { 36 char c = chr[i]; 37 switch (c) { 38 case '\t': 39 buffer.append("\\t"); 40 break; 41 case '\r': 42 buffer.append("\\r"); 43 break; 44 case '\n': 45 buffer.append("\\n"); 46 break; 47 case '\b': 48 buffer.append("\\b"); 49 break; 50 case '\f': 51 buffer.append("\\f"); 52 break; 53 case '"': 54 case '\\': 55 buffer.append('\\'); 56 buffer.append(c); 57 break; 58 default: 59 if (' ' <= c && c <= 127) { 60 buffer.append(c); 61 } else { 62 buffer.append("\\u"); 63 buffer.append(int2digit(c >> 12)); 64 buffer.append(int2digit(c >> 8)); 65 buffer.append(int2digit(c >> 4)); 66 buffer.append(int2digit(c)); 67 } 68 break; 69 } 70 } 71 72 final String encoded = buffer.toString(); 73 return encoded; 74 } 75 76 82 public static String escapeString(String string) { 83 char chr[] = string.toCharArray(); 84 StringBuffer buffer = new StringBuffer (); 85 for (int i = 0; i < chr.length; i++) { 86 char c = chr[i]; 87 if (c <= 127) { 88 buffer.append(c); 89 } else { 90 buffer.append("\\u"); 91 buffer.append(int2digit(c >> 12)); 92 buffer.append(int2digit(c >> 8)); 93 buffer.append(int2digit(c >> 4)); 94 buffer.append(int2digit(c)); 95 } 96 } 97 98 final String encoded = buffer.toString(); 99 return encoded; 100 } 101 102 private static char int2digit(int x) { 103 x &= 0xF; 104 if (x <= 9) return (char)(x + '0'); 105 else return (char)(x - 10 + 'A'); 106 } 107 108 111 public String escape(String string) { 112 return escapeString(string); 113 } 114 115 119 public String escapeJava(String string) { 120 return escapeJavaString(string); 121 } 122 123 130 public String prefix(String string) { 131 char chr[] = string.toCharArray(); 132 int i; 133 for (i = 0; i < chr.length; i++) { 134 if (chr[i] == '\n' || chr[i] == '\r') 135 break; 136 } 137 if (i == chr.length) { 138 return ""; 139 } 140 141 int j = 0; 142 for (; i < chr.length; i++) { 143 if (chr[i] == '\n' || chr[i] == '\r') { 144 j = 0; 145 } else if (!Character.isSpaceChar(chr[i])) { 146 break; 147 } else { 148 j ++; 149 } 150 } 151 152 StringBuffer buffer = new StringBuffer (); 154 for (i = 0; i < j; i++) { 155 buffer.append(' '); 156 } 157 return buffer.toString(); 158 } 159 160 167 public String suffix(String string) { 168 char chr[] = string.toCharArray(); 169 170 int j = 0; 171 for (int i = chr.length-1; i >=0; i--) { 172 if (chr[i] == '\n' || chr[i] == '\r') 173 break; 174 if (!Character.isSpaceChar(chr[i])) 175 return ""; 176 j ++; 177 } 178 179 StringBuffer buffer = new StringBuffer (); 181 for (int i = 0; i < j; i++) { 182 buffer.append(' '); 183 } 184 return buffer.toString(); 185 } 186 } 187 | Popular Tags |