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
 *    Release: @release@
9
 *
10
 *   '$Author: bojilova $'
11
 *     '$Date: 2000-09-15 17:40:40 -0700 (Fri, 15 Sep 2000) $'
12
 * '$Revision: 457 $'
13
 */
14

    
15
package edu.ucsb.nceas.metacat;
16

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

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

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

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

    
49
   private static final int MAXDATACHARS = 4000;
50

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

    
60
     // Create the stack for keeping track of node context
61
     // if it doesn't already exist
62
     if (!stackCreated) {
63
       nodeStack = new Stack();
64
       stackCreated = true;
65
     }
66
   }
67
 
68
   /** Construct an instance of the handler class 
69
    *
70
    * @param conn the JDBC connection to which information is written
71
    * @param action - "INSERT" or "UPDATE"
72
    * @param docid to be inserted or updated into JDBC connection
73
    * @param user the user connected to MetaCat servlet
74
    */
75
   public DBSAXHandler(Connection conn,String action,String docid,String user)
76
   {
77
     this(conn);
78
     this.action = action;
79
     this.docid = docid;
80
     this.user = user;
81
   }
82

    
83
   /** SAX Handler that receives notification of beginning of the document */
84
   public void startDocument() throws SAXException {
85
     MetaCatUtil.debugMessage("start Document");
86

    
87
     // Create the document node representation as root
88
     rootNode = new DBSAXNode(conn, this.docid);
89
     // Add the node to the stack, so that any text data can be 
90
     // added as it is encountered
91
     nodeStack.push(rootNode);
92
   }
93

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

    
107
   /** SAX Handler that is called at the start of each XML element */
108
   public void startElement(String uri, String localName,
109
                            String qName, Attributes atts) 
110
               throws SAXException {
111
     MetaCatUtil.debugMessage("Start ELEMENT " + localName);
112

    
113
     DBSAXNode parentNode = null;
114
     DBSAXNode currentNode = null;
115

    
116
     // Get a reference to the parent node for the id
117
     try {
118
       parentNode = (DBSAXNode)nodeStack.peek();
119
     } catch (EmptyStackException e) {
120
       parentNode = null;
121
     }
122

    
123
     // Document representation that points to the root document node
124
     if (atFirstElement) {
125
       atFirstElement = false;
126
       // If no DOCTYPE declaration: docname = root element name 
127
       if (docname == null) {
128
         docname = localName;
129
         doctype = docname;
130
         MetaCatUtil.debugMessage("DOCNAME-a: " + docname);
131
         MetaCatUtil.debugMessage("DOCTYPE-a: " + doctype);
132
       } else if (doctype == null) {
133
         doctype = docname;
134
         MetaCatUtil.debugMessage("DOCTYPE-b: " + doctype);
135
       }
136
       rootNode.writeNodename(docname);
137
       try {
138
         currentDocument = new DocumentImpl(conn, rootNode.getNodeID(), 
139
                                       docname, doctype, docid, action, user);
140
       } catch (Exception ane) {
141
         throw (new SAXException("Error with " + action, ane));
142
       }
143
       // not needed any more
144
       //rootNode.writeDocID(currentDocument.getDocID());
145
     }      
146

    
147
     // Create the current node representation
148
     currentNode = new DBSAXNode(conn, localName, parentNode,
149
                                 currentDocument.getRootNodeID(),docid,
150
                                 currentDocument.getDoctype());
151

    
152
     // Add all of the attributes
153
     for (int i=0; i<atts.getLength(); i++) {
154
       currentNode.setAttribute(atts.getLocalName(i), atts.getValue(i), docid);
155
     }      
156

    
157
     // Add the node to the stack, so that any text data can be 
158
     // added as it is encountered
159
     nodeStack.push(currentNode);
160
  }
161

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

    
186
       // Write the content of the node to the database
187
       currentNode.writeChildNodeToDB("TEXT", null, data, docid);
188
     }
189
   }
190

    
191
   /** 
192
    * SAX Handler that is called for each XML text node that is Ignorable
193
    * white space
194
    */
195
   public void ignorableWhitespace(char[] cbuf, int start, int len) {
196
     MetaCatUtil.debugMessage("IGNORABLEWHITESPACE");
197
   }
198

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

    
210
   /** SAX Handler that is called at the end of each XML element */
211
   public void endElement(String uri, String localName,
212
                          String qName) throws SAXException {
213
     MetaCatUtil.debugMessage("End ELEMENT " + localName);
214

    
215
     // Get the node from the stack
216
     DBSAXNode currentNode = (DBSAXNode)nodeStack.pop();
217
   }
218

    
219
   //
220
   // the next section implements the LexicalHandler interface
221
   //
222

    
223
   /** SAX Handler that receives notification of DOCTYPE. Sets the DTD */
224
   public void startDTD(String name, String publicId, String systemId) 
225
               throws SAXException {
226
     docname = name;
227
     doctype = publicId;
228
     systemid = systemId;
229

    
230
     MetaCatUtil.debugMessage("Start DTD");
231
     MetaCatUtil.debugMessage("DOCNAME: " + docname);
232
     MetaCatUtil.debugMessage("DOCTYPE: " + doctype);
233
     MetaCatUtil.debugMessage("  SYSID: " + systemid);
234
   }
235

    
236
   /** 
237
    * SAX Handler that receives notification of end of DTD 
238
    */
239
   public void endDTD() throws SAXException {
240
     MetaCatUtil.debugMessage("end DTD");
241
   }
242

    
243
   /** 
244
    * SAX Handler that receives notification of comments in the DTD
245
    */
246
   public void comment(char[] ch, int start, int length) throws SAXException {
247
     MetaCatUtil.debugMessage("COMMENT");
248
     DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
249
     currentNode.writeChildNodeToDB("COMMENT", null, new String(ch), docid);
250
   }
251

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

    
259
   /** 
260
    * SAX Handler that receives notification of the end of CDATA sections
261
    */
262
   public void endCDATA() throws SAXException {
263
     MetaCatUtil.debugMessage("end CDATA");
264
   }
265

    
266
   /** 
267
    * SAX Handler that receives notification of the start of entities
268
    */
269
   public void startEntity(String name) throws SAXException {
270
     MetaCatUtil.debugMessage("start ENTITY: " + name);
271
     if (name.equals("[dtd]")) {
272
       processingDTD = true;
273
     }
274
   }
275

    
276
   /** 
277
    * SAX Handler that receives notification of the end of entities
278
    */
279
   public void endEntity(String name) throws SAXException {
280
     MetaCatUtil.debugMessage("end ENTITY: " + name);
281
     if (name.equals("[dtd]")) {
282
       processingDTD = false;
283
     }
284
   }
285

    
286
   /** 
287
    * SAX Handler that receives notification of element declarations
288
    */
289
   public void elementDecl(String name, String model)
290
                        throws org.xml.sax.SAXException {
291
     MetaCatUtil.debugMessage("ELEMENTDECL: " + name + " " + model);
292
   }
293

    
294
   /** 
295
    * SAX Handler that receives notification of attribute declarations
296
    */
297
   public void attributeDecl(String eName, String aName,
298
                        String type, String valueDefault, String value)
299
                        throws org.xml.sax.SAXException {
300
     MetaCatUtil.debugMessage("ATTRIBUTEDECL: " + eName + " " 
301
                        + aName + " " + type + " " + valueDefault + " "
302
                        + value);
303
   }
304

    
305
   /** 
306
    * SAX Handler that receives notification of internal entity declarations
307
    */
308
   public void internalEntityDecl(String name, String value)
309
                        throws org.xml.sax.SAXException {
310
     MetaCatUtil.debugMessage("INTERNENTITYDECL: " + name + " " + value);
311
   }
312

    
313
   /** 
314
    * SAX Handler that receives notification of external entity declarations
315
    */
316
   public void externalEntityDecl(String name, String publicId,
317
                        String systemId)
318
                        throws org.xml.sax.SAXException {
319
     MetaCatUtil.debugMessage("EXTERNENTITYDECL: " + name + " " + publicId 
320
                              + " " + systemId);
321
   }
322

    
323
   //
324
   // the next section implements the ErrorHandler interface
325
   //
326

    
327
   /** 
328
    * SAX Handler that receives notification of fatal parsing errors
329
    */
330
   public void fatalError(SAXParseException exception) throws SAXException {
331
     MetaCatUtil.debugMessage("FATALERROR");
332
     throw (new SAXException("Fatal processing error.", exception));
333
   }
334

    
335
   /** 
336
    * SAX Handler that receives notification of recoverable parsing errors
337
    */
338
   public void error(SAXParseException exception) throws SAXException {
339
     MetaCatUtil.debugMessage("ERROR");
340
   }
341

    
342
   /** 
343
    * SAX Handler that receives notification of warnings
344
    */
345
   public void warning(SAXParseException exception) throws SAXException {
346
     MetaCatUtil.debugMessage("FATALERROR");
347
   }
348

    
349
   // 
350
   // Helper, getter and setter methods
351
   //
352
   
353
   /**
354
    * get the document name
355
    */
356
   public String getDocname() {
357
     return docname;
358
   }
359

    
360
   /**
361
    * get the document processing state
362
    */
363
   public boolean processingDTD() {
364
     return processingDTD;
365
   }
366
}
(9-9/28)