1 30 31 package org.jboss.axis.components.encoding; 32 33 34 import org.jboss.axis.utils.Messages; 35 36 import java.io.UnsupportedEncodingException ; 37 38 39 55 56 public abstract class AbstractXMLEncoder implements XMLEncoder 57 { 58 59 private static final byte[] AMP = "&".getBytes(); 60 61 private static final byte[] QUOTE = """.getBytes(); 62 63 private static final byte[] LESS = "<".getBytes(); 64 65 private static final byte[] GREATER = ">".getBytes(); 66 67 private static final byte[] LF = "\n".getBytes(); 68 69 private static final byte[] CR = "\r".getBytes(); 70 71 private static final byte[] TAB = "\t".getBytes(); 72 73 74 80 81 public String encode(String xmlString) 82 { 83 84 if (xmlString == null) 85 { 86 87 return ""; 88 89 } 90 91 char[] characters = xmlString.toCharArray(); 92 93 EncodedByteArray out = null; 94 95 char character; 96 97 98 for (int i = 0; i < characters.length; i++) 99 { 100 101 character = characters[i]; 102 103 switch (character) 104 { 105 106 108 110 case '&': 111 112 if (out == null) 113 { 114 115 out = getInitialByteArray(xmlString, i); 116 117 } 118 119 out.append(AMP); 120 121 break; 122 123 case '"': 124 125 if (out == null) 126 { 127 128 out = getInitialByteArray(xmlString, i); 129 130 } 131 132 out.append(QUOTE); 133 134 break; 135 136 case '<': 137 138 if (out == null) 139 { 140 141 out = getInitialByteArray(xmlString, i); 142 143 } 144 145 out.append(LESS); 146 147 break; 148 149 case '>': 150 151 if (out == null) 152 { 153 154 out = getInitialByteArray(xmlString, i); 155 156 } 157 158 out.append(GREATER); 159 160 break; 161 162 case '\n': 163 164 if (out == null) 165 { 166 167 out = getInitialByteArray(xmlString, i); 168 169 } 170 171 out.append(LF); 172 173 break; 174 175 case '\r': 176 177 if (out == null) 178 { 179 180 out = getInitialByteArray(xmlString, i); 181 182 } 183 184 out.append(CR); 185 186 break; 187 188 case '\t': 189 190 if (out == null) 191 { 192 193 out = getInitialByteArray(xmlString, i); 194 195 } 196 197 out.append(TAB); 198 199 break; 200 201 default: 202 203 if (character < 0x20) 204 { 205 206 throw new IllegalArgumentException (Messages.getMessage("invalidXmlCharacter00", Integer.toHexString(character), xmlString)); 207 208 } 209 else if (needsEncoding(character)) 210 { 211 212 if (out == null) 213 { 214 215 out = getInitialByteArray(xmlString, i); 216 217 } 218 219 appendEncoded(out, character); 220 221 } 222 else 223 { 224 225 if (out != null) 226 { 227 228 out.append(character); 229 230 } 231 232 } 233 234 break; 235 236 } 237 238 } 239 240 if (out == null) 241 { 242 243 return xmlString; 244 245 } 246 247 try 248 { 249 250 return out.toString(getEncoding()); 251 252 } 253 catch (UnsupportedEncodingException e) 254 { 255 256 258 throw new IllegalStateException (Messages.getMessage("encodingDisappeared00", getEncoding())); 259 260 } 261 262 } 263 264 265 public abstract String getEncoding(); 266 267 268 public abstract boolean needsEncoding(char c); 269 270 271 public abstract void appendEncoded(EncodedByteArray out, char c); 272 273 274 private EncodedByteArray getInitialByteArray(String aXmlString, int pos) 275 { 276 277 try 278 { 279 280 return new EncodedByteArray(aXmlString.getBytes(getEncoding()), 0, pos); 281 282 } 283 catch (UnsupportedEncodingException e) 284 { 285 286 288 throw new IllegalStateException (Messages.getMessage("encodingDisappeared00", getEncoding())); 289 290 } 291 292 } 293 294 } 295 296 | Popular Tags |