1 16 package org.apache.cocoon.forms.datatype.convertor; 17 18 import org.apache.avalon.framework.CascadingRuntimeException; 19 import org.apache.commons.jxpath.JXPathContext; 20 21 import org.xml.sax.ContentHandler ; 22 import org.xml.sax.SAXException ; 23 24 import java.util.Locale ; 25 import java.util.Map ; 26 import java.util.WeakHashMap ; 27 28 29 42 public class BeanConvertor 43 implements Convertor 44 { 45 47 private Class m_class; 48 49 private Map m_objects = new WeakHashMap ( ); 50 51 private String m_idPath; 52 53 55 64 public BeanConvertor( final String className, 65 final String idPath ) 66 { 67 try 68 { 69 m_class = Class.forName( className ); 70 } 71 catch( ClassNotFoundException e ) 72 { 73 throw new CascadingRuntimeException( "Class " + className + 74 " not found", e ); 75 } 76 77 m_idPath = idPath; 78 } 79 80 82 85 public Class getTypeClass( ) 86 { 87 return m_class; 88 } 89 90 95 public ConversionResult convertFromString( final String value, 96 final Locale locale, 97 final FormatCache formatCache ) 98 { 99 return new ConversionResult( m_objects.get( value ) ); 100 } 101 102 107 public String convertToString( final Object value, 108 final Locale locale, 109 final FormatCache formatCache ) 110 { 111 String idValue = ""; 112 113 if( null != value ) 114 { 115 if( m_idPath != null ) 116 { 117 final JXPathContext ctx = JXPathContext.newContext( value ); 118 idValue = ctx.getValue( m_idPath ).toString( ); 119 } 120 else 121 { 122 idValue = value.toString( ); 123 } 124 } 125 126 m_objects.put( idValue, value ); 127 128 return idValue; 129 } 130 131 139 public void generateSaxFragment( final ContentHandler contentHandler, 140 final Locale locale ) 141 throws SAXException 142 { 143 } 145 } 146 | Popular Tags |