📄 hashmapperstringtoschematype.java
字号:
while (iter.hasNext()) { // first make sure we have a value Map.Entry entry = (Map.Entry)iter.next(); Object value = entry.getValue(); if (value != null) { // write element with key attribute ctx.startTagAttributes(m_index, ENTRY_ELEMENT_NAME); ctx.attribute(m_index, KEY_ATTRIBUTE_NAME, entry.getKey().toString()); // translate value object class to schema type String tname = value.getClass().getName(); int type = s_javaTypesEnum.getValue(tname); if (type < 0) { throw new JiBXException("Value of type " + tname + " with key " + entry.getKey() + " is not a supported type"); } // generate xsi:type attribute for value ctx.attribute(ixsi, TYPE_ATTRIBUTE_NAME, XSD_PREFIX_LEAD + s_schemaTypesEnum.getName(type)); ctx.closeStartContent(); // handle the actual value conversion based on type switch (type) { case BOOLEAN_TYPE: ctx.content(Utility.serializeBoolean (((Boolean)value).booleanValue())); break; case BYTE_TYPE: ctx.content(Utility. serializeByte(((Byte)value).byteValue())); break; case DOUBLE_TYPE: ctx.content(Utility. serializeDouble(((Double)value).doubleValue())); break; case FLOAT_TYPE: ctx.content(Utility. serializeFloat(((Float)value).floatValue())); break; case INT_TYPE: ctx.content(((Integer)value).intValue()); break; case LONG_TYPE: ctx.content(Utility. serializeLong(((Long)value).longValue())); break; case SHORT_TYPE: ctx.content(Utility. serializeShort(((Short)value).shortValue())); break; case DATETIME_TYPE: ctx.content(Utility.serializeDateTime((Date)value)); break; //#!j2me{ case DATE_TYPE: ctx.content(Utility. serializeSqlDate((java.sql.Date)value)); break; case TIME_TYPE: ctx.content(Utility. serializeSqlTime((java.sql.Time)value)); break;//#j2me} case BYTERRAY_TYPE: ctx.content(Utility.serializeBase64((byte[])value)); break; case DECIMAL_TYPE: case INTEGER_TYPE: case STRING_TYPE: ctx.content(value.toString()); break; } // finish with close tag for entry element ctx.endTag(m_index, ENTRY_ELEMENT_NAME); } } // finish with end tag for container element ctx.endTag(m_index, m_name); xwrite.popExtensionNamespaces(); } } /* (non-Javadoc) * @see org.jibx.runtime.IUnmarshaller#isPresent(org.jibx.runtime.IUnmarshallingContext) */ public boolean isPresent(IUnmarshallingContext ctx) throws JiBXException { return ctx.isAt(m_uri, m_name); } /* (non-Javadoc) * @see org.jibx.runtime.IUnmarshaller#unmarshal(java.lang.Object, * org.jibx.runtime.IUnmarshallingContext) */ public Object unmarshal(Object obj, IUnmarshallingContext ictx) throws JiBXException { // make sure we're at the appropriate start tag UnmarshallingContext ctx = (UnmarshallingContext)ictx; if (!ctx.isAt(m_uri, m_name)) { ctx.throwStartTagNameError(m_uri, m_name); } // lookup the prefixes assigned to required namespaces int nscnt = ctx.getActiveNamespaceCount(); String xsdlead = null; for (int i = nscnt-1; i >= 0; i--) { String uri = ctx.getActiveNamespaceUri(i); if (XSD_NAMESPACE_URI.equals(uri)) { String prefix = ctx.getActiveNamespacePrefix(i); if (!"".equals(prefix)) { xsdlead = prefix + ':'; break; } } } if (xsdlead == null) { throw new JiBXException ("Missing required schema namespace declaration"); } // create new hashmap if needed int size = ctx.attributeInt(m_uri, SIZE_ATTRIBUTE_NAME, DEFAULT_SIZE); Map map = (Map)obj; if (map == null) { map = new HashMap(size); } // process all entries present in document ctx.parsePastStartTag(m_uri, m_name); String tdflt = xsdlead + "string"; while (ctx.isAt(m_uri, ENTRY_ELEMENT_NAME)) { // unmarshal key and type from start tag attributes Object key = ctx.attributeText(m_uri, KEY_ATTRIBUTE_NAME); String tname = ctx.attributeText(XSI_NAMESPACE_URI, TYPE_ATTRIBUTE_NAME, tdflt); // convert type name to type index number int type = -1; if (tname.startsWith(xsdlead)) { type = s_schemaTypesEnum. getValue(tname.substring(xsdlead.length())); } if (type < 0) { throw new JiBXException("Value of type " + tname + " with key " + key + " is not a supported type"); } // deserialize content as specified type String text = ctx.parseElementText(m_uri, ENTRY_ELEMENT_NAME); Object value = null; switch (type) { case BOOLEAN_TYPE: value = Utility.parseBoolean(text) ? Boolean.TRUE : Boolean.FALSE; break; case BYTE_TYPE: value = new Byte(Utility.parseByte(text)); break; case DOUBLE_TYPE: value = new Double(Utility.parseDouble(text)); break; case FLOAT_TYPE: value = new Float(Utility.parseFloat(text)); break; case INT_TYPE: value = new Integer(Utility.parseInt(text)); break; case LONG_TYPE: value = new Long(Utility.parseLong(text)); break; case SHORT_TYPE: value = new Short(Utility.parseShort(text)); break; case DATETIME_TYPE: value = Utility.deserializeDateTime(text); break; //#!j2me{ case DATE_TYPE: value = Utility.deserializeSqlDate(text); break; case TIME_TYPE: value = Utility.deserializeSqlTime(text); break;//#j2me} case BYTERRAY_TYPE: value = Utility.deserializeBase64(text); break; case DECIMAL_TYPE: value = new BigDecimal(text); break; case INTEGER_TYPE: value = new BigInteger(text); break; case STRING_TYPE: value = text; break; } // add key-value pair to map map.put(key, value); } // finish by skipping past wrapper end tag ctx.parsePastEndTag(m_uri, m_name); return map; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -