View Javadoc

1   /* ***** BEGIN LICENSE BLOCK *****
2    * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3    *
4    * The contents of this file are subject to the Mozilla Public License Version
5    * 1.1 (the "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis,
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11   * for the specific language governing rights and limitations under the
12   * License.
13   *
14   * The Original Code is "SMS Library for the Java platform".
15   *
16   * The Initial Developer of the Original Code is Markus Eriksson.
17   * Portions created by the Initial Developer are Copyright (C) 2002
18   * the Initial Developer. All Rights Reserved.
19   *
20   * Contributor(s):
21   *
22   * Alternatively, the contents of this file may be used under the terms of
23   * either the GNU General Public License Version 2 or later (the "GPL"), or
24   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25   * in which case the provisions of the GPL or the LGPL are applicable instead
26   * of those above. If you wish to allow use of your version of this file only
27   * under the terms of either the GPL or the LGPL, and not to allow others to
28   * use your version of this file under the terms of the MPL, indicate your
29   * decision by deleting the provisions above and replace them with the notice
30   * and other provisions required by the GPL or the LGPL. If you do not delete
31   * the provisions above, a recipient may use your version of this file under
32   * the terms of any one of the MPL, the GPL or the LGPL.
33   *
34   * ***** END LICENSE BLOCK ***** */
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              // Build DOM
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         // return null if the string is empty
158         return (strBuff.length() > 0)?strBuff.toString():null;
159     }   
160 }