Bug-61792 simplify sxssf code that writes chars

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1818223 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2017-12-15 00:54:52 +00:00
parent f79e00625a
commit e74e6d749a
1 changed files with 22 additions and 60 deletions

View File

@ -38,6 +38,7 @@ import org.apache.poi.ss.usermodel.FormulaError;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.util.StringCodepointsIterable;
import org.apache.poi.util.TempFile;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
@ -348,93 +349,54 @@ public class SheetDataWriter implements Closeable {
return false;
}
//Taken from jdk1.3/src/javax/swing/text/html/HTMLWriter.java
protected void outputQuotedString(String s) throws IOException {
if (s == null || s.length() == 0) {
return;
}
char[] chars = s.toCharArray();
int last = 0;
int length = s.length();
for (int counter = 0; counter < length; counter++) {
char c = chars[counter];
switch (c) {
case '<':
writeLastChars(_out, chars, last, counter);
last = counter + 1;
for (String codepoint : new StringCodepointsIterable(s)) {
switch (codepoint) {
case "<":
_out.write("&lt;");
break;
case '>':
writeLastChars(_out, chars, last, counter);
last = counter + 1;
case ">":
_out.write("&gt;");
break;
case '&':
writeLastChars(_out, chars, last, counter);
last = counter + 1;
case "&":
_out.write("&amp;");
break;
case '"':
writeLastChars(_out, chars, last, counter);
last = counter + 1;
case "\"":
_out.write("&quot;");
break;
// Special characters
case '\n':
writeLastChars(_out, chars, last, counter);
case "\n":
_out.write("&#xa;");
last = counter + 1;
break;
case '\r':
writeLastChars(_out, chars, last, counter);
case "\r":
_out.write("&#xd;");
last = counter + 1;
break;
case '\t':
writeLastChars(_out, chars, last, counter);
case "\t":
_out.write("&#x9;");
last = counter + 1;
break;
case 0xa0:
writeLastChars(_out, chars, last, counter);
case "\u00A0": // NO-BREAK SPACE
_out.write("&#xa0;");
last = counter + 1;
break;
default:
if (codepoint.length() == 1) {
char c = codepoint.charAt(0);
// YK: XmlBeans silently replaces all ISO control characters ( < 32) with question marks.
// the same rule applies to "not a character" symbols.
if (replaceWithQuestionMark(c)) {
writeLastChars(_out, chars, last, counter);
_out.write('?');
last = counter + 1;
}
else if (Character.isHighSurrogate(c) || Character.isLowSurrogate(c)) {
writeLastChars(_out, chars, last, counter);
} else {
_out.write(c);
last = counter + 1;
}
else if (c > 127) {
writeLastChars(_out, chars, last, counter);
last = counter + 1;
// If the character is outside of ascii, write the
// numeric value.
_out.write("&#");
_out.write(String.valueOf((int) c));
_out.write(";");
} else {
_out.write(codepoint);
}
break;
}
}
if (last < length) {
_out.write(chars, last, length - last);
}
}
private static void writeLastChars(Writer out, char[] chars, int last, int counter) throws IOException {
if (counter > last) {
out.write(chars, last, counter - last);
}
}
static boolean replaceWithQuestionMark(char c) {