View Javadoc

1   package org.codehaus.xfire.xmlbeans;
2   
3   import java.util.HashSet;
4   import java.util.Set;
5   
6   import javax.xml.namespace.QName;
7   import javax.xml.stream.XMLStreamException;
8   
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  import org.apache.xmlbeans.SchemaProperty;
12  import org.apache.xmlbeans.SchemaType;
13  import org.apache.xmlbeans.XmlBeans;
14  import org.apache.xmlbeans.XmlCursor;
15  import org.apache.xmlbeans.XmlException;
16  import org.apache.xmlbeans.XmlObject;
17  import org.codehaus.xfire.MessageContext;
18  import org.codehaus.xfire.XFireRuntimeException;
19  import org.codehaus.xfire.aegis.MessageReader;
20  import org.codehaus.xfire.aegis.MessageWriter;
21  import org.codehaus.xfire.aegis.stax.ElementReader;
22  import org.codehaus.xfire.aegis.stax.ElementWriter;
23  import org.codehaus.xfire.aegis.type.Type;
24  import org.codehaus.xfire.fault.XFireFault;
25  import org.codehaus.xfire.util.STAXUtils;
26  import org.codehaus.yom.Element;
27  
28  /***
29   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
30   * @since Nov 13, 2004
31   */
32  public class XmlBeansType 
33      extends Type
34  {
35      private SchemaType schemaType;
36  
37      private final static Log logger = LogFactory.getLog(XmlBeansType.class); 
38      
39      public XmlBeansType()
40      {
41      }
42      
43      public XmlBeansType(SchemaType schemaType)
44      {
45          this.schemaType = schemaType;
46          setTypeClass(schemaType.getJavaClass());
47      }
48  
49      public XmlBeansType(Class clazz)
50      {
51          this.schemaType = XmlBeans.typeForClass(clazz);
52          setTypeClass(clazz);
53      }
54  
55      public void writeSchema(Element root)
56      {
57      }
58      
59      public boolean isComplex()
60      {
61          return !schemaType.isPrimitiveType();
62      }
63  
64      public boolean isAbstract()
65      {
66          return schemaType.isAbstract();
67      }
68  
69      public Set getDependencies()
70      {
71          SchemaProperty[] properties = schemaType.getProperties();
72          HashSet deps = new HashSet();
73          for (int i = 0; i < properties.length; i++)
74          {
75              SchemaType etype = properties[i].getType();
76              SchemaProperty[] iprops = etype.getElementProperties();
77              for (int j = 0; j < iprops.length; j++)
78              {
79                  SchemaType itype = iprops[j].getType();
80                  
81                  if (!itype.isPrimitiveType() && itype.getSourceName() != null)
82                  {
83                      deps.add(new XmlBeansType(itype));
84                  }
85              }
86          }
87          return deps;
88      }
89  
90      public QName getSchemaType()
91      {
92          if (schemaType.isDocumentType())
93              return schemaType.getDocumentElementName();
94          else if (schemaType.getName() != null)
95              return schemaType.getName();
96          else
97          {
98              // No name for this type, use outer type (and recur up if same)
99              SchemaType outer = schemaType.getOuterType();
100             while (outer != null)
101             {
102                 if (outer.isDocumentType())
103                     return outer.getDocumentElementName();
104                 else if (outer.getName() != null)
105                     return outer.getName();
106                 else
107                     outer = outer.getOuterType();
108             }
109             
110             // No outer type, no type on this, should not be possible, so explode
111             throw new XFireRuntimeException("No type name is defined for <" + schemaType + "> " +
112                                             "and no outer type containing the inline type -- this " +
113                                             "should not be possible to be a legally defined schema");
114         }
115     }
116 
117     public Object readObject(MessageReader reader, MessageContext context)
118         throws XFireFault
119     {
120         try
121         {
122             return XmlObject.Factory.parse(((ElementReader)reader).getXMLStreamReader());
123         }
124         catch( XmlException e )
125         {
126             throw new XFireFault("Could not read request.", e, XFireFault.SENDER);
127         }
128     }
129 
130     public void writeObject(Object value, MessageWriter writer, MessageContext context)
131         throws XFireFault
132     {
133         try
134         {
135             XmlObject obj = (XmlObject) value; 
136        
137             XmlCursor cursor = obj.newCursor();
138             if (cursor.toFirstChild() && cursor.toFirstChild())
139             {
140                 do
141                 {
142                     STAXUtils.copy(cursor.newXMLStreamReader(), 
143                                    ((ElementWriter) writer).getXMLStreamWriter());
144                 }
145                 while(cursor.toNextSibling());
146             }
147         } 
148         catch (XMLStreamException e)
149         {
150             throw new XFireFault("Could not write response.", e, XFireFault.SENDER);
151         }
152     }
153 }