00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 package org.apache.xmlrpc.parser;
00020
00021 import java.util.ArrayList;
00022 import java.util.List;
00023
00024 import javax.xml.namespace.QName;
00025
00026 import org.apache.ws.commons.util.NamespaceContextImpl;
00027 import org.apache.xmlrpc.common.TypeFactory;
00028 import org.apache.xmlrpc.common.XmlRpcStreamConfig;
00029 import org.xml.sax.Attributes;
00030 import org.xml.sax.SAXException;
00031 import org.xml.sax.SAXParseException;
00032
00033
00037 public class XmlRpcRequestParser extends RecursiveTypeParserImpl {
00038 private int level;
00039 private boolean inMethodName;
00040 private String methodName;
00041 private List params;
00042
00047 public XmlRpcRequestParser(XmlRpcStreamConfig pConfig, TypeFactory pTypeFactory) {
00048 super(pConfig, new NamespaceContextImpl(), pTypeFactory);
00049 }
00050
00051 protected void addResult(Object pResult) {
00052 params.add(pResult);
00053 }
00054
00055 public void startDocument() throws SAXException {
00056 super.startDocument();
00057 level = 0;
00058 inMethodName = false;
00059 methodName = null;
00060 params = null;
00061 }
00062
00063
00064 public void characters(char[] pChars, int pOffset, int pLength) throws SAXException {
00065 if (inMethodName) {
00066 String s = new String(pChars, pOffset, pLength);
00067 methodName = methodName == null ? s : methodName + s;
00068 } else {
00069 super.characters(pChars, pOffset, pLength);
00070 }
00071 }
00072
00073 public void startElement(String pURI, String pLocalName, String pQName,
00074 Attributes pAttrs) throws SAXException {
00075 switch (level++) {
00076 case 0:
00077 if (!"".equals(pURI) || !"methodCall".equals(pLocalName)) {
00078 throw new SAXParseException("Expected root element 'methodCall', got "
00079 + new QName(pURI, pLocalName),
00080 getDocumentLocator());
00081 }
00082 break;
00083 case 1:
00084 if (methodName == null) {
00085 if ("".equals(pURI) && "methodName".equals(pLocalName)) {
00086 inMethodName = true;
00087 } else {
00088 throw new SAXParseException("Expected methodName element, got "
00089 + new QName(pURI, pLocalName),
00090 getDocumentLocator());
00091 }
00092 } else if (params == null) {
00093 if ("".equals(pURI) && "params".equals(pLocalName)) {
00094 params = new ArrayList();
00095 } else {
00096 throw new SAXParseException("Expected params element, got "
00097 + new QName(pURI, pLocalName),
00098 getDocumentLocator());
00099 }
00100 } else {
00101 throw new SAXParseException("Expected /methodCall, got "
00102 + new QName(pURI, pLocalName),
00103 getDocumentLocator());
00104 }
00105 break;
00106 case 2:
00107 if (!"".equals(pURI) || !"param".equals(pLocalName)) {
00108 throw new SAXParseException("Expected param element, got "
00109 + new QName(pURI, pLocalName),
00110 getDocumentLocator());
00111 }
00112 break;
00113 case 3:
00114 if (!"".equals(pURI) || !"value".equals(pLocalName)) {
00115 throw new SAXParseException("Expected value element, got "
00116 + new QName(pURI, pLocalName),
00117 getDocumentLocator());
00118 }
00119 startValueTag();
00120 break;
00121 default:
00122 super.startElement(pURI, pLocalName, pQName, pAttrs);
00123 break;
00124 }
00125 }
00126
00127 public void endElement(String pURI, String pLocalName, String pQName) throws SAXException {
00128 switch(--level) {
00129 case 0:
00130 break;
00131 case 1:
00132 if (inMethodName) {
00133 if ("".equals(pURI) && "methodName".equals(pLocalName)) {
00134 if (methodName == null) {
00135 methodName = "";
00136 }
00137 } else {
00138 throw new SAXParseException("Expected /methodName, got "
00139 + new QName(pURI, pLocalName),
00140 getDocumentLocator());
00141 }
00142 inMethodName = false;
00143 } else if (!"".equals(pURI) || !"params".equals(pLocalName)) {
00144 throw new SAXParseException("Expected /params, got "
00145 + new QName(pURI, pLocalName),
00146 getDocumentLocator());
00147 }
00148 break;
00149 case 2:
00150 if (!"".equals(pURI) || !"param".equals(pLocalName)) {
00151 throw new SAXParseException("Expected /param, got "
00152 + new QName(pURI, pLocalName),
00153 getDocumentLocator());
00154 }
00155 break;
00156 case 3:
00157 if (!"".equals(pURI) || !"value".equals(pLocalName)) {
00158 throw new SAXParseException("Expected /value, got "
00159 + new QName(pURI, pLocalName),
00160 getDocumentLocator());
00161 }
00162 endValueTag();
00163 break;
00164 default:
00165 super.endElement(pURI, pLocalName, pQName);
00166 break;
00167 }
00168 }
00169
00173 public String getMethodName() { return methodName; }
00177 public List getParams() { return params; }
00178 }