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: 2001-09-14 11:59:33 -0700 (Fri, 14 Sep 2001) $'
12
 * '$Revision: 831 $'
13
 *
14
 * This program is free software; you can redistribute it and/or modify
15
 * it under the terms of the GNU General Public License as published by
16
 * the Free Software Foundation; either version 2 of the License, or
17
 * (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU General Public License
25
 * along with this program; if not, write to the Free Software
26
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
 */
28

    
29
package edu.ucsb.nceas.metacat;
30

    
31
import java.sql.*;
32
import java.io.StringReader;
33
import java.util.Stack;
34
import java.util.Vector;
35
import java.util.Hashtable;
36
import java.util.Enumeration;
37
import java.util.EmptyStackException;
38

    
39
import org.xml.sax.Attributes;
40
import org.xml.sax.SAXException;
41
import org.xml.sax.SAXParseException;
42
import org.xml.sax.ext.DeclHandler;
43
import org.xml.sax.ext.LexicalHandler;
44
import org.xml.sax.helpers.DefaultHandler;
45

    
46
/** 
47
 * A database aware Class implementing callback bethods for the SAX parser to
48
 * call when processing the XML stream and generating events
49
 */
50
public class DBSAXHandler extends DefaultHandler 
51
                          implements LexicalHandler, DeclHandler, Runnable {
52

    
53
   private boolean	atFirstElement;
54
   private boolean	processingDTD;
55
   private String 	docname = null;
56
   private String 	doctype;
57
   private String 	systemid;
58
   private boolean 	stackCreated = false;
59
   private Stack 	  nodeStack;
60
   private Vector   nodeIndex;
61
   private Connection	  conn = null;
62
   private DocumentImpl currentDocument;
63
   private DBSAXNode    rootNode;
64
   private String   action = null;
65
   private String   docid = null;
66
   private String   user = null;
67
   private String[] groups = null;
68
   private String   pub = null;
69
   private Thread   xmlIndex;
70
   private boolean endDocument = false;
71
   private int serverCode = 1;
72
   private Hashtable namespaces = new Hashtable();
73

    
74
   private static final int MAXDATACHARS = 4000;
75
// DOCTITLE attr cleared from the db
76
//   private static final int MAXTITLELEN = 1000;
77

    
78
   /** Construct an instance of the handler class 
79
    *
80
    * @param conn the JDBC connection to which information is written
81
    */
82
   public DBSAXHandler(Connection conn) {
83
     this.conn = conn;
84
     this.atFirstElement = true;
85
     this.processingDTD = false;
86

    
87
     // Create the stack for keeping track of node context
88
     // if it doesn't already exist
89
     if (!stackCreated) {
90
       nodeStack = new Stack();
91
       nodeIndex = new Vector();
92
       stackCreated = true;
93
     }
94
   }
95
   
96
   /** Construct an instance of the handler class 
97
    *
98
    * @param conn the JDBC connection to which information is written
99
    * @param action - "INSERT" or "UPDATE"
100
    * @param docid to be inserted or updated into JDBC connection
101
    * @param user the user connected to MetaCat servlet and owns the document
102
    * @param groups the groups to which user belongs
103
    * @param pub flag for public "read" access on document
104
    * @param serverCode the serverid from xml_replication on which this document
105
    *        resides.
106
    *
107
    */
108
   public DBSAXHandler(Connection conn, String action, String docid,
109
                       String user, String[] groups, String pub, int serverCode)
110
   {
111
     this(conn);
112
     this.action = action;
113
     this.docid = docid;
114
     this.user = user;
115
     this.groups = groups;
116
     this.pub = pub;
117
     this.serverCode = serverCode;
118
     this.xmlIndex = new Thread(this);
119
   }
120
 
121
   /** SAX Handler that receives notification of beginning of the document */
122
   public void startDocument() throws SAXException {
123
     MetaCatUtil.debugMessage("start Document");
124

    
125
     // Create the document node representation as root
126
     rootNode = new DBSAXNode(conn, this.docid);
127
     // Add the node to the stack, so that any text data can be 
128
     // added as it is encountered
129
     nodeStack.push(rootNode);
130
   }
131

    
132
   /** SAX Handler that receives notification of end of the document */
133
   public void endDocument() throws SAXException {
134
     MetaCatUtil.debugMessage("end Document");
135
     // Starting new thread for writing XML Index.
136
     // It calls the run method of the thread.
137
     try {
138
       xmlIndex.start();
139
     } catch (NullPointerException e) {
140
       xmlIndex = null;
141
       throw new 
142
       SAXException("Problem with starting thread for writing XML Index. " +
143
                    e.getMessage());
144
     }
145
   }
146

    
147
   /** SAX Handler that is called at the start of Namespace */
148
   public void startPrefixMapping(String prefix, String uri) 
149
                                          throws SAXException
150
   {
151
    MetaCatUtil.debugMessage("NAMESPACE");
152

    
153
    namespaces.put(prefix, uri);
154
   }
155
   
156
   /** SAX Handler that is called at the start of each XML element */
157
   public void startElement(String uri, String localName,
158
                            String qName, Attributes atts) 
159
               throws SAXException {
160
     MetaCatUtil.debugMessage("Start ELEMENT " + qName);
161
 
162
     DBSAXNode parentNode = null;
163
     DBSAXNode currentNode = null;
164

    
165
     // Get a reference to the parent node for the id
166
     try {
167
       parentNode = (DBSAXNode)nodeStack.peek();
168
     } catch (EmptyStackException e) {
169
       parentNode = null;
170
     }
171

    
172
     // Document representation that points to the root document node
173
     if (atFirstElement) {
174
       atFirstElement = false;
175
       // If no DOCTYPE declaration: docname = root element name 
176
       if (docname == null) {
177
         docname = localName;
178
         doctype = docname;
179
         MetaCatUtil.debugMessage("DOCNAME-a: " + docname);
180
         MetaCatUtil.debugMessage("DOCTYPE-a: " + doctype);
181
       } else if (doctype == null) {
182
         doctype = docname;
183
         MetaCatUtil.debugMessage("DOCTYPE-b: " + doctype);
184
       }
185
       rootNode.writeNodename(docname);
186
       try {
187
         // for validated XML Documents store a reference to XML DB Catalog
188
         String catalogid = null;
189
         if ( systemid != null ) {
190
           Statement stmt = conn.createStatement();
191
           ResultSet rs = stmt.executeQuery(
192
                          "SELECT catalog_id FROM xml_catalog " +
193
                          "WHERE entry_type = 'DTD' " + 
194
                          "AND public_id = '" + doctype + "'");
195
           boolean hasRow = rs.next();
196
           if ( hasRow ) {
197
            catalogid = rs.getString(1);
198
           }
199
           stmt.close();
200
         }
201
         currentDocument = new DocumentImpl(conn, rootNode.getNodeID(), 
202
                               docname, doctype, docid, action, user, 
203
                               this.pub, catalogid, this.serverCode);
204
       } catch (Exception ane) {
205
         throw (new SAXException("Error in DBSaxHandler.startElement " + 
206
                                 action, ane));
207
       }
208
     }      
209

    
210
     // Create the current node representation
211
     currentNode = new DBSAXNode(conn, qName, localName, parentNode,
212
                                 currentDocument.getRootNodeID(),docid,
213
                                 currentDocument.getDoctype());
214
                               
215
     // Add all of the namespaces
216
     String prefix;
217
     String nsuri;
218
     Enumeration prefixes = namespaces.keys();
219
     while ( prefixes.hasMoreElements() ) {
220
       prefix = (String)prefixes.nextElement();
221
       nsuri = (String)namespaces.get(prefix);
222
       currentNode.setNamespace(prefix, nsuri, docid);
223
     }
224
     namespaces = null;
225
     namespaces = new Hashtable();
226

    
227
     // Add all of the attributes
228
     for (int i=0; i<atts.getLength(); i++) {
229
       currentNode.setAttribute(atts.getQName(i), atts.getValue(i), docid);
230
     }      
231

    
232
     // Add the node to the stack, so that any text data can be 
233
     // added as it is encountered
234
     nodeStack.push(currentNode);
235
     // Add the node to the vector used by thread for writing XML Index
236
     nodeIndex.addElement(currentNode);
237

    
238
  }
239
  
240
  /* The run method of xmlIndex thread. It writes XML Index for the document. */
241
  public void run () {
242
    DBSAXNode currNode = null;
243
    DBSAXNode prevNode = null;
244
    Connection dbconn = null;
245
    String doctype = currentDocument.getDoctype();
246
    int step = 0;
247
    int counter = 0;
248

    
249
    try {
250
      // Opening separate db connection for writing XML Index
251
      MetaCatUtil util = new MetaCatUtil();
252
      dbconn = util.openDBConnection();
253
      dbconn.setAutoCommit(false);
254

    
255
      // Going through the elements of the document and writing its Index
256
      Enumeration nodes = nodeIndex.elements();
257
      while ( nodes.hasMoreElements() ) {
258
        currNode = (DBSAXNode)nodes.nextElement();
259
        currNode.updateNodeIndex(dbconn, docid, doctype);
260
      }
261
    
262
      dbconn.commit();
263
         
264
      //if this is a package file
265
      if ( doctype.equals(util.getOption("packagedoctype")) )
266
      {
267
        // write the package info to xml_relation table
268
        RelationHandler rth = new RelationHandler(docid, dbconn);
269
        // from the relations get the access file id for that package
270
        String aclid = rth.getAccessFileID(docid);
271
        // if there are access file, write ACL for that package
272
        if ( aclid != null ) {
273
          runAccessControlList(dbconn, aclid);
274
        }
275
      }
276
      // if it is an access file
277
      else if ( doctype.equals(util.getOption("accessdoctype")) )
278
      {
279
        // write ACL for the package
280
        runAccessControlList(dbconn, docid);
281
      }
282
      
283
      dbconn.close();
284

    
285
    } catch (Exception e) {
286
      try {
287
        dbconn.rollback();
288
        dbconn.close();
289
      } catch (SQLException sqle) {}
290
      System.out.println("Error in DBSAXHandler.run " + e.getMessage());
291
      e.printStackTrace();
292
    }      
293
  }
294
  
295
  // It runs in xmlIndex thread. It writes ACL for a package.
296
  private void runAccessControlList (Connection conn, String aclid)
297
                                                throws Exception
298
  {
299
    // read the access file from xml_nodes
300
    // parse the access file and store the access info into xml_access
301
    AccessControlList aclobj = 
302
    new AccessControlList(conn, aclid, //new StringReader(xml),
303
                          user, groups, serverCode);
304
    conn.commit();
305
  }
306

    
307

    
308
  /** SAX Handler that is called for each XML text node */
309
  public void characters(char[] cbuf, int start, int len) throws SAXException {
310
     MetaCatUtil.debugMessage("CHARACTERS");
311
     DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
312
     String data = null;
313
     int leftover = len;
314
     int offset = start;
315
     boolean moredata = true;
316
    
317
     // This loop deals with the case where there are more characters 
318
     // than can fit in a single database text field (limit is 
319
     // MAXDATACHARS).  If the text to be inserted exceeds MAXDATACHARS,
320
     // write a series of nodes that are MAXDATACHARS long, and then the
321
     // final node contains the remainder
322
     while (moredata) {
323
       if (leftover > MAXDATACHARS) {
324
         data = new String(cbuf, offset, MAXDATACHARS);
325
         leftover -= MAXDATACHARS;
326
         offset += MAXDATACHARS;
327
       } else {
328
         data = new String(cbuf, offset, leftover);
329
         moredata = false;
330
       }
331

    
332
       // Write the content of the node to the database
333
       currentNode.writeChildNodeToDB("TEXT", null, data, docid);
334
     }
335
   }
336

    
337
   /** 
338
    * SAX Handler that is called for each XML text node that is
339
    * Ignorable white space
340
    */
341
   public void ignorableWhitespace(char[] cbuf, int start, int len)
342
               throws SAXException {
343
     // When validation is turned "on", white spaces are reported here
344
     // When validation is turned "off" white spaces are not reported here,
345
     // but through characters() callback
346
     MetaCatUtil.debugMessage("IGNORABLEWHITESPACE");
347
     //System.out.println("Whitespace:" + len + "x" + new String(cbuf, start, len) + "x");
348

    
349
     DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
350
     String data = null;
351
     int leftover = len;
352
     int offset = start;
353
     boolean moredata = true;
354
     
355
     // This loop deals with the case where there are more characters 
356
     // than can fit in a single database text field (limit is 
357
     // MAXDATACHARS).  If the text to be inserted exceeds MAXDATACHARS,
358
     // write a series of nodes that are MAXDATACHARS long, and then the
359
     // final node contains the remainder
360
     while (moredata) {
361
       if (leftover > MAXDATACHARS) {
362
         data = new String(cbuf, offset, MAXDATACHARS);
363
         leftover -= MAXDATACHARS;
364
         offset += MAXDATACHARS;
365
       } else {
366
         data = new String(cbuf, offset, leftover);
367
         moredata = false;
368
       }
369

    
370
       // Write the content of the node to the database
371
       currentNode.writeChildNodeToDB("TEXT", null, data, docid);
372
     }
373
   }
374

    
375
   /** 
376
    * SAX Handler called once for each processing instruction found: 
377
    * node that PI may occur before or after the root element.
378
    */
379
   public void processingInstruction(String target, String data) 
380
          throws SAXException {
381
     MetaCatUtil.debugMessage("PI");
382
     DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
383
     currentNode.writeChildNodeToDB("PI", target, data, docid);
384
   }
385

    
386
   /** SAX Handler that is called at the end of each XML element */
387
   public void endElement(String uri, String localName,
388
                          String qName) throws SAXException {
389
     MetaCatUtil.debugMessage("End ELEMENT " + qName);
390

    
391
     // Get the node from the stack
392
     DBSAXNode currentNode = (DBSAXNode)nodeStack.pop();
393
   }
394

    
395
   //
396
   // the next section implements the LexicalHandler interface
397
   //
398

    
399
   /** SAX Handler that receives notification of DOCTYPE. Sets the DTD */
400
   public void startDTD(String name, String publicId, String systemId) 
401
               throws SAXException {
402
     docname = name;
403
     doctype = publicId;
404
     systemid = systemId;
405

    
406
     MetaCatUtil.debugMessage("Start DTD");
407
     MetaCatUtil.debugMessage("DOCNAME: " + docname);
408
     MetaCatUtil.debugMessage("DOCTYPE: " + doctype);
409
     MetaCatUtil.debugMessage("  SYSID: " + systemid);
410
   }
411

    
412
   /** 
413
    * SAX Handler that receives notification of end of DTD 
414
    */
415
   public void endDTD() throws SAXException {
416
    
417
     MetaCatUtil.debugMessage("end DTD");
418
   }
419

    
420
   /** 
421
    * SAX Handler that receives notification of comments in the DTD
422
    */
423
   public void comment(char[] ch, int start, int length) throws SAXException {
424
     MetaCatUtil.debugMessage("COMMENT");
425
     if ( !processingDTD ) {
426
       DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
427
       currentNode.writeChildNodeToDB("COMMENT", null, new String(ch), docid);
428
     }
429
   }
430

    
431
   /** 
432
    * SAX Handler that receives notification of the start of CDATA sections
433
    */
434
   public void startCDATA() throws SAXException {
435
     MetaCatUtil.debugMessage("start CDATA");
436
   }
437

    
438
   /** 
439
    * SAX Handler that receives notification of the end of CDATA sections
440
    */
441
   public void endCDATA() throws SAXException {
442
     MetaCatUtil.debugMessage("end CDATA");
443
   }
444

    
445
   /** 
446
    * SAX Handler that receives notification of the start of entities
447
    */
448
   public void startEntity(String name) throws SAXException {
449
     MetaCatUtil.debugMessage("start ENTITY: " + name);
450
//System.out.println("start ENTITY: " + name);
451
     if (name.equals("[dtd]")) {
452
       processingDTD = true;
453
     }
454
   }
455

    
456
   /** 
457
    * SAX Handler that receives notification of the end of entities
458
    */
459
   public void endEntity(String name) throws SAXException {
460
     MetaCatUtil.debugMessage("end ENTITY: " + name);
461
//System.out.println("end ENTITY: " + name);
462
     if (name.equals("[dtd]")) {
463
       processingDTD = false;
464
     }
465
   }
466

    
467
   /** 
468
    * SAX Handler that receives notification of element declarations
469
    */
470
   public void elementDecl(String name, String model)
471
                        throws org.xml.sax.SAXException {
472
//System.out.println("ELEMENTDECL: " + name + " " + model);
473
     MetaCatUtil.debugMessage("ELEMENTDECL: " + name + " " + model);
474
   }
475

    
476
   /** 
477
    * SAX Handler that receives notification of attribute declarations
478
    */
479
   public void attributeDecl(String eName, String aName,
480
                        String type, String valueDefault, String value)
481
                        throws org.xml.sax.SAXException {
482

    
483
//System.out.println("ATTRIBUTEDECL: " + eName + " " 
484
//                        + aName + " " + type + " " + valueDefault + " "
485
//                        + value);
486
     MetaCatUtil.debugMessage("ATTRIBUTEDECL: " + eName + " " 
487
                        + aName + " " + type + " " + valueDefault + " "
488
                        + value);
489
   }
490

    
491
   /** 
492
    * SAX Handler that receives notification of internal entity declarations
493
    */
494
   public void internalEntityDecl(String name, String value)
495
                        throws org.xml.sax.SAXException {
496
//System.out.println("INTERNENTITYDECL: " + name + " " + value);
497
     MetaCatUtil.debugMessage("INTERNENTITYDECL: " + name + " " + value);
498
   }
499

    
500
   /** 
501
    * SAX Handler that receives notification of external entity declarations
502
    */
503
   public void externalEntityDecl(String name, String publicId,
504
                        String systemId)
505
                        throws org.xml.sax.SAXException {
506
//System.out.println("EXTERNENTITYDECL: " + name + " " + publicId 
507
//                              + " " + systemId);
508
     MetaCatUtil.debugMessage("EXTERNENTITYDECL: " + name + " " + publicId 
509
                              + " " + systemId);
510
     // it processes other external entity, not the DTD;
511
     // it doesn't signal for the DTD here
512
     processingDTD = false;
513
   }
514

    
515
   //
516
   // the next section implements the ErrorHandler interface
517
   //
518

    
519
   /** 
520
    * SAX Handler that receives notification of fatal parsing errors
521
    */
522
   public void fatalError(SAXParseException exception) throws SAXException {
523
     MetaCatUtil.debugMessage("FATALERROR");
524
     throw (new SAXException("Fatal processing error.", exception));
525
   }
526

    
527
   /** 
528
    * SAX Handler that receives notification of recoverable parsing errors
529
    */
530
   public void error(SAXParseException exception) throws SAXException {
531
     MetaCatUtil.debugMessage("ERROR");
532
     throw (new SAXException("Processing error.", exception));
533
   }
534

    
535
   /** 
536
    * SAX Handler that receives notification of warnings
537
    */
538
   public void warning(SAXParseException exception) throws SAXException {
539
     MetaCatUtil.debugMessage("WARNING");
540
     throw (new SAXException("Warning.", exception));
541
   }
542

    
543
   // 
544
   // Helper, getter and setter methods
545
   //
546
   
547
   /**
548
    * get the document name
549
    */
550
   public String getDocname() {
551
     return docname;
552
   }
553

    
554
   /**
555
    * get the document processing state
556
    */
557
   public boolean processingDTD() {
558
     return processingDTD;
559
   }
560
}
(15-15/40)