package com.wayne.www.waynelib.util

import com.thoughtworks.xstream.XStream
import com.thoughtworks.xstream.io.xml.DomDriver
import com.wayne.www.waynelib.entity.*

class GenericDisplayCmdParser {
    companion object {
        private var xStream: XStream? = null

        fun getXStream(): XStream {
            if (xStream == null) {
                xStream = XStream(DomDriver())
                xStream!!.alias("GenericDisplayCommand", GenericDisplayCommand::class.java)
                xStream!!.alias("LinearLayout", LinearLayout::class.java)
                xStream!!.alias("VideoView", VideoView::class.java)
                xStream!!.alias("TextView", TextView::class.java)
                xStream!!.alias("AllowAction", AllowAction::class.java)
                xStream!!.alias("AllowActions", AllowActions::class.java)
                xStream!!.processAnnotations(AllowAction::class.java)
                xStream!!.addImplicitArray(LinearLayout::class.java, "TextViewList", TextView::class.java)
                xStream!!.addImplicitArray(LinearLayout::class.java, "LinearLayoutList", LinearLayout::class.java)
                xStream!!.addImplicitCollection(AllowActions::class.java, "list")
                //xStream!!.addImplicitArray(LinearLayout::class.java, "AllowActions", AllowAction::class.java)
                //x!!.useAttributeFor(Display::class.java, "ScreenType")
            }
            return xStream as XStream
        }

        fun fromXml(xml: String): GenericDisplayCommand {
            var layout = getXStream().fromXML(xml)
            return layout as GenericDisplayCommand
        }

        fun getDemo(xml: String): Any {
            var xStream = XStream(DomDriver())
            xStream!!.alias("AllowAction", AllowAction::class.java)
            xStream.processAnnotations(AllowAction::class.java)
            var result = xStream.fromXML(xml)
            return result
        }

        fun toXml(genericDisplayCommand: GenericDisplayCommand): String {
            var xml = getXStream().toXML(genericDisplayCommand)
            return xml
        }
    }
}