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