1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.wotonomy.web.xml;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.URL;
24
25 import javax.xml.parsers.ParserConfigurationException;
26 import javax.xml.parsers.SAXParser;
27 import javax.xml.parsers.SAXParserFactory;
28
29 import net.wotonomy.foundation.internal.WotonomyException;
30 import net.wotonomy.foundation.xml.XMLDecoder;
31
32 import org.xml.sax.SAXException;
33
34 /***
35 * An implementation of XMLDecoder that reads objects from
36 * XMLRPC format.
37 * This implementation is not thread-safe, so a new instances
38 * should be created to accomodate multiple threads.
39 */
40 public class XMLRPCDecoder implements XMLDecoder
41 {
42 XMLRPCDecoderHelper helper = new XMLRPCDecoderHelper();
43
44 /***
45 * Decodes an object in XML-RPC format from the specified input stream.
46 * @param anInputStream The input stream from which to read.
47 * The stream will be read fully.
48 * @param aDescription A description to accompany error messages
49 * for the stream, typically a file name.
50 * @param aURL A URL against which relative references within the
51 * XML will be resolved.
52 * @return The object that was constructed from the XML content,
53 * or null if no object could be constructed.
54 */
55 public Object decode(
56 InputStream anInputStream, String aDescription, URL aURL )
57 {
58 Object result = null;
59
60 try {
61 SAXParser sparser = SAXParserFactory.newInstance().newSAXParser();
62 sparser.parse(anInputStream,helper,aURL.toExternalForm());
63 result = helper.getResult();
64 } catch (ParserConfigurationException e) {
65 throw new WotonomyException("Problem in parser configuration", e );
66 } catch (IOException e) {
67 throw new WotonomyException("IOException thrown while parsing", e );
68 } catch (SAXException e) {
69 throw new WotonomyException("SAXException thrown while parsing", e );
70 }
71 helper.reset();
72 return result;
73 }
74
75 /***
76 * Decodes an XML-RPC message from the specified input stream.
77 * Stand-alone values not wrapped in "methodCall" or "param"
78 * tags will be treated as a response.
79 * @param anInputStream The input stream from which to read.
80 * The stream will be read fully.
81 * @param aReceiver an XMLRPCReceiver that will be invoked with
82 * the appropriate method: request, response, or fault.
83 */
84 public void decode(
85 InputStream anInputStream, XMLRPCReceiver aReceiver )
86 {
87 try
88 {
89 SAXParser sparser = SAXParserFactory.newInstance().newSAXParser();
90 sparser.parse(anInputStream,helper);
91
92 if ( helper.isRequest() )
93 {
94 aReceiver.request( helper.getMethodName(), helper.getParameters() );
95 }
96 else
97 if ( helper.isFault() )
98 {
99 aReceiver.fault( helper.getFaultCode(), helper.getFaultString() );
100 }
101 else
102 {
103 aReceiver.response( helper.getResult() );
104 }
105 } catch (ParserConfigurationException e) {
106 throw new WotonomyException("Problem in parser configuration", e );
107 } catch (IOException e) {
108 throw new WotonomyException("IOException thrown while parsing", e );
109 } catch (SAXException e) {
110 throw new WotonomyException("SAXException thrown while parsing", e );
111 }
112 helper.reset();
113 }
114 }