1 28 29 package org.jibx.runtime.impl; 30 31 import java.io.IOException ; 32 import java.io.Writer ; 33 34 import org.jibx.runtime.ICharacterEscaper; 35 36 44 45 public class UTF8Escaper implements ICharacterEscaper 46 { 47 48 private static final UTF8Escaper s_instance = new UTF8Escaper(); 49 50 53 54 private UTF8Escaper() {} 55 56 64 65 public void writeAttribute(String text, Writer writer) throws IOException { 66 int mark = 0; 67 for (int i = 0; i < text.length(); i++) { 68 char chr = text.charAt(i); 69 if (chr == '"') { 70 writer.write(text, mark, i-mark); 71 mark = i+1; 72 writer.write("""); 73 } else if (chr == '&') { 74 writer.write(text, mark, i-mark); 75 mark = i+1; 76 writer.write("&"); 77 } else if (chr == '<') { 78 writer.write(text, mark, i-mark); 79 mark = i+1; 80 writer.write("<"); 81 } else if (chr == '>' && i > 2 && text.charAt(i-1) == ']' && 82 text.charAt(i-2) == ']') { 83 writer.write(text, mark, i-mark-2); 84 mark = i+1; 85 writer.write("]]>"); 86 } else if (chr < 0x20) { 87 if (chr != 0x9 && chr != 0xA && chr != 0xD) { 88 throw new IOException ("Illegal character code 0x" + 89 Integer.toHexString(chr) + " in attribute value text"); 90 } 91 } else if (chr > 0xD7FF && (chr < 0xE000 || chr == 0xFFFE || 92 chr == 0xFFFF || chr > 0x10FFFF)) { 93 throw new IOException ("Illegal character code 0x" + 94 Integer.toHexString(chr) + " in attribute value text"); 95 } 96 } 97 writer.write(text, mark, text.length()-mark); 98 } 99 100 107 108 public void writeContent(String text, Writer writer) throws IOException { 109 int mark = 0; 110 for (int i = 0; i < text.length(); i++) { 111 char chr = text.charAt(i); 112 if (chr == '&') { 113 writer.write(text, mark, i-mark); 114 mark = i+1; 115 writer.write("&"); 116 } else if (chr == '<') { 117 writer.write(text, mark, i-mark); 118 mark = i+1; 119 writer.write("<"); 120 } else if (chr == '>' && i > 2 && text.charAt(i-1) == ']' && 121 text.charAt(i-2) == ']') { 122 writer.write(text, mark, i-mark-2); 123 mark = i+1; 124 writer.write("]]>"); 125 } else if (chr < 0x20) { 126 if (chr != 0x9 && chr != 0xA && chr != 0xD) { 127 throw new IOException ("Illegal character code 0x" + 128 Integer.toHexString(chr) + " in content text"); 129 } 130 } else if (chr > 0xD7FF && (chr < 0xE000 || chr == 0xFFFE || 131 chr == 0xFFFF || chr > 0x10FFFF)) { 132 throw new IOException ("Illegal character code 0x" + 133 Integer.toHexString(chr) + " in content text"); 134 } 135 } 136 writer.write(text, mark, text.length()-mark); 137 } 138 139 148 149 public void writeCData(String text, Writer writer) throws IOException { 150 writer.write("<![CDATA["); 151 for (int i = 0; i < text.length(); i++) { 152 char chr = text.charAt(i); 153 if (chr == '>' && i > 2 && text.charAt(i-1) == ']' && 154 text.charAt(i-2) == ']') { 155 throw new IOException ("Sequence \"]]>\" is not allowed " + 156 "within CDATA section text"); 157 } else if (chr < 0x20) { 158 if (chr != 0x9 && chr != 0xA && chr != 0xD) { 159 throw new IOException ("Illegal character code 0x" + 160 Integer.toHexString(chr) + " in CDATA section"); 161 } 162 } else if (chr > 0xD7FF && 163 (chr < 0xE000 || chr == 0xFFFE || chr == 0xFFFF)) { 164 throw new IOException ("Illegal character code 0x" + 165 Integer.toHexString(chr) + " in CDATA section"); 166 } 167 } 168 writer.write(text); 169 writer.write("]]>"); 170 } 171 172 177 178 public static ICharacterEscaper getInstance() { 179 return s_instance; 180 } 181 } | Popular Tags |