KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > internal > XmlBeanActionForm


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

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 JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27
28 public class XmlBeanActionForm extends AnyBeanActionForm
29 {
30     private static final Logger _log = Logger.getInstance( XmlBeanActionForm.class );
31     
32     private String JavaDoc _formClassName;
33
34     
35     public XmlBeanActionForm()
36     {
37     }
38     
39     public XmlBeanActionForm( Object JavaDoc xml )
40     {
41         setBean( xml );
42     }
43     
44     public String JavaDoc getXmlString()
45     {
46         Object JavaDoc xmlBean = getBean();
47         
48         if ( xmlBean == null) return null;
49         
50         try
51         {
52             return ( String JavaDoc ) xmlBean.getClass().getMethod( "xmlText", null ).invoke( xmlBean, null );
53         }
54         catch ( InvocationTargetException JavaDoc e )
55         {
56             _log.error( "Error while getting XML String", e.getCause() );
57         }
58         catch ( Exception JavaDoc e )
59         {
60             assert e instanceof NoSuchMethodException JavaDoc || e instanceof IllegalAccessException JavaDoc : e.getClass().getName();
61             _log.error( "Error while getting XML String", e );
62         }
63         
64         return null;
65     }
66     
67     public void setXmlString( String JavaDoc xml )
68     {
69         assert _formClassName != null;
70         setBean( invokeFactoryMethod( "parse", new Class JavaDoc[]{ String JavaDoc.class }, new Object JavaDoc[]{ xml } ) );
71     }
72
73     public void reset( ActionMapping mapping, HttpServletRequest JavaDoc 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 JavaDoc[0], new Object JavaDoc[0] ) );
85         }
86     }
87     
88     private Object JavaDoc invokeFactoryMethod( String JavaDoc methodName, Class JavaDoc[] argTypes, Object JavaDoc[] args )
89     {
90         String JavaDoc factoryClassName = _formClassName + "$Factory";
91
92         try
93         {
94             Class JavaDoc factoryClass = Class.forName( factoryClassName );
95             Method JavaDoc newInstanceMethod = factoryClass.getMethod( methodName, argTypes );
96             return newInstanceMethod.invoke( factoryClass, args );
97         }
98         catch ( Exception JavaDoc e )
99         {
100             // Can be any exception -- not just the reflection-related exceptions...
101
// because the exception could be thrown while creating the XML bean.
102
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