|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package net.sf.sojo.interchange.xmlrpc; |
|
17 |
| |
|
18 |
| import java.io.ByteArrayInputStream; |
|
19 |
| import java.util.Map; |
|
20 |
| |
|
21 |
| import javax.xml.parsers.SAXParser; |
|
22 |
| import javax.xml.parsers.SAXParserFactory; |
|
23 |
| |
|
24 |
| import org.xml.sax.InputSource; |
|
25 |
| import org.xml.sax.SAXParseException; |
|
26 |
| import org.xml.sax.XMLReader; |
|
27 |
| |
|
28 |
| public class XmlRpcParser { |
|
29 |
| |
|
30 |
| private boolean returnValueAsList = true; |
|
31 |
| private boolean convertResult2XmlRpcExceptionAndThrow = false; |
|
32 |
| private String methodName = null; |
|
33 |
| |
|
34 |
| |
|
35 |
49
| public String getMethodName() { return methodName; }
|
|
36 |
| |
|
37 |
56
| public void setReturnValueAsList(boolean pvReturnValueAsList) { returnValueAsList = pvReturnValueAsList; }
|
|
38 |
57
| public boolean getReturnValueAsList() { return returnValueAsList; }
|
|
39 |
| |
|
40 |
56
| public void setConvertResult2XmlRpcExceptionAndThrow(boolean pvConvertResult2XmlRpcException) {
|
|
41 |
56
| convertResult2XmlRpcExceptionAndThrow = pvConvertResult2XmlRpcException;
|
|
42 |
| } |
|
43 |
4
| public boolean getConvertResult2XmlRpcExceptionAndThrow() {
|
|
44 |
4
| return convertResult2XmlRpcExceptionAndThrow;
|
|
45 |
| } |
|
46 |
| |
|
47 |
58
| public Object parse(final String pvXmlRpcString) throws XmlRpcException {
|
|
48 |
58
| Object lvReturn = null;
|
|
49 |
58
| boolean lvIsFault = false;
|
|
50 |
58
| try {
|
|
51 |
58
| if (pvXmlRpcString != null) {
|
|
52 |
57
| SAXParserFactory lvFactory = SAXParserFactory.newInstance();
|
|
53 |
57
| SAXParser lvParser = lvFactory.newSAXParser();
|
|
54 |
57
| XMLReader lvReader = lvParser.getXMLReader();
|
|
55 |
57
| XmlRpcContentHandler lvContentHandler = new XmlRpcContentHandler();
|
|
56 |
57
| lvContentHandler.setReturnValueAsList(getReturnValueAsList());
|
|
57 |
57
| lvReader.setContentHandler(lvContentHandler);
|
|
58 |
| |
|
59 |
57
| ByteArrayInputStream lvArrayInputStream = new ByteArrayInputStream(pvXmlRpcString.getBytes());
|
|
60 |
57
| lvReader.parse(new InputSource(lvArrayInputStream));
|
|
61 |
51
| lvReturn = lvContentHandler.getResults();
|
|
62 |
51
| methodName = lvContentHandler.getMethodName();
|
|
63 |
51
| lvIsFault = lvContentHandler.isFault();
|
|
64 |
| } |
|
65 |
| } catch (SAXParseException e) { |
|
66 |
1
| throw new XmlRpcException(e.getMessage(), e);
|
|
67 |
| } catch (Exception e) { |
|
68 |
5
| throw new XmlRpcException("Exception by parse XML-RPC-String: " + pvXmlRpcString, e);
|
|
69 |
| } |
|
70 |
| |
|
71 |
52
| if (lvIsFault == true && getConvertResult2XmlRpcExceptionAndThrow()) {
|
|
72 |
2
| Map lvMap = (Map) lvReturn;
|
|
73 |
2
| Object lvFaultCode = lvMap.get("faultCode");
|
|
74 |
2
| Object lvMessage = lvMap.get("faultString");
|
|
75 |
2
| throw new XmlRpcException(lvFaultCode + ": " + lvMessage);
|
|
76 |
| } |
|
77 |
50
| return lvReturn;
|
|
78 |
| |
|
79 |
| } |
|
80 |
| } |