public Object decode () throws BeanCoderException { final int type = currentType(); //if (log.isDebugEnabled()) // log.debug("decode() type is "+type); if (type == T_NULL) return null; if (type == T_ARRAY) return decodeArray(); if (type == T_BYTE_ARRAY) return decodeByteArray(); if (type == T_MAP) return decodeMap(); if (type == T_COLLECTION) return decodeCollection(); if (type == T_PRIMITIVE) return decodePrimitive(); // else... return decodeBean(); }
protected void encodeMapEntry (final java.util.Map.Entry e) throws BeanCoderException { beginMapEntry(); encode(e.getKey()); encode(e.getValue()); endElement(); }
protected void encodeArray (final Object array) throws BeanCoderException { try { encodeByteArray((byte[])array); return; } catch (final Throwable t) { // it's definitively not a byte array // so let's go on } final int length = Array.getLength(array); beginArray(length); for (int i=0; i<length; i++) { final Object elt = Array.get(array, i); //encode(elt); encodeEntry(i, elt); } endElement(); }
protected java.util.Collection decodeCollection () throws BeanCoderException { final java.util.Collection result = (java.util.Collection)newInstance(currentClass()); final java.util.Iterator it = subElementIterator(); while (it.hasNext()) { it.next(); result.add(decode()); } return result; }
protected Object decodeBean () throws BeanCoderException final Class beanClass = currentClass(); Object result = newInstance(beanClass); java.util.Iterator it = subElementIterator(); while (it.hasNext()) String fn = currentFieldName(); final Object value = decodeFieldValue();
protected java.util.Map decodeMap () throws BeanCoderException { final java.util.Map result = (java.util.Map)newInstance(currentClass()); final java.util.Iterator it = subElementIterator(); while (it.hasNext()) { it.next(); final Object[] kv = decodeMapEntry(); if (kv == null) continue; // // could make sense in some implementations result.put(kv[0], kv[1]); } return result; }
protected void encodeBean (final Object bean) throws BeanCoderException beginBean(bean); final String fieldName = determineFieldName(getMethod); final Object value = getMethod.invoke(bean, args); encodeField(fieldName, value); endElement();
protected void encodeCollection (final Object collection) throws BeanCoderException { final java.util.Collection col = (java.util.Collection)collection; beginCollection(col); synchronized (collection) { final java.util.Iterator it = col.iterator(); for (int i=0; it.hasNext(); i++) //encode(it.next()); encodeEntry(i, it.next()); } endElement(); }
protected void encodeMap (final Object map) throws BeanCoderException { final java.util.Map theMap = (java.util.Map)map; beginMap(theMap); synchronized (map) { final java.util.Iterator it = theMap.entrySet().iterator(); while (it.hasNext()) { final java.util.Map.Entry entry = (java.util.Map.Entry)it.next(); encodeMapEntry(entry); } } endElement(); }
/** * Overriding decodeBean() in the parent class to make sure that * embedded XML content doesn't get mistaken as a bean. */ protected Object decodeBean () throws BeanCoderException { if (currentType() == T_XML) return decodeXmlContent(); return super.decodeBean(); }
protected Object decodePrimitive () throws BeanCoderException { final Class primitiveClass = currentClass(); final String text = currentText(); return BeanCoderUtils.newPrimitive(primitiveClass, text); }
/** * Overriding the decodePrimitive() method of the parent class * to take care of binary values. */ protected Object decodePrimitive () throws BeanCoderException { final Class primitiveClass = currentClass(); if (primitiveClass != byte[].class) return super.decodePrimitive(); return decodeByteArray(); }
/** * Overriding encode() to remove potential previous nodes with the * same name ('override'). Only occurs at the very beginning of * the encoding work. */ public void encode (final Object o) throws BeanCoderException { if (this.override && ( ! this.overrideDone)) { final java.util.Iterator it = this.currentNode().getNodes(); while (it.hasNext()) { final Node n = (Node)it.next(); if (n.getName().equals(this.currentItemName)) n.remove(); } this.overrideDone = true; } super.encode(o); }
/** * Overriding decodeBean() to intercept decode request for * primitive encoded as beans (it occurs for certains map keys). */ protected Object decodeBean () throws BeanCoderException { if (this.currentNode().hasProperty(this.nsp + PN_PVALUE)) { return BeanCoderUtils.newPrimitive (currentClass(), this.currentNode().getProperty (this.nsp + PN_PVALUE).getString()); } return super.decodeBean(); }