001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Core License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: HashtableContentHandler.java,v 1.1 2001/12/18 10:31:31 per_nyfelt Exp $
008:
009: package org.ozoneDB.core.xml;
010:
011: import java.io.*;
012: import java.util.*;
013: import java.lang.reflect.*;
014:
015: import org.xml.sax.*;
016: import org.apache.xerces.parsers.SAXParser;
017:
018: /**
019: * This class handles a special part of the XML and transform it into an Hashtable.
020: *
021: * @version $Revision: 1.1 $
022: * @author <a href="http://www.softwarebuero.de">SMB</a>
023: */
024: public class HashtableContentHandler extends XML2ObjectContentHandler {
025:
026: //
027: // member
028: //
029:
030: /**
031: */
032: protected Stack hashEndStack;
033:
034: /**
035: */
036: private int memberStartStackSize = 0;
037:
038: /**
039: * Mode contains the special mode (NOMODE/KEYMODE/VALUEMODE/NEXTMODE).
040: */
041: private int mode = NOMODE;
042:
043: final static int NOMODE = 0;
044: final static int KEYMODE = 1;
045: final static int VALUEMODE = 2;
046: final static int NEXTMODE = 3;
047:
048: //
049: // construcor
050: //
051:
052: /**
053: */
054: public HashtableContentHandler() {
055: }
056:
057: /**
058: * @param locator
059: * @param hash
060: */
061: public HashtableContentHandler(Locator locator, Hashtable hash) {
062: super ();
063:
064: stack.push(hash);
065:
066: setDocumentLocator(locator);
067:
068: hashEndStack = new Stack();
069: hashEndStack.push("START");
070:
071: }
072:
073: /**
074: * The method memberStartElement set the mode on KEYMODE, VALUEMODE or NEXTMODE
075: * if the member has the attribute name with the value "key", "value" or "next".
076: * And only if mode is on KEYMODE or VALUEMODE it refers to handleMemberStartElement.
077: *
078: * @param atts (the attributes)
079: */
080: protected void memberStartElement(Attributes atts) {
081:
082: if ((atts.getValue("name").equals("key")
083: || atts.getValue("name").equals("value") || atts
084: .getValue("name").equals("next"))
085: && mode != NOMODE) {
086:
087: System.out
088: .println("Object Hashtable has member with name key/value/next !");
089: }
090:
091: if (atts.getValue("name").equals("key")) {
092: mode = KEYMODE;
093: memberStartStackSize = stack.size();
094: }
095:
096: if (atts.getValue("name").equals("value")) {
097: mode = VALUEMODE;
098: memberStartStackSize = stack.size();
099: }
100:
101: if (atts.getValue("name").equals("next")) {
102: mode = NEXTMODE;
103: memberStartStackSize = stack.size();
104: }
105:
106: if (mode != KEYMODE && mode != VALUEMODE) {
107: stack.push("Member " + atts.getValue("name"));
108: return;
109: }
110:
111: handleMemberStartElement(atts);
112: }
113:
114: /**
115: * This methode handles the memberEndElement.
116: *
117: */
118: protected void memberEndElement() {
119:
120: if (mode == NEXTMODE) {
121: stack.pop(); // Member next
122:
123: Object value = stack.pop();
124: Object key = stack.pop();
125:
126: Hashtable hash = (Hashtable) stack.elementAt(0);
127: hash.put(key, value);
128:
129: stack.remove(0);
130: stack.add(0, hash);
131:
132: mode = NOMODE;
133: memberStartStackSize = 0;
134: return;
135: }
136:
137: int currentStackSize = stack.size();
138: if ((mode == KEYMODE || mode == VALUEMODE)
139: && (currentStackSize - 2) == memberStartStackSize) {
140:
141: Object value = stack.pop();
142:
143: stack.pop();
144: stack.push(value);
145: mode = NOMODE;
146: return;
147: }
148:
149: if ((mode == KEYMODE || mode == VALUEMODE)
150: && (currentStackSize - 2) != memberStartStackSize) {
151:
152: handleMemberEndElement();
153:
154: } else
155: stack.pop();
156: }
157:
158: /**
159: * The method valueStartElement refers to handleValueStartElement
160: * if mode is on KEYMODE or VALUEMODE.
161: *
162: * @param atts (the attributes)
163: */
164: protected void valueStartElement(Attributes atts) {
165:
166: if (mode != KEYMODE && mode != VALUEMODE) {
167: stack.push("Value");
168: return;
169: }
170:
171: handleValueStartElement(atts);
172: }
173:
174: /**
175: * The method valueEndElement refers to handleValueEndElement
176: * if mode is on KEYMODE or VALUEMODE.
177: */
178: protected void valueEndElement() {
179: if (mode != KEYMODE && mode != VALUEMODE) {
180: stack.pop();
181: return;
182: }
183:
184: handleValueEndElement();
185: }
186:
187: /**
188: * The method values refers to handleValues if mode is on KEYMODE or VALUEMODE.
189: *
190: * @param ch (char-array)
191: * @param start (start of the array)
192: * @param end (end of the array)
193: */
194: public void values(char[] ch, int start, int end) {
195: if (mode != KEYMODE && mode != VALUEMODE)
196: return;
197:
198: handleValues(ch, start, end);
199: }
200:
201: /**
202: * The method valueObjStartElement refers to handleValueObjStartElement
203: * if mode is on KEYMODE or VALUEMODE.
204: *
205: * @param atts (the attributes)
206: */
207: protected void valueObjStartElement(Attributes atts) {
208: if (mode != KEYMODE && mode != VALUEMODE) {
209: stack.push("ValueObj");
210: return;
211: }
212:
213: handleValueObjStartElement(atts);
214: }
215:
216: /**
217: * The method valueObjEndElement refers to handleValueObjEndElement
218: * if mode is on KEYMODE or VALUEMODE.
219: */
220: protected void valueObjEndElement() {
221: if (mode != KEYMODE && mode != VALUEMODE) {
222: stack.pop();
223: return;
224: }
225:
226: handleValueObjEndElement();
227: }
228:
229: /**
230: * The method valueArrayStartElement refers to handleValueArrayStartElement
231: * if mode is on KEYMODE or VALUEMODE.
232: *
233: * @param atts (the attributes)
234: */
235: protected void valueArrayStartElement(Attributes atts) {
236: if (mode != KEYMODE && mode != VALUEMODE) {
237: stack.push("ValueArray");
238: return;
239: }
240:
241: handleValueArrayStartElement(atts);
242: }
243:
244: /**
245: * The method valueArrayEndElement refers to handleValueArrayEndElement
246: * if mode is on KEYMODE or VALUEMODE.
247: */
248: protected void valueArrayEndElement() {
249:
250: if (mode != KEYMODE && mode != VALUEMODE) {
251: stack.pop();
252: return;
253: }
254:
255: handleValueArrayEndElement();
256: }
257:
258: /**
259: * This method returns the stack.
260: *
261: * @return Stack
262: */
263: public Stack getStack() {
264: return stack;
265: }
266:
267: }
|