Commit bdf2baf0 authored by Ricki Hirner's avatar Ricki Hirner

Refactoring

parent c1a82b27
Pipeline #15713443 failed with stages
in 1 minute 43 seconds
......@@ -60,8 +60,7 @@ dependencies {
androidTestCompile 'com.squareup.okhttp3:mockwebserver:3.9.1'
androidTestCompile 'junit:junit:4.12'
//noinspection GradleDynamicVersion
androidTestCompile 'com.android.support.test:runner:+'
androidTestCompile 'com.android.support.test:runner:1.0.1'
testCompile 'junit:junit:4.12'
testCompile 'com.squareup.okhttp3:mockwebserver:3.9.1'
......
......@@ -390,7 +390,7 @@ open class DavResource @JvmOverloads constructor(
var eventType = parser.eventType
while (!(eventType == XmlPullParser.END_TAG && parser.depth == depth)) {
if (eventType == XmlPullParser.START_TAG && parser.depth == depth+1) {
if (eventType == XmlPullParser.START_TAG && parser.depth == depth + 1) {
val name = Property.Name(parser.namespace, parser.name)
val property = PropertyRegistry.create(name, parser)
if (property != null)
......
......@@ -15,7 +15,7 @@ import java.util.logging.Level
object PropertyRegistry {
val factories = mutableMapOf<String /*namespace*/, MutableMap<String /*name*/, PropertyFactory>>()
private val factories = mutableMapOf<Property.Name, PropertyFactory>()
init {
Constants.log.info("Registering DAV property factories")
......@@ -27,24 +27,15 @@ object PropertyRegistry {
private fun register(factory: PropertyFactory) {
val name = factory.getName()
var nsFactories = factories[name.namespace]
if (nsFactories == null) {
nsFactories = mutableMapOf<String, PropertyFactory>()
factories[name.namespace] = nsFactories
}
nsFactories[name.name] = factory
factories[factory.getName()] = factory
}
fun create(name: Property.Name, parser: XmlPullParser): Property? {
try {
factories[name.namespace]?.let { nsFactories ->
nsFactories[name.name]?.let { factory -> return factory.create(parser) }
fun create(name: Property.Name, parser: XmlPullParser) =
try {
factories[name]?.create(parser)
} catch (e: XmlPullParserException) {
Constants.log.log(Level.WARNING, "Couldn't parse $name", e)
null
}
} catch (e: XmlPullParserException) {
Constants.log.log(Level.WARNING, "Couldn't parse $name", e)
}
return null
}
}
......@@ -39,14 +39,13 @@ object XmlUtils {
@Throws(IOException::class, XmlPullParserException::class)
fun processTag(parser: XmlPullParser, namespace: String, name: String, lambda: () -> Unit) {
fun processTag(parser: XmlPullParser, namespace: String, name: String, processor: () -> Unit) {
val depth = parser.depth
var eventType = parser.eventType
while (!(eventType == XmlPullParser.END_TAG && parser.depth == depth)) {
while (!((eventType == XmlPullParser.END_TAG || eventType == XmlPullParser.END_DOCUMENT) && parser.depth == depth)) {
if (eventType == XmlPullParser.START_TAG && parser.depth == depth + 1 &&
parser.namespace == namespace && parser.name == name) {
lambda()
}
parser.namespace == namespace && parser.name == name)
processor()
eventType = parser.next()
}
}
......@@ -56,10 +55,9 @@ object XmlUtils {
var text: String? = null
val depth = parser.depth
var eventType = parser.eventType
while (!(eventType == XmlPullParser.END_TAG && parser.depth == depth)) {
if (eventType == XmlPullParser.TEXT && parser.depth == depth)
if (eventType == XmlPullParser.TEXT && parser.depth == depth && !parser.isWhitespace)
text = parser.text
eventType = parser.next()
}
......@@ -70,9 +68,8 @@ object XmlUtils {
@Throws(IOException::class, XmlPullParserException::class)
fun readTextPropertyList(parser: XmlPullParser, name: Property.Name, list: MutableCollection<String>) {
val depth = parser.depth
var eventType = parser.eventType
while (!(eventType == XmlPullParser.END_TAG && parser.depth == depth)) {
while (!((eventType == XmlPullParser.END_TAG || eventType == XmlPullParser.END_DOCUMENT) && parser.depth == depth)) {
if (eventType == XmlPullParser.START_TAG && parser.depth == depth + 1 &&
Property.Name(parser.namespace, parser.name) == name)
list.add(parser.nextText())
......
/*
* Copyright © Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.dav4android
import junit.framework.Assert.assertEquals
import junit.framework.Assert.assertTrue
import org.junit.Test
import org.xmlpull.v1.XmlPullParser
import java.io.StringReader
class XmlUtilsTest {
@Test
fun testProcessTagRoot() {
val parser = XmlUtils.newPullParser()
parser.setInput(StringReader("<test></test>"))
// now on START_DOCUMENT [0]
var processed = false
XmlUtils.processTag(parser, "", "test", {
processed = true
})
assertTrue(processed)
}
@Test
fun testProcessTagDepth1() {
val parser = XmlUtils.newPullParser()
parser.setInput(StringReader("<root><test></test></root>"))
parser.next() // now on START_TAG <root> [1]
var processed = false
XmlUtils.processTag(parser, "", "test", {
processed = true
})
assertTrue(processed)
}
@Test
fun testReadText() {
val parser = XmlUtils.newPullParser()
parser.setInput(StringReader("<test>Test 1</test><test><garbage/>Test 2</test>"))
parser.next() // now on START_TAG <test> [1]
assertEquals("Test 1", XmlUtils.readText(parser))
assertEquals(XmlPullParser.END_TAG, parser.eventType)
parser.next()
assertEquals("Test 2", XmlUtils.readText(parser))
assertEquals(XmlPullParser.END_TAG, parser.eventType)
}
@Test
fun testReadTextPropertyRoot() {
val parser = XmlUtils.newPullParser()
parser.setInput(StringReader("<entry>Test 1</entry><entry>Test 2</entry>"))
// now on START_DOCUMENT [0]
val entries = mutableListOf<String>()
XmlUtils.readTextPropertyList(parser, Property.Name("", "entry"), entries)
assertEquals("Test 1", entries[0])
assertEquals("Test 2", entries[1])
assertEquals(XmlPullParser.END_DOCUMENT, parser.eventType)
}
@Test
fun testReadTextPropertyListDepth1() {
val parser = XmlUtils.newPullParser()
parser.setInput(StringReader("<test><entry>Test 1</entry><entry>Test 2</entry></test>"))
parser.next() // now on START_TAG <test> [1]
val entries = mutableListOf<String>()
XmlUtils.readTextPropertyList(parser, Property.Name("", "entry"), entries)
assertEquals("Test 1", entries[0])
assertEquals("Test 2", entries[1])
assertEquals(XmlPullParser.END_TAG, parser.eventType)
assertEquals("test", parser.name)
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment