ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - asm0dey/staks: Kotlin wrapper for StAX
Kotlin wrapper for StAX. Contribute to asm0dey/staks development by creating an account on GitHub.
Visit Site

GitHub - asm0dey/staks: Kotlin wrapper for StAX

GitHub - asm0dey/staks: Kotlin wrapper for StAX

staks Maven Central

Kotlin wrapper for StAX

How many times did you try to remember how to use StAX or SAX? How many times did you need to parse more or less complex data structures from XML using StAX API? It is a pain, isn't it?

This project aims to solve this problem in idiomatic way for Kotlin.

Why should I use it?

You should use it in several cases:

  1. You don't want to build the complex parser based on SAX/StAX yourself
  2. DOM is too heavy for your goals
  3. Your XML document may be not well-formed and the DOM parser will fail on parsing.

Installation

For Maven:

<dependency>
  <groupId>com.github.asm0dey</groupId>
  <artifactId>staks</artifactId>
  <version>0.0.1</version>
</dependency>

For Gradle:

implementation 'com.github.asm0dey:staks:0.0.1'

And then import

import staks.*

Usage

The core of library is staks function, lying in package staks. For example imagine we need to gather all texts of a tag in XML document. Code will looks like this:

val result = staks<List<String>>("root-tag"){ // ①
  val texts = list(tagText("a")); // ②
  { texts() } // ③
}

What we can see here:

  1. We initialize staks function, defining return type (Yes, to a pity we should do it for now).
  2. We are creating a list of texts of contents of the a tag. Please, note the semicolon on this line. We need this because the next line starts with {, without semicolon Kotlin will decide that it's a call of the function.
  3. We're defining closure, building return type. Please, note that on this line we are calling texts variable. That's because all Handlers are functions which eventually return parsed value or will throw an exception.

But obviously, this is not enough to build a list of strings, in real life we usually need to build more or less complex structures. Let's look at more complex examples.

        val inp =
            "<root><el><a>a1</a><b>b1</b><c>c1</c></el><el><a>a2</a><b>b2</b><c>c2</c></el><el><a>a3</a><b>b3</b><c>c3</c></el></root>".byteInputStream()
        val lst = staks<List<Triple<String, String, String>>>(inp) {
            val lst = list("el") {
                val a = tagText("a")
                val b = tagText("b")
                val c = tagText("c");
                { Triple(a(), b(), c()) }
            };
            { lst() }
        }

Here we're building Triples of a, b and c subtags of el subtag. Note that these elements may be in any order and at any depth inside the el tag as well as the el tag may be located at any depth inside the root of XML. BTW, you may operate on root tags too!

How to not parse the whole document?

Actually, there are to types of handlers in staks — singular and plural. The only plural handlers OOTB are list handlers. If you have no list builders inside your code — it will stop parsing as soon as all the data will be found.

But what if you have a list in the first part of the document inside a certain tag and don't want to process the document after this tag?

Easy! You need to define a tag inside which you will look for data and we have single for that.

For example:

val lst = staks<List<Pair<Int, String>>>(input){
  val data = single("header"){
    val list = list("person"){
      val name = tagText("name")
      val age = tagText("age").int();
      { age() to name() }
    };
    { list() };
  };
  { data() }
}

Here we will look for person tags only inside the header tag. As soon as we meet the closing header tag, processing will be stopped and we'll obtain a list of persons inside lst variable.

One more thing to meet look at is .int() call. It's delegation to the handler, which just converts String to int, its code is very simple and may be found here: https://github.com/asm0dey/staks/blob/f8fd73e1482e838dfa8c17781226407ba49b7fde/src/main/kotlin/staks/delegates.kt#L9-L17

Also, there is .optional() delegate which allows to return null from standard handlers (of course you can write your own, allowing null by default).

Further usage examples may be found in tests

Writing own handlers

A handler is just a class, which implements Handler interface.

To make parent handlers aware of child ones we should call register or decorate on children. For example list from first exampe decorates underlying handler: https://github.com/asm0dey/staks/blob/f8fd73e1482e838dfa8c17781226407ba49b7fde/src/main/kotlin/staks/base.kt#L196, and tagText https://github.com/asm0dey/staks/blob/f8fd73e1482e838dfa8c17781226407ba49b7fde/src/main/kotlin/staks/base.kt#L166-L167 registers new one in parent.

Both of these methods are available to extensions of CompountHandler interface.

Be advised to read docs of all Hadler methods.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory