Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that handles the SAX XML events as they
4
 *             are generated from XML documents
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Matt Jones, Jivka Bojilova
8
 *
9
 *   '$Author: jones $'
10
 *     '$Date: 2000-06-29 16:27:08 -0700 (Thu, 29 Jun 2000) $'
11
 * '$Revision: 243 $'
12
 */
13

    
14
package edu.ucsb.nceas.metacat;
15

    
16
import java.sql.*;
17
import java.util.Stack;
18
import java.util.EmptyStackException;
19

    
20
import org.xml.sax.Attributes;
21
import org.xml.sax.SAXException;
22
import org.xml.sax.SAXParseException;
23
import org.xml.sax.ext.DeclHandler;
24
import org.xml.sax.ext.LexicalHandler;
25
import org.xml.sax.helpers.DefaultHandler;
26

    
27
/** 
28
 * A database aware Class implementing callback bethods for the SAX parser to
29
 * call when processing the XML stream and generating events
30
 */
31
public class DBSAXHandler extends DefaultHandler 
32
                          implements LexicalHandler, DeclHandler {
33

    
34
   private boolean	atFirstElement;
35
   private boolean	processingDTD;
36
   private String 	docname = null;
37
   private String 	doctype;
38
   private String 	systemid;
39
   private boolean 	stackCreated = false;
40
   private Stack 	nodeStack;
41
   private Connection	conn = null;
42
   private DBSAXDocument currentDocument;
43
   private DBSAXNode    rootNode;
44
   private String       action = null;
45
   private String       docid = null;
46

    
47
   private static final int MAXDATACHARS = 4000;
48

    
49
   /** Construct an instance of the handler class 
50
    *
51
    * @param conn the JDBC connection to which information is written
52
    */
53
   public DBSAXHandler(Connection conn) {
54
     this.conn = conn;
55
     this.atFirstElement = true;
56
     this.processingDTD = false;
57

    
58
     // Create the stack for keeping track of node context
59
     // if it doesn't already exist
60
     if (!stackCreated) {
61
       nodeStack = new Stack();
62
       stackCreated = true;
63
     }
64
   }
65
 
66
   /** Construct an instance of the handler class 
67
    *
68
    * @param conn the JDBC connection to which information is written
69
    */
70
   public DBSAXHandler(Connection conn, String action, String docid) {
71
     this(conn);
72
     this.action = action;
73
     this.docid = docid;
74
   }
75

    
76
   /** SAX Handler that receives notification of beginning of the document */
77
   public void startDocument() throws SAXException {
78
     MetaCatUtil.debugMessage("start Document");
79

    
80
     // Create the document node representation as root
81
     rootNode = new DBSAXNode(conn, docname);
82
     MetaCatUtil.debugMessage("PRINTING DOCNAME FROM ROOTNODE: " + 
83
                               rootNode.getTagName());
84
     // Add the node to the stack, so that any text data can be 
85
     // added as it is encountered
86
     nodeStack.push(rootNode);
87
   }
88

    
89
   /** SAX Handler that receives notification of end of the document */
90
   public void endDocument() throws SAXException {
91
     currentDocument.setTitleFromChildElement();
92
     MetaCatUtil.debugMessage("end Document");
93
     if ((docid != null) && (!docid.equals(currentDocument.getDocID()))) {
94
       throw (new SAXException("New document ID generated:",
95
           new AccessionNumberGeneratedException(currentDocument.getDocID())));
96
     } else {
97
       throw (new SAXException("New document ID generated:",
98
           new AccessionNumberGeneratedException(currentDocument.getDocID())));
99
     }
100
   }
101

    
102
   /** SAX Handler that is called at the start of each XML element */
103
   public void startElement(String uri, String localName,
104
                            String qName, Attributes atts) 
105
               throws SAXException {
106
     MetaCatUtil.debugMessage("Start ELEMENT " + localName);
107

    
108
     DBSAXNode parentNode = null;
109
     DBSAXNode currentNode = null;
110

    
111
     // Get a reference to the parent node for the id
112
     try {
113
       parentNode = (DBSAXNode)nodeStack.peek();
114
     } catch (EmptyStackException e) {
115
     }
116

    
117
     // Document representation that points to the root document node
118
     if (atFirstElement) {
119
       atFirstElement = false;
120
       // If no DOCTYPE declaration: docname = root element name 
121
       if (docname == null) {
122
         docname = localName;
123
         doctype = docname;
124
         MetaCatUtil.debugMessage("DOCNAME-a: " + docname);
125
         MetaCatUtil.debugMessage("DOCTYPE-a: " + doctype);
126
       } else if (doctype == null) {
127
         doctype = docname;
128
         //doctype = DBEntityResolver.doctype;
129
         MetaCatUtil.debugMessage("DOCTYPE-b: " + doctype);
130
       }
131
       rootNode.writeNodename(docname);
132
       rootNode.writeRootNodeID(rootNode.getNodeID());
133
       try {
134
         currentDocument = new DBSAXDocument(conn, rootNode.getNodeID(), 
135
                                           docname, doctype, docid, action);
136
         } catch (AccessionNumberException ane) {
137
           throw (new SAXException("Error with " + action, ane));
138
         }
139
       rootNode.writeDocID(currentDocument.getDocID());
140
     }      
141

    
142
     // Create the current node representation
143
     currentNode = new DBSAXNode(conn, localName, parentNode, 
144
                                 rootNode, currentDocument);
145

    
146
     // Add all of the attributes
147
     for (int i=0; i<atts.getLength(); i++) {
148
       currentNode.setAttribute(atts.getLocalName(i), atts.getValue(i));
149
     }      
150

    
151
     // Add the node to the stack, so that any text data can be 
152
     // added as it is encountered
153
     nodeStack.push(currentNode);
154
  }
155

    
156
   /** SAX Handler that is called for each XML text node */
157
   public void characters(char[] cbuf, int start, int len) {
158
     MetaCatUtil.debugMessage("CHARACTERS");
159
     DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
160
     String data = null;
161
     int leftover = len;
162
     int offset = start;
163
     boolean moredata = true;
164
    
165
     // This loop deals with the case where there are more characters 
166
     // than can fit in a single database text field (limit is 
167
     // MAXDATACHARS).  If the text to be inserted exceeds MAXDATACHARS,
168
     // write a series of nodes that are MAXDATACHARS long, and then the
169
     // final node contains the remainder
170
     while (moredata) {
171
       if (leftover > MAXDATACHARS) {
172
         data = new String(cbuf, offset, MAXDATACHARS);
173
         leftover -= MAXDATACHARS;
174
         offset += MAXDATACHARS;
175
       } else {
176
         data = new String(cbuf, offset, leftover);
177
         moredata = false;
178
       }
179

    
180
       // Write the content of the node to the database
181
       currentNode.writeChildNodeToDB("TEXT", null, data);
182
     }
183
   }
184

    
185
   /** 
186
    * SAX Handler that is called for each XML text node that is Ignorable
187
    * white space
188
    */
189
   public void ignorableWhitespace(char[] cbuf, int start, int len) {
190
     MetaCatUtil.debugMessage("IGNORABLEWHITESPACE");
191
   }
192

    
193
   /** 
194
    * SAX Handler called once for each processing instruction found: 
195
    * node that PI may occur before or after the root element.
196
    */
197
   public void processingInstruction(String target, String data) 
198
          throws SAXException {
199
     MetaCatUtil.debugMessage("PI");
200
     DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
201
     currentNode.writeChildNodeToDB("PI", target, data);
202
   }
203

    
204
   /** SAX Handler that is called at the end of each XML element */
205
   public void endElement(String uri, String localName,
206
                          String qName) throws SAXException {
207
     MetaCatUtil.debugMessage("End ELEMENT " + localName);
208

    
209
     // Get the node from the stack
210
     DBSAXNode currentNode = (DBSAXNode)nodeStack.pop();
211
   }
212

    
213
   //
214
   // the next section implements the LexicalHandler interface
215
   //
216

    
217
   /** SAX Handler that receives notification of DOCTYPE. Sets the DTD */
218
   public void startDTD(String name, String publicId, String systemId) 
219
               throws SAXException {
220
     docname = name;
221
     doctype = publicId;
222
     systemid = systemId;
223

    
224
     MetaCatUtil.debugMessage("Start DTD");
225
     MetaCatUtil.debugMessage("DOCNAME: " + docname);
226
     MetaCatUtil.debugMessage("DOCTYPE: " + doctype);
227
     MetaCatUtil.debugMessage("  SYSID: " + systemid);
228
   }
229

    
230
   /** 
231
    * SAX Handler that receives notification of end of DTD 
232
    */
233
   public void endDTD() throws SAXException {
234
     MetaCatUtil.debugMessage("end DTD");
235
   }
236

    
237
   /** 
238
    * SAX Handler that receives notification of comments in the DTD
239
    */
240
   public void comment(char[] ch, int start, int length) throws SAXException {
241
     MetaCatUtil.debugMessage("COMMENT");
242
     DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
243
     currentNode.writeChildNodeToDB("COMMENT", null, new String(ch));
244
   }
245

    
246
   /** 
247
    * SAX Handler that receives notification of the start of CDATA sections
248
    */
249
   public void startCDATA() throws SAXException {
250
     MetaCatUtil.debugMessage("start CDATA");
251
   }
252

    
253
   /** 
254
    * SAX Handler that receives notification of the end of CDATA sections
255
    */
256
   public void endCDATA() throws SAXException {
257
     MetaCatUtil.debugMessage("end CDATA");
258
   }
259

    
260
   /** 
261
    * SAX Handler that receives notification of the start of entities
262
    */
263
   public void startEntity(String name) throws SAXException {
264
     MetaCatUtil.debugMessage("start ENTITY: " + name);
265
     if (name.equals("[dtd]")) {
266
       processingDTD = true;
267
     }
268
   }
269

    
270
   /** 
271
    * SAX Handler that receives notification of the end of entities
272
    */
273
   public void endEntity(String name) throws SAXException {
274
     MetaCatUtil.debugMessage("end ENTITY: " + name);
275
     if (name.equals("[dtd]")) {
276
       processingDTD = false;
277
     }
278
   }
279

    
280
   /** 
281
    * SAX Handler that receives notification of element declarations
282
    */
283
   public void elementDecl(String name, String model)
284
                        throws org.xml.sax.SAXException {
285
     MetaCatUtil.debugMessage("ELEMENTDECL: " + name + " " + model);
286
   }
287

    
288
   /** 
289
    * SAX Handler that receives notification of attribute declarations
290
    */
291
   public void attributeDecl(String eName, String aName,
292
                        String type, String valueDefault, String value)
293
                        throws org.xml.sax.SAXException {
294
     MetaCatUtil.debugMessage("ATTRIBUTEDECL: " + eName + " " 
295
                        + aName + " " + type + " " + valueDefault + " "
296
                        + value);
297
   }
298

    
299
   /** 
300
    * SAX Handler that receives notification of internal entity declarations
301
    */
302
   public void internalEntityDecl(String name, String value)
303
                        throws org.xml.sax.SAXException {
304
     MetaCatUtil.debugMessage("INTERNENTITYDECL: " + name + " " + value);
305
   }
306

    
307
   /** 
308
    * SAX Handler that receives notification of external entity declarations
309
    */
310
   public void externalEntityDecl(String name, String publicId,
311
                        String systemId)
312
                        throws org.xml.sax.SAXException {
313
     MetaCatUtil.debugMessage("EXTERNENTITYDECL: " + name + " " + publicId 
314
                              + " " + systemId);
315
   }
316

    
317
   //
318
   // the next section implements the ErrorHandler interface
319
   //
320

    
321
   /** 
322
    * SAX Handler that receives notification of fatal parsing errors
323
    */
324
   public void fatalError(SAXParseException exception) throws SAXException {
325
     MetaCatUtil.debugMessage("FATALERROR");
326
     throw (new SAXException("Fatal processing error.", exception));
327
   }
328

    
329
   /** 
330
    * SAX Handler that receives notification of recoverable parsing errors
331
    */
332
   public void error(SAXParseException exception) throws SAXException {
333
     MetaCatUtil.debugMessage("ERROR");
334
   }
335

    
336
   /** 
337
    * SAX Handler that receives notification of warnings
338
    */
339
   public void warning(SAXParseException exception) throws SAXException {
340
     MetaCatUtil.debugMessage("FATALERROR");
341
   }
342

    
343
   // 
344
   // Helper, getter and setter methods
345
   //
346
   
347
   /**
348
    * get the document name
349
    */
350
   public String getDocname() {
351
     return docname;
352
   }
353

    
354
   /**
355
    * get the document processing state
356
    */
357
   public boolean processingDTD() {
358
     return processingDTD;
359
   }
360
}
361

    
362
/**
363
 * '$Log$
364
 * 'Revision 1.30  2000/06/28 03:14:35  jones
365
 * 'Fixed bug where TEXT nodes couldn't be longer than 4000 characters, which
366
 * 'is the maximum length of a VARCHAR2 field in Oracle.  Now, if text
367
 * 'exceeds the field length, I break the text up into a series of TEXT
368
 * 'nodes each of the max field length, and the remainder in the last
369
 * 'TEXT node. The only problem with this is that our current search
370
 * 'algorithms only will find phrases within a single TEXT nodes, so if
371
 * 'the search term spans the node boundary, the search algorithm will not
372
 * 'return a hit. I expect this is extremely rare, basically inconsequential.
373
 * '
374
 * 'Revision 1.29  2000/06/27 04:31:07  jones
375
 * 'Fixed bugs associated with the new UPDATE and DELETE functions of
376
 * 'DBWriter.  There were problematic interactions between some static
377
 * 'variables used in DBEntityResolver and the way in which the
378
 * 'Servlet objects are re-used across multiple client invocations.
379
 * '
380
 * 'Generally cleaned up error reporting.  Now all errors and success
381
 * 'results are reported as XML documents from MetaCatServlet.  Need
382
 * 'to make the command line tools do the same.
383
 * '
384
 * 'Revision 1.28  2000/06/26 10:35:05  jones
385
 * 'Merged in substantial changes to DBWriter and associated classes and to
386
 * 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE
387
 * 'functions.  The command line tools and the parameters for the
388
 * 'servlet have changed substantially.
389
 * '
390
 * 'Revision 1.27.2.6  2000/06/26 02:02:20  jones
391
 * 'Continued fixing problems with exception handling that deals
392
 * 'with INSERT and UPDATE actions and the docid passed to DBWriter
393
 * '
394
 * 'Revision 1.27.2.5  2000/06/26 00:51:06  jones
395
 * 'If docid passed to DBWriter.write() is not unique, classes now generate
396
 * 'an AccessionNumberException containing the new docid generated as a
397
 * 'replacement.  The docid is then extracted from the exception and
398
 * 'returned to the calling application for user feedback or client processing.
399
 * '
400
 * 'Revision 1.27.2.4  2000/06/25 23:38:16  jones
401
 * 'Added RCSfile keyword
402
 * '
403
 * 'Revision 1.27.2.3  2000/06/25 23:34:17  jones
404
 * 'Changed documentation formatting, added log entries at bottom of source files
405
 * ''
406
 */
(12-12/25)