Skip to content

Commit

Permalink
Provide actual support for ExtLocationInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
pdvrieze committed Feb 14, 2024
1 parent cfc50ba commit 38c9320
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 16 deletions.
6 changes: 5 additions & 1 deletion core/api/android/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public final class nl/adaptivity/xmlutil/AndroidXmlReader : nl/adaptivity/xmluti
public fun getDepth ()I
public fun getEncoding ()Ljava/lang/String;
public fun getEventType ()Lnl/adaptivity/xmlutil/EventType;
public fun getExtLocationInfo ()Lnl/adaptivity/xmlutil/XmlReader$LocationInfo;
public fun getLocalName ()Ljava/lang/String;
public fun getLocationInfo ()Ljava/lang/String;
public fun getNamespaceContext ()Lnl/adaptivity/xmlutil/IterableNamespaceContext;
Expand Down Expand Up @@ -100,6 +101,7 @@ public final class nl/adaptivity/xmlutil/DomReader : nl/adaptivity/xmlutil/XmlRe
public fun getDepth ()I
public fun getEncoding ()Ljava/lang/String;
public fun getEventType ()Lnl/adaptivity/xmlutil/EventType;
public fun getExtLocationInfo ()Lnl/adaptivity/xmlutil/XmlReader$LocationInfo;
public fun getLocalName ()Ljava/lang/String;
public fun getLocationInfo ()Ljava/lang/String;
public fun getNamespaceContext ()Lnl/adaptivity/xmlutil/IterableNamespaceContext;
Expand Down Expand Up @@ -617,8 +619,9 @@ public final class nl/adaptivity/xmlutil/XmlReader$DefaultImpls {
public static fun require (Lnl/adaptivity/xmlutil/XmlReader;Lnl/adaptivity/xmlutil/EventType;Ljavax/xml/namespace/QName;)V
}

public final class nl/adaptivity/xmlutil/XmlReader$ExtLocationInfo {
public final class nl/adaptivity/xmlutil/XmlReader$ExtLocationInfo : nl/adaptivity/xmlutil/XmlReader$LocationInfo {
public fun <init> (III)V
public fun toString ()Ljava/lang/String;
}

public abstract interface class nl/adaptivity/xmlutil/XmlReader$LocationInfo {
Expand Down Expand Up @@ -905,6 +908,7 @@ public final class nl/adaptivity/xmlutil/core/KtXmlReader : nl/adaptivity/xmluti
public fun getDepth ()I
public fun getEncoding ()Ljava/lang/String;
public fun getEventType ()Lnl/adaptivity/xmlutil/EventType;
public fun getExtLocationInfo ()Lnl/adaptivity/xmlutil/XmlReader$LocationInfo;
public final fun getLineNumber ()I
public fun getLocalName ()Ljava/lang/String;
public fun getLocationInfo ()Ljava/lang/String;
Expand Down
6 changes: 5 additions & 1 deletion core/api/jvm/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public final class nl/adaptivity/xmlutil/DomReader : nl/adaptivity/xmlutil/XmlRe
public fun getDepth ()I
public fun getEncoding ()Ljava/lang/String;
public fun getEventType ()Lnl/adaptivity/xmlutil/EventType;
public fun getExtLocationInfo ()Lnl/adaptivity/xmlutil/XmlReader$LocationInfo;
public fun getLocalName ()Ljava/lang/String;
public fun getLocationInfo ()Ljava/lang/String;
public fun getNamespaceContext ()Lnl/adaptivity/xmlutil/IterableNamespaceContext;
Expand Down Expand Up @@ -176,6 +177,7 @@ public final class nl/adaptivity/xmlutil/StAXReader : nl/adaptivity/xmlutil/XmlR
public fun getDepth ()I
public fun getEncoding ()Ljava/lang/String;
public fun getEventType ()Lnl/adaptivity/xmlutil/EventType;
public fun getExtLocationInfo ()Lnl/adaptivity/xmlutil/XmlReader$LocationInfo;
public fun getLocalName ()Ljava/lang/String;
public final fun getLocation ()Ljavax/xml/stream/Location;
public fun getLocationInfo ()Ljava/lang/String;
Expand Down Expand Up @@ -640,8 +642,9 @@ public final class nl/adaptivity/xmlutil/XmlReader$DefaultImpls {
public static fun require (Lnl/adaptivity/xmlutil/XmlReader;Lnl/adaptivity/xmlutil/EventType;Ljavax/xml/namespace/QName;)V
}

public final class nl/adaptivity/xmlutil/XmlReader$ExtLocationInfo {
public final class nl/adaptivity/xmlutil/XmlReader$ExtLocationInfo : nl/adaptivity/xmlutil/XmlReader$LocationInfo {
public fun <init> (III)V
public fun toString ()Ljava/lang/String;
}

public abstract interface class nl/adaptivity/xmlutil/XmlReader$LocationInfo {
Expand Down Expand Up @@ -928,6 +931,7 @@ public final class nl/adaptivity/xmlutil/core/KtXmlReader : nl/adaptivity/xmluti
public fun getDepth ()I
public fun getEncoding ()Ljava/lang/String;
public fun getEventType ()Lnl/adaptivity/xmlutil/EventType;
public fun getExtLocationInfo ()Lnl/adaptivity/xmlutil/XmlReader$LocationInfo;
public final fun getLineNumber ()I
public fun getLocalName ()Ljava/lang/String;
public fun getLocationInfo ()Ljava/lang/String;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright (c) 2018.
* Copyright (c) 2024.
*
* This file is part of XmlUtil.
* This file is part of xmlutil.
*
* This file is licenced to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
Expand Down Expand Up @@ -129,13 +129,22 @@ public class AndroidXmlReader(public val parser: XmlPullParser) : XmlReader {
return null
}

@Deprecated("Use extLocationInfo as that allows more detailed information", ReplaceWith("extLocationInfo?.toString()"))
override val locationInfo: String
get() = buildString {
append(parser.lineNumber)
append(':')
append(parser.columnNumber)
}

override val extLocationInfo: XmlReader.LocationInfo
get() = XmlReader.ExtLocationInfo(
col = parser.columnNumber,
line = parser.lineNumber,
offset = -1
)


override val standalone: Boolean
get() = parser.getProperty("xmldecl-standalone") as Boolean

Expand Down
14 changes: 10 additions & 4 deletions core/src/commonMain/kotlin/nl/adaptivity/xmlutil/DomReader.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023.
* Copyright (c) 2024.
*
* This file is part of xmlutil.
*
Expand Down Expand Up @@ -120,9 +120,8 @@ public class DomReader(public val delegate: Node2) : XmlReader {

}

override val locationInfo: String
override val extLocationInfo: XmlReader.LocationInfo
get() {

fun <A : Appendable> helper(node: Node2?, result: A): A = when (node?.nodetype) {
null, NodeType.DOCUMENT_NODE
-> result
Expand All @@ -136,9 +135,16 @@ public class DomReader(public val delegate: Node2) : XmlReader {
else -> helper(node.parentNode, result).apply { append("/.") }
}

return helper(current, StringBuilder()).toString()
return XmlReader.StringLocationInfo(helper(current, StringBuilder()).toString())
}

@Deprecated(
"Use extLocationInfo as that allows more detailed information",
replaceWith = ReplaceWith("extLocationInfo?.toString()")
)
override val locationInfo: String
get() = extLocationInfo.toString()

private val requireCurrent get() = current ?: throw IllegalStateException("No current element")
private val requireCurrentElem get() = currentElement ?: throw IllegalStateException("No current element")

Expand Down
19 changes: 18 additions & 1 deletion core/src/commonMain/kotlin/nl/adaptivity/xmlutil/XmlReader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,24 @@ public interface XmlReader : Closeable, Iterator<EventType> {
/**
* Extended location info that actually provides column, line, and or file/string offset information
*/
public class ExtLocationInfo(private val col: Int, private val line: Int, private val offset:Int)
public class ExtLocationInfo(private val col: Int, private val line: Int, private val offset: Int) : LocationInfo {
override fun toString(): String = buildString {
when {
line >= 0 -> {
append(line)
if (col >= 0) {
append(':')
append(col)
}
}

offset >= 0 -> append('@').append(offset)

else -> append("<unknown>")
}

}
}
}

public val XmlReader.attributes: Array<out XmlEvent.Attribute>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023.
* Copyright (c) 2024.
*
* This file is part of xmlutil.
*
Expand Down Expand Up @@ -37,6 +37,7 @@ public class KtXmlReader internal constructor(

private var line = 1
private var column = 0
private var offset = 0

private var _eventType: EventType? = null //START_DOCUMENT // Already have this state
public override val eventType: EventType
Expand Down Expand Up @@ -692,6 +693,7 @@ public class KtXmlReader internal constructor(
}

peekCount--
offset++
column++
if (result == '\n'.code) {
line++
Expand Down Expand Up @@ -812,6 +814,9 @@ public class KtXmlReader internal constructor(
override val locationInfo: String
get() = "$line:$column"

override val extLocationInfo: XmlReader.LocationInfo
get() = XmlReader.ExtLocationInfo(col = column, line = line, offset = offset)

public fun getLineNumber(): Int {
return line
}
Expand Down
16 changes: 15 additions & 1 deletion core/src/jvmMain/kotlin/nl/adaptivity/xmlutil/StAXReader.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023.
* Copyright (c) 2024.
*
* This file is part of xmlutil.
*
Expand Down Expand Up @@ -126,12 +126,26 @@ public class StAXReader(private val delegate: XMLStreamReader) : XmlReader {
return delegate.namespaceContext.getPrefix(namespaceUri)
}

@Deprecated(
"Use extLocationInfo as that allows more detailed information",
replaceWith = ReplaceWith("extLocationInfo?.toString()")
)
override val locationInfo: String?
get() {
val location = delegate.location
return location?.toString()
}

override val extLocationInfo: XmlReader.LocationInfo?
get() {
val l = delegate.location ?: return null
return XmlReader.ExtLocationInfo(
col = l.columnNumber,
line = l.lineNumber,
offset = l.characterOffset,
)
}

public val location: Location
@Deprecated("")
get() = delegate.location
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023.
* Copyright (c) 2024.
*
* This file is part of xmlutil.
*
Expand Down Expand Up @@ -436,11 +436,11 @@ public sealed class XmlValueDescriptor(
defaultValue(xmlCodecBase.serializersModule, xmlCodecBase.config, deserializer)

xmlCodecBase is XmlDecoderBase ->
deserializer.deserialize(xmlCodecBase.StringDecoder(this, default))
deserializer.deserialize(xmlCodecBase.StringDecoder(this, XmlReader.ExtLocationInfo(0,0,0), default))

else -> xmlCodecBase.run {
val dec = XmlDecoderBase(serializersModule, config, CompactFragment("").getXmlReader())
.StringDecoder(this@XmlValueDescriptor, default)
.StringDecoder(this@XmlValueDescriptor, XmlReader.ExtLocationInfo(0,0,0), default)

deserializer.deserialize(dec)
}
Expand All @@ -461,8 +461,7 @@ public sealed class XmlValueDescriptor(

effectiveOutputKind.let { it == OutputKind.Attribute || it == OutputKind.Text } -> {
val xmlDecoderBase: XmlDecoderBase = XmlDecoderBase(serializersModule, config, CompactFragment(default).getXmlReader())
val dec = xmlDecoderBase
.StringDecoder(this, default)
val dec = xmlDecoderBase.StringDecoder(this, XmlReader.ExtLocationInfo(0, 0, 0), default)
deserializer.deserialize(dec)
}

Expand Down

0 comments on commit 38c9320

Please sign in to comment.