1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.marre.sms.transport.pswincom;
36
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.io.InputStreamReader;
40 import java.io.Reader;
41
42 import javax.xml.parsers.DocumentBuilder;
43 import javax.xml.parsers.DocumentBuilderFactory;
44 import javax.xml.parsers.ParserConfigurationException;
45
46 import org.marre.sms.SmsException;
47 import org.w3c.dom.Document;
48 import org.w3c.dom.Node;
49 import org.w3c.dom.NodeList;
50 import org.w3c.dom.Text;
51 import org.xml.sax.InputSource;
52 import org.xml.sax.SAXException;
53
54 /***
55 * Parses the response from PsWin.
56 *
57 * Uses a DOM parser internally. Currently only looking at the LOGIN result.
58 *
59 * TODO: Rewrite to use SAX instead. We aren't really intrested in the tree structure...
60 *
61 * @author Markus
62 * @version $Id: PsWinXmlResponseParser.java,v 1.2 2005/11/26 16:24:23 c95men Exp $
63 */
64 public class PsWinXmlResponseParser
65 {
66 protected InputStream xmlInputStream_;
67 protected Document respDoc_;
68
69 /***
70 * Creates the response parser.
71 *
72 * @param xmlInputStream
73 */
74 public PsWinXmlResponseParser(InputStream xmlInputStream)
75 {
76 xmlInputStream_ = xmlInputStream;
77 }
78
79 /***
80 * Parses the response.
81 *
82 * @throws IOException
83 * @throws SmsException
84 */
85 public void parse() throws IOException, SmsException
86 {
87 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
88
89 try
90 {
91 Reader isReader = new InputStreamReader(xmlInputStream_);
92 Reader filterReader = new PsWinXmlCleanupReader(isReader);
93
94
95 DocumentBuilder builder = factory.newDocumentBuilder();
96 respDoc_ = builder.parse( new InputSource(filterReader) );
97 }
98 catch (ParserConfigurationException ex)
99 {
100 throw new SmsException("Something wrong with xml", ex);
101 }
102 catch (SAXException ex)
103 {
104 throw new SmsException("Failed to parse xml response", ex);
105 }
106 }
107
108 /***
109 * Returns the logon result.
110 *
111 * @return
112 */
113 public String getLogon()
114 {
115 NodeList listOfLogons = respDoc_.getElementsByTagName( "LOGON" );
116 Node logon = listOfLogons.item(0);
117 return getText(logon);
118 }
119
120 /***
121 * Returns the reason result.
122 *
123 * @return
124 */
125 public String getReason()
126 {
127 NodeList listOfReasons = respDoc_.getElementsByTagName( "REASON" );
128 Node reason = listOfReasons.item(0);
129 return getText(reason);
130 }
131
132 /***
133 * Returns the text within the given node.
134 *
135 * @param node
136 * @return
137 */
138 protected String getText(Node node)
139 {
140 StringBuffer strBuff = new StringBuffer();
141
142 if (node.hasChildNodes())
143 {
144 Node child = node.getFirstChild();
145 while (child != null)
146 {
147 if (child.getNodeType() == Node.TEXT_NODE)
148 {
149 Text childText = (Text)child;
150 String text = childText.getData();
151 strBuff.append(text);
152 }
153 child = child.getNextSibling();
154 }
155 }
156
157
158 return (strBuff.length() > 0)?strBuff.toString():null;
159 }
160 }