1 package org.codehaus.xfire.aegis.type;
2
3 import java.util.List;
4 import java.util.Map;
5
6 import ognl.NoSuchPropertyException;
7 import ognl.Ognl;
8 import ognl.OgnlException;
9
10 import org.codehaus.xfire.SOAPConstants;
11 import org.codehaus.xfire.XFireRuntimeException;
12 import org.codehaus.xfire.aegis.AegisService;
13 import org.codehaus.xfire.aegis.mapping.TypeRegistry;
14 import org.codehaus.xfire.fault.XFireFault;
15 import org.dom4j.Attribute;
16 import org.dom4j.Element;
17 import org.dom4j.QName;
18
19 /***
20 * An Aegis Type. Something that is read/written.
21 *
22 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
23 * @since Aug 19, 2004
24 */
25 public class SimpleType
26 extends Type
27 {
28 private String key;
29
30 public SimpleType()
31 {
32 }
33
34 /***
35 * Coerce a value to a string (i.e. create a ISO-8601 string
36 * from a Date).
37 *
38 * @param value
39 * @return
40 */
41 public String coerceValue(Object value)
42 {
43 return value.toString();
44 }
45
46 /***
47 * Coerce the string to an object (i.e. create a date from
48 * a string).
49 *
50 * @param string
51 * @return
52 */
53 public Object coerceString(String string)
54 {
55 return string;
56 }
57
58 public void write( Element element, Map context ) throws XFireFault
59 {
60 Element typeEl = element.addElement( getQName() );
61 try
62 {
63 Object value = Ognl.getValue( getOgnl(), context, (Object) null );
64
65 if ( value != null )
66 {
67 typeEl.setText( coerceValue( value ) );
68 }
69 }
70 catch (NoSuchPropertyException e)
71 {
72 }
73 catch (OgnlException e)
74 {
75
76 throw new XFireFault( "Couldn't create message.", e, XFireFault.SENDER );
77 }
78 catch (NullPointerException e)
79 {
80 throw new XFireRuntimeException( "Couldn't create message type: " + getQName(), e );
81 }
82
83 }
84
85 public void read( Element element, Map context ) throws XFireFault
86 {
87 read( element, 0, context );
88 }
89
90 /***
91 * @see org.codehaus.xfire.aegis.type.Type#read(org.dom4j.Element, int, java.util.Map)
92 */
93 public void read(Element element, int occurrence, Map context) throws XFireFault
94 {
95 List elements = element.elements( getQName() );
96 if ( elements.size() <= occurrence )
97 return;
98
99 Element typeEl = (Element) elements.get(occurrence);
100
101 if ( typeEl != null )
102 {
103 try
104 {
105 if ( key != null )
106 {
107 Object value = Ognl.getValue( getOgnl(), context, coerceString( typeEl.getText() ) );
108
109 context.put( key, value );
110 }
111 else
112 {
113 Ognl.setValue( getOgnl(), context, (Object) null, coerceString( typeEl.getText() ) );
114 }
115 }
116 catch (OgnlException e)
117 {
118 throw new XFireFault( "Couldn't process message.", e, XFireFault.SENDER );
119 }
120 }
121 }
122
123 /***
124 * @see org.codehaus.xfire.aegis.type.Type#writeSchema(org.dom4j.Element)
125 */
126 public void writeSchema(Element element)
127 {
128
129 }
130
131 /***
132 * @see org.codehaus.xfire.aegis.type.Type#configure(org.dom4j.Element)
133 */
134 public void configure(Element configuration, AegisService service, TypeRegistry reg)
135 {
136 setName( configuration.attribute("name").getStringValue() );
137 Attribute ognlAttr = configuration.attribute("ognl");
138 if ( ognlAttr == null )
139 setOgnl( "#this" );
140 else
141 setOgnl( ognlAttr.getStringValue() );
142
143 setQName( QName.get( getName(), service.getDefaultNamespace() ) );
144 setKey( configuration.attributeValue("key") );
145 setDocumentation( configuration.getTextTrim() );
146
147 Attribute xsdAt = configuration.attribute("schemaType");
148 if ( xsdAt != null )
149 {
150 setSchemaType( null );
151 }
152 else
153 {
154 setSchemaType( getDefaultSchemaType() );
155 }
156 }
157
158 public QName getDefaultSchemaType()
159 {
160 return QName.get("string", SOAPConstants.XSD);
161 }
162
163 public String getKey()
164 {
165 return key;
166 }
167
168 public void setKey(String key)
169 {
170 this.key = key;
171 }
172 }