Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Ronove
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Kaj Koivunen
Ronove
Commits
ee21cb34
Commit
ee21cb34
authored
2 years ago
by
Kaj Koivunen
Browse files
Options
Downloads
Patches
Plain Diff
testejä
parent
2cf6005b
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/kotlin/guru/kake/xmlp/XMLParser.kt
+11
-3
11 additions, 3 deletions
src/main/kotlin/guru/kake/xmlp/XMLParser.kt
src/test/kotlin/guru/kake/xmlp/XMLParserTest.kt
+58
-0
58 additions, 0 deletions
src/test/kotlin/guru/kake/xmlp/XMLParserTest.kt
with
69 additions
and
3 deletions
src/main/kotlin/guru/kake/xmlp/XMLParser.kt
+
11
−
3
View file @
ee21cb34
package
guru.kake.xmlp
package
guru.kake.xmlp
import
java.io.FileInputStream
import
java.io.FileInputStream
import
java.io.InputStream
import
javax.xml.namespace.QName
import
javax.xml.namespace.QName
import
javax.xml.stream.XMLEventReader
import
javax.xml.stream.XMLEventReader
import
javax.xml.stream.XMLInputFactory
import
javax.xml.stream.XMLInputFactory
...
@@ -14,18 +15,25 @@ annotation class DSL
...
@@ -14,18 +15,25 @@ annotation class DSL
* Implements a DSL-language for event-based parsing of XML files.
* Implements a DSL-language for event-based parsing of XML files.
*/
*/
@DSL
@DSL
class
XMLParser
private
constructor
(
path
:
String
)
{
class
XMLParser
private
constructor
(
stream
:
InputStream
)
{
companion
object
{
companion
object
{
/**
/**
* Parse an XML-file using an event-based interface
* Parse an XML-file using an event-based interface
* @param path path to file
* @param path path to file
* @param init lambda to execute on each XML event
* @param init lambda to execute on each XML event
*/
*/
fun
parse
(
path
:
String
,
init
:
XMLParser
.()
->
Unit
):
Unit
=
XMLParser
(
path
).
init
(
init
)
fun
parse
(
path
:
String
,
init
:
XMLParser
.()
->
Unit
):
Unit
=
XMLParser
(
FileInputStream
(
path
)).
init
(
init
)
/**
* Parse an XML-file using an event-based interface
* @param stream [InputStream] containing XML data
* @param init lambda to execute on each XML event
*/
fun
parse
(
stream
:
InputStream
,
init
:
XMLParser
.()
->
Unit
):
Unit
=
XMLParser
(
stream
).
init
(
init
)
}
}
private
val
factory
:
XMLInputFactory
=
XMLInputFactory
.
newInstance
()
private
val
factory
:
XMLInputFactory
=
XMLInputFactory
.
newInstance
()
private
val
reader
:
XMLEventReader
=
factory
.
createXMLEventReader
(
FileInputStream
(
path
)
)
private
val
reader
:
XMLEventReader
=
factory
.
createXMLEventReader
(
stream
)
private
val
elements
:
MutableMap
<
String
,
Element
>
=
mutableMapOf
()
private
val
elements
:
MutableMap
<
String
,
Element
>
=
mutableMapOf
()
private
fun
init
(
init
:
XMLParser
.()
->
Unit
)
{
private
fun
init
(
init
:
XMLParser
.()
->
Unit
)
{
...
...
This diff is collapsed.
Click to expand it.
src/test/kotlin/guru/kake/xmlp/XMLParserTest.kt
0 → 100644
+
58
−
0
View file @
ee21cb34
package
guru.kake.xmlp
import
org.junit.jupiter.api.Assertions.assertEquals
import
org.junit.jupiter.api.Test
import
kotlin.test.assertFailsWith
class
XMLParserTest
{
private
val
xml
:
String
=
"""
<tag>
<tag><cont foo="not" bar="not">one</cont></tag>
<tag><attr foo="two" bar="not">not</attr></tag>
<tag><tag><both foo="not" bar="three">four</both></tag></tag>
<attr foo="five" />
</tag>
"""
.
trimIndent
()
private
val
eof
:
String
=
"""
<tag><tag><tag></tag></tag>
"""
.
trimIndent
()
private
val
close
:
String
=
"""
<tag></tag></tag>
"""
.
trimIndent
()
@Test
fun
parseXML
()
{
val
sb
=
StringBuilder
()
XMLParser
.
parse
(
xml
.
byteInputStream
())
{
element
(
"cont"
)
{
start
{
sb
.
append
(
content
())
}
}
element
(
"attr"
)
{
start
{
sb
.
append
(
attribute
(
"foo"
))}
}
element
(
"both"
)
{
start
{
sb
.
append
(
content
())
sb
.
append
(
attribute
(
"bar"
))
}
}
element
(
"tag"
)
{
start
{
sb
.
append
(
"{"
)
}
end
{
sb
.
append
(
"}"
)
}
}
}
assertEquals
(
sb
.
toString
(),
"{{one}{two}{{fourthree}}five}"
)
}
@Test
fun
malformed
()
{
assertFailsWith
<
com
.
ctc
.
wstx
.
exc
.
WstxEOFException
>
{
XMLParser
.
parse
(
eof
.
byteInputStream
())
{}}
assertFailsWith
<
com
.
ctc
.
wstx
.
exc
.
WstxParsingException
>
{
XMLParser
.
parse
(
close
.
byteInputStream
())
{}}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment