From 6940973089008a9b1a4102926301beea387a5268 Mon Sep 17 00:00:00 2001 From: Koen Lavooij Date: Thu, 19 May 2022 11:43:41 +0200 Subject: [PATCH 1/2] Be able to point to the exact loctions of attributes --- .../java/com/ctc/wstx/sr/AttributeCollector.java | 9 +++++++++ .../java/com/ctc/wstx/sr/BasicStreamReader.java | 6 ++++++ src/main/java/com/ctc/wstx/util/TextBuilder.java | 15 +++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/main/java/com/ctc/wstx/sr/AttributeCollector.java b/src/main/java/com/ctc/wstx/sr/AttributeCollector.java index a0b5e1f4..10f24589 100644 --- a/src/main/java/com/ctc/wstx/sr/AttributeCollector.java +++ b/src/main/java/com/ctc/wstx/sr/AttributeCollector.java @@ -30,6 +30,7 @@ import org.codehaus.stax2.typed.TypedValueDecoder; import org.codehaus.stax2.typed.TypedXMLStreamException; import org.codehaus.stax2.validation.XMLValidator; +import org.codehaus.stax2.XMLStreamLocation2; import com.ctc.wstx.api.ReaderConfig; import com.ctc.wstx.cfg.ErrorConsts; @@ -523,6 +524,14 @@ public final int getXmlIdAttrIndex() { return mXmlIdAttrIndex; } + public final XMLStreamLocation2 getValueLocationStart(int index) { + return mNamespaceBuilder.getLocation(index * 2); + } + + public final XMLStreamLocation2 getValueLocationEnd(int index) { + return mNamespaceBuilder.getLocation(index * 2 + 1); + } + /* ////////////////////////////////////////////////////// // Type-safe accessors to support TypedXMLStreamReader diff --git a/src/main/java/com/ctc/wstx/sr/BasicStreamReader.java b/src/main/java/com/ctc/wstx/sr/BasicStreamReader.java index 4378eefe..38e7f527 100644 --- a/src/main/java/com/ctc/wstx/sr/BasicStreamReader.java +++ b/src/main/java/com/ctc/wstx/sr/BasicStreamReader.java @@ -3150,7 +3150,10 @@ private final boolean handleNsAttrs(char c) } else { tb = ac.getAttrBuilder(prefix, localName); } + tb.pushLocation(getCurrentLocation()); parseAttrValue(c, tb); + tb.pushLocation(getCurrentLocation()); + /* 19-Jul-2004, TSa: Need to check that non-default namespace * URI is NOT empty, as per XML namespace specs, #2, @@ -3224,8 +3227,11 @@ private final boolean handleNonNsAttrs(char c) } // And then the actual value + tb.pushLocation(getCurrentLocation()); parseAttrValue(c, tb); + tb.pushLocation(getCurrentLocation()); // and then we need to iterate some more + c = (mInputPtr < mInputEnd) ? mInputBuffer[mInputPtr++] : getNextCharFromCurrent(SUFFIX_IN_ELEMENT); } diff --git a/src/main/java/com/ctc/wstx/util/TextBuilder.java b/src/main/java/com/ctc/wstx/util/TextBuilder.java index f9c18133..8fffdb0e 100644 --- a/src/main/java/com/ctc/wstx/util/TextBuilder.java +++ b/src/main/java/com/ctc/wstx/util/TextBuilder.java @@ -1,5 +1,9 @@ package com.ctc.wstx.util; +import org.codehaus.stax2.XMLStreamLocation2; + +import java.util.ArrayList; + /** * Class similar to {@link StringBuilder}, except that it can be used to * construct multiple Strings, that will share same underlying character @@ -17,6 +21,8 @@ public final class TextBuilder private String mResultString; + private ArrayList mLocations; + /* /////////////////////////////////////////////////////////////////////// // Life-cycle: @@ -32,6 +38,7 @@ public TextBuilder(int initialSize) charSize = MAX_LEN; } mBuffer = new char[charSize]; + mLocations = new ArrayList<>(initialSize << 1); } /** @@ -40,6 +47,7 @@ public TextBuilder(int initialSize) */ public void reset() { mBufferLen = 0; + mLocations.clear(); mResultString = null; } @@ -61,6 +69,9 @@ public String getAllValues() return mResultString; } + public void pushLocation(XMLStreamLocation2 location) { + mLocations.add(location); + } /** * Method that gives access to underlying character buffer */ @@ -131,4 +142,8 @@ private void resize(int needSpaceFor) { mBuffer = new char[oldLen+addition]; System.arraycopy(old, 0, mBuffer, 0, mBufferLen); } + + public XMLStreamLocation2 getLocation(int index) { + return mLocations.get(index); + } } From 9b827e3dc66897af045331e71d0305f00fb0b666 Mon Sep 17 00:00:00 2001 From: Koen Lavooij Date: Thu, 19 May 2022 20:27:33 +0200 Subject: [PATCH 2/2] Get rid of diamond operator --- src/main/java/com/ctc/wstx/util/TextBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/ctc/wstx/util/TextBuilder.java b/src/main/java/com/ctc/wstx/util/TextBuilder.java index 8fffdb0e..575fbf3e 100644 --- a/src/main/java/com/ctc/wstx/util/TextBuilder.java +++ b/src/main/java/com/ctc/wstx/util/TextBuilder.java @@ -38,7 +38,7 @@ public TextBuilder(int initialSize) charSize = MAX_LEN; } mBuffer = new char[charSize]; - mLocations = new ArrayList<>(initialSize << 1); + mLocations = new ArrayList(initialSize << 1); } /**