valuechild.java

来自「对xml很好的java处理引擎,编译中绑定xml」· Java 代码 · 共 884 行 · 第 1/3 页

JAVA
884
字号
     * Generate marshalling code. This internal method generates the     * necessary code for handling the marshalling operation.  The code     * generated by this method restores the stack to the original state     * when done.     *     * @param mb method builder     * @throws JiBXException if error in configuration     */    private void genMarshal(ContextMethodBuilder mb) throws JiBXException {        if (m_constantValue == null) {                        // first part of generated instruction sequence is to generate a            //  check for an optional property present, then load the context            //  and the name information (if present) for later use, then            //  finally load the actual property value            BranchWrapper ifmiss = null;            if (m_property.hasTest()) {                mb.loadObject();                ifmiss = m_property.genTest(mb);            } else if (m_isNillable) {                                // check for null object                BranchWrapper ifhit;                if (m_property.isImplicit()) {                    mb.appendDUP();                    ifhit = mb.appendIFNONNULL(this);                    mb.appendPOP();                } else {                    mb.loadObject();                    m_property.genLoad(mb);                    ifhit = mb.appendIFNONNULL(this);                }                                // generate empty element with xsi:nil="true"                mb.loadContext();                m_name.genPushIndexPair(mb);                mb.appendCallVirtual(MARSHAL_STARTTAG_ATTRIBUTES,                    MARSHAL_STARTTAG_SIGNATURE);                mb.appendLoadConstant(2);                mb.appendLoadConstant("nil");                mb.appendLoadConstant("true");                mb.appendCallVirtual(MARSHAL_ATTRIBUTE, MARSHAL_SIGNATURE);                mb.appendCallVirtual(MARSHAL_CLOSESTART_EMPTY,                    MARSHAL_CLOSESTART_EMPTY_SIGNATURE);                mb.appendPOP();                ifmiss = mb.appendUnconditionalBranch(this);                mb.targetNext(ifhit);                            }            String type = m_property.getTypeName();            if (m_name != null) {                                // handle implicit property by first saving value to local, then                //  reloading after the name information is on the stack                Type tobj = ClassItem.typeFromName(type);                if (m_property.isImplicit()) {                    mb.defineSlot(this, tobj);                }                m_name.genPushIndexPair(mb);                if (m_property.isImplicit()) {                    mb.appendLoadLocal(mb.getSlot(this));                    mb.freeSlot(this);                }            }            if (!m_property.isImplicit()) {                mb.loadObject();                m_property.genLoad(mb);            }                        // check for object identity definition (accessed through property)            StringConversion convert = m_conversion;            if (m_identType == REF_IDENT) {                m_idRefMap.getImplComponent().genLoadId(mb);                convert = BindingDefinition.s_stringConversion;                type = "java.lang.String";            }                        // convert to expected type if object            if (!ClassItem.isPrimitive(type)) {                mb.appendCreateCast(type);            }                            // convert and marshal value            boolean isatt = m_valueStyle == ValueChild.ATTRIBUTE_STYLE;            if (m_valueStyle == ValueChild.TEXT_STYLE ||                m_valueStyle == ValueChild.CDATA_STYLE) {                convert.genToText(type, mb);                String name = (m_valueStyle == ValueChild.TEXT_STYLE) ?                    MARSHAL_TEXT_NAME : MARSHAL_CDATA_NAME;                mb.appendCallVirtual(name, MARSHAL_TEXT_SIGNATURE);            } else if (m_property.isOptional()) {                convert.genWriteOptional(isatt, type, mb);            } else {                convert.genWriteRequired(isatt, type, mb);            }                        // finish by setting target for missing optional property test            mb.targetNext(ifmiss);                    } else {                        // just write constant value directly            if (m_name != null) {                m_name.genPushIndexPair(mb);            }            mb.appendLoadConstant(m_constantValue);            switch (m_valueStyle) {                case ATTRIBUTE_STYLE:                    mb.appendCallVirtual(MARSHAL_ATTRIBUTE, MARSHAL_SIGNATURE);                    break;                case ELEMENT_STYLE:                    mb.appendCallVirtual(MARSHAL_ELEMENT, MARSHAL_SIGNATURE);                    break;                case TEXT_STYLE:                    mb.appendCallVirtual(MARSHAL_TEXT_NAME,                        MARSHAL_TEXT_SIGNATURE);                    break;                case CDATA_STYLE:                    mb.appendCallVirtual(MARSHAL_CDATA_NAME,                        MARSHAL_TEXT_SIGNATURE);                    break;            }        }    }    /**     * Get property name. If the child has an associated property this returns     * the name of that property.     *      * @return name for child property     */        public String getPropertyName() {        if (m_property == null) {            return null;        } else {            return m_property.getName();        }    }        /**     * Check if implicit.     *      * @return <code>true</code> if implicit, <code>false</code> if not     */    public boolean isImplicit() {        return m_property.isThis();    }        /**     * Switch property from "this" to "implicit".     */    public void switchProperty() {        m_property.switchProperty();    }        //    // IComponent interface method definitions    public boolean isOptional() {        return m_property.isOptional();    }        public boolean hasAttribute() {        return m_valueStyle == ATTRIBUTE_STYLE;    }    public void genAttrPresentTest(ContextMethodBuilder mb)        throws JiBXException {                // make sure this is an appropriate call        if (m_valueStyle != ATTRIBUTE_STYLE || m_name == null) {            throw new JiBXException("Method call on invalid structure");        }                // generate load of the unmarshalling context and the name information,        //  then just call the attribute check method        mb.loadContext();        m_name.genPushUriPair(mb);        mb.appendCallVirtual(CHECK_ATTRIBUTE_NAME, CHECK_SIGNATURE);    }    public void genAttributeUnmarshal(ContextMethodBuilder mb)        throws JiBXException {        if (m_valueStyle == ATTRIBUTE_STYLE) {            genUnmarshal(mb);        }    }    public void genAttributeMarshal(ContextMethodBuilder mb)        throws JiBXException {        if (m_valueStyle == ATTRIBUTE_STYLE) {            genMarshal(mb);        }    }    public boolean hasContent() {        return m_valueStyle != ATTRIBUTE_STYLE;    }        public void genContentPresentTest(ContextMethodBuilder mb)        throws JiBXException {                // make sure this is an appropriate call        if (m_valueStyle != ELEMENT_STYLE) {            throw new JiBXException("Method call on invalid structure");        }                // generate load of the unmarshalling context and the name information,        //  then just call the attribute check method        mb.loadContext();        m_name.genPushUriPair(mb);        mb.appendCallVirtual(CHECK_ELEMENT_NAME, CHECK_SIGNATURE);    }    public void genContentUnmarshal(ContextMethodBuilder mb)        throws JiBXException {        if (m_valueStyle != ATTRIBUTE_STYLE) {            genUnmarshal(mb);        }    }    public void genContentMarshal(ContextMethodBuilder mb)        throws JiBXException {        if (m_valueStyle != ATTRIBUTE_STYLE) {            genMarshal(mb);        }    }        public void genNewInstance(ContextMethodBuilder mb) {        throw new IllegalStateException            ("Internal error - no instance creation");    }        public String getType() {        return m_type;    }    public boolean hasId() {        return m_identType == DEF_IDENT;    }        public void genLoadId(ContextMethodBuilder mub) throws JiBXException {        m_property.genLoad(mub);    }        public NameDefinition getWrapperName() {        return (m_valueStyle == ELEMENT_STYLE) ? m_name : null;    }    public void setLinkages() throws JiBXException {        if (m_identType == REF_IDENT) {            String type;            if (m_property == null) {                type = m_objContext.getBoundClass().getClassFile().getName();            } else {                type = m_property.getTypeName();            }            m_idRefMap = m_container.getDefinitionContext().                getClassMapping(type);            if (m_idRefMap == null) {                throw new JiBXException("No mapping defined for " +                    type + " used as IDREF target");            } else if (!m_idRefMap.getImplComponent().hasId()) {                throw new JiBXException("No ID value defined for " +                    type + " used as IDREF target");            }        }    }        // DEBUG    public void print(int depth) {        BindingDefinition.indent(depth);        if (m_valueStyle == ELEMENT_STYLE) {            System.out.print("element");        } else if (m_valueStyle == ATTRIBUTE_STYLE) {            System.out.print("attribute");        } else if (m_valueStyle == TEXT_STYLE) {            System.out.print("text");        } else if (m_valueStyle == CDATA_STYLE) {            System.out.print("cdata");        }        if (m_name != null) {            System.out.print(" " + m_name.toString());        }        if (m_property != null) {            System.out.print(" from " + m_property.toString());        }        System.out.println();    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?