stringattributes.java
来自「对xml很好的java处理引擎,编译中绑定xml」· Java 代码 · 共 538 行 · 第 1/2 页
JAVA
538 行
* Get base format information. This method is only usable after a * call to {@link #validate}. * * @return base format element (or <code>null</code> if none) */ public FormatElement getBaseFormat() { return m_baseFormat; } /** * JiBX access method to set format label as qualified name. * * @param label format label text (<code>null</code> if none) * @param ictx unmarshalling context * @throws JiBXException on deserialization error */ private void setQualifiedFormat(String label, IUnmarshallingContext ictx) throws JiBXException { setFormatQName(QName.deserialize(label, ictx)); } /** * JiBX access method to get format label as qualified name. * * @param ictx marshalling context * @return format label text (<code>null</code> if none) * @throws JiBXException on deserialization error */ private String getQualifiedFormat(IMarshallingContext ictx) throws JiBXException { return QName.serialize(getFormatQName(), ictx); } /* (non-Javadoc) * @see org.jibx.binding.model.AttributeBase#prevalidate(org.jibx.binding.model.ValidationContext) */ public void prevalidate(ValidationContext vctx) { // make sure the type has been configured if (m_typeClass == null) { vctx.addFatal("Missing type information for conversion to string"); } else { // get the base format (if any) DefinitionContext dctx = vctx.getDefinitions(); if (m_formatName == null) { m_baseFormat = dctx.getBestFormat(m_typeClass); } else { m_baseFormat = dctx.getNamedFormat(m_formatName); if (m_baseFormat == null) { String name = m_formatName; if (name.startsWith("{}")) { name = name.substring(2); } vctx.addError("Unknown format " + name); } } // check specified serializer and deserializer String tname = m_typeClass.getName(); if (vctx.isOutBinding()) { if (m_serializerName != null) { // build all possible signature variations String[] tsigs = ClassUtils. getSignatureVariants(tname, vctx); int vcnt = SERIALIZER_SIGNATURE_VARIANTS.length; String[] msigs = new String[tsigs.length * vcnt]; for (int i = 0; i < tsigs.length; i++) { for (int j = 0; j < vcnt; j++) { msigs[i*vcnt + j] = "(" + tsigs[i] + SERIALIZER_SIGNATURE_VARIANTS[j] + ")Ljava/lang/String;"; } } // find a matching static method m_serializerItem = ClassUtils. findStaticMethod(m_serializerName, msigs, vctx); if (m_serializerItem == null) { vctx.addError("Static serializer method " + m_serializerName + " not found"); } } else { // try to find an inherited serializer FormatElement ances = m_baseFormat; while (ances != null) { m_serializerItem = ances.getSerializer(); if (m_serializerItem == null) { ances = ances.getBaseFormat(); } else { break; } } if (m_serializerItem == null) { m_serializerItem = m_typeClass.getMethod("toString", "()Ljava/lang/String;"); if (m_serializerItem == null) { vctx.addError("toString method not found"); } } } } if (vctx.isInBinding() || m_defaultText != null) { if (m_deserializerName != null) { // find a matching static method m_deserializerItem = ClassUtils. findStaticMethod(m_deserializerName, DESERIALIZER_SIGNATURES, vctx); if (m_deserializerItem == null) { vctx.addError("Static deserializer method " + m_deserializerName + " not found"); } else { String result = m_deserializerItem.getTypeName(); if (!ClassUtils.isAssignable(result, tname, vctx)) { vctx.addError("Static deserializer method " + m_deserializerName + " has incompatible result type"); } } } else { // check for a Java 5 enumeration boolean isenum = false; IClass sclas = m_typeClass; while ((sclas = sclas.getSuperClass()) != null) { if (sclas.getName().equals("java.lang.Enum")) { isenum = true; break; } } if (isenum) { m_deserializerItem = m_typeClass.getMethod("valueOf", "(Ljava/lang/String;)"); } if (m_deserializerItem == null) { // try to find an inherited deserializer FormatElement ances = m_baseFormat; while (ances != null) { m_deserializerItem = ances.getDeserializer(); if (m_deserializerItem == null) { ances = ances.getBaseFormat(); } else { break; } } if (m_deserializerItem == null) { // try to find constructor from string as last resort m_deserializerItem = m_typeClass. getInitializerMethod(STRING_CONSTRUCTOR_SIGNATURE); if (m_deserializerItem == null) { // error unless predefined formats if (vctx.getNestingDepth() > 0) { StringBuffer buff = new StringBuffer(); buff.append ("Need deserializer or constructor "); buff.append("from string"); if (!vctx.isInBinding()) { buff.append(" for default value of type "); buff.append(tname); } else { buff.append(" for type "); buff.append(tname); } vctx.addError(buff.toString()); } } } } } } // check for default value to be converted if (m_defaultText != null && m_deserializerItem != null) { // first load the class to handle conversion IClass iclas = m_deserializerItem.getOwningClass(); Class clas = iclas.loadClass(); Exception ex = null; boolean construct = false; try { if (clas == null) { vctx.addError("Unable to load class " + iclas.getName() + " for converting default value of type " + tname); } else if (m_deserializerItem.isInitializer()) { // invoke constructor to process default value construct = true; Constructor cons = clas.getConstructor (STRING_CONSTRUCTOR_ARGUMENT_CLASSES); try { cons.setAccessible(true); } catch (Exception e) { /* deliberately left empty */ } Object[] args = new Object[1]; args[0] = m_defaultText; m_default = cons.newInstance(args); } else { // invoke deserializer to convert default value String mname = m_deserializerItem.getName(); Method deser = clas.getDeclaredMethod(mname, STRING_CONSTRUCTOR_ARGUMENT_CLASSES); try { deser.setAccessible(true); } catch (Exception e) { /* deliberately left empty */ } Object[] args = new Object[1]; args[0] = m_defaultText; m_default = deser.invoke(null, args); } } catch (SecurityException e) { StringBuffer buff = new StringBuffer("Unable to access "); if (construct) { buff.append("constructor from string"); } else { buff.append("deserializer "); buff.append(m_deserializerName); } buff.append(" for converting default value of type "); buff.append(tname); vctx.addError(buff.toString()); } catch (NoSuchMethodException e) { StringBuffer buff = new StringBuffer("Unable to find "); if (construct) { buff.append("constructor from string"); } else { buff.append("deserializer "); buff.append(m_deserializerName); } buff.append(" for converting default value of type "); buff.append(tname); vctx.addError(buff.toString()); } catch (IllegalArgumentException e) { ex = e; } catch (InstantiationException e) { ex = e; } catch (IllegalAccessException e) { ex = e; } catch (InvocationTargetException e) { ex = e; } finally { if (ex != null) { StringBuffer buff = new StringBuffer("Error calling "); if (construct) { buff.append("constructor from string"); } else { buff.append("deserializer "); buff.append(m_deserializerName); } buff.append(" for converting default value of type "); buff.append(tname); vctx.addError(buff.toString()); } } } } super.prevalidate(vctx); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?