📄 valuechild.java
字号:
/*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.*;import org.jibx.binding.classes.*;import org.jibx.runtime.JiBXException;/** * Attribute or simple content value definition from binding. This organizes * information for anything that can be converted to and from a simple * <code>String</code>. Content values include both elements with only character * data content and text, as character data content or CDATA sections. * * @author Dennis M. Sosnoski * @version 1.0 */public class ValueChild implements IComponent{ // // Ident type enumeration. /*package*/ static final int DIRECT_IDENT = 0; /*package*/ static final int AUTO_IDENT = 1; /*package*/ static final int DEF_IDENT = 2; /*package*/ static final int REF_IDENT = 3; // // Value style enumeration. /*package*/ static final int ATTRIBUTE_STYLE = 0; /*package*/ static final int ELEMENT_STYLE = 1; /*package*/ static final int TEXT_STYLE = 2; /*package*/ static final int CDATA_STYLE = 3; // // Constants for unmarshalling. /** Prefix used for backfill classes. */ private static final String BACKFILL_SUFFIX = "_backfill_"; private static final String[] BACKFILL_INTERFACES = { "org.jibx.runtime.impl.BackFillReference" }; private static final String BACKFILL_METHODNAME = "backfill"; private static final Type[] BACKFILL_METHODARGS = { Type.OBJECT }; private static final String BOUNDREF_NAME = "m_obj"; private static final String CHECK_ELEMENT_NAME = "org.jibx.runtime.impl.UnmarshallingContext.isAt"; private static final String CHECK_ATTRIBUTE_NAME = "org.jibx.runtime.impl.UnmarshallingContext.hasAttribute"; private static final String CHECK_SIGNATURE = "(Ljava/lang/String;Ljava/lang/String;)Z"; private static final String UNMARSHAL_DEFREF_ATTR_NAME = "org.jibx.runtime.impl.UnmarshallingContext.attributeExistingIDREF"; private static final String UNMARSHAL_DEFREF_ELEM_NAME = "org.jibx.runtime.impl.UnmarshallingContext.parseElementExistingIDREF"; private static final String UNMARSHAL_FWDREF_ATTR_NAME = "org.jibx.runtime.impl.UnmarshallingContext.attributeForwardIDREF"; private static final String UNMARSHAL_FWDREF_ELEM_NAME = "org.jibx.runtime.impl.UnmarshallingContext.parseElementForwardIDREF"; private static final String UNMARSHAL_DEFREF_SIGNATURE = "(Ljava/lang/String;Ljava/lang/String;I)Ljava/lang/Object;"; private static final String REGISTER_BACKFILL_NAME = "org.jibx.runtime.impl.UnmarshallingContext.registerBackFill"; private static final String REGISTER_BACKFILL_SIGNATURE = "(ILorg/jibx/runtime/impl/BackFillReference;)V"; private static final String DEFINE_ID_NAME = "org.jibx.runtime.impl.UnmarshallingContext.defineID"; private static final String DEFINE_ID_SIGNATURE = "(Ljava/lang/String;ILjava/lang/Object;)V"; private static final String UNMARSHAL_TEXT_NAME = "org.jibx.runtime.impl.UnmarshallingContext.parseContentText"; private static final String UNMARSHAL_TEXT_SIGNATURE = "()Ljava/lang/String;"; private static final String UNMARSHAL_ELEMENT_TEXT_NAME = "org.jibx.runtime.impl.UnmarshallingContext.parseElementText"; private static final String UNMARSHAL_ELEMENT_TEXT_SIGNATURE = "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"; private static final String UNMARSHAL_PARSE_IF_START_NAME = "org.jibx.runtime.impl.UnmarshallingContext.parseIfStartTag"; private static final String UNMARSHAL_PARSE_IF_START_SIGNATURE = "(Ljava/lang/String;Ljava/lang/String;)Z"; private static final String UNMARSHAL_PARSE_TO_START_NAME = "org.jibx.runtime.impl.UnmarshallingContext.parseToStartTag"; private static final String UNMARSHAL_PARSE_TO_START_SIGNATURE = "(Ljava/lang/String;Ljava/lang/String;)V"; private static final String UNMARSHAL_PARSE_PAST_END_NAME = "org.jibx.runtime.impl.UnmarshallingContext.parsePastEndTag"; private static final String UNMARSHAL_PARSE_PAST_END_SIGNATURE = "(Ljava/lang/String;Ljava/lang/String;)V"; private static final String MARSHAL_TEXT_NAME = "org.jibx.runtime.impl.MarshallingContext.writeContent"; private static final String MARSHAL_CDATA_NAME = "org.jibx.runtime.impl.MarshallingContext.writeCData"; private static final String MARSHAL_TEXT_SIGNATURE = "(Ljava/lang/String;)Lorg/jibx/runtime/impl/MarshallingContext;"; private static final String UNMARSHALLING_THROWEXCEPTION_METHOD = "org.jibx.runtime.impl.UnmarshallingContext.throwException"; private static final String UNMARSHALLING_THROWEXCEPTION_SIGNATURE = "(Ljava/lang/String;)V"; protected static final String MARSHAL_ATTRIBUTE = "org.jibx.runtime.impl.MarshallingContext.attribute"; protected static final String MARSHAL_ELEMENT = "org.jibx.runtime.impl.MarshallingContext.element"; protected static final String MARSHAL_SIGNATURE = "(ILjava/lang/String;Ljava/lang/String;)" + "Lorg/jibx/runtime/impl/MarshallingContext;"; protected static final String MARSHAL_STARTTAG_ATTRIBUTES = "org.jibx.runtime.impl.MarshallingContext.startTagAttributes"; protected static final String MARSHAL_STARTTAG_SIGNATURE = "(ILjava/lang/String;)Lorg/jibx/runtime/impl/MarshallingContext;"; protected static final String MARSHAL_CLOSESTART_EMPTY = "org.jibx.runtime.impl.MarshallingContext.closeStartEmpty"; protected static final String MARSHAL_CLOSESTART_EMPTY_SIGNATURE = "()Lorg/jibx/runtime/impl/MarshallingContext;"; protected static final String UNMARSHAL_ATTRIBUTE_BOOLEAN_NAME = "org.jibx.runtime.impl.UnmarshallingContext.attributeBoolean"; protected static final String UNMARSHAL_ATTRIBUTE_BOOLEAN_SIGNATURE = "(Ljava/lang/String;Ljava/lang/String;Z)Z"; // // Actual instance data /** Containing binding definition structure. */ private final IContainer m_container; /** Containing object context. */ private final IContextObj m_objContext; /** Value style code. */ private final int m_valueStyle; /** Constant value. */ private final String m_constantValue; /** Ident type code. */ private final int m_identType; /** Attribute or element name information. */ private final NameDefinition m_name; /** Fully qualified name of type. */ private final String m_type; /** Nillable element flag. */ private final boolean m_isNillable; /** Linked property information. */ private final PropertyDefinition m_property; /** Conversion handling for value. */ private final StringConversion m_conversion; /** Mapping definition for object class supplying identifier. */ private IMapping m_idRefMap; /** * Constructor. Saves the context information for later use. * * @param contain containing binding definition structure * @param objc containing object context * @param name element or attribute name information (may be * <code>null</code>) * @param prop property reference information * @param conv string conversion handler * @param style value style code * @param ident identifier type code * @param constant value for constant * @param nillable nillable element flag */ public ValueChild(IContainer contain, IContextObj objc, NameDefinition name, PropertyDefinition prop, StringConversion conv, int style, int ident, String constant, boolean nillable) { m_container = contain; m_objContext = objc; m_name = name; m_property = prop; m_type = prop.getTypeName(); m_conversion = conv; m_valueStyle = style; m_identType = ident; m_constantValue = constant; m_isNillable = nillable; } /** * Create backfill handler class if it does not already exist. This either * looks up the existing backfill handler class or creates a new one * specifically for this value. * * @return backfill handler class for value * @throws JiBXException if error in configuration */ private ClassFile createBackfillClass() throws JiBXException { // create the new class BoundClass bc = m_objContext.getBoundClass(); BindingDefinition def = m_container.getBindingRoot(); String name = bc.getClassFile().deriveClassName(def.getPrefix(), BACKFILL_SUFFIX + m_property.getName()); ClassFile base = ClassCache.getClassFile("java.lang.Object"); ClassFile cf = new ClassFile(name, bc.getClassFile().getRoot(), base, Constants.ACC_PUBLIC, BACKFILL_INTERFACES); // add member variable for bound class reference String type = bc.getClassFile().getName(); ClassItem ref = cf.addPrivateField(type, BOUNDREF_NAME); // add the constructor taking bound class reference Type[] args = new Type[] { ClassItem.typeFromName(type) }; MethodBuilder mb = new ExceptionMethodBuilder("<init>", Type.VOID, args, cf, (short)0); // call the superclass constructor mb.appendLoadLocal(0); mb.appendCallInit("java.lang.Object", "()V"); // store bound class reference to member variable mb.appendLoadLocal(0); mb.appendLoadLocal(1); mb.appendPutField(ref); mb.appendReturn(); mb.codeComplete(false); mb.addMethod(); // add actual backfill interface implementation method mb = new ExceptionMethodBuilder(BACKFILL_METHODNAME, Type.VOID, BACKFILL_METHODARGS, cf, Constants.ACC_PUBLIC); mb.appendLoadLocal(0); mb.appendGetField(ref); mb.appendLoadLocal(1); mb.appendCreateCast(m_property.getSetValueType()); m_property.genStore(mb); mb.appendReturn(); mb.codeComplete(false); mb.addMethod(); // return unique instance of class return MungedClass.getUniqueSupportClass(cf); } /** * Generate unmarshalling code for object identifier reference. The code * generated by this method assumes the unmarshalling context and name have * already been loaded to the stack, and these are consumed by the code. * * @param mb method builder * @throws JiBXException if error in configuration */ private void genParseIdRef(ContextMethodBuilder mb) throws JiBXException { // first part of generated instruction sequence is to check if optional // value is present BranchWrapper ifmiss = null; if (m_property.isOptional()) { // use existing context reference and name information to check for // attribute or element present String name = m_valueStyle == ValueChild.ATTRIBUTE_STYLE ?
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -