All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.parzivalExe.guiApi.antlr.Visitor.kt Maven / Gradle / Ivy

Go to download

With GuiAPI you can create Guis for your Bukkit/Spigot-Plugin in seconds while at the same time saving many lines of code

The newest version!
package io.github.parzivalExe.guiApi.antlr

import io.github.parzivalExe.guiApi.Gui
import io.github.parzivalExe.guiApi.antlr.elements.*
import io.github.parzivalExe.guiApi.antlr.source.XMLParser
import io.github.parzivalExe.guiApi.antlr.source.XMLParserBaseVisitor

class Visitor(private val documentContext: XMLParser.DocumentContext) : XMLParserBaseVisitor() {


    fun buildGui(): Gui {
        val guiElement = (visitElement(documentContext.element()) as GuiElement)

        val libraryElement = (guiElement.content?.elements?.firstOrNull { element -> element is LibraryElement } ?: LibraryElement()) as LibraryElement
        val library = libraryElement.createLibrary()

        return guiElement.createGui(library)
    }


    override fun visitContent(context: XMLParser.ContentContext?): IXMLRule {
        if(context == null)
            throw IllegalArgumentException("context can't be null")

        val content = Content()
        val elements = arrayListOf()

        for(contextElement in context.element()) {
            elements.add(visitElement(contextElement) as Element)
        }
        for(charData in context.chardata()) {
            val contextString = charData.text
            if(contextString != "\n")
                content.contentValue = contextString
        }

        content.elements = elements
        return content
    }

    override fun visitElement(contextElement: XMLParser.ElementContext?): IXMLRule {
        if(contextElement == null)
            throw IllegalArgumentException("element can't be null")

        val element = Element.getElementFromName(contextElement.name.text)

        if(contextElement.content() != null)
            element.content = visitContent(contextElement.content()) as Content

        val attributes = arrayListOf()
        for(attribute in contextElement.attribute()) {
            attributes.add(visitAttribute(attribute) as Attribute)
        }
        element.attributes = attributes

        return element
    }

    override fun visitAttribute(contextAttribute: XMLParser.AttributeContext?): IXMLRule {
        if(contextAttribute == null)
            throw  IllegalArgumentException("attribute can't be null")
        //Substring cutting up first and last char to cut up the " that start and end this value
        val value = contextAttribute.STRING().text.substring(1, contextAttribute.STRING().text.length - 1)
        return Attribute(contextAttribute.Name().text, value)
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy