/** * This method just wraps newInstance() invocation and turns potential * exceptions into BeanCoderException. */ protected Object newInstance (final Class c) throws BeanCoderException { try { return c.newInstance(); } catch (final Exception e) { throw new BeanCoderException ("Failed to instantiate object of class "+c, e); } }
protected Class currentClass () throws BeanCoderException { final String className = currentClassName(); try { return Class.forName(className); } catch (final Throwable t) { throw new BeanCoderException ("failed to find class named '"+className+"'", t); } }
/** * Given a primitive class (Long, Integer, Boolean, ...), builds * such a primitive by calling the constructor(String s) method * of that primitive. */ public static Object newPrimitive (final Class primitiveClass, final String value) throws BeanCoderException { if (primitiveClass == Character.class) return new Character(value.charAt(0)); try { final Constructor c = primitiveClass .getConstructor(new Class[] { String.class }); return c.newInstance(new Object[] { value }); } catch (final Throwable t) { throw new BeanCoderException ("failed to build primitive of class '"+primitiveClass+ "' from text '"+Utils.summarize(value)+"'", t); } }
protected void encodeByteArray (final byte[] array) throws BeanCoderException { final org.jdom.Element e = new org.jdom.Element(E_BYTE_ARRAY); String s = null; try { s = new String(Base64.encode(array), "ascii"); } catch (final Throwable t) { throw new BeanCoderException ("failed to encode byte array", t); } final org.jdom.CDATA cd = new org.jdom.CDATA(s); e.addContent(cd); addContent(e); }
protected byte[] decodeByteArray () throws BeanCoderException { final org.jdom.CDATA cd = (org.jdom.CDATA)this.currentElement.getContent().get(0); final String s = cd.getTextTrim(); try { return Base64.decode(s.getBytes("ascii")); } catch (final Throwable t) { throw new BeanCoderException ("failed to decode base64 into a byte array", t); } }
throw new BeanCoderException ("failed to set bean field via "+setMethod.getName()+ "() with value of class "+valueClassName,
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(); }
throw new BeanCoderException ("Failed to encode field named '"+fieldName+ "' of class '"+bean.getClass().getName()+"'",
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); } }