Project

General

Profile

« Previous | Next » 

Revision 17

Added by Matt Jones about 24 years ago

Split XML SAX handler class into seperate file DBSAXHandler.java

View differences:

DBSAXWriter.java
81 81

  
82 82
        // Use the XMLDocumentHandler interface for namespace support
83 83
        // instead of org.xml.sax.DocumentHandler
84
        XMLDocumentHandler xmlDocHandler = new XMLDocumentHandlerImpl(conn);
84
        XMLDocumentHandler xmlDocHandler = new DBSAXHandler(conn);
85 85

  
86 86
        // For all the other interface use the default provided by
87 87
        // Handler base
......
149 149
  }
150 150

  
151 151
}
152

  
153
/****************************************************************
154
 * Implementation of XMLDocumentHandler interface.              *
155
 * Other interfaces are implemented in the class HandlerBase.   *
156
 ****************************************************************/
157

  
158
class XMLDocumentHandlerImpl extends DefaultXMLDocumentHandler
159
{
160

  
161
   private boolean 	debug 	= false;
162
   private boolean 	stackCreated = false;
163
   private Stack 	elementStack;
164
   private Connection	conn = null;
165

  
166
   public XMLDocumentHandlerImpl(Connection conn)
167
   {
168
      System.out.println("\nINITIALIZING HANDLER....\n");
169

  
170
      this.conn = conn;
171

  
172
      // Create the stack for keeping track of element context
173
      // if it doesn't already exist
174
      if (!stackCreated) {
175
        elementStack = new Stack();
176
        stackCreated = true;
177
      }
178

  
179
   }
180
 
181
   public void startElement(NSName name, SAXAttrList atts) throws SAXException 
182
   {
183

  
184
      // Use the methods getQualifiedName(), getLocalName(), getNamespace()
185
      // and getExpandedName() in NSName interface to get Namespace
186
      // information.
187

  
188
      String qName;
189
      String localName;
190
      String nsName;
191
      String expName;
192
      DBSAXElement currentElement;
193

  
194
      qName = name.getQualifiedName();
195
      localName = name.getLocalName();
196
      nsName = name.getNamespace();
197
      expName = name.getExpandedName();
198
      
199
      // Create the current element representation
200
      currentElement = new DBSAXElement(conn, 1, localName, false, 0);
201
      System.out.println("Element created:" + currentElement.getTagName());
202

  
203
      // Add all of the attributes
204
      for (int i=0; i<atts.getLength(); i++)
205
      {
206

  
207
         // Use the methods getQualifiedName(), getLocalName(), getNamespace()
208
         // and getExpandedName() in SAXAttrList interface to get Namespace
209
         // information.
210

  
211
         qName = atts.getQualifiedName(i);
212
         localName = atts.getLocalName(i);
213
         nsName = atts.getNamespace(i);
214
         expName = atts.getExpandedName(i);
215

  
216
         // You can get the type and value of the attributes either
217
         // by index or by the Qualified Name.
218

  
219
         String type = atts.getType(qName);
220
         String value = atts.getValue(qName);
221

  
222
         currentElement.setAttribute(localName, value);
223

  
224
         System.out.println(" Added ATTRIBUTE Name:" + localName);
225
         System.out.println("                 Type:" + type);
226
         System.out.println("                Value:" + value);
227
         System.out.println();
228

  
229
      }      
230

  
231
      // Add the element to the stack, so that any text data can be 
232
      // added as it is encountered
233
      elementStack.push(currentElement);
234

  
235
   }
236

  
237
   public void characters(char[] cbuf, int start, int len)
238
   {
239
      DBSAXElement currentElement = (DBSAXElement)elementStack.peek();
240
      currentElement.appendContent(cbuf,start,len);
241
      System.out.println("   Characters: " + new String(cbuf,start,len));
242
      System.out.println("     Appended: " + currentElement.getContent());
243
   }
244

  
245
   public void ignorableWhitespace(char[] cbuf, int start, int len)
246
   {
247
      System.out.println("IgnorableWhiteSpace");
248
   }
249

  
250
   public void endElement(NSName name) throws SAXException 
251
   {
252
      // Use the methods getQualifiedName(), getLocalName(), getNamespace()
253
      // and getExpandedName() in NSName interface to get Namespace
254
      // information.
255

  
256
      String expName = name.getExpandedName();
257

  
258
      // Remove the element from the stack
259
      DBSAXElement currentElement = (DBSAXElement)elementStack.pop();
260

  
261
      System.out.println("Final content: " + currentElement.getContent());
262
      System.out.println("Closing element: " + expName);
263
   }
264

  
265
   /** Debug routine */
266
   private void db(int flag) {
267
     if (debug) {
268
       System.err.println("DEBUG POSITION " + flag);
269
     }
270
   }   
271
}

Also available in: Unified diff