未设置对象的值

时间:2016-09-11 15:33:04

标签: xml groovy

我有一个班级WorldGodsVoice

class WorldGodsVoice extends Thread implements IThreadable {
    def innerId
    boolean isSequenced
    long wait
    def messages = []
    volatile isEternal = true
    def seq_id = 0
    ....
}

WorldGodsVoice包装另一个类

class GodsVoiceSubsystem extends Subsystem {

    def component = new WorldGodsVoice()

    def activate() {
        component.start()
    }

    def disable() {
        component.stopThread()
    }

    String toString() {
        component
    }
}

将该包装类保留在某个全局对象LifeFramework

@Component
class LifeFramework extends ComplexFramework {
    static final service_config = "config/service-config.xml";
    def file
    {
        file = Gdx.files.internal(service_config).file
        loaders <<      [ "godsVoice" : new WorldGodsVoiceConfigLoader() ]
        subsystems <<   [ "godsVoice" :  new GodsVoiceSubsystem() ]
    }

    def initSubsystems() {
        loaders.each { k, v -> v.load(file, subsystems[k]) }

        subsystems.each { k, v ->
            v.activate()
        }
    }

    def destroySubsystems() {
        subsystems.each { k, v ->
            v.disable()
        }
    }

    def accessSubsystem(id) {
        subsystems[id]
    }
}

ComplexFramework只是一个包含方法和loaders, subsystems字段的抽象类。

WorldGodsVoice的配置我保留在xml文件

<?xml version="1.0"?>
<services>
    <service id="godsVoice">
        <innerId>GOD_S_VOICE</innerId>
        <wait>1000</wait>
        <messages sequenced="false">
            The world is living...Without a hero!
            The world is filled with life!
            The world is eternal!
         </messages>
    </service>
</services>

我像这样处理xml

abstract class XmlConfigSubsystemLoader {

    def load(file, subsystem) {
        def xml = new ServiceConfigLoader().load(file) // new XmlSlurper().parse(file)
        processXml(xml.'*'.find { node -> node.@id == subsystemId() }, subsystem)
    }

    abstract subsystemId()
    abstract processXml(xml, subsystem)
}

抽象方法看起来像这样

class WorldGodsVoiceConfigLoader extends XmlConfigSubsystemLoader {

    def subsystemId() { "godsVoice" }

    def processXml(xml, subsystem) {
        subsystem.component.innerId = xml.innerId.text()
        subsystem.component.wait = xml.wait.text() as Long
        subsystem.component.messages = xml.messages.text().trim().split("!").collect { it.trim() } as String[]
        subsystem.component.isSequenced = xml.messages.@sequenced
        println "${ xml.messages.@sequenced }" // false
        println "${ subsystem.component.isSequenced }" // true
    }
}

问题是尽管设置了所有其他字段 - 但它不想为对象设置isSequenced字段。它始终返回isSequenced默认值或设置在对象内(如boolean isSequenced = true)值。

有什么问题?

1 个答案:

答案 0 :(得分:1)

Groovy真理说任何非空的非空字符串都是真的

你需要

subsystem.component.isSequenced = Boolean.valueOf(xml.messages.@sequenced)