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
2121package 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