Skip to content

Commit

Permalink
rescue TeiUtils + test
Browse files Browse the repository at this point in the history
  • Loading branch information
brambg committed Jun 18, 2024
1 parent d322099 commit 5ee8099
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 58 deletions.
70 changes: 70 additions & 0 deletions src/main/kotlin/nl/knaw/huygens/tei/TeiUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package nl.knaw.huygens.tei

/*
* #%L
* VisiTEI
* =======
* Copyright (C) 2011 - 2017 Huygens ING
* =======
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

class TeiUtils private constructor() {
init {
throw AssertionError("Non-instantiable class")
}

companion object {
/**
* Returns the nearest ancestor with the specified name,
* `null` if no such an element exists.
*/
fun getAncestor(origElement: Element, name: String): Element? {
var element: Element? = origElement
element = element?.parent
while (element != null) {
if (element.hasName(name)) {
return element
}
element = element.parent
}
return null
}

/**
* Returns `true` if the specified element has an ancestor
* with the specified name, `false` otherwise.
*/
fun hasAncestor(element: Element, name: String): Boolean =
getAncestor(element, name) != null

/**
* Returns `true` if the specified element has an ancestor
* with one of the specified names, `false` otherwise.
*/
fun hasAncestor(originalElement: Element, names: Collection<String>): Boolean {
var element: Element? = originalElement
element = element?.parent
while (element != null) {
if (names.contains(element.name)) {
return true
}
element = element.parent
}
return false
}
}
}
58 changes: 0 additions & 58 deletions src/test/java/nl/knaw/huygens/tei/TeiUtilsTest.java

This file was deleted.

55 changes: 55 additions & 0 deletions src/test/kotlin/nl/knaw/huygens/tei/TeiUtilsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package nl.knaw.huygens.tei

import kotlin.test.*
import org.junit.Test

/*
* #%L
* VisiTEI
* =======
* Copyright (C) 2011 - 2017 Huygens ING
* =======
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

class TeiUtilsTest {
private val testDocument: Document
get() {
val xml = "<root><a n=\"1\"><a n=\"2\"><x/></a></a><y/></root>"
return Document.createFromXml(xml, false)
}

@Test
fun testGetAncestorWithMissingParent() {
val document = testDocument
val elements = document.getElementsByTagName("y")
assertEquals(1, elements.size.toLong())
val element = elements[0]
val ancestor: Element? = TeiUtils.getAncestor(element, "a")
assertNull(ancestor)
}

@Test
fun testGetAncestorWithParent() {
val document = testDocument
val elements = document.getElementsByTagName("x")
assertEquals(1, elements.size.toLong())
val element = elements[0]
val ancestor: Element? = TeiUtils.getAncestor(element, "a")
assertNotNull(ancestor)
assertTrue(ancestor.hasName("a") && ancestor.hasAttribute("n", "2"))
}
}

0 comments on commit 5ee8099

Please sign in to comment.