/** * A convenience method for encoding a bean to a JCR repository. */ public static void encode (final String namespace, final javax.jcr.Node targetNode, final String beanNodeName, final Object bean) throws BeanCoderException { final Node n = (Node)JcrProxy.wrap(targetNode); final JcrBeanCoder jbc = new JcrBeanCoder(namespace, n, beanNodeName); jbc.encode(bean); }
protected void encodeNull () throws BeanCoderException { beginElement(); this.currentNode() .setProperty(this.nsp + PN_CLASS, C_NULL, PropertyType.NAME); }
protected void beginMap (final java.util.Map map) throws BeanCoderException { beginWithClass(map); }
protected void encodeField (final String fieldName, final Object value) throws BeanCoderException { //log.debug("encodeField() '"+fieldName+"'"); final Node beanNode = this.currentNode(); this.currentItemName = fieldName; encode(value); //if (this.currentItem != beanNode) // endElement(); if (this.currentItem != beanNode) { this.previousItem = this.currentItem; this.currentItem = beanNode; } }
final Object[] pe = asPropertyMapEntry(key, val); this.currentNode().setProperty(key.toString(), sVal, tVal); final Node mapNode = this.currentNode(); this.nsp + NN_ENTRY + "_" + getUniqueLocalId(); encodeBean(key); encode(val);
protected int currentType () throws BeanCoderException { if (this.currentItem instanceof Property) return T_PRIMITIVE; final String className = currentClassName(); if (className == null) { if (this.currentNode().hasProperty(this.nsp + PN_SIZE)) return T_ARRAY; throw new JcrException ("property 'class' is missing "+ "("+this.currentItem.getPath()+")"); } if (className.equals(C_NULL)) { return T_NULL; } final Class c = currentClass(); //if (byte[].class == c) // return T_BYTE_ARRAY; if (java.util.Map.class.isAssignableFrom(c)) return T_MAP; if (java.util.Collection.class.isAssignableFrom(c)) return T_COLLECTION; return T_BEAN; }
final Node nk = this.currentNode(); key = decode(); value = decode(); this.currentProperty().getName().substring(this.nsp.length()); value = this.currentProperty().getValue().getValue(); if (this.currentProperty().getValue().getType() == PropertyType.NAME && value.equals(V_NULL))
protected String currentClassName () throws BeanCoderException { if ( ! this.currentItem.isNode()) return currentPrimitiveClass().getName(); if ( ! this.currentNode().hasProperty(this.nsp + PN_CLASS)) return null; return this.currentNode() .getProperty(this.nsp + PN_CLASS).getString(); }
/** * A convenience method for decoding a bean from a JCR node. */ public static Object decode (final String namespace, final javax.jcr.Node beanNode) throws BeanCoderException { final Node n = (Node)JcrProxy.wrap(beanNode); final JcrBeanCoder jbc = new JcrBeanCoder(namespace, n); return jbc.decode(); }
/** * 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(); }
protected java.util.Iterator subElementIterator () throws BeanCoderException { final int type = this.currentType(); if (type == T_ARRAY || type == T_COLLECTION) return new IndexedIterator(this.currentNode()); return new ItemIterator(this.currentNode()); }
@Override protected Object[] asPropertyMapEntry(Object key, Object value) { if ((key instanceof StringAttribute) && isSafeForAnItemName(key.toString())) { if (value instanceof StringAttribute) { return new Object[]{key.toString(), value.toString()}; } } return super.asPropertyMapEntry(key, value); } }
protected void beginBean (final Object bean) throws BeanCoderException { beginWithClass(bean); if (BeanCoderUtils.isPrimitive(bean)) // // this may happen, for map entry keys that are primitive but // whose value is not a primitive { this.currentNode() .setProperty(this.nsp + PN_PVALUE, bean.toString()); } }
/** * 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(); }
protected void encodeByteArray (final byte[] array) throws BeanCoderException { this.currentNode().setProperty (this.currentItemName, new java.io.ByteArrayInputStream(array)); }
protected String currentText () throws BeanCoderException { //if (this.currentProperty().getDefinition().isMultiple()) // text = this.currentProperty().getValues()[1].getString(); //else return this.currentProperty().getString(); }
protected Class currentClass () throws BeanCoderException { if ( ! this.currentItem.isNode()) return currentPrimitiveClass(); String className = null; try { className = this.currentNode() .getProperty(this.nsp + PN_CLASS).getString(); return Class.forName(className); } catch (final JcrException e) { throw e; } catch (final Throwable t) { throw new BeanCoderException ("failed to find class named '"+className+"'", t); } }
protected void beginElement () throws BeanCoderException { if (this.currentItemName == null) // // use current node (todo #1560001) // return; this.currentItem = this.currentNode().addNode(this.currentItemName); this.currentItemName = null; // // will provoke NPEs :-) //log.debug("beginElement() '"+this.currentItem.getName()+"'"); }
protected byte[] decodeByteArray () throws BeanCoderException { final java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(); final java.io.InputStream is = this.currentProperty() .getValue().getStream(); try { ByteUtils.copy(is, baos, 1024); } catch (final Throwable t) { throw new BeanCoderException ("Failed to decode byte array for '"+this.currentItemName+ "' out of stream", t); } return baos.toByteArray(); }
protected void beginArray (int length) throws BeanCoderException { beginElement(); this.currentNode().setProperty (this.nsp + PN_SIZE, ""+length, PropertyType.LONG); }