Skip to content
This repository has been archived by the owner on Feb 5, 2019. It is now read-only.

Commit

Permalink
Incorrect escape handling caused 'SAAJ0543: Entity References are not…
Browse files Browse the repository at this point in the history
… allowed in SOAP documents' when custom handler is added.

Fixes #1179

Signed-off-by: Roman Grigoriadi <[email protected]>
  • Loading branch information
Roman Grigoriadi committed Apr 20, 2018
1 parent 2de3ad0 commit 8f1e74a
Showing 1 changed file with 3 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997-2018 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -48,6 +48,7 @@
import javax.xml.stream.XMLStreamWriter;

import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
import com.sun.xml.bind.marshaller.NoEscapeHandler;
import com.sun.xml.bind.v2.runtime.JAXBContextImpl;
import com.sun.xml.bind.v2.runtime.XMLSerializer;

Expand Down Expand Up @@ -86,7 +87,7 @@ public static XmlOutput create(XMLStreamWriter out, JAXBContextImpl context, Cha
}

CharacterEscapeHandler xmlStreamEscapeHandler = escapeHandler != null ?
escapeHandler : NewLineEscapeHandler.theInstance;
escapeHandler : NoEscapeHandler.theInstance;

// otherwise the normal writer.
return new XMLStreamWriterOutput(out, xmlStreamEscapeHandler);
Expand Down Expand Up @@ -232,44 +233,6 @@ private static Constructor<? extends XmlOutput> initStAXExOutputClass() {
}
}

/**
* Performs character escaping only for new lines.
*/
private static class NewLineEscapeHandler implements CharacterEscapeHandler {

public static final NewLineEscapeHandler theInstance = new NewLineEscapeHandler();

@Override
public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException {
int limit = start+length;
int lastEscaped = start;
for (int i = start; i < limit; i++) {
char c = ch[i];
if (c == '\r' || c == '\n') {
if (i != lastEscaped) {
out.write(ch, lastEscaped, i - lastEscaped);
}
lastEscaped = i + 1;
if (out instanceof XmlStreamOutWriterAdapter) {
try {
((XmlStreamOutWriterAdapter)out).writeEntityRef("#x" + Integer.toHexString(c));
} catch (XMLStreamException e) {
throw new IOException("Error writing xml stream", e);
}
} else {
out.write("&#x");
out.write(Integer.toHexString(c));
out.write(';');
}
}
}

if (lastEscaped != limit) {
out.write(ch, lastEscaped, length - lastEscaped);
}
}
}

private static final class XmlStreamOutWriterAdapter extends Writer {

private final XMLStreamWriter writer;
Expand Down

0 comments on commit 8f1e74a

Please sign in to comment.