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: StatisticsHandler.java,v 1.3 2005/10/21 10:33:08 indal Exp $
19   */
20  package no.feide.moria.servlet;
21  
22  import org.xml.sax.helpers.DefaultHandler;
23  import org.xml.sax.Attributes;
24  import org.xml.sax.SAXException;
25  import java.util.Vector;
26  
27  
28  /***
29   * @author Eva Indal
30   * @version $Revision: 1.3 $
31   *
32   */
33  public class StatisticsHandler extends DefaultHandler {
34      
35      private String currentchars = null;
36      private String statname = "";
37      private Vector ignorevector = new Vector();
38      private StatisticsCollection accumstats = new StatisticsCollection("");
39      private Vector orgstats = new Vector();
40      
41      /***
42       * Constructor
43       *
44       */
45      public StatisticsHandler()  {
46      }
47      
48      public StatisticsCollection getAccumStatistics() {
49          return this.accumstats;
50      }
51      public int getNumStatisticsCollections() {
52          return this.orgstats.size();
53      }
54      public StatisticsCollection getStatisticsCollection(final int idx) {
55          return (StatisticsCollection) this.orgstats.get(idx);
56      }
57      
58      public void addIgnoreService(final String servicename) {
59          this.ignorevector.add(servicename);
60      }
61      
62      private boolean shouldIgnore(final String servicename) {
63          for (int i = 0; i < this.ignorevector.size(); i++) {
64              String s = (String) this.ignorevector.get(i);
65              if (s.equals(servicename)) return true;
66          }
67          return false;
68      }
69      
70      /***
71       * Implements callback that is called at start of document. Empty for now.
72       *
73       * @throws SAXException
74       *          Required by interface.
75       */
76      public void startDocument() throws SAXException {
77      }
78      
79      
80      
81      /***
82       * Implements callback that is called at end of document. Empty for now.
83       *
84       * @throws SAXException
85       *          Required by interface.
86       */
87      public void endDocument() throws SAXException {
88      }
89      
90      /***
91       * 
92       * @param namespaceURI
93       * @param sName
94       * @param qName
95       * @param attrs
96       * @throws SAXException
97       */
98      public final void startElement(final String namespaceURI, final String sName, final String qName, final Attributes attrs)
99      throws SAXException {
100         String eName = sName;
101         if (eName.equals("")) eName = qName;
102 
103         /* Look for <Service> and allocate a new StatisticsData if found */
104         if (eName.equals("Service")) {
105             this.statname = "";
106         }
107         else if (eName.equals("Name")) {
108             currentchars = "";
109         }
110         else if (eName.equals("Month")) {
111             if (!this.shouldIgnore(this.statname)) {
112                 final String monthname = attrs.getValue(new String("name"));
113                 final String countstring = attrs.getValue(new String("count"));
114                 String orgname = attrs.getValue(new String("org"));
115                 
116                 // in case an old statistics.xml file is parsed
117                 if (orgname == null) orgname = "Unknown Organization";
118                 
119                 if (monthname != null && countstring != null) {
120                     try {
121                         Integer tmp = new Integer(countstring);
122                         this.accumstats.addMonth(this.statname, monthname, tmp.intValue());
123                         this.findStatisticsCollection(orgname).addMonth(this.statname,
124                                                       monthname,
125                                                       tmp.intValue());
126                     } 
127                     catch (NumberFormatException e) {
128                         // something is wrong in the xml file
129                     }
130                 }
131             }
132         }
133     }
134     
135     /***
136      * Implements callback that is called at end of an xml element.
137      *
138      * @param namespaceURI  Namespace URI.
139      * @param sName  The local name (without prefix), or the empty string if Namespace processing is not being performed.
140      * @param qName  The qualified XML 1.0 name (with prefix), or the empty string if qualified names are not available.
141      * @throws SAXException
142      *          Required by interface.
143      *
144      * @see org.xml.sax.helpers.DefaultHandler#endElement
145      *          for information about the parameters
146      */
147     public final void endElement(final String namespaceURI, final String sName, final String qName) throws SAXException {
148         String eName = sName;
149         if (eName.equals("")) eName = qName;
150         
151         if (eName.equals("Service")) {
152         }
153         else if (eName.equals("Name")) {
154             this.statname = currentchars;
155             currentchars = null;
156         }
157     }
158     public final void characters(final char[] buf, final int offset, final int len) throws SAXException {
159         if (currentchars != null) {
160             String s = new String(buf, offset, len);
161             currentchars += s;
162         }
163     }
164     private StatisticsCollection findStatisticsCollection(final String orgname) {
165         for (int i = 0; i < this.orgstats.size(); i++) {
166             StatisticsCollection col = (StatisticsCollection) this.orgstats.get(i);
167             if (col.getOrgName().equals(orgname)) return col;
168         }
169         StatisticsCollection col = new StatisticsCollection(orgname);
170         this.orgstats.add(col);
171         return col;
172     }
173 
174     
175     
176     
177 
178 }