1 16 package org.apache.cocoon.components.serializers.encoding; 17 18 24 public class XMLEncoder extends CompiledEncoder { 25 26 private static final char ENCODE_HEX[] = "0123456789ABCDEF".toCharArray(); 27 private static final char ENCODE_QUOT[] = """.toCharArray(); 28 private static final char ENCODE_AMP[] = "&".toCharArray(); 29 private static final char ENCODE_APOS[] = "'".toCharArray(); 30 private static final char ENCODE_LT[] = "<".toCharArray(); 31 private static final char ENCODE_GT[] = ">".toCharArray(); 32 33 36 public XMLEncoder() { 37 super("X-W3C-XML"); 38 } 39 40 46 protected XMLEncoder(String name) { 47 super(name); 48 } 49 50 63 protected boolean compile(char c) { 64 if ((c == 0x09) || (c == 0x0a) || (c == 0x0d)) { return(true); 68 } 69 70 if ((c == 0x22) || (c == 0x26) || (c == 0x27) || (c == 0x3c) || (c == 0x3e) || (c < 0x20) || ((c > 0xd7ff) && (c < 0xe000)) || (c > 0xfffd)) { 77 return(false); 78 } 79 80 return(true); 81 } 82 83 87 public char[] encode(char c) { 88 switch (c) { 89 case 0x22: return(ENCODE_QUOT); case 0x26: return(ENCODE_AMP); case 0x27: return(ENCODE_APOS); case 0x3c: return(ENCODE_LT); case 0x3e: return(ENCODE_GT); default: { 95 if (c > 0xfff) { 96 char ret[] = { '&', '#', 'x', 97 ENCODE_HEX[c >> 0xc & 0xf], 98 ENCODE_HEX[c >> 0x8 & 0xf], 99 ENCODE_HEX[c >> 0x4 & 0xf], 100 ENCODE_HEX[c & 0xf], ';' 101 }; 102 return(ret); 103 } 104 if (c > 0xff) { 105 char ret[] = { '&', '#', 'x', 106 ENCODE_HEX[c >> 0x8 & 0xf], 107 ENCODE_HEX[c >> 0x4 & 0xf], 108 ENCODE_HEX[c & 0xf], ';' 109 }; 110 return(ret); 111 } 112 char ret[] = { '&', '#', 'x', 113 ENCODE_HEX[c >> 0x4 & 0xf], 114 ENCODE_HEX[c & 0xf], ';' 115 }; 116 return(ret); 117 } 118 } 119 } 120 } 121 | Popular Tags |