From d99c129a06debd70b1a14c73a550f1fdf075a85a Mon Sep 17 00:00:00 2001 From: JFronny Date: Sat, 20 Apr 2024 19:49:07 +0200 Subject: [PATCH] feat(serialize-xml): Add NativeXmlReader.copyTo --- .../serialize/xml/NativeXmlReader.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/commons-serialize-xml/src/main/java/io/gitlab/jfronny/commons/serialize/xml/NativeXmlReader.java b/commons-serialize-xml/src/main/java/io/gitlab/jfronny/commons/serialize/xml/NativeXmlReader.java index 3f4c6e9..80a3ad3 100644 --- a/commons-serialize-xml/src/main/java/io/gitlab/jfronny/commons/serialize/xml/NativeXmlReader.java +++ b/commons-serialize-xml/src/main/java/io/gitlab/jfronny/commons/serialize/xml/NativeXmlReader.java @@ -822,4 +822,33 @@ public class NativeXmlReader implements Closeable { stackSize = 1; in.close(); } + + /** + * Copies the current element to the writer. + * + * @param writer the writer to copy to + * @throws IOException if an error occurs + */ + public void copyTo(NativeXmlWriter writer) throws IOException { + switch (peek()) { + case BEGIN_TAG -> { + beginTag(); + writer.beginTag(pathNames[stackSize - 1]); + while (hasNext()) { + copyTo(writer); + } + endTag(); + writer.endTag(); + } + case END_TAG -> throw new IllegalStateException("Cannot copy standalone END_TAG"); + case TEXT -> writer.text(nextText()); + case CDATA -> writer.cdata(nextCData()); + case ATTRIBUTE_NAME -> { + String name = nextAttributeName(); + writer.attribute(name, nextAttributeValue()); + } + case ATTRIBUTE_VALUE -> writer.attributeValue(nextAttributeValue()); + case EOF -> throw new IllegalStateException("Cannot copy END_DOCUMENT"); + } + } }