1 18 package org.apache.beehive.netui.pageflow.internal; 19 20 import org.apache.beehive.netui.pageflow.config.PageFlowActionMapping; 21 import org.apache.beehive.netui.util.logging.Logger; 22 import org.apache.struts.action.ActionMapping; 23 24 import javax.servlet.http.HttpServletRequest ; 25 import java.lang.reflect.Method ; 26 import java.lang.reflect.InvocationTargetException ; 27 28 public class XmlBeanActionForm extends AnyBeanActionForm 29 { 30 private static final Logger _log = Logger.getInstance( XmlBeanActionForm.class ); 31 32 private String _formClassName; 33 34 35 public XmlBeanActionForm() 36 { 37 } 38 39 public XmlBeanActionForm( Object xml ) 40 { 41 setBean( xml ); 42 } 43 44 public String getXmlString() 45 { 46 Object xmlBean = getBean(); 47 48 if ( xmlBean == null) return null; 49 50 try 51 { 52 return ( String ) xmlBean.getClass().getMethod( "xmlText", null ).invoke( xmlBean, null ); 53 } 54 catch ( InvocationTargetException e ) 55 { 56 _log.error( "Error while getting XML String", e.getCause() ); 57 } 58 catch ( Exception e ) 59 { 60 assert e instanceof NoSuchMethodException || e instanceof IllegalAccessException : e.getClass().getName(); 61 _log.error( "Error while getting XML String", e ); 62 } 63 64 return null; 65 } 66 67 public void setXmlString( String xml ) 68 { 69 assert _formClassName != null; 70 setBean( invokeFactoryMethod( "parse", new Class []{ String .class }, new Object []{ xml } ) ); 71 } 72 73 public void reset( ActionMapping mapping, HttpServletRequest request ) 74 { 75 if ( _formClassName == null ) 76 { 77 assert mapping instanceof PageFlowActionMapping : mapping.getClass().getName(); 78 _formClassName = ( ( PageFlowActionMapping ) mapping ).getFormClass(); 79 assert _formClassName != null; 80 } 81 82 if ( getBean() == null ) 83 { 84 setBean( invokeFactoryMethod( "newInstance", new Class [0], new Object [0] ) ); 85 } 86 } 87 88 private Object invokeFactoryMethod( String methodName, Class [] argTypes, Object [] args ) 89 { 90 String factoryClassName = _formClassName + "$Factory"; 91 92 try 93 { 94 Class factoryClass = Class.forName( factoryClassName ); 95 Method newInstanceMethod = factoryClass.getMethod( methodName, argTypes ); 96 return newInstanceMethod.invoke( factoryClass, args ); 97 } 98 catch ( Exception e ) 99 { 100 if ( _log.isErrorEnabled() ) 103 { 104 _log.error( "Error while creating XML object of type " + _formClassName, e ); 105 } 106 107 return null; 108 } 109 } 110 } 111 | Popular Tags |