View Javadoc

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