⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 directobject.java

📁 对xml很好的java处理引擎,编译中绑定xml
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*Copyright (c) 2003-2005, Dennis M. SosnoskiAll rights reserved.Redistribution and use in source and binary forms, with or without modification,are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this   list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice,   this list of conditions and the following disclaimer in the documentation   and/or other materials provided with the distribution. * Neither the name of JiBX nor the names of its contributors may be used   to endorse or promote products derived from this software without specific   prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" ANDANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIEDWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FORANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ONANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/package org.jibx.binding.def;import org.apache.bcel.Constants;import org.apache.bcel.generic.Type;import org.jibx.binding.classes.*;import org.jibx.runtime.JiBXException;/** * Linkage to object with supplied marshaller and unmarshaller. This provides * methods used to generate code for calling the supplied classes. * * @author Dennis M. Sosnoski * @version 1.0 */public class DirectObject implements IComponent{    //    // Constants and such related to code generation.    private static final String GETUNMARSHALLER_METHOD =        "org.jibx.runtime.IUnmarshallingContext.getUnmarshaller";    private static final String GETUNMARSHALLER_SIGNATURE =        "(I)Lorg/jibx/runtime/IUnmarshaller;";    private static final String GETMARSHALLER_METHOD =        "org.jibx.runtime.IMarshallingContext.getMarshaller";    private static final String GETMARSHALLER_SIGNATURE =        "(ILjava/lang/String;)Lorg/jibx/runtime/IMarshaller;";    private static final String MARSHALLER_MARSHAL_METHOD =        "org.jibx.runtime.IMarshaller.marshal";    private static final String MARSHALLER_MARSHAL_SIGNATURE =        "(Ljava/lang/Object;Lorg/jibx/runtime/IMarshallingContext;)V";    private static final String UNMARSHALLER_TESTPRESENT_METHOD =        "org.jibx.runtime.IUnmarshaller.isPresent";    private static final String UNMARSHALLER_TESTPRESENT_SIGNATURE =        "(Lorg/jibx/runtime/IUnmarshallingContext;)Z";    private static final String UNMARSHALLER_UNMARSHAL_METHOD =        "org.jibx.runtime.IUnmarshaller.unmarshal";    private static final String UNMARSHALLER_UNMARSHAL_SIGNATURE =        "(Ljava/lang/Object;Lorg/jibx/runtime/IUnmarshallingContext;)" +        "Ljava/lang/Object;";    private static final String ABSTRACTMARSHALLER_INTERFACE =        "org.jibx.runtime.IAbstractMarshaller";    private static final String ABSTRACTMARSHAL_METHOD =        "org.jibx.runtime.IAbstractMarshaller.baseMarshal";    private static final String ABSTRACTMARSHAL_SIGNATURE =        MARSHALLER_MARSHAL_SIGNATURE;    private static final String ALIASABLE_INTERFACETYPE =        "Lorg/jibx/runtime/IAliasable;";    private static final String ANY_INIT_SIG = "()V";    private static final String ANY_INITCLASS_SIG = "(Ljava/lang/String;)V";    private static final String MARSHALUNMARSHAL_INIT_SIG =        "(Ljava/lang/String;ILjava/lang/String;)V";    private static final String MARSHALONLY_INIT_SIG = "(ILjava/lang/String;)V";    private static final String UNMARSHALONLY_INIT_SIG =        "(Ljava/lang/String;Ljava/lang/String;)V";    private static final String MARSHALUNMARSHAL_INITCLASS_SIG =        "(Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;)V";    private static final String MARSHALONLY_INITCLASS_SIG =        "(ILjava/lang/String;Ljava/lang/String;)V";    private static final String UNMARSHALONLY_INITCLASS_SIG =        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V";    //    // Actual instance data.    /** Containing binding definition structure. */    private final IContainer m_parent;        /** Definition context for resolving names. */    private final DefinitionContext m_defContext;    /** Abstract mapping flag. If this is set the marshalling code will call the      special interface method used to verify the type of a passed object and      marshal it with the proper handling. */    private final boolean m_isAbstract;        /** Element name information (<code>null</code> if no bound element). */    private final NameDefinition m_name;        /** Flag for marshaller/unmarshaller slot defined. This is done during code     generation, rather than during linking, so that all mapped bindings can be     defined with lower index numbers than for marshallers/unmarshallers used     only in a specific context. */    private boolean m_isSlotSet;        /** Marshaller/unmarshaller slot number. */    private int m_mumSlot;        /** Class handled by this binding. */    private final ClassFile m_targetClass;    /** Marshaller base class. */    private final ClassFile m_marshallerBase;    /** Unmarshaller base class. */    private final ClassFile m_unmarshallerBase;    /** Marshaller class (lazy create on first use if name supplied). */    private ClassFile m_marshaller;    /** Unmarshaller class (lazy create on first use if name supplied). */    private ClassFile m_unmarshaller;    /**     * Constructor.     *     * @param parent containing binding definition structure     * @param target class handled by this binding     * @param abs abstract mapping flag     * @param mcf marshaller class information (<code>null</code> if input only     * binding)     * @param ucf unmarshaller class information (<code>null</code> if output     * only binding)     * @param slot marshaller/unmarshaller slot number (<code>-1</code> if to be     * defined later)     * @param name element name information (<code>null</code> if no element     * name)     * @throws JiBXException if configuration error     */    public DirectObject(IContainer parent, DefinitionContext defc,        ClassFile target, boolean abs, ClassFile mcf, ClassFile ucf, int slot,        NameDefinition name) throws JiBXException {                // initialize the basic information        m_parent = parent;        m_defContext = (defc == null) ? m_parent.getDefinitionContext() : defc;        m_isAbstract = abs;        m_targetClass = target;        m_marshallerBase = mcf;        m_unmarshallerBase = ucf;        m_name = name;        m_mumSlot = slot;        m_isSlotSet = slot >= 0;                // check for marshaller and/or unmarshaller configuration valid        if (name == null) {                        // no name, make sure there's an appropriate constructor            if (mcf != null) {                if (mcf.getInitializerMethod(ANY_INIT_SIG) != null) {                    m_marshaller = mcf;                } else if (mcf.getInitializerMethod                    (ANY_INITCLASS_SIG) == null) {                    throw new JiBXException("Marshaller class " +                        mcf.getName() + " requires name to be set");                }            }            if (ucf != null) {                if (ucf.getInitializerMethod(ANY_INIT_SIG) != null) {                    m_unmarshaller = ucf;                } else if (ucf.getInitializerMethod                    (ANY_INITCLASS_SIG) == null) {                    throw new JiBXException("Unmarshaller class " +                        ucf.getName() + " requires name to be set");                }            }        }         if (name != null) {                        // name supplied, make sure both support it            if (mcf != null && !mcf.isImplements(ALIASABLE_INTERFACETYPE)) {                throw new JiBXException("Marshaller class " +                    mcf.getName() + " does not allow name to be set");            }            if (ucf != null && !ucf.isImplements(ALIASABLE_INTERFACETYPE)) {                throw new JiBXException("Unmarshaller class " +                    ucf.getName() + " does not allow name to be set");            }        }    }    /**     * Get marshaller/unmarshaller index. This is the index into the tables of     * marshaller and unmarshaller instances maintained by the respective     * contexts. On the first time call this gets the index number from the     * binding definition, then the index number is reused on subsequent calls.     *     * @return slot number for binding     * @throws JiBXException on configuration error     */    private int getSlot() throws JiBXException {        if (!m_isSlotSet) {                        // first set slot in case called recursively            BindingDefinition bdef = m_parent.getBindingRoot();            m_mumSlot = bdef.getMarshallerUnmarshallerIndex                (m_targetClass.getName());            m_isSlotSet = true;                        // now generate the classes (if needed) and set the names            String mclas = null;            String uclas = null;            if (bdef.isOutput()) {                if (m_marshaller == null) {                    createSubclass(true);                }                mclas = m_marshaller.getName();            }            if (bdef.isInput()) {                if (m_unmarshaller == null) {                    createSubclass(false);                }                uclas = m_unmarshaller.getName();            }            bdef.setMarshallerUnmarshallerClasses(m_mumSlot, mclas, uclas);        }        return m_mumSlot;    }    /**     * Load marshaller/unmarshaller index. This is assigned by the binding     * definition and used thereafter.     *     * @param mb method builder     */    private void genLoadSlot(ContextMethodBuilder mb) throws JiBXException {        mb.appendLoadConstant(getSlot());    }    /**     * Create aliased subclass for marshaller or unmarshaller with element name     * defined by binding. If the same aliasable superclass is defined for use     * as both a marshaller and an unmarshaller a single subclass is generated     * to handle both uses.     *     * @param out <code>true</code> if alias needed for marshalling,     * <code>false</code> if for unmarshalling     * @throws JiBXException on configuration error     */    private void createSubclass(boolean out) throws JiBXException {                    // find initializer call to be used        ClassItem init = null;        boolean dual = false;        boolean classed = true;        boolean named = false;        ClassFile base = out ? m_marshallerBase : m_unmarshallerBase;                // check for name supplied        if (m_name == null) {            init = base.getInitializerMethod(ANY_INITCLASS_SIG);            classed = init != null;        }        if (init == null) {            named = true;            if (m_unmarshallerBase == m_marshallerBase) {                            // single class, look first for signature with class name                init = base.getInitializerMethod                    (MARSHALUNMARSHAL_INITCLASS_SIG);                if (init == null) {                    classed = false;                    init = base.getInitializerMethod(MARSHALUNMARSHAL_INIT_SIG);                }                dual = true;                        } else {                            // using only one function, first check one way with class name                String sig = out ?                    MARSHALONLY_INITCLASS_SIG : UNMARSHALONLY_INITCLASS_SIG;                init = base.getInitializerMethod(sig);                if (init == null) {                                    // now check dual function with class name                    sig = MARSHALUNMARSHAL_INITCLASS_SIG;                    init = base.getInitializerMethod(sig);                    dual = true;                    if (init == null) {                                            // now try alternatives without class name included                        classed = false;                        sig = out ? MARSHALONLY_INIT_SIG :                            UNMARSHALONLY_INIT_SIG;                        init = base.getInitializerMethod(sig);                        dual = false;                        if (init == null) {                            sig = MARSHALUNMARSHAL_INIT_SIG;

⌨️ 快捷键说明

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