View Javadoc

1   /*
2    * Copyright (c) 2004 UNINETT FAS
3    *
4    * This program is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU General Public License as published by the Free
6    * Software Foundation; either version 2 of the License, or (at your option)
7    * any later version.
8    *
9    * This program is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12   * more details.
13   *
14   * You should have received a copy of the GNU General Public License along with
15   * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16   * Place - Suite 330, Boston, MA 02111-1307, USA.
17   *
18   * $Id: BackendStatusHandler.java,v 1.2 2005/02/01 12:08:02 indal Exp $
19   */
20  
21  package no.feide.moria.servlet;
22  
23  import java.util.HashMap;
24  import org.xml.sax.helpers.DefaultHandler;
25  import org.xml.sax.Attributes;
26  import org.xml.sax.SAXException;
27  
28  /***
29   * @author Eva Indal
30   * @version $Revision: 1.2 $
31   *
32   * The BackendStatusHandler class extends the xml DefaultHandler
33   * to read the status xml file.
34   */
35  public class BackendStatusHandler extends DefaultHandler {
36  
37      /*** Hash map of parsed users. */
38      private HashMap bsdata;
39  
40      /*** The user being parsed. */
41      private BackendStatusUser currentuser;
42  
43      /*** String value of current attribute. */
44      private String currentchars;
45  
46      /***
47       * Constructor.
48       *
49       */
50      public BackendStatusHandler()  {
51          currentuser = null;
52          bsdata = new HashMap();
53      }
54      
55      
56      /***
57       * Implements callback that is called at start of document. Empty for now.
58       *
59       * @throws SAXException
60       *          Required by interface.
61       */
62      public void startDocument() throws SAXException {
63      }
64      
65      /***
66       * Implements callback that is called at end of document. Empty for now.
67       *
68       * @throws SAXException
69       *          Required by interface.
70       */
71      public void endDocument() throws SAXException {
72      }
73      
74      public final void startElement(final String namespaceURI, final String sName, final String qName, final Attributes attrs)
75      throws SAXException {
76      String eName = sName;
77      if (eName.equals("")) eName = qName;
78  
79      /* Look for <user> and allocate a new BackendStatusUser if found */
80      if (eName.equals("User")) {
81          currentuser = new BackendStatusUser();
82          currentchars = null;
83      } else if (currentuser != null) {
84          currentchars = "";
85      }
86  }
87  
88      /***
89       * Implements callback that is called at end of an xml element.
90       *
91       * @param namespaceURI  Namespace URI.
92       * @param sName  The local name (without prefix), or the empty string if Namespace processing is not being performed.
93       * @param qName  The qualified XML 1.0 name (with prefix), or the empty string if qualified names are not available.
94       * @throws SAXException
95       *          Required by interface.
96       *
97       * @see org.xml.sax.helpers.DefaultHandler#endElement
98       *          for information about the parameters
99       */
100     public final void endElement(final String namespaceURI, final String sName, final String qName) throws SAXException {
101         String eName = sName;
102         if (eName.equals("")) eName = qName;
103 
104         /* Wait for </user> */
105         if (eName.equals("User")) {
106             bsdata.put(currentuser.getName(), currentuser);
107             currentuser = null;
108         } else if (currentuser != null ) {
109             if (eName.equals("Name")) {
110                 currentuser.setName(currentchars);
111             }
112             else if (eName.equals("Password")) {
113                 currentuser.setPassword(currentchars);
114             }
115            else if (eName.equals("Organization")) {
116                currentuser.setOrganization(currentchars);
117            }
118            else if (eName.equals("Contact")) {
119                currentuser.setContact(currentchars);
120            }
121         }
122     }
123     
124     /***
125      * Implements callback that is called to process data for an element.
126      *
127      * @param buf  The characters.
128      * @param offset  The start position in the character array.
129      * @param len  The number of characters to use from the character array.
130      * @throws SAXException
131      *          Required by interface.
132      *
133      * @see org.xml.sax.helpers.DefaultHandler#characters
134      *          for information about the parameters
135      */
136     public final void characters(final char[] buf, final int offset, final int len) throws SAXException {
137         String s = new String(buf, offset, len);
138         currentchars += s;
139     }
140 
141     /***
142      * Gets parsed attributes.
143      *
144      * Each element in the returned HashMap is an AttribsData instance
145      * @return The parsed attributes.
146      */
147     public final HashMap getAttribs() {
148         return bsdata;
149     }
150 }