-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
125 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} | ||
} |