代码之家  ›  专栏  ›  技术社区  ›  Gauvain Klug

将PropertyBusinessObject序列化为XML

  •  1
  • Gauvain Klug  · 技术社区  · 6 年前

    我正在尝试升级我的应用程序中可以在动态中显示数据的部分的代码质量。 Form 通过转换为对象的XML字符串配置的。到目前为止,我只使用自定义方法进行解析和序列化,几乎自己编写了每一行XML。这显然是一个错误的方法,现在我非常熟悉代号1,我完全理解了它。

    所以我重写了我的对象来使用 PropertyBusinessObject 接口和以下是生成的类:

    public class CForm implements PropertyBusinessObject 
    {
    
        Property<String, CForm> type = new Property<>("type");
        Property<String, CForm> label = new Property<>("label");
        IntProperty<CForm> currentStep = new IntProperty<>("currentstep");
        IntProperty<CForm> maxsteps = new IntProperty<>("maxsteps");
    
        ListProperty<CFormField, CForm> fields = new ListProperty<>("fields", CFormField.class);
        ListProperty<CFormStep, CForm> steps = new ListProperty<>("steps", CFormStep.class);
    
        public CForm() 
        {
    
        }
    
        PropertyIndex index;
        @Override
        public PropertyIndex getPropertyIndex() 
        {
            if(index == null)
                index = new PropertyIndex(this, "CForm", 
                    new PropertyBase[] {type, label, currentStep, maxsteps, fields, steps});
            return index;
        }
    }
    

    这个类是主要的类,描述流程的一般结构。类型和标签在内部用于标识所使用的模型以及在客户处要做的工作。字段是将在每个步骤中显示的动态字段,这些字段是全局字段,如备注等。这些步骤是不同的动态步骤 形式 将显示给用户的。 maxSteps currentStep 用于知道用户在进程中的位置。

    public class CFormStep implements PropertyBusinessObject 
    {
        Property<String, CFormStep> name = new Property<String, CFormStep>("name");
        Property<String, CFormStep> label = new Property<String, CFormStep>("label");
        IntProperty<CFormStep> value = new IntProperty<CFormStep>("value");
    
        ListProperty<CFormField, CFormStep> fields = new ListProperty<>("fields", CFormField.class);
    
        public CFormStep() {}
    
        PropertyIndex index;
        @Override
        public PropertyIndex getPropertyIndex() 
        {
            if(index == null)
                index = new PropertyIndex(this, "CFormStep", 
                    new PropertyBase[] { label, name, value, fields});
    
            return index;
        }
    }
    

    每个步骤都用一个唯一的名称、一个用作动态窗体标题的标签和一个值来描述,以便在 CForm . 就像它 窗体 父级,它有一组字段。

    public class CFormField implements PropertyBusinessObject 
    {
        Property<String, CFormField> label = new Property<String, CFormField>("label");
        Property<String, CFormField> name = new Property<String, CFormField>("name");
        Property<String, CFormField> type = new Property<String, CFormField>("type");
        Property<String, CFormField> value = new Property<String, CFormField>("value");
        Property<String, CFormField> parent = new Property<String, CFormField>("parent");
    
        public CFormField() {}
    
        PropertyIndex index;
        @Override
        public PropertyIndex getPropertyIndex()
        {
            if(index == null)
                index = new PropertyIndex(this, "CFormField", 
                    new Property[] { label, name, type, value, parent });
            return index;
        }
    }
    

    这些字段由一个标签、一个唯一的名称、一个用于定义将使用哪个组件来呈现它的类型、由格式化为字符串的用户选择的值以及最后它的可选父名称组成。

    旧的实现很有意思,但是很落后,很难发展,这就是为什么我开始写这个新的实现。

    但我有一个问题:我已经重写了 PropertyIndex.fromXML(Element e) 因为它在打电话时创建了NPE

     public void fromXml(Element e) {
            Hashtable atts = e.getAttributes();
            for(Object a : atts.keySet()) { <--- NPE there if no attributes
    

    也不支持 ListProperties .

    读我的 窗体 从XML可以很好地工作,但是当我试图将它序列化回XML时,我得到了一个 <CForm><CForm/> 不完全理解 PropertyXMLElement 作品。。。我已经调试了程序和 属性业务对象 一点也不空。知道如何用这些值填充XML吗?

    下面是使用的XML示例:

    <CForm>
        <type>delivery-install</type>
        <label>Intervention Test</label>
        <currentstep/>
        <maxsteps>3</maxsteps>
        <fields>
            <CFormField>
                <label>Remarques</label>
                <name>globalTEXTAREA6</name>
                <type>TEXTAREA</type>
                <value/>
            </CFormField>
        </fields>
        <steps>
            <CFormStep>
                <name>step1</name>
                <label>Arrivée sur place</label>
                <value>1</value>
                <fields>
                    <CFormField>
                        <label>Heure d'arrivée</label>
                        <name>step1TIME3</name>
                        <type>TIME</type>
                        <value/>
                    </CFormField>
                </fields>
            </CFormStep>
            <CFormStep>
                <name>step2</name>
                <label>Sur place</label>
                <value>2</value>
                <fields>
                    <CFormField>
                        <label>Produits livrés / utilisés</label>
                        <name>step2PRODUCTTABLE7</name>
                        <type>PRODUCTTABLE</type>
                        <value/>
                    </CFormField>
                </fields>
            </CFormStep>
            <CFormStep>
                <name>step3</name>
                <label>Départ</label>
                <value>3</value>
                <fields>
                    <CFormField>
                        <label>Heure de départ</label>
                        <name>step3TIME4</name>
                        <type>TIME</type>
                        <value/>
                    </CFormField>
                    <CFormField>
                        <label>Signature client</label>
                        <name>step3SIGN5</name>
                        <type>SIGN</type>
                        <value/>
                    </CFormField>
                </fields>
            </CFormStep>
        </steps>
    </CForm>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Shai Almog    6 年前

    属性业务对象中的XML支持仍然在进行中,这就是为什么我没有在博客中讨论它的原因。我修复了NPE问题,并添加了对列表属性的支持,以修复提出的两个问题。注意,我这样做只是为了阅读,所以在编写这些对象时可能会出现问题,因为我们没有时间对这个特性进行QA。

    同时请注意:

    Property<String, CForm> type = new Property<>("type");
    

    应该是:

    Property<String, CForm> type = new Property<>("type", CForm.class);
    

    所以代码可以正确地检测递归。对于所有非字符串泛型属性,这应该是正确的。它是为 IntProperty 以及类似的类型。