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 USASCIIEscaper implements ICharacterEscaper 46 { 47 48 private static final USASCIIEscaper s_instance = new USASCIIEscaper(); 49 50 53 54 private USASCIIEscaper() {} 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 > 0x7F) { 92 writer.write(text, mark, i-mark); 93 mark = i+1; 94 if (chr > 0xD7FF && (chr < 0xE000 || chr == 0xFFFE || 95 chr == 0xFFFF || chr > 0x10FFFF)) { 96 throw new IOException ("Illegal character code 0x" + 97 Integer.toHexString(chr) + " in attribute value text"); 98 } 99 writer.write("&#x"); 100 writer.write(Integer.toHexString(chr)); 101 writer.write(';'); 102 } 103 } 104 writer.write(text, mark, text.length()-mark); 105 } 106 107 114 115 public void writeContent(String text, Writer writer) throws IOException { 116 int mark = 0; 117 for (int i = 0; i < text.length(); i++) { 118 char chr = text.charAt(i); 119 if (chr == '&') { 120 writer.write(text, mark, i-mark); 121 mark = i+1; 122 writer.write("&"); 123 } else if (chr == '<') { 124 writer.write(text, mark, i-mark); 125 mark = i+1; 126 writer.write("<"); 127 } else if (chr == '>' && i > 2 && text.charAt(i-1) == ']' && 128 text.charAt(i-2) == ']') { 129 writer.write(text, mark, i-mark-2); 130 mark = i+1; 131 writer.write("]]>"); 132 } else if (chr < 0x20) { 133 if (chr != 0x9 && chr != 0xA && chr != 0xD) { 134 throw new IOException ("Illegal character code 0x" + 135 Integer.toHexString(chr) + " in content text"); 136 } 137 } else if (chr > 0x7F) { 138 writer.write(text, mark, i-mark); 139 mark = i+1; 140 if (chr > 0xD7FF && (chr < 0xE000 || chr == 0xFFFE || 141 chr == 0xFFFF || chr > 0x10FFFF)) { 142 throw new IOException ("Illegal character code 0x" + 143 Integer.toHexString(chr) + " in content text"); 144 } 145 writer.write("&#x"); 146 writer.write(Integer.toHexString(chr)); 147 writer.write(';'); 148 } 149 } 150 writer.write(text, mark, text.length()-mark); 151 } 152 153 162 163 public void writeCData(String text, Writer writer) throws IOException { 164 writer.write("<![CDATA["); 165 for (int i = 0; i < text.length(); i++) { 166 char chr = text.charAt(i); 167 if (chr == '>' && i > 2 && text.charAt(i-1) == ']' && 168 text.charAt(i-2) == ']') { 169 throw new IOException ("Sequence \"]]>\" is not allowed " + 170 "within CDATA section text"); 171 } else if (chr < 0x20) { 172 if (chr != 0x9 && chr != 0xA && chr != 0xD) { 173 throw new IOException ("Illegal character code 0x" + 174 Integer.toHexString(chr) + " in CDATA section"); 175 } 176 } else if (chr >= 0x80) { 177 throw new IOException ("Character code 0x" + 178 Integer.toHexString(chr) + 179 " not supported by encoding in CDATA section"); 180 } 181 } 182 writer.write(text); 183 writer.write("]]>"); 184 } 185 186 191 192 public static ICharacterEscaper getInstance() { 193 return s_instance; 194 } 195 } | Popular Tags |