KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MyCounter


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: MyCounter.java,v 1.7 2004/02/17 19:14:20 minchau Exp $
18  */

19 import java.util.Hashtable JavaDoc;
20
21 public class MyCounter {
22   static Hashtable JavaDoc counters = new Hashtable JavaDoc ();
23
24
25   public void init(org.apache.xalan.extensions.XSLProcessorContext context,
26                    org.w3c.dom.Element JavaDoc elem)
27   {
28     String JavaDoc name = elem.getAttribute("name");
29     String JavaDoc value = elem.getAttribute("value");
30     int val;
31     try
32     {
33       val = Integer.parseInt (value);
34     }
35     catch (NumberFormatException JavaDoc e)
36     {
37       e.printStackTrace ();
38       val = 0;
39     }
40     counters.put (name, new Integer JavaDoc (val));
41   }
42
43   public int read(String JavaDoc name)
44   {
45     Integer JavaDoc cval = (Integer JavaDoc)counters.get(name);
46     return (cval == null) ? 0 : cval.intValue();
47   }
48
49   public void incr(org.apache.xalan.extensions.XSLProcessorContext context,
50                    org.w3c.dom.Element JavaDoc elem) {
51     String JavaDoc name = elem.getAttribute("name");
52     Integer JavaDoc cval = (Integer JavaDoc) counters.get(name);
53     int nval = (cval == null) ? 0 : (cval.intValue () + 1);
54     counters.put (name, new Integer JavaDoc (nval));
55   }
56 }
57
Popular Tags