KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > xml > HashtableContentHandler


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Core License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: HashtableContentHandler.java,v 1.3 2002/09/18 06:54:17 per_nyfelt Exp $
8

9 package org.ozoneDB.core.xml;
10
11 import org.xml.sax.Attributes JavaDoc;
12 import org.xml.sax.Locator JavaDoc;
13
14 import java.util.Hashtable JavaDoc;
15 import java.util.Stack JavaDoc;
16
17 /**
18  * This class handles a special part of the XML and transform it into an Hashtable.
19  *
20  * @version $Revision: 1.3 $
21  * @author <a HREF="http://www.softwarebuero.de">SMB</a>
22  */

23 public class HashtableContentHandler extends XML2ObjectContentHandler {
24
25     //
26
// member
27
//
28

29     /**
30      */

31     protected Stack JavaDoc hashEndStack;
32
33     /**
34      */

35     private int memberStartStackSize = 0;
36
37     /**
38      * Mode contains the special mode (NOMODE/KEYMODE/VALUEMODE/NEXTMODE).
39      */

40     private int mode = NOMODE;
41
42     final static int NOMODE = 0;
43     final static int KEYMODE = 1;
44     final static int VALUEMODE = 2;
45     final static int NEXTMODE = 3;
46
47     //
48
// construcor
49
//
50

51     /**
52      */

53     public HashtableContentHandler() {
54     }
55
56     /**
57      * @param locator
58      * @param hash
59      */

60     public HashtableContentHandler(Locator JavaDoc locator, Hashtable JavaDoc hash) {
61         super();
62
63         stack.push(hash);
64
65         setDocumentLocator (locator);
66
67         hashEndStack = new Stack JavaDoc();
68         hashEndStack.push("START");
69
70     }
71
72     /**
73      * The method memberStartElement set the mode on KEYMODE, VALUEMODE or NEXTMODE
74      * if the member has the attribute name with the value "key", "value" or "next".
75      * And only if mode is on KEYMODE or VALUEMODE it refers to handleMemberStartElement.
76      *
77      * @param atts (the attributes)
78      */

79     protected void memberStartElement(Attributes JavaDoc atts) {
80
81         if ((atts.getValue("name").equals("key") ||
82              atts.getValue("name").equals("value") ||
83              atts.getValue("name").equals("next")) && mode != NOMODE) {
84
85             System.out.println("Object Hashtable has member with name key/value/next !");
86         }
87
88         if (atts.getValue("name").equals("key")) {
89             mode = KEYMODE;
90             memberStartStackSize = stack.size();
91         }
92
93         if (atts.getValue("name").equals("value")) {
94             mode = VALUEMODE;
95             memberStartStackSize = stack.size();
96         }
97
98         if (atts.getValue("name").equals("next")) {
99             mode = NEXTMODE;
100             memberStartStackSize = stack.size();
101         }
102
103         if (mode != KEYMODE && mode != VALUEMODE) {
104             stack.push("Member " + atts.getValue("name"));
105             return;
106         }
107
108         handleMemberStartElement(atts);
109     }
110
111     /**
112      * This methode handles the memberEndElement.
113      *
114      */

115     protected void memberEndElement() {
116
117         if (mode == NEXTMODE) {
118             stack.pop(); // Member next
119

120             Object JavaDoc value = stack.pop();
121             Object JavaDoc key = stack.pop();
122
123             Hashtable JavaDoc hash = (Hashtable JavaDoc)stack.elementAt(0);
124             hash.put (key, value);
125
126             stack.remove(0);
127             stack.add(0, hash);
128
129             mode = NOMODE;
130             memberStartStackSize = 0;
131             return;
132         }
133
134         int currentStackSize = stack.size();
135         if ((mode == KEYMODE || mode == VALUEMODE) && (currentStackSize-2) == memberStartStackSize) {
136
137             Object JavaDoc value = stack.pop();
138
139             stack.pop();
140             stack.push(value);
141             mode = NOMODE;
142             return;
143         }
144
145         if ((mode == KEYMODE || mode == VALUEMODE) && (currentStackSize-2) != memberStartStackSize) {
146
147             handleMemberEndElement();
148
149         } else
150             stack.pop();
151     }
152
153     /**
154      * The method valueStartElement refers to handleValueStartElement
155      * if mode is on KEYMODE or VALUEMODE.
156      *
157      * @param atts (the attributes)
158      */

159     protected void valueStartElement(Attributes JavaDoc atts) {
160
161         if (mode != KEYMODE && mode != VALUEMODE) {
162             stack.push("Value");
163             return;
164         }
165
166         handleValueStartElement(atts);
167     }
168
169     /**
170      * The method valueEndElement refers to handleValueEndElement
171      * if mode is on KEYMODE or VALUEMODE.
172      */

173     protected void valueEndElement() {
174         if (mode != KEYMODE && mode != VALUEMODE) {
175             stack.pop();
176             return;
177         }
178
179         handleValueEndElement();
180     }
181
182     /**
183      * The method values refers to handleValues if mode is on KEYMODE or VALUEMODE.
184      *
185      * @param ch (char-array)
186      * @param start (start of the array)
187      * @param end (end of the array)
188      */

189     public void values (char[] ch, int start, int end) {
190         if (mode != KEYMODE && mode != VALUEMODE)
191             return;
192
193         handleValues(ch, start, end);
194     }
195
196     /**
197      * The method valueObjStartElement refers to handleValueObjStartElement
198      * if mode is on KEYMODE or VALUEMODE.
199      *
200      * @param atts (the attributes)
201      */

202     protected void valueObjStartElement(Attributes JavaDoc atts) {
203         if (mode != KEYMODE && mode != VALUEMODE) {
204             stack.push("ValueObj");
205             return;
206         }
207
208         handleValueObjStartElement(atts);
209     }
210
211     /**
212      * The method valueObjEndElement refers to handleValueObjEndElement
213      * if mode is on KEYMODE or VALUEMODE.
214      */

215     protected void valueObjEndElement() {
216         if (mode != KEYMODE && mode != VALUEMODE) {
217             stack.pop();
218             return;
219         }
220
221         handleValueObjEndElement();
222     }
223
224
225     /**
226      * The method valueArrayStartElement refers to handleValueArrayStartElement
227      * if mode is on KEYMODE or VALUEMODE.
228      *
229      * @param atts (the attributes)
230      */

231     protected void valueArrayStartElement(Attributes JavaDoc atts) {
232         if (mode != KEYMODE && mode != VALUEMODE) {
233             stack.push("ValueArray");
234             return;
235         }
236
237         handleValueArrayStartElement(atts);
238     }
239
240     /**
241      * The method valueArrayEndElement refers to handleValueArrayEndElement
242      * if mode is on KEYMODE or VALUEMODE.
243      */

244     protected void valueArrayEndElement() {
245
246         if (mode != KEYMODE && mode != VALUEMODE) {
247             stack.pop();
248             return;
249         }
250
251         handleValueArrayEndElement();
252     }
253
254     /**
255      * This method returns the stack.
256      *
257      * @return Stack
258      */

259     public Stack JavaDoc getStack() {
260         return stack;
261     }
262
263 }
264
Popular Tags