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