Skip to content

Commit

Permalink
First minor fix wrt #121; only affects case of (not) detecting invali…
Browse files Browse the repository at this point in the history
…d ctrl chars in CDATA

(for one specific read method)
  • Loading branch information
cowtowncoder committed Jan 5, 2021
1 parent 46ea1b4 commit b8eba25
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 58 deletions.
93 changes: 44 additions & 49 deletions src/main/java/com/ctc/wstx/sr/BasicStreamReader.java
Expand Up @@ -5317,58 +5317,54 @@ private int readAndWriteCData(Writer w)

quick_loop:
while (true) {
if (c > CHAR_CR_LF_OR_NULL) {
if (c >= CHAR_SPACE) {
if (c == ']') {
break quick_loop;
}
} else {
if (c < CHAR_SPACE) {
if (c == '\n') {
markLF();
} else if (c == '\r') {
char d;
if (mInputPtr >= mInputEnd) {
/* If we can't peek easily, let's flush past stuff
* and load more... (have to flush, since new read
* will overwrite inbut buffers)
*/
int len = mInputPtr - start;
if (len > 0) {
w.write(mInputBuffer, start, len);
count += len;
}
d = getNextChar(SUFFIX_IN_CDATA);
start = mInputPtr; // to mark 'no past content'
} else {
d = mInputBuffer[mInputPtr++];
}
if (d == '\n') {
if (mNormalizeLFs) {
/* Let's flush content prior to 2-char LF, and
* start the new segment on the second char...
* this way, no mods are needed for the buffer,
* AND it'll also work on split 2-char lf!
*/
int len = mInputPtr - 2 - start;
if (len > 0) {
w.write(mInputBuffer, start, len);
count += len;
}
start = mInputPtr-1; // so '\n' is the first char
} else {
// otherwise it's good as is
}
} else { // not 2-char... need to replace?
--mInputPtr;
if (mNormalizeLFs) {
mInputBuffer[mInputPtr-1] = '\n';
}
} else if (c == '\n') {
markLF();
} else if (c == '\r') {
char d;
if (mInputPtr >= mInputEnd) {
/* If we can't peek easily, let's flush past stuff
* and load more... (have to flush, since new read
* will overwrite inbut buffers)
*/
int len = mInputPtr - start;
if (len > 0) {
w.write(mInputBuffer, start, len);
count += len;
}
d = getNextChar(SUFFIX_IN_CDATA);
start = mInputPtr; // to mark 'no past content'
} else {
d = mInputBuffer[mInputPtr++];
}
if (d == '\n') {
if (mNormalizeLFs) {
/* Let's flush content prior to 2-char LF, and
* start the new segment on the second char...
* this way, no mods are needed for the buffer,
* AND it'll also work on split 2-char lf!
*/
int len = mInputPtr - 2 - start;
if (len > 0) {
w.write(mInputBuffer, start, len);
count += len;
}
markLF();
} else if (c != '\t') {
throwInvalidSpace(c);
start = mInputPtr-1; // so '\n' is the first char
} else {
// otherwise it's good as is
}
} else { // not 2-char... need to replace?
--mInputPtr;
if (mNormalizeLFs) {
mInputBuffer[mInputPtr-1] = '\n';
}
}
markLF();
} else if (c != '\t') {
throwInvalidSpace(c);
}
// Reached the end of buffer? Need to flush, then
if (mInputPtr >= mInputEnd) {
Expand All @@ -5386,9 +5382,8 @@ private int readAndWriteCData(Writer w)

// Anything to flush once we hit ']'?
{
/* -1 since the last char in there (a '[') is NOT to be
* output at this point
*/
// -1 since the last char in there (a '[') is NOT to be
// output at this point
int len = mInputPtr - start - 1;
if (len > 0) {
w.write(mInputBuffer, start, len);
Expand Down
24 changes: 15 additions & 9 deletions src/test/java/wstxtest/stream/TestStreaming.java
Expand Up @@ -65,30 +65,36 @@ public void testTextStreaming()
public void testCDataStreaming()
throws IOException, XMLStreamException
{
String CONTENT_INOUT =
"Some content\nthat will be stored in a\n"
+"CDATA <yes!> Block <[*]>\n"
// 05-Jan-2021, tatu: let's quickly verify LF-normalization too
// (changed for [woodstox-core#121] verification)
final String CONTENT_IN =
"Some content\r\nthat will be stored in a\n"
+"CDATA <yes!> Block <[*]>\r"
+" yet not be split in any way...."
;

final String CONTENT_OUT = CONTENT_IN.replace("\r\n", "\n")
.replace("\r", "\n");

/* Let's also add trailing text, to ensure no coalescing is done
* when not requested
*/
String XML = "<root><![CDATA[" + CONTENT_INOUT + "]]>some text!</root>";
String XML = "<root><![CDATA[" + CONTENT_IN + "]]>some text!</root>";
XMLStreamReader2 sr = getReader(XML, false);
assertTokenType(START_ELEMENT, sr.next());
assertTokenType(CDATA, sr.next());
StringWriter sw = new StringWriter();
sr.getText(sw, false);
String act = sw.toString();
if (!act.equals(CONTENT_INOUT)) {
if (CONTENT_INOUT.startsWith(act)) {
if (!act.equals(CONTENT_OUT)) {
if (CONTENT_OUT.startsWith(act)) {
fail("Streaming text accessors returned partial match; first "
+act.length()+" chars of the expected "
+CONTENT_INOUT.length()+" chars");
+CONTENT_OUT.length()+" chars");
}
fail("Content accessed using streaming text accessor (len "
+act.length()+"; exp "+CONTENT_INOUT.length()+" chars) wrong: "
+"expected ["+CONTENT_INOUT+"], got ["+act+"]");
+act.length()+"; exp "+CONTENT_OUT.length()+" chars) wrong: "
+"expected ["+CONTENT_OUT+"], got ["+act+"]");
}

// And should get the following CHARACTERS then:
Expand Down

0 comments on commit b8eba25

Please sign in to comment.