KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > datatype > convertor > BeanConvertor


1 /*
2  * Copyright 1999-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 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 JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23
24 import java.util.Locale JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.WeakHashMap JavaDoc;
27
28
29 /**
30  * Converts String representation of beans to bean instances and vice versa.
31  *
32  * <p>
33  * Sometimes the toString() method doesn't give a good representation of a
34  * Java Bean suited for selection list IDs. For this an optional
35  * &lt;fd:id-path&gt;jx-path&lt;/fd:id-path&gt; attribute can be specified to
36  * have this convertor to use a different string representation.
37  * </p>
38  *
39  * @author <a HREF="mailto:giacomo@apache.org">Giacomo Pati</a>
40  * @version $Id$
41  */

42 public class BeanConvertor
43     implements Convertor
44 {
45     //~ Instance fields --------------------------------------------------------
46

47     private Class JavaDoc m_class;
48
49     private Map JavaDoc m_objects = new WeakHashMap JavaDoc( );
50
51     private String JavaDoc m_idPath;
52
53     //~ Constructors -----------------------------------------------------------
54

55     /**
56      * Construct a new BeanConvertor for a class
57      *
58      * @param className The package-qualified name of the class implementing
59      * the typesafe enum pattern.
60      * @param idPath Path to the identity field of the bean
61      *
62      * @throws CascadingRuntimeException If the class cannot be found
63      */

64     public BeanConvertor( final String JavaDoc className,
65                           final String JavaDoc idPath )
66     {
67         try
68         {
69             m_class = Class.forName( className );
70         }
71         catch( ClassNotFoundException JavaDoc e )
72         {
73             throw new CascadingRuntimeException( "Class " + className +
74                                                  " not found", e );
75         }
76
77         m_idPath = idPath;
78     }
79
80     //~ Methods ----------------------------------------------------------------
81

82     /**
83      * @see org.apache.cocoon.forms.datatype.convertor.Convertor#getTypeClass()
84      */

85     public Class JavaDoc getTypeClass( )
86     {
87         return m_class;
88     }
89
90     /**
91      * @see org.apache.cocoon.forms.datatype.convertor.Convertor#convertFromString(java.lang.String,
92      * java.util.Locale,
93      * org.apache.cocoon.forms.datatype.convertor.Convertor.FormatCache)
94      */

95     public ConversionResult convertFromString( final String JavaDoc value,
96                                                final Locale JavaDoc locale,
97                                                final FormatCache formatCache )
98     {
99         return new ConversionResult( m_objects.get( value ) );
100     }
101
102     /**
103      * @see org.apache.cocoon.forms.datatype.convertor.Convertor#convertToString(java.lang.Object,
104      * java.util.Locale,
105      * org.apache.cocoon.forms.datatype.convertor.Convertor.FormatCache)
106      */

107     public String JavaDoc convertToString( final Object JavaDoc value,
108                                    final Locale JavaDoc locale,
109                                    final FormatCache formatCache )
110     {
111         String JavaDoc 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     /**
132      * We do not enerate any SAX events
133      *
134      * @param contentHandler The contentHandler
135      * @param locale The locale
136      *
137      * @throws SAXException Just in case of failure that could never happen
138      */

139     public void generateSaxFragment( final ContentHandler JavaDoc contentHandler,
140                                      final Locale JavaDoc locale )
141         throws SAXException JavaDoc
142     {
143         // intentionally empty
144
}
145 }
146
Popular Tags