Skip to content

Commit 6322e20

Browse files
committed
Add support for expanding entities.
1 parent 6522f45 commit 6322e20

File tree

18 files changed

+457
-238
lines changed

18 files changed

+457
-238
lines changed

core/android/api/coreAndroid.api

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
public final class nl/adaptivity/xmlutil/core/AndroidStreamingFactory : nl/adaptivity/xmlutil/XmlStreamingFactory {
22
public fun <init> ()V
3-
public fun newReader (Ljava/io/InputStream;)Lnl/adaptivity/xmlutil/XmlReader;
4-
public fun newReader (Ljava/io/InputStream;Ljava/lang/String;)Lnl/adaptivity/xmlutil/XmlReader;
5-
public fun newReader (Ljava/io/Reader;)Lnl/adaptivity/xmlutil/XmlReader;
3+
public fun newReader (Ljava/io/InputStream;Ljava/lang/String;Z)Lnl/adaptivity/xmlutil/XmlReader;
4+
public fun newReader (Ljava/io/InputStream;Z)Lnl/adaptivity/xmlutil/XmlReader;
5+
public fun newReader (Ljava/io/Reader;Z)Lnl/adaptivity/xmlutil/XmlReader;
66
public fun newReader (Ljavax/xml/transform/Source;)Lnl/adaptivity/xmlutil/XmlReader;
77
public fun newWriter (Ljava/io/OutputStream;Ljava/lang/String;ZLnl/adaptivity/xmlutil/XmlDeclMode;)Lnl/adaptivity/xmlutil/XmlWriter;
88
public fun newWriter (Ljava/io/Writer;ZLnl/adaptivity/xmlutil/XmlDeclMode;)Lnl/adaptivity/xmlutil/XmlWriter;
99
public fun newWriter (Ljavax/xml/transform/Result;ZLnl/adaptivity/xmlutil/XmlDeclMode;)Lnl/adaptivity/xmlutil/XmlWriter;
1010
}
1111

1212
public final class nl/adaptivity/xmlutil/core/AndroidXmlReader : nl/adaptivity/xmlutil/XmlReader {
13-
public fun <init> (Ljava/io/InputStream;Ljava/lang/String;)V
13+
public fun <init> (Ljava/io/InputStream;Ljava/lang/String;Z)V
14+
public synthetic fun <init> (Ljava/io/InputStream;Ljava/lang/String;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V
1415
public fun <init> (Ljava/io/Reader;)V
16+
public fun <init> (Ljava/io/Reader;Z)V
17+
public synthetic fun <init> (Ljava/io/Reader;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V
1518
public fun <init> (Lorg/xmlpull/v1/XmlPullParser;)V
19+
public fun <init> (Lorg/xmlpull/v1/XmlPullParser;Z)V
1620
public fun close ()V
1721
public fun getAttributeCount ()I
1822
public fun getAttributeLocalName (I)Ljava/lang/String;
@@ -23,6 +27,7 @@ public final class nl/adaptivity/xmlutil/core/AndroidXmlReader : nl/adaptivity/x
2327
public fun getDepth ()I
2428
public fun getEncoding ()Ljava/lang/String;
2529
public fun getEventType ()Lnl/adaptivity/xmlutil/EventType;
30+
public final fun getExpandEntities ()Z
2631
public fun getExtLocationInfo ()Lnl/adaptivity/xmlutil/XmlReader$LocationInfo;
2732
public fun getLocalName ()Ljava/lang/String;
2833
public fun getLocationInfo ()Ljava/lang/String;

core/android/src/main/kotlin/nl/adaptivity/xmlutil/core/AndroidStreamingFactory.kt

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
/*
2-
* Copyright (c) 2024.
2+
* Copyright (c) 2024-2025.
33
*
44
* This file is part of xmlutil.
55
*
6-
* This file is licenced to you under the Apache License, Version 2.0 (the
7-
* "License"); you may not use this file except in compliance
8-
* with the License. You should have received a copy of the license with the source distribution.
9-
* Alternatively, you may obtain a copy of the License at
6+
* This file is licenced to you under the Apache License, Version 2.0
7+
* (the "License"); you may not use this file except in compliance
8+
* with the License. You should have received a copy of the license
9+
* with the source distribution. Alternatively, you may obtain a copy
10+
* of the License at
1011
*
1112
* http://www.apache.org/licenses/LICENSE-2.0
1213
*
13-
* Unless required by applicable law or agreed to in writing,
14-
* software distributed under the License is distributed on an
15-
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16-
* KIND, either express or implied. See the License for the
17-
* specific language governing permissions and limitations
18-
* under the License.
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17+
* implied. See the License for the specific language governing
18+
* permissions and limitations under the License.
1919
*/
2020

2121
package nl.adaptivity.xmlutil.core
@@ -61,32 +61,32 @@ public class AndroidStreamingFactory : XmlStreamingFactory {
6161
}
6262

6363
@Throws(XmlException::class)
64-
override fun newReader(reader: Reader): XmlReader {
64+
override fun newReader(reader: Reader, expandEntities: Boolean): XmlReader {
6565
try {
66-
return AndroidXmlReader(reader)
66+
return AndroidXmlReader(reader, expandEntities)
6767
} catch (e: XmlPullParserException) {
6868
throw XmlException(e)
6969
}
7070

7171
}
7272

7373
@Throws(XmlException::class)
74-
override fun newReader(inputStream: InputStream): XmlReader {
74+
override fun newReader(inputStream: InputStream, expandEntities: Boolean): XmlReader {
7575
try {
7676
val parser = XmlPullParserFactory.newInstance().newPullParser().also {
7777
it.setInput(inputStream, null)
7878
}
79-
return AndroidXmlReader(parser)
79+
return AndroidXmlReader(parser, expandEntities)
8080
} catch (e: XmlPullParserException) {
8181
throw XmlException(e)
8282
}
8383

8484
}
8585

8686
@Throws(XmlException::class)
87-
override fun newReader(inputStream: InputStream, encoding: String): XmlReader {
87+
override fun newReader(inputStream: InputStream, encoding: String, expandEntities: Boolean): XmlReader {
8888
try {
89-
return AndroidXmlReader(inputStream, encoding)
89+
return AndroidXmlReader(inputStream, encoding, expandEntities)
9090
} catch (e: XmlPullParserException) {
9191
throw XmlException(e)
9292
}

core/android/src/main/kotlin/nl/adaptivity/xmlutil/core/AndroidXmlReader.kt

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
/*
2-
* Copyright (c) 2024.
2+
* Copyright (c) 2024-2025.
33
*
44
* This file is part of xmlutil.
55
*
6-
* This file is licenced to you under the Apache License, Version 2.0 (the
7-
* "License"); you may not use this file except in compliance
8-
* with the License. You should have received a copy of the license with the source distribution.
9-
* Alternatively, you may obtain a copy of the License at
6+
* This file is licenced to you under the Apache License, Version 2.0
7+
* (the "License"); you may not use this file except in compliance
8+
* with the License. You should have received a copy of the license
9+
* with the source distribution. Alternatively, you may obtain a copy
10+
* of the License at
1011
*
1112
* http://www.apache.org/licenses/LICENSE-2.0
1213
*
13-
* Unless required by applicable law or agreed to in writing,
14-
* software distributed under the License is distributed on an
15-
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16-
* KIND, either express or implied. See the License for the
17-
* specific language governing permissions and limitations
18-
* under the License.
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17+
* implied. See the License for the specific language governing
18+
* permissions and limitations under the License.
1919
*/
2020

2121
package nl.adaptivity.xmlutil.core
@@ -29,17 +29,20 @@ import java.io.Reader
2929
/**
3030
* And XMLReader implementation that works on Android
3131
*/
32-
public class AndroidXmlReader(public val parser: XmlPullParser) : XmlReader {
32+
public class AndroidXmlReader(public val parser: XmlPullParser, public val expandEntities: Boolean) : XmlReader {
3333
override var isStarted: Boolean = false
3434
private set
3535

36-
private constructor() : this(XmlPullParserFactory.newInstance().apply { isNamespaceAware = true }.newPullParser())
36+
public constructor(parser: XmlPullParser): this(parser, false)
3737

38-
public constructor(reader: Reader) : this() {
38+
private constructor(expandEntities: Boolean) : this(XmlPullParserFactory.newInstance().apply { isNamespaceAware = true }.newPullParser(), expandEntities)
39+
40+
@JvmOverloads
41+
public constructor(reader: Reader, expandEntities: Boolean = false) : this(expandEntities) {
3942
parser.setInput(reader)
4043
}
4144

42-
public constructor(input: InputStream, encoding: String) : this() {
45+
public constructor(input: InputStream, encoding: String, expandEntities: Boolean = false) : this(expandEntities) {
4346
parser.setInput(input, encoding)
4447
}
4548

@@ -67,13 +70,15 @@ public class AndroidXmlReader(public val parser: XmlPullParser) : XmlReader {
6770
}
6871
else -> n
6972
}
70-
return DELEGATE_TO_LOCAL[nextToken].also {
73+
return delegateToLocal(nextToken).also {
7174
eventType = it
7275
if (it == EventType.START_DOCUMENT) version = parser.getAttributeValue(null, "version")
7376
isStarted = true
7477
}
7578
}
7679

80+
private fun delegateToLocal(nextToken: Int): EventType = when { else -> DELEGATE_TO_LOCAL[nextToken] }
81+
7782
@Throws(XmlException::class)
7883
override fun nextTag(): EventType {
7984
var et = next()
@@ -202,6 +207,8 @@ public class AndroidXmlReader(public val parser: XmlPullParser) : XmlReader {
202207

203208
@Suppress("UNCHECKED_CAST")
204209
private val DELEGATE_TO_LOCAL = arrayOfNulls<EventType>(11) as Array<EventType>
210+
@Suppress("UNCHECKED_CAST")
211+
private val DELEGATE_TO_LOCAL_EXPANDED: Array<EventType>
205212

206213
private val LOCAL_TO_DELEGATE: IntArray = IntArray(12)
207214

@@ -218,6 +225,13 @@ public class AndroidXmlReader(public val parser: XmlPullParser) : XmlReader {
218225
DELEGATE_TO_LOCAL[XmlPullParser.START_TAG] = EventType.START_ELEMENT
219226
DELEGATE_TO_LOCAL[XmlPullParser.TEXT] = EventType.TEXT
220227

228+
DELEGATE_TO_LOCAL_EXPANDED = Array<EventType>(11) {
229+
when (val v = DELEGATE_TO_LOCAL[it]) {
230+
EventType.ENTITY_REF -> EventType.TEXT
231+
else -> v
232+
}
233+
}
234+
221235
LOCAL_TO_DELEGATE[EventType.CDSECT.ordinal] = XmlPullParser.CDSECT
222236
LOCAL_TO_DELEGATE[EventType.COMMENT.ordinal] = XmlPullParser.COMMENT
223237
LOCAL_TO_DELEGATE[EventType.DOCDECL.ordinal] = XmlPullParser.DOCDECL

0 commit comments

Comments
 (0)