KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > daemon > messageservice > message > rpc > RPCXMLParser


1 /*
2  * MessageQueueClient: The message queue client library
3  * Copyright (C) 2007 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * RPCXMLParser.java
20  */

21
22 // package path
23
package com.rift.coad.daemon.messageservice.message.rpc;
24
25 // java imports
26
import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.Serializable JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30 import javax.xml.parsers.SAXParserFactory JavaDoc;
31 import javax.xml.parsers.SAXParser JavaDoc;
32 import org.xml.sax.InputSource JavaDoc;
33 import org.xml.sax.helpers.DefaultHandler JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35 import org.xml.sax.Attributes JavaDoc;
36
37
38
39 /**
40  * This object is responsible for parsing the RPC xml.
41  *
42  * @author Brett Chaldecott
43  */

44 public class RPCXMLParser implements Serializable JavaDoc {
45     
46     /**
47      *
48      */

49     
50     
51     /**
52      * This method will handle the parsing of the RPC xml.
53      */

54     public class RPCXMLHandler extends DefaultHandler JavaDoc implements Serializable JavaDoc {
55         // class constants
56
private final static String JavaDoc METHOD = "method";
57         private final static String JavaDoc NAME = "name";
58         private final static String JavaDoc TYPE = "type";
59         private final static String JavaDoc ARGUMENT = "argument";
60         
61         // private member variables
62
private List JavaDoc argumentNameList = new ArrayList JavaDoc();
63         private List JavaDoc argumentTypeList = new ArrayList JavaDoc();
64         private boolean inMethod = false;
65         
66         /**
67          * The constructor of the rpc xml handler
68          */

69         public RPCXMLHandler() {
70             
71         }
72         
73         
74         /**
75          * Parse the start of an element
76          *
77          * @param uri The uri of the start element
78          * @param localName The name of local object.
79          * @param qName The quick name
80          * @param attributes The attributes associated with this element.
81          * @exception SAXException
82          */

83         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
84                          Attributes JavaDoc attributes) throws SAXException JavaDoc {
85             try {
86                 System.out.println(qName);
87                 // handle a package and retrieve the value information
88
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 JavaDoc 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 JavaDoc ex) {
109                 throw new SAXException JavaDoc("Failed to parse the element : " +
110                         ex.getMessage(),ex);
111             }
112         }
113         
114         
115         /**
116          * Handle the end of an element
117          *
118          * @param uri The uri of the element
119          * @param localName The local name of the element.
120          * @param qName The quick name of the element
121          * @exception SAXException
122          */

123         public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
124                 throws SAXException JavaDoc {
125             try {
126                 // handle a package and retrieve the value information
127
if (qName.compareToIgnoreCase(METHOD) == 0) {
128                     inMethod = false;
129                     argumentTypes = new Class JavaDoc[argumentTypeList.size()];
130                     argumentNames = new String JavaDoc[argumentNameList.size()];
131                     for (int index = 0; index < argumentTypeList.size();
132                     index++) {
133                         String JavaDoc name = (String JavaDoc)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 JavaDoc ex) {
140                                 argumentTypes[index] = Thread.currentThread().
141                                         getContextClassLoader().loadClass(name);
142                             }
143                         }
144                         argumentNames[index] =
145                                 (String JavaDoc)argumentNameList.get(index);
146                     }
147                 }
148             } catch (Exception JavaDoc ex) {
149                 throw new SAXException JavaDoc("Failed to parse the element : " +
150                         ex.getMessage(),ex);
151             }
152         }
153         
154         /**
155          * This method returns true if the name identifies a primitive.
156          *
157          * @return TRUE if a primitive, FALSE if not.
158          * @param name The name of the primitive.
159          */

160         private boolean isPrimitive(String JavaDoc 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         /**
172          * This method returns the primitive for the name.
173          *
174          * @return The reference to the class definition for the primitive
175          * @param name The name of the primitive.
176          * @exception SAXException
177          */

178         private Class JavaDoc getPrimitive(String JavaDoc name) throws SAXException JavaDoc {
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 JavaDoc("Unrecognised basic type : " + name);
199         }
200     }
201     
202     
203     // the classes private member variables
204
private String JavaDoc rpcXML = null;
205     private Object JavaDoc returnType = null;
206     private String JavaDoc method = null;
207     private Class JavaDoc[] argumentTypes = null;
208     private String JavaDoc[] argumentNames = null;
209     
210     
211     /**
212      * Creates a new instance of RPCXMLParser.
213      *
214      * @param rpcXML The string to parse.
215      * @exception RPCXMLException
216      */

217     public RPCXMLParser(String JavaDoc rpcXML) throws RPCXMLException {
218         this.rpcXML = rpcXML;
219         try {
220             RPCXMLHandler handler = new RPCXMLHandler();
221             SAXParser JavaDoc parser = SAXParserFactory.newInstance().newSAXParser();
222             InputSource JavaDoc source = new InputSource JavaDoc(
223                     new ByteArrayInputStream JavaDoc(rpcXML.getBytes()));
224             parser.parse(source,handler);
225         } catch (Exception JavaDoc ex) {
226             throw new RPCXMLException("Failed to parse the xml : " +
227                     ex.getMessage(),ex);
228         }
229     }
230     
231     
232     /**
233      * This method returns the RPC xml.
234      *
235      * @return The string containing the RPC xml.
236      */

237     public String JavaDoc getRPCXML() {
238         return rpcXML;
239     }
240     
241     
242     /**
243      * This method returns the return type of the rpc call.
244      *
245      * @return The object containing the return type.
246      * @exception RPCXMLException
247      */

248     public Object JavaDoc getReturnType() {
249         return returnType;
250     }
251     
252     
253     /**
254      * This method returns the name of the method that the XML defines.
255      *
256      * @return The name of this method.
257      */

258     public String JavaDoc getMethodName() {
259         return method;
260     }
261     
262     
263     /**
264      * This method returns the list of argument types.
265      *
266      * @return The array of argument types.
267      */

268     public Class JavaDoc[] getArgumentTypes() {
269         return argumentTypes;
270     }
271     
272     
273     /**
274      * This method returns the list of argument names
275      *
276      * @return The array of argument names.
277      */

278     private String JavaDoc[] argumentNames() {
279         return argumentNames;
280     }
281 }
282
Popular Tags