Skip to content

Commit

Permalink
some code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
brambg committed Jun 18, 2024
1 parent c11d55b commit a056543
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 33 deletions.
30 changes: 10 additions & 20 deletions src/main/kotlin/nl/knaw/huygens/tei/DelegatingVisitor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,22 @@ open class DelegatingVisitor<T : Context>(context: T) : DefaultVisitor() {
get() = context.result

fun setTextHandler(handler: TextHandler<T>) {
textHandler = Preconditions.checkNotNull(handler)
textHandler = handler
}

fun setCommentHandler(handler: CommentHandler<T>) {
commentHandler = Preconditions.checkNotNull(handler)
commentHandler = handler
}

fun setProcessingInstructionHandler(handler: ProcessingInstructionHandler<T>) {
processingInstructionHandler = Preconditions.checkNotNull(handler)
processingInstructionHandler = handler
}

fun setDefaultElementHandler(handler: ElementHandler<T>) {
defaultHandler = Preconditions.checkNotNull(handler)
defaultHandler = handler
}

fun addElementHandler(handler: ElementHandler<T>, vararg names: String) {
Preconditions.checkNotNull(handler)
for (name in names) {
handlers[name] = handler
}
Expand Down Expand Up @@ -93,23 +92,14 @@ open class DelegatingVisitor<T : Context>(context: T) : DefaultVisitor() {
}

// --- Visiting ------------------------------------------------------
override fun enterElement(element: Element): Traversal {
return getElementHandler(element).enterElement(element, context)
}
override fun enterElement(element: Element): Traversal = getElementHandler(element).enterElement(element, context)

override fun leaveElement(element: Element): Traversal {
return getElementHandler(element).leaveElement(element, context)
}
override fun leaveElement(element: Element): Traversal = getElementHandler(element).leaveElement(element, context)

override fun visitText(text: Text): Traversal {
return textHandler.visitText(text, context)
}
override fun visitText(text: Text): Traversal = textHandler.visitText(text, context)

override fun visitComment(comment: Comment): Traversal {
return commentHandler.visitComment(comment, context)
}
override fun visitComment(comment: Comment): Traversal = commentHandler.visitComment(comment, context)

override fun visitProcessingInstruction(processingInstruction: ProcessingInstruction): Traversal {
return processingInstructionHandler.visitProcessingInstruction(processingInstruction, context)
}
override fun visitProcessingInstruction(processingInstruction: ProcessingInstruction): Traversal =
processingInstructionHandler.visitProcessingInstruction(processingInstruction, context)
}
2 changes: 1 addition & 1 deletion src/main/kotlin/nl/knaw/huygens/tei/Element.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class Element @JvmOverloads constructor(// -------------------------------------

fun hasAttribute(key: String): Boolean {
val value = attributes[key]
return (value != null) && (value.isNotEmpty())
return !value.isNullOrEmpty()
}

fun hasAttribute(key: String, value: String): Boolean {
Expand Down
3 changes: 0 additions & 3 deletions src/main/kotlin/nl/knaw/huygens/tei/TeiException.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,4 @@ class TeiException : Exception {

constructor(format: String, vararg args: Any) : super(String.format(format, *args))

companion object {
private const val serialVersionUID = 1L
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ open class RenderTextHandler<T : Context> : TextHandler<T> {
return Traversal.NEXT
}

protected open fun filterText(content: String): String {
return content
}
protected open fun filterText(content: String): String =
content
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import nl.knaw.huygens.tei.Context
*/

class XmlTextHandler<T : Context> : RenderTextHandler<T>() {
override fun filterText(text: String): String {
val n = text.length
override fun filterText(content: String): String {
val n = content.length
val builder = StringBuilder((n * 1.1).toInt())
for (i in 0 until n) {
when (val c = text[i]) {
when (val c = content[i]) {
'<' -> builder.append("&lt;")
'>' -> builder.append("&gt;")
'&' -> builder.append("&amp;")
Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/nl/knaw/huygens/tei/xpath/XPathUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ object XPathUtil {
}

@Throws(XPathExpressionException::class)
fun evaluate(xpathQuery: String, xml: String): String {
return evaluate(xpathQuery, xml, String::class.java)
}
fun evaluate(xpathQuery: String, xml: String): String =
evaluate(xpathQuery, xml, String::class.java)

fun getNamespaceInfo(xml: String): Map<String, String> {
val namespaces: MutableMap<String, String> = Maps.newIdentityHashMap()
Expand Down
12 changes: 12 additions & 0 deletions src/test/kotlin/nl/knaw/huygens/tei/DefaultContextTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package nl.knaw.huygens.tei

import kotlin.test.assertEquals
import org.junit.Test

class DefaultContextTest {
@Test
fun `result is empty`() {
val context = DefaultContext()
assertEquals("", context.result)
}
}
6 changes: 6 additions & 0 deletions src/test/kotlin/nl/knaw/huygens/tei/TeiExceptionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ class TeiExceptionTest {
val e = TeiException("Test %d", 1)
assertEquals("Test 1", e.message)
}

@Test
fun testConstructor() {
val e = TeiException("Test")
assertEquals("Test", e.message)
}
}
15 changes: 15 additions & 0 deletions src/test/kotlin/nl/knaw/huygens/tei/handlers/XmlTextHandlerTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package nl.knaw.huygens.tei.handlers

import kotlin.test.Test
import nl.knaw.huygens.tei.DefaultContext
import nl.knaw.huygens.tei.Text

class XmlTextHandlerTest {
@Test
fun test() {
val handler = XmlTextHandler<DefaultContext>()
val context = DefaultContext()
handler.visitText(Text("test"), context)
}
}

0 comments on commit a056543

Please sign in to comment.