1 16 package org.apache.axis.components.encoding; 17 18 import org.apache.axis.utils.Messages; 19 20 33 public abstract class AbstractXMLEncoder implements XMLEncoder { 34 protected static final String AMP = "&"; 35 protected static final String QUOTE = """; 36 protected static final String LESS = "<"; 37 protected static final String GREATER = ">"; 38 protected static final String LF = "\n"; 39 protected static final String CR = "\r"; 40 protected static final String TAB = "\t"; 41 42 46 public abstract String getEncoding(); 47 48 53 public String encode(String xmlString) { 54 if(xmlString == null) { 55 return ""; 56 } 57 char[] characters = xmlString.toCharArray(); 58 StringBuffer out = null; 59 char character; 60 61 for (int i = 0; i < characters.length; i++) { 62 character = characters[i]; 63 switch (character) { 64 case '&': 67 if (out == null) { 68 out = getInitialByteArray(xmlString, i); 69 } 70 out.append(AMP); 71 break; 72 case '"': 73 if (out == null) { 74 out = getInitialByteArray(xmlString, i); 75 } 76 out.append(QUOTE); 77 break; 78 case '<': 79 if (out == null) { 80 out = getInitialByteArray(xmlString, i); 81 } 82 out.append(LESS); 83 break; 84 case '>': 85 if (out == null) { 86 out = getInitialByteArray(xmlString, i); 87 } 88 out.append(GREATER); 89 break; 90 case '\n': 91 if (out == null) { 92 out = getInitialByteArray(xmlString, i); 93 } 94 out.append(LF); 95 break; 96 case '\r': 97 if (out == null) { 98 out = getInitialByteArray(xmlString, i); 99 } 100 out.append(CR); 101 break; 102 case '\t': 103 if (out == null) { 104 out = getInitialByteArray(xmlString, i); 105 } 106 out.append(TAB); 107 break; 108 default: 109 if (character < 0x20) { 110 throw new IllegalArgumentException (Messages.getMessage("invalidXmlCharacter00", Integer.toHexString(character), xmlString)); 111 } else { 112 if (out != null) { 113 out.append(character); 114 } 115 } 116 break; 117 } 118 } 119 if (out == null) { 120 return xmlString; 121 } 122 return out.toString(); 123 } 124 125 protected StringBuffer getInitialByteArray(String aXmlString, int pos) { 126 return new StringBuffer (aXmlString.substring(0, pos)); 127 } 128 } 129 | Popular Tags |