Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CDATA support in the event writer (WIP) #50

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public XmlEventWriter(Writer baos, final String defaultPrefix, boolean includeXM
this.rootElement = rootElement;
}

private StringBuilder charactersBuffer = new StringBuilder();

public void add(final XMLEvent event) throws XMLStreamException {
if (event != null) {
try {
Expand Down Expand Up @@ -127,11 +129,12 @@ public void add(final XMLEvent event) throws XMLStreamException {
closeStartTagIfNeeded();
final Characters ce = event.asCharacters();
final char[] arr = ce.getData().toCharArray();
out.write(escape(arr));
this.charactersBuffer.append(arr);
break;
}

case XMLEvent.END_ELEMENT: {
writeCharactersIfNeeded();
this.nestedLevel--;
closeStartTagIfNeeded();
final EndElement ee = event.asEndElement();
Expand Down Expand Up @@ -171,6 +174,13 @@ public void add(final XMLEvent event) throws XMLStreamException {
}
}

private void writeCharactersIfNeeded() {
if (this.charactersBuffer.length() > 0) {
out.write(escape(this.charactersBuffer.toString()));
this.charactersBuffer = new StringBuilder();
}
}

/**
* For a nested level above zero, writes the proportional identation
*/
Expand Down Expand Up @@ -199,8 +209,9 @@ private String namespace(final Namespace namespace) {
/**
* Inplace escape por xml
* @since 7.8
* asdfasd <![CDATA asdflaskjdfasd ]] as&&& <<<>></>df asdf<![CDATA asdflaskjdfasd ]] asdfasd
*/
private String escape(char[] arr) {
String escape(char[] arr) {
final StringBuilder sb = new StringBuilder(arr.length);
// TODO Consider code in com.sun.xml.bind.marshaller.DumbEscapeHandler for replacements
for (int i = 0; i < arr.length; i++) {
Expand Down