📄 stringconversion.java
字号:
/*Copyright (c) 2003-2004, 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.classfile.Utility;import org.jibx.binding.classes.*;import org.jibx.runtime.JiBXException;import org.jibx.runtime.QName;/** * String conversion handling. Defines serialization handling for converting * to and from a <code>String</code> value. This uses an inheritance approach, * where each serialization definition is initialized based on the handling * set for the containing definition of the same (or parent class) type. * * @author Dennis M. Sosnoski * @version 1.0 */public abstract class StringConversion{ // // Constants for code generation. protected static final String UNMARSHAL_OPT_ATTRIBUTE = "org.jibx.runtime.impl.UnmarshallingContext.attributeText"; protected static final String UNMARSHAL_OPT_ELEMENT = "org.jibx.runtime.impl.UnmarshallingContext.parseElementText"; protected static final String UNMARSHAL_OPT_SIGNATURE = "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)" + "Ljava/lang/String;"; protected static final String UNMARSHAL_REQ_ATTRIBUTE = "org.jibx.runtime.impl.UnmarshallingContext.attributeText"; protected static final String UNMARSHAL_REQ_ELEMENT = "org.jibx.runtime.impl.UnmarshallingContext.parseElementText"; protected static final String UNMARSHAL_REQ_SIGNATURE = "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"; 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 COMPARE_OBJECTS_METHOD = "org.jibx.runtime.Utility.isEqual"; protected static final String COMPARE_OBJECTS_SIGNATURE = "(Ljava/lang/Object;Ljava/lang/Object;)Z"; protected static final String[] DESERIALIZER_SIGNATURES = { "(Ljava/lang/String;)", "(Ljava/lang/String;Lorg/jibx/runtime/IUnmarshallingContext;)" }; // values used for name in marshalling; must be 1 or 2 public static final int MARSHAL_NAME_VALUES = 2; // // Actual instance data /** Default value used for this type (wrapper for primitives, otherwise <code>String</code> or <code>null</code>). */ protected Object m_default; /** Serializer method information. */ protected ClassItem m_serializer; /** Deserializer method information. */ protected ClassItem m_deserializer; /** Fully qualified name of class handled by conversion. */ protected String m_typeName; /** Signature of class handled by conversion. */ protected String m_typeSignature; /** * Constructor. This internal form only initializes the type information. * * @param type fully qualified name of class handled by conversion */ private StringConversion(String type) { m_typeName = type; m_typeSignature = Utility.getSignature(type); } /** * Constructor. Initializes conversion handling based on the supplied * inherited handling. * * @param type fully qualified name of class handled by conversion * @param inherit conversion information inherited by this conversion */ protected StringConversion(String type, StringConversion inherit) { this(type); m_default = inherit.m_default; m_serializer = inherit.m_serializer; m_deserializer = inherit.m_deserializer; } /** * Constructor. Initializes conversion handling based on argument values. * This form is only used for constructing the default set of conversions. * Because of this, it throws an unchecked exception on error. * * @param dflt default value object (wrapped value for primitive types, * otherwise <code>String</code>) * @param ser fully qualified name of serialization method * @param deser fully qualified name of deserialization method * @param type fully qualified name of class handled by conversion */ /*package*/ StringConversion(Object dflt, String ser, String deser, String type) { this(type); m_default = dflt; try { if (ser != null) { setSerializer(ser); } if (deser != null) { setDeserializer(deser); } } catch (JiBXException ex) { throw new IllegalArgumentException(ex.getMessage()); } } /** * Get name of type handled by this conversion. * * @return fully qualified class name of type handled by conversion */ public String getTypeName() { return m_typeName; } /** * Generate code to convert <code>String</code> representation. The * code generated by this method assumes that the <code>String</code> * value has already been pushed on the stack. It consumes this and * leaves the converted value on the stack. * * @param mb method builder * @throws JiBXException if error in configuration */ public abstract void genFromText(ContextMethodBuilder mb) throws JiBXException; /** * Generate code to parse and convert optional attribute or element. This * abstract base class method must be implemented by every subclass. The * code generated by this method assumes that the unmarshalling context * and name information for the attribute or element have already * been pushed on the stack. It consumes these and leaves the converted * value (or converted default value, if the item itself is missing) on * the stack. * * @param attr item is an attribute (vs element) flag * @param mb method builder * @throws JiBXException if error in configuration */ public abstract void genParseOptional(boolean attr, ContextMethodBuilder mb) throws JiBXException; /** * Generate code to parse and convert required attribute or element. This * abstract base class method must be implemented by every subclass. The * code generated by this method assumes that the unmarshalling context and * name information for the attribute or element have already been pushed * on the stack. It consumes these and leaves the converted value on the * stack. * * @param attr item is an attribute (vs element) flag * @param mb method builder * @throws JiBXException if error in configuration */ public abstract void genParseRequired(boolean attr, ContextMethodBuilder mb) throws JiBXException; /** * Generate code to write <code>String</code> value to generated document. * The code generated by this method assumes that the marshalling context, * the name information, and the actual value to be converted have already * been pushed on the stack. It consumes these, leaving the marshalling * context on the stack. * * @param attr item is an attribute (vs element) flag * @param mb method builder */ public void genWriteText(boolean attr, ContextMethodBuilder mb) { // append code to call the appropriate generic marshalling context
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -