1 21 22 package com.rift.coad.daemon.messageservice.message.rpc; 24 25 import java.io.ByteArrayInputStream ; 27 import java.io.Serializable ; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 import javax.xml.parsers.SAXParserFactory ; 31 import javax.xml.parsers.SAXParser ; 32 import org.xml.sax.InputSource ; 33 import org.xml.sax.helpers.DefaultHandler ; 34 import org.xml.sax.SAXException ; 35 import org.xml.sax.Attributes ; 36 37 38 39 44 public class RPCXMLParser implements Serializable { 45 46 49 50 51 54 public class RPCXMLHandler extends DefaultHandler implements Serializable { 55 private final static String METHOD = "method"; 57 private final static String NAME = "name"; 58 private final static String TYPE = "type"; 59 private final static String ARGUMENT = "argument"; 60 61 private List argumentNameList = new ArrayList (); 63 private List argumentTypeList = new ArrayList (); 64 private boolean inMethod = false; 65 66 69 public RPCXMLHandler() { 70 71 } 72 73 74 83 public void startElement(String uri, String localName, String qName, 84 Attributes attributes) throws SAXException { 85 try { 86 System.out.println(qName); 87 if (qName.compareToIgnoreCase(METHOD) == 0) { 89 method = attributes.getValue(NAME); 90 if (isPrimitive(attributes.getValue(TYPE))) { 91 returnType = getPrimitive(attributes.getValue(TYPE)); 92 } else { 93 try { 94 returnType = Class.forName( 95 attributes.getValue(TYPE)); 96 } catch (Exception ex) { 97 returnType = Thread.currentThread(). 98 getContextClassLoader().loadClass( 99 attributes.getValue(TYPE)); 100 } 101 } 102 inMethod = true; 103 } else if (inMethod && 104 qName.compareToIgnoreCase(ARGUMENT) == 0) { 105 argumentNameList.add(attributes.getValue(NAME)); 106 argumentTypeList.add(attributes.getValue(TYPE)); 107 } 108 } catch (Exception ex) { 109 throw new SAXException ("Failed to parse the element : " + 110 ex.getMessage(),ex); 111 } 112 } 113 114 115 123 public void endElement(String uri, String localName, String qName) 124 throws SAXException { 125 try { 126 if (qName.compareToIgnoreCase(METHOD) == 0) { 128 inMethod = false; 129 argumentTypes = new Class [argumentTypeList.size()]; 130 argumentNames = new String [argumentNameList.size()]; 131 for (int index = 0; index < argumentTypeList.size(); 132 index++) { 133 String name = (String )argumentTypeList.get(index); 134 if (isPrimitive(name)) { 135 argumentTypes[index] = getPrimitive(name); 136 } else { 137 try { 138 argumentTypes[index] = Class.forName(name); 139 } catch (Exception ex) { 140 argumentTypes[index] = Thread.currentThread(). 141 getContextClassLoader().loadClass(name); 142 } 143 } 144 argumentNames[index] = 145 (String )argumentNameList.get(index); 146 } 147 } 148 } catch (Exception ex) { 149 throw new SAXException ("Failed to parse the element : " + 150 ex.getMessage(),ex); 151 } 152 } 153 154 160 private boolean isPrimitive(String name) { 161 if (name.equals("byte") || name.equals("short") || 162 name.equals("int") || name.equals("long") || 163 name.equals("float") || name.equals("double") || 164 name.equals("boolean") || name.equals("char") || 165 name.equals("void")) { 166 return true; 167 } 168 return false; 169 } 170 171 178 private Class getPrimitive(String name) throws SAXException { 179 if (name.equals("byte")) { 180 return byte.class; 181 } else if (name.equals("short")) { 182 return short.class; 183 } else if (name.equals("int")) { 184 return int.class; 185 } else if (name.equals("long")) { 186 return long.class; 187 } else if (name.equals("float")) { 188 return float.class; 189 } else if (name.equals("double")) { 190 return double.class; 191 } else if (name.equals("boolean")) { 192 return boolean.class; 193 } else if (name.equals("char")) { 194 return char.class; 195 } else if (name.equals("void")) { 196 return void.class; 197 } 198 throw new SAXException ("Unrecognised basic type : " + name); 199 } 200 } 201 202 203 private String rpcXML = null; 205 private Object returnType = null; 206 private String method = null; 207 private Class [] argumentTypes = null; 208 private String [] argumentNames = null; 209 210 211 217 public RPCXMLParser(String rpcXML) throws RPCXMLException { 218 this.rpcXML = rpcXML; 219 try { 220 RPCXMLHandler handler = new RPCXMLHandler(); 221 SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); 222 InputSource source = new InputSource ( 223 new ByteArrayInputStream (rpcXML.getBytes())); 224 parser.parse(source,handler); 225 } catch (Exception ex) { 226 throw new RPCXMLException("Failed to parse the xml : " + 227 ex.getMessage(),ex); 228 } 229 } 230 231 232 237 public String getRPCXML() { 238 return rpcXML; 239 } 240 241 242 248 public Object getReturnType() { 249 return returnType; 250 } 251 252 253 258 public String getMethodName() { 259 return method; 260 } 261 262 263 268 public Class [] getArgumentTypes() { 269 return argumentTypes; 270 } 271 272 273 278 private String [] argumentNames() { 279 return argumentNames; 280 } 281 } 282 | Popular Tags |