1 6 7 package com.quikj.application.web.talk.feature.proactive.messaging; 8 9 import javax.xml.parsers.*; 11 import org.xml.sax.*; 12 import org.w3c.dom.*; 13 14 import net.n3.nanoxml.*; 16 17 import java.io.*; 18 19 import com.quikj.application.web.talk.messaging.*; 20 24 public class ProactiveMessageParser 25 { 26 private static int parserType = TalkMessageParser.DOM_PARSER; 27 private String errorMessage = ""; 28 private XMLParserDOM domParser = null; 29 private XMLParserNANO nanoParser = null; 30 private ProactiveMessageInterface message = null; 31 32 public ProactiveMessageParser(DocumentBuilder builder) 34 throws AceMessageException 35 { 36 if (TalkMessageParser.getParserType() != TalkMessageParser.DOM_PARSER) 37 { 38 throw new AceMessageException("You can invoke this constructor only for a DOM parser"); 39 } 40 41 parserType = TalkMessageParser.DOM_PARSER; 42 domParser = new XMLParserDOM(builder); 43 } 44 45 public ProactiveMessageParser() 47 throws ClassNotFoundException , InstantiationException , IllegalAccessException , AceMessageException 48 { 49 if (TalkMessageParser.getParserType() != TalkMessageParser.NANOXML_PARSER) 50 { 51 throw new AceMessageException("You can invoke this constructor only for a NANOXML parser"); 52 } 53 54 nanoParser = new XMLParserNANO(); 55 parserType = TalkMessageParser.NANOXML_PARSER; 56 } 57 58 62 public java.lang.String getErrorMessage() 63 { 64 return errorMessage; 65 } 66 67 71 public void setErrorMessage(java.lang.String errorMessage) 72 { 73 this.errorMessage = errorMessage; 74 } 75 76 public boolean parse(String body) 77 { 78 message = null; 79 80 Object root_element = null; 81 String node_name = null; 82 83 if (parserType == TalkMessageParser.DOM_PARSER) 84 { 85 if (domParser.parse(body) == false) 86 { 87 return false; 88 } 89 90 root_element = domParser.getRootElement(); 91 node_name = domParser.getNodeName(); 92 } 93 else { 95 if (nanoParser.parse(body) == false) 96 { 97 return false; 98 } 99 100 root_element = nanoParser.getRootElement(); 101 node_name = nanoParser.getNodeName(); 102 } 103 104 if (node_name.equals("proactive-notification") == true) 105 { 106 ProactiveNotificationMessage m = new ProactiveNotificationMessage(); 107 if (m.parse(root_element) == false) 108 { 109 errorMessage = m.getErrorMessage(); 110 return false; 111 } 112 113 message = m; 114 } 115 else if (node_name.equals("call-request") == true) 116 { 117 CallRequestMessage m = new CallRequestMessage(); 118 if (m.parse(root_element) == false) 119 { 120 errorMessage = m.getErrorMessage(); 121 return false; 122 } 123 124 message = m; 125 } 126 else if (node_name.equals("call-response") == true) 127 { 128 CallResponseMessage m = new CallResponseMessage(); 129 if (m.parse(root_element) == false) 130 { 131 errorMessage = m.getErrorMessage(); 132 return false; 133 } 134 135 message = m; 136 } 137 else if (node_name.equals("info-request") == true) 138 { 139 InformationRequestMessage m = new InformationRequestMessage(); 140 if (m.parse(root_element) == false) 141 { 142 errorMessage = m.getErrorMessage(); 143 return false; 144 } 145 146 message = m; 147 } 148 else if (node_name.equals("info-response") == true) 149 { 150 InformationResponseMessage m = new InformationResponseMessage(); 151 if (m.parse(root_element) == false) 152 { 153 errorMessage = m.getErrorMessage(); 154 return false; 155 } 156 157 message = m; 158 } 159 161 return true; 162 } 163 164 168 public com.quikj.application.web.talk.feature.proactive.messaging.ProactiveMessageInterface getMessage() 169 { 170 return message; 171 } 172 173 177 public void setMessage(com.quikj.application.web.talk.feature.proactive.messaging.ProactiveMessageInterface message) 178 { 179 this.message = message; 180 } 181 182 class XMLParserDOM 183 { 184 public XMLParserDOM (DocumentBuilder builder) 185 { 186 this.builder = builder; 187 } 188 189 public boolean parse (String body) 190 { 191 try 192 { 193 InputSource source = new InputSource(new StringReader(body)); 194 Document doc = builder.parse(source); 195 196 if (doc.getNodeType() != Node.DOCUMENT_NODE) 197 { 198 errorMessage = "The message does not begin with an Document node"; 200 return false; 201 } 202 203 Node root = null; 204 boolean root_found = false; 205 for (root = doc.getFirstChild(); 206 root != null; 207 root = root.getNextSibling()) 208 { 209 if (root.getNodeType() == Node.ELEMENT_NODE) 210 { 211 root_found = true; 212 break; 213 214 } 215 } 217 218 if (root_found == false) 219 { 220 errorMessage = "Root element not found"; 221 return false; 222 } 223 else 224 { 225 rootElement = root; 226 nodeName = root.getNodeName(); 227 } 228 } 229 catch (SAXException ex) 230 { 231 errorMessage = "Malformatted message: " 232 + ex.getMessage(); 233 return false; 234 } 235 catch (IOException ex) 236 { 237 errorMessage = "IO error: " 238 + ex.getMessage(); 239 return false; 240 } 241 242 return true; 243 } 244 245 248 public java.lang.String getNodeName() 249 { 250 return nodeName; 251 } 252 253 256 public void setNodeName(java.lang.String nodeName) 257 { 258 this.nodeName = nodeName; 259 } 260 261 264 public java.lang.Object getRootElement() 265 { 266 return rootElement; 267 } 268 269 272 public void setRootElement(java.lang.Object rootElement) 273 { 274 this.rootElement = rootElement; 275 } 276 277 private String nodeName = null; 278 private Object rootElement = null; 279 private DocumentBuilder builder = null; 280 } 281 282 class XMLParserNANO 283 { 284 public XMLParserNANO() 285 throws ClassNotFoundException , InstantiationException , IllegalAccessException 286 { 287 nanoXMLParser = XMLParserFactory.createXMLParser("net.n3.nanoxml.StdXMLParser", 288 new StdXMLBuilder()); 289 } 290 291 public boolean parse (String body) 292 { 293 try 294 { 295 nanoXMLParser.setReader(StdXMLReader.stringReader(body)); 296 IXMLElement element = (IXMLElement)nanoXMLParser.parse(); 297 298 rootElement = element; 300 nodeName = element.getFullName(); 301 } 302 catch (XMLException ex) 303 { 304 errorMessage = "Malformatted message: " 305 + ex.getMessage(); 306 return false; 307 } 308 309 return true; 310 } 311 312 315 public java.lang.String getNodeName() 316 { 317 return nodeName; 318 } 319 320 323 public void setNodeName(java.lang.String nodeName) 324 { 325 this.nodeName = nodeName; 326 } 327 328 331 public java.lang.Object getRootElement() 332 { 333 return rootElement; 334 } 335 336 339 public void setRootElement(java.lang.Object rootElement) 340 { 341 this.rootElement = rootElement; 342 } 343 344 private String nodeName = null; 345 private Object rootElement = null; 346 private IXMLParser nanoXMLParser = null; 347 } 348 349 } 350 | Popular Tags |