Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
XmlRpcRequestParser |
|
| 5.0;5 |
1 | /* |
|
2 | * Copyright 1999,2005 The Apache Software Foundation. |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | */ |
|
16 | package org.apache.xmlrpc.parser; |
|
17 | ||
18 | import java.util.ArrayList; |
|
19 | import java.util.List; |
|
20 | ||
21 | import javax.xml.namespace.QName; |
|
22 | ||
23 | import org.apache.ws.commons.util.NamespaceContextImpl; |
|
24 | import org.apache.xmlrpc.common.TypeFactory; |
|
25 | import org.apache.xmlrpc.common.XmlRpcStreamConfig; |
|
26 | import org.xml.sax.Attributes; |
|
27 | import org.xml.sax.SAXException; |
|
28 | import org.xml.sax.SAXParseException; |
|
29 | ||
30 | ||
31 | /** A SAX parser for an {@link org.apache.xmlrpc.client.XmlRpcClient}'s |
|
32 | * request. |
|
33 | */ |
|
34 | public class XmlRpcRequestParser extends RecursiveTypeParserImpl { |
|
35 | private int level; |
|
36 | private boolean inMethodName; |
|
37 | private String methodName; |
|
38 | private List params; |
|
39 | ||
40 | /** Creates a new instance, which parses a clients request. |
|
41 | * @param pConfig The client configuration. |
|
42 | * @param pTypeFactory The type factory. |
|
43 | */ |
|
44 | public XmlRpcRequestParser(XmlRpcStreamConfig pConfig, TypeFactory pTypeFactory) { |
|
45 | 280 | super(pConfig, new NamespaceContextImpl(), pTypeFactory); |
46 | 280 | } |
47 | ||
48 | protected void addResult(Object pResult) { |
|
49 | 280 | params.add(pResult); |
50 | 280 | } |
51 | ||
52 | public void startDocument() throws SAXException { |
|
53 | 280 | super.startDocument(); |
54 | 280 | level = 0; |
55 | 280 | inMethodName = false; |
56 | 280 | methodName = null; |
57 | 280 | } |
58 | ||
59 | ||
60 | public void characters(char[] pChars, int pOffset, int pLength) throws SAXException { |
|
61 | 696 | if (inMethodName) { |
62 | 280 | String s = new String(pChars, pOffset, pLength); |
63 | 280 | methodName = methodName == null ? s : methodName + s; |
64 | } else { |
|
65 | 416 | super.characters(pChars, pOffset, pLength); |
66 | } |
|
67 | 696 | } |
68 | ||
69 | public void startElement(String pURI, String pLocalName, String pQName, |
|
70 | Attributes pAttrs) throws SAXException { |
|
71 | 1888 | switch (level++) { |
72 | case 0: |
|
73 | 280 | if (!"".equals(pURI) || !"methodCall".equals(pLocalName)) { |
74 | 0 | throw new SAXParseException("Expected root element 'methodCall', got " |
75 | 0 | + new QName(pURI, pLocalName), |
76 | 0 | getDocumentLocator()); |
77 | } |
|
78 | 0 | break; |
79 | case 1: |
|
80 | 560 | if (methodName == null) { |
81 | 280 | if ("".equals(pURI) && "methodName".equals(pLocalName)) { |
82 | 280 | inMethodName = true; |
83 | } else { |
|
84 | 0 | throw new SAXParseException("Expected methodName element, got " |
85 | 0 | + new QName(pURI, pLocalName), |
86 | 0 | getDocumentLocator()); |
87 | } |
|
88 | 280 | } else if (params == null) { |
89 | 280 | if ("".equals(pURI) && "params".equals(pLocalName)) { |
90 | 280 | params = new ArrayList(); |
91 | } else { |
|
92 | 0 | throw new SAXParseException("Expected params element, got " |
93 | 0 | + new QName(pURI, pLocalName), |
94 | 0 | getDocumentLocator()); |
95 | } |
|
96 | } else { |
|
97 | 0 | throw new SAXParseException("Expected /methodCall, got " |
98 | 0 | + new QName(pURI, pLocalName), |
99 | 0 | getDocumentLocator()); |
100 | } |
|
101 | 0 | break; |
102 | case 2: |
|
103 | 280 | if (!"".equals(pURI) || !"param".equals(pLocalName)) { |
104 | 0 | throw new SAXParseException("Expected param element, got " |
105 | 0 | + new QName(pURI, pLocalName), |
106 | 0 | getDocumentLocator()); |
107 | } |
|
108 | 0 | break; |
109 | case 3: |
|
110 | 280 | if (!"".equals(pURI) || !"value".equals(pLocalName)) { |
111 | 0 | throw new SAXParseException("Expected value element, got " |
112 | 0 | + new QName(pURI, pLocalName), |
113 | 0 | getDocumentLocator()); |
114 | } |
|
115 | 280 | startValueTag(); |
116 | 280 | break; |
117 | default: |
|
118 | 488 | super.startElement(pURI, pLocalName, pQName, pAttrs); |
119 | break; |
|
120 | } |
|
121 | 1888 | } |
122 | ||
123 | public void endElement(String pURI, String pLocalName, String pQName) throws SAXException { |
|
124 | 1888 | switch(--level) { |
125 | case 0: |
|
126 | 280 | break; |
127 | case 1: |
|
128 | 560 | if (inMethodName) { |
129 | 280 | if ("".equals(pURI) && "methodName".equals(pLocalName)) { |
130 | 280 | if (methodName == null) { |
131 | 0 | methodName = ""; |
132 | } |
|
133 | } else { |
|
134 | 0 | throw new SAXParseException("Expected /methodName, got " |
135 | 0 | + new QName(pURI, pLocalName), |
136 | 0 | getDocumentLocator()); |
137 | } |
|
138 | 280 | inMethodName = false; |
139 | 280 | } else if (!"".equals(pURI) || !"params".equals(pLocalName)) { |
140 | 0 | throw new SAXParseException("Expected /params, got " |
141 | 0 | + new QName(pURI, pLocalName), |
142 | 0 | getDocumentLocator()); |
143 | } |
|
144 | 0 | break; |
145 | case 2: |
|
146 | 280 | if (!"".equals(pURI) || !"param".equals(pLocalName)) { |
147 | 0 | throw new SAXParseException("Expected /param, got " |
148 | 0 | + new QName(pURI, pLocalName), |
149 | 0 | getDocumentLocator()); |
150 | } |
|
151 | 0 | break; |
152 | case 3: |
|
153 | 280 | if (!"".equals(pURI) || !"value".equals(pLocalName)) { |
154 | 0 | throw new SAXParseException("Expected /value, got " |
155 | 0 | + new QName(pURI, pLocalName), |
156 | 0 | getDocumentLocator()); |
157 | } |
|
158 | 280 | endValueTag(); |
159 | 280 | break; |
160 | default: |
|
161 | 488 | super.endElement(pURI, pLocalName, pQName); |
162 | break; |
|
163 | } |
|
164 | 1888 | } |
165 | ||
166 | /** Returns the method name being invoked. |
|
167 | * @return Requested method name. |
|
168 | */ |
|
169 | 280 | public String getMethodName() { return methodName; } |
170 | /** Returns the parameter list. |
|
171 | * @return Parameter list. |
|
172 | */ |
|
173 | 280 | public List getParams() { return params; } |
174 | } |