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: tao $'
11
 *     '$Date: 2003-10-31 18:02:16 -0800 (Fri, 31 Oct 2003) $'
12
 * '$Revision: 1841 $'
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 edu.ucsb.nceas.morpho.datapackage.Triple;
32
import edu.ucsb.nceas.morpho.datapackage.TripleCollection;
33

    
34
import java.sql.*;
35
import java.io.StringReader;
36
import java.util.Stack;
37
import java.util.Vector;
38
import java.util.Hashtable;
39
import java.util.Enumeration;
40
import java.util.EmptyStackException;
41

    
42
import org.xml.sax.Attributes;
43
import org.xml.sax.SAXException;
44
import org.xml.sax.SAXParseException;
45
import org.xml.sax.ext.DeclHandler;
46
import org.xml.sax.ext.LexicalHandler;
47
import org.xml.sax.helpers.DefaultHandler;
48

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

    
56
   protected boolean	atFirstElement;
57
   protected boolean	processingDTD;
58
   protected String 	docname = null;
59
   protected String 	doctype;
60
   protected String 	systemid;
61
   private boolean 	stackCreated = false;
62
   protected Stack 	  nodeStack;
63
   protected Vector   nodeIndex;
64
   protected DBConnection	  connection = null;
65
   protected DocumentImpl currentDocument;
66
   protected DBSAXNode    rootNode;
67
   protected String   action = null;
68
   protected String   docid = null;
69
   protected String   revision = null;
70
   protected String   user = null;
71
   protected String[] groups = null;
72
   protected String   pub = null;
73
   protected Thread   xmlIndex;
74
   private boolean endDocument = false;
75
   protected int serverCode = 1;
76
   protected Hashtable namespaces = new Hashtable();
77
   protected boolean hitTextNode = false; // a flag to hit text node
78
   // a buffer to keep all text nodes for same element
79
   // it is for element was splited
80
   protected StringBuffer textBuffer = new StringBuffer();
81
   protected Stack textBufferStack = new Stack();
82

    
83
   protected static final int MAXDATACHARS = 4000;
84
   //protected static final int MAXDATACHARS = 50;
85
   protected static final long INDEXDELAY = 10000;
86
   // methods writeChildNodeToDB, setAttribute, setNamespace, 
87
   // writeTextForDBSAXNode will increase endNodeId.
88
   protected long  endNodeId = -1;    // The end node id for a substree
89
// DOCTITLE attr cleared from the db
90
//   private static final int MAXTITLELEN = 1000;
91
   //HandlerTriple stuff
92
   TripleCollection tripleList = new TripleCollection();
93
   Triple currentTriple        = new Triple();
94
   boolean startParseTriple    = false;
95
   boolean hasTriple           = false;
96

    
97
   /** Construct an instance of the handler class
98
    *
99
    * @param conn the JDBC connection to which information is written
100
    */
101
   public DBSAXHandler(DBConnection conn) {
102
     this.connection = conn;
103
     this.atFirstElement = true;
104
     this.processingDTD = false;
105

    
106
     // Create the stack for keeping track of node context
107
     // if it doesn't already exist
108
     if (!stackCreated) {
109
       nodeStack = new Stack();
110
       nodeIndex = new Vector();
111
       stackCreated = true;
112
     }
113
   }
114

    
115
  /** Construct an instance of the handler class
116
    *
117
    * @param conn the JDBC connection to which information is written
118
    * @param action - "INSERT" or "UPDATE"
119
    * @param docid to be inserted or updated into JDBC connection
120
    * @param user the user connected to MetaCat servlet and owns the document
121
    * @param groups the groups to which user belongs
122
    * @param pub flag for public "read" access on document
123
    * @param serverCode the serverid from xml_replication on which this document
124
    *        resides.
125
    *
126
    */
127
   public DBSAXHandler(DBConnection conn, String action, String docid,
128
                      String user, String[] groups, String pub, int serverCode)
129
   {
130
     this(conn);
131
     this.action = action;
132
     this.docid = docid;
133
     this.user = user;
134
     this.groups = groups;
135
     this.pub = pub;
136
     this.serverCode = serverCode;
137
     this.xmlIndex = new Thread(this);
138
   }
139

    
140
  /** Construct an instance of the handler class
141
    * In this constructor, user can specify the version need to upadate
142
    *
143
    * @param conn the JDBC connection to which information is written
144
    * @param action - "INSERT" or "UPDATE"
145
    * @param docid to be inserted or updated into JDBC connection
146
    * @param revision, the user specified the revision need to be update
147
    * @param user the user connected to MetaCat servlet and owns the document
148
    * @param groups the groups to which user belongs
149
    * @param pub flag for public "read" access on document
150
    * @param serverCode the serverid from xml_replication on which this document
151
    *        resides.
152
    *
153
    */
154
   public DBSAXHandler(DBConnection conn, String action, String docid,
155
     String revision, String user, String[] groups, String pub, int serverCode)
156
   {
157
     this(conn);
158
     this.action = action;
159
     this.docid = docid;
160
     this.revision = revision;
161
     this.user = user;
162
     this.groups = groups;
163
     this.pub = pub;
164
     this.serverCode = serverCode;
165
     this.xmlIndex = new Thread(this);
166
   }
167

    
168
   /** SAX Handler that receives notification of beginning of the document */
169
   public void startDocument() throws SAXException {
170
     MetaCatUtil.debugMessage("start Document", 50);
171

    
172
     // Create the document node representation as root
173
     rootNode = new DBSAXNode(connection, this.docid);
174
     // Add the node to the stack, so that any text data can be
175
     // added as it is encountered
176
     nodeStack.push(rootNode);
177
   }
178

    
179
   /** SAX Handler that receives notification of end of the document */
180
   public void endDocument() throws SAXException {
181
     MetaCatUtil.debugMessage("end Document", 50);
182
     // Starting new thread for writing XML Index.
183
     // It calls the run method of the thread.
184
     
185
     //if it is data package insert triple into relationtion table;
186
     if ( doctype != null && 
187
         MetaCatUtil.getOptionList(MetaCatUtil.getOption("packagedoctype")).
188
         contains(doctype) && hasTriple)
189
      {
190
        try
191
        {
192
          //initial handler and write into relationdb
193
          RelationHandler handler = 
194
                        new RelationHandler(docid,doctype, connection, tripleList);
195
        }
196
        catch (Exception e)
197
        {
198
          MetaCatUtil.debugMessage
199
                            ("Failed to write triples into relation table" + 
200
                                                           e.getMessage(), 30);
201
          throw new SAXException("Failed to write triples into relation table "+ e.getMessage());
202
        }
203
      }
204
     
205
     
206
     try {
207
       xmlIndex.start();
208
     } catch (NullPointerException e) {
209
       xmlIndex = null;
210
       throw new
211
       SAXException("Problem with starting thread for writing XML Index. " +
212
                    e.getMessage());
213
     }
214
   }
215

    
216
   /** SAX Handler that is called at the start of Namespace */
217
   public void startPrefixMapping(String prefix, String uri)
218
                                          throws SAXException
219
   {
220
    MetaCatUtil.debugMessage("NAMESPACE", 50);
221

    
222
    namespaces.put(prefix, uri);
223
   }
224

    
225
   /** SAX Handler that is called at the start of each XML element */
226
   public void startElement(String uri, String localName,
227
                            String qName, Attributes atts)
228
               throws SAXException {
229
     // for element <eml:eml...> qname is "eml:eml", local name is "eml"            
230
     // for element <acl....> both qname and local name is "eml"
231
     // uri is namesapce
232
     MetaCatUtil.debugMessage("Start ELEMENT(qName) " + qName, 50);
233
     MetaCatUtil.debugMessage("Start ELEMENT(localName) " + localName, 50);
234
     MetaCatUtil.debugMessage("Start ELEMENT(uri) " + uri, 50);
235
     
236
     DBSAXNode parentNode = null;
237
     DBSAXNode currentNode = null;
238

    
239
     // Get a reference to the parent node for the id
240
     try {
241
       parentNode = (DBSAXNode)nodeStack.peek();
242
     } catch (EmptyStackException e) {
243
       parentNode = null;
244
     }
245
     
246
     // If hit a text node, we need write this text for current's parent node
247
     // This will happend if the element is mixted
248
     if (hitTextNode && parentNode != null)
249
     {
250
       // write the textbuffer into db for parent node.
251
        endNodeId = writeTextForDBSAXNode(endNodeId, textBuffer, parentNode);
252
        // rest hitTextNode
253
        hitTextNode =false;
254
        // reset textbuffer
255
        textBuffer = null;
256
        textBuffer = new StringBuffer();
257
       
258
     }
259

    
260
     // Document representation that points to the root document node
261
     if (atFirstElement) 
262
     {
263
       atFirstElement = false;
264
       // If no DOCTYPE declaration: docname = root element
265
       // doctype = root element name or name space
266
       if (docname == null) 
267
       {
268
         docname = localName;
269
         // if uri isn't null doctype = uri(namespace)
270
         // othewise root element
271
         if (uri != null && !(uri.trim()).equals(""))
272
         {
273
           doctype = uri;
274
         }
275
         else
276
         {
277
           doctype = docname;
278
         }
279
         MetaCatUtil.debugMessage("DOCNAME-a: " + docname, 30);
280
         MetaCatUtil.debugMessage("DOCTYPE-a: " + doctype, 30);
281
       } 
282
       else if (doctype == null) 
283
       {
284
         // because docname is not null and it is declared in dtd
285
         // so could not be in schema, no namespace
286
         doctype = docname;
287
         MetaCatUtil.debugMessage("DOCTYPE-b: " + doctype, 30);
288
       }
289
       rootNode.writeNodename(docname);
290
       try {
291
         // for validated XML Documents store a reference to XML DB Catalog
292
         // Because this is select statement and it needn't to roll back if
293
         // insert document action fialed.
294
         // In order to decrease DBConnection usage count, we get a new
295
         // dbconnection from pool
296
         String catalogid = null;
297
         DBConnection dbConn = null;
298
         int serialNumber = -1;
299

    
300
         if ( systemid != null ) {
301
           try
302
           {
303
            // Get dbconnection
304
            dbConn=DBConnectionPool.getDBConnection
305
                                          ("DBSAXHandler.startElement");
306
            serialNumber=dbConn.getCheckOutSerialNumber();
307

    
308
            Statement stmt = dbConn.createStatement();
309
            ResultSet rs = stmt.executeQuery(
310
                          "SELECT catalog_id FROM xml_catalog " +
311
                          "WHERE entry_type = 'DTD' " +
312
                          "AND public_id = '" + doctype + "'");
313
            boolean hasRow = rs.next();
314
            if ( hasRow ) {
315
              catalogid = rs.getString(1);
316
            }
317
            stmt.close();
318
           }//try
319
           finally
320
           {
321
             // Return dbconnection
322
             DBConnectionPool.returnDBConnection(dbConn, serialNumber);
323
           }//finally
324
         }
325

    
326
         //create documentImpl object by the constructor which can specify
327
         //the revision
328
         currentDocument = new DocumentImpl(connection, rootNode.getNodeID(),
329
                               docname, doctype, docid, revision, action, user,
330
                               this.pub, catalogid, this.serverCode);
331

    
332

    
333
       } catch (Exception ane) {
334
         throw (new SAXException("Error in DBSaxHandler.startElement " +
335
                                 action, ane));
336
       }
337
     }
338

    
339
     // Create the current node representation
340
     currentNode = new DBSAXNode(connection, qName, localName, parentNode,
341
                                 currentDocument.getRootNodeID(),docid,
342
                                 currentDocument.getDoctype());
343

    
344
     // Add all of the namespaces
345
     String prefix;
346
     String nsuri;
347
     Enumeration prefixes = namespaces.keys();
348
     while ( prefixes.hasMoreElements() ) {
349
       prefix = (String)prefixes.nextElement();
350
       nsuri = (String)namespaces.get(prefix);
351
       currentNode.setNamespace(prefix, nsuri, docid);
352
     }
353
     namespaces = null;
354
     namespaces = new Hashtable();
355

    
356
     // Add all of the attributes
357
     for (int i=0; i<atts.getLength(); i++) 
358
     {
359
       String attributeName = atts.getQName(i);
360
       String attributeValue = atts.getValue(i);
361
       endNodeId = 
362
                currentNode.setAttribute(attributeName, attributeValue, docid);
363
       
364
       // To handle name space and schema location if the attribute name is
365
       // xsi:schemaLocation. If the name space is in not in catalog table
366
       // it will be regeistered.
367
       if (attributeName != null && 
368
           attributeName.indexOf(MetaCatServlet.SCHEMALOCATIONKEYWORD) != -1)
369
       {
370
         SchemaLocationResolver resolver = 
371
                                  new SchemaLocationResolver(attributeValue);
372
         resolver.resolveNameSpace();
373
         
374
       }
375
     }
376

    
377
     // Add the node to the stack, so that any text data can be
378
     // added as it is encountered
379
     nodeStack.push(currentNode);
380
     // Add the node to the vector used by thread for writing XML Index
381
     nodeIndex.addElement(currentNode);
382
     // start parsing triple
383
     if ( doctype != null && 
384
         MetaCatUtil.getOptionList(MetaCatUtil.getOption("packagedoctype")).contains(doctype)
385
         && localName.equals("triple"))
386
      {
387
        startParseTriple = true;
388
        hasTriple = true;
389
        currentTriple = new Triple();
390
      }
391
  }
392

    
393
  /* The run method of xmlIndex thread. It writes XML Index for the document. */
394
  public void run () {
395
    DBSAXNode currNode = null;
396
    DBSAXNode prevNode = null;
397
    DBConnection dbConn = null;
398
    int serialNumber = -1;
399
    String doctype = currentDocument.getDoctype();
400
    int step = 0;
401
    int counter = 0;
402

    
403
    try 
404
    {
405
      // stop 5 second
406
      Thread.sleep(5000);
407
      // Opening separate db connection for writing XML Index
408
      dbConn=DBConnectionPool.getDBConnection("DBSAXHandler.run");
409
      serialNumber=dbConn.getCheckOutSerialNumber();
410
      dbConn.setAutoCommit(false);
411
      //make sure record is done
412
      checkDocumentTable();   
413
      // Going through the elements of the document and writing its Index
414
      Enumeration nodes = nodeIndex.elements();
415
      while ( nodes.hasMoreElements() ) {
416
        currNode = (DBSAXNode)nodes.nextElement();
417
        currNode.updateNodeIndex(dbConn, docid, doctype);
418
      }
419
      dbConn.commit();
420

    
421
    } catch (Exception e) {
422
      try {
423
        dbConn.rollback();
424
        //dbconn.close();
425
      } catch (SQLException sqle) {}
426
      MetaCatUtil.debugMessage("Error in DBSAXHandler.run " + 
427
                                e.getMessage(), 30);
428
      
429
    }
430
    finally
431
    {
432
      DBConnectionPool.returnDBConnection(dbConn, serialNumber);
433
    }//finally
434
  }
435

    
436
  /* method to make sure insert is finished before create index table
437
   * If new version of record is in xml_documents every thing will be fine
438
   */
439
  private void checkDocumentTable() throws Exception
440
  {
441
    
442
    DBConnection dbConn = null;
443
    int serialNumber = -1;
444
   
445
    try 
446
    {
447
       // Opening separate db connection for writing XML Index
448
      dbConn=DBConnectionPool.getDBConnection("DBSAXHandler.checkDocumentTable");
449
      serialNumber=dbConn.getCheckOutSerialNumber();
450
     
451
      //the following while loop construct checks to make sure that the docid
452
      //of the document that we are trying to index is already
453
      //in the xml_documents table.  if this is not the case, the foreign
454
      //key relationship between xml_documents and xml_index is temporarily
455
      //broken causing multiple problems.
456
      boolean inxmldoc = false;
457
      long startTime = System.currentTimeMillis();
458
      while(!inxmldoc)
459
      {
460
        String xmlDocumentsCheck = "select distinct docid from xml_documents" +
461
                                   " where docid ='" + docid + "' and " +
462
                                   " rev ='" + revision + "'";
463
       
464
        PreparedStatement xmlDocCheck =
465
                                 dbConn.prepareStatement(xmlDocumentsCheck);
466
        // Increase usage count
467
        dbConn.increaseUsageCount(1);
468
        xmlDocCheck.execute();
469
        ResultSet doccheckRS = xmlDocCheck.getResultSet();
470
        boolean tableHasRows = doccheckRS.next();
471
        if (tableHasRows)
472
        {
473
           MetaCatUtil.debugMessage("=========== find the correct document", 35);
474
           inxmldoc = true;
475
        }
476
         doccheckRS.close();
477
         xmlDocCheck.close();
478
         // make sure the while loop will be ended in reseaonable time
479
         long stopTime = System.currentTimeMillis();
480
         if ((stopTime - startTime) > INDEXDELAY)
481
         {
482
           throw new Exception("Couldn't find the docid for index build in" +
483
                              "reseaonable time!");
484
         }
485
       }//while
486
    } 
487
    catch (Exception e) 
488
    {
489
      try 
490
      {
491
        dbConn.rollback();
492
        //dbconn.close();
493
      } 
494
      catch (SQLException sqle) 
495
      {}
496
      MetaCatUtil.debugMessage("Error in DBSAXHandler.run " + 
497
                                e.getMessage(), 30);
498
      
499
    }
500
    finally
501
    {
502
      DBConnectionPool.returnDBConnection(dbConn, serialNumber);
503
    }//finally
504
  
505
  }
506
  
507
  /** SAX Handler that is called for each XML text node */
508
  public void characters(char[] cbuf, int start, int len) throws SAXException 
509
  {
510
     MetaCatUtil.debugMessage("CHARACTERS", 50);
511
     // buffer all text nodes for same element. This is for text was splited
512
     // into different nodes
513
     textBuffer.append(new String(cbuf, start,len));
514
     // set hittextnode true
515
     hitTextNode = true;
516
     // if text buffer .size is greater than max, write it to db. 
517
     // so we can save memory
518
     if (textBuffer.length() > MAXDATACHARS)
519
     {
520
       MetaCatUtil.debugMessage("Write text into DB in charaters" + 
521
                   " when text buffer size is greater than maxmum number", 50);
522
       DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
523
       endNodeId = writeTextForDBSAXNode(endNodeId, textBuffer, currentNode);
524
       textBuffer = null;
525
       textBuffer = new StringBuffer();
526
     }
527
  }
528

    
529
   /**
530
    * SAX Handler that is called for each XML text node that is
531
    * Ignorable white space
532
    */
533
   public void ignorableWhitespace(char[] cbuf, int start, int len)
534
               throws SAXException {
535
     // When validation is turned "on", white spaces are reported here
536
     // When validation is turned "off" white spaces are not reported here,
537
     // but through characters() callback
538
     MetaCatUtil.debugMessage("IGNORABLEWHITESPACE", 50);
539

    
540

    
541
     DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
542
     String data = null;
543
     int leftover = len;
544
     int offset = start;
545
     boolean moredata = true;
546

    
547
     // This loop deals with the case where there are more characters
548
     // than can fit in a single database text field (limit is
549
     // MAXDATACHARS).  If the text to be inserted exceeds MAXDATACHARS,
550
     // write a series of nodes that are MAXDATACHARS long, and then the
551
     // final node contains the remainder
552
     while (moredata) {
553
       if (leftover > MAXDATACHARS) {
554
         data = new String(cbuf, offset, MAXDATACHARS);
555
         leftover -= MAXDATACHARS;
556
         offset += MAXDATACHARS;
557
       } else {
558
         data = new String(cbuf, offset, leftover);
559
         moredata = false;
560
       }
561

    
562
       // Write the content of the node to the database
563
       endNodeId = currentNode.writeChildNodeToDB("TEXT", null, data, docid);
564
     }
565
   }
566

    
567
   /**
568
    * SAX Handler called once for each processing instruction found:
569
    * node that PI may occur before or after the root element.
570
    */
571
   public void processingInstruction(String target, String data)
572
          throws SAXException {
573
     MetaCatUtil.debugMessage("PI", 50);
574
     DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
575
     endNodeId = currentNode.writeChildNodeToDB("PI", target, data, docid);
576
   }
577

    
578
   /** SAX Handler that is called at the end of each XML element */
579
   public void endElement(String uri, String localName,
580
                          String qName) throws SAXException {
581
     MetaCatUtil.debugMessage("End ELEMENT " + qName, 50);
582
     
583
     // write buffered text nodes into db (so no splited)
584
     DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
585
      
586
     // If before the end element, the parser hit text nodes and store them
587
     // into the buffer, write the buffer to data base. The reason we put
588
     // write database here is for xerces some time split text node
589
     if (hitTextNode)
590
     {
591
       MetaCatUtil.debugMessage("Write text into DB in End Element", 50);
592
       endNodeId = writeTextForDBSAXNode(endNodeId, textBuffer, currentNode);
593
       
594
       //if it is triple parsing process
595
       if (startParseTriple)
596
       {
597
         
598
          String content = textBuffer.toString().trim();
599
          if(localName.equals("subject"))
600
          { //get the subject content
601
            currentTriple.setSubject(content);
602
          }
603
          else if(localName.equals("relationship"))
604
          { //get the relationship content
605
            currentTriple.setRelationship(content);
606
          }
607
          else if(localName.equals("object"))
608
          { //get the object content
609
            currentTriple.setObject(content);
610
          }
611
       }
612
       
613
     }//if
614
     
615
     //set hitText false
616
     hitTextNode = false;
617
     // reset textbuff
618
     textBuffer = null;
619
     textBuffer = new StringBuffer();
620

    
621
     // Get the node from the stack
622
     currentNode = (DBSAXNode)nodeStack.pop();
623
     //finishing parsing single triple
624
     if (startParseTriple && localName.equals("triple"))
625
      {
626
        // add trip to triple collection
627
        tripleList.addTriple(currentTriple);
628
        //rest variable
629
        currentTriple = null;
630
        startParseTriple = false;   
631
      }
632
   }
633

    
634
   //
635
   // the next section implements the LexicalHandler interface
636
   //
637

    
638
   /** SAX Handler that receives notification of DOCTYPE. Sets the DTD */
639
   public void startDTD(String name, String publicId, String systemId)
640
               throws SAXException {
641
     docname = name;
642
     doctype = publicId;
643
     systemid = systemId;
644

    
645
     processingDTD = true;
646
     DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
647
     //create a DTD node and write docname,publicid and system id into db
648
     // we don't put the dtd node into node stack
649
     DBSAXNode dtdNode = new DBSAXNode (connection, name, publicId, systemId, 
650
                               currentNode, currentNode.getRootNodeID(), docid);
651
     MetaCatUtil.debugMessage("Start DTD", 50);
652
     MetaCatUtil.debugMessage("Setting processingDTD to true", 50);
653
     MetaCatUtil.debugMessage("DOCNAME: " + docname, 50);
654
     MetaCatUtil.debugMessage("DOCTYPE: " + doctype, 50);
655
     MetaCatUtil.debugMessage("  SYSID: " + systemid, 50);
656
   }
657

    
658
   /**
659
    * SAX Handler that receives notification of end of DTD
660
    */
661
   public void endDTD() throws SAXException {
662

    
663
     processingDTD = false;
664
     MetaCatUtil.debugMessage("Setting processingDTD to false", 50);
665
     MetaCatUtil.debugMessage("end DTD", 50);
666
   }
667

    
668
   /**
669
    * SAX Handler that receives notification of comments in the DTD
670
    */
671
   public void comment(char[] ch, int start, int length) throws SAXException {
672
     MetaCatUtil.debugMessage("COMMENT", 50);
673
     if ( !processingDTD ) {
674
       DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
675
       endNodeId = 
676
        currentNode.writeChildNodeToDB("COMMENT", null, 
677
                                       new String(ch, start, length), docid);
678
     }
679
   }
680

    
681
   /**
682
    * SAX Handler that receives notification of the start of CDATA sections
683
    */
684
   public void startCDATA() throws SAXException {
685
     MetaCatUtil.debugMessage("start CDATA", 50);
686
   }
687

    
688
   /**
689
    * SAX Handler that receives notification of the end of CDATA sections
690
    */
691
   public void endCDATA() throws SAXException {
692
     MetaCatUtil.debugMessage("end CDATA", 50);
693
   }
694

    
695
   /**
696
    * SAX Handler that receives notification of the start of entities
697
    */
698
   public void startEntity(String name) throws SAXException {
699
     MetaCatUtil.debugMessage("start ENTITY: " + name, 50);
700
//System.out.println("start ENTITY: " + name);
701
     if (name.equals("[dtd]")) {
702
       processingDTD = true;
703
     }
704
   }
705

    
706
   /**
707
    * SAX Handler that receives notification of the end of entities
708
    */
709
   public void endEntity(String name) throws SAXException {
710
     MetaCatUtil.debugMessage("end ENTITY: " + name, 50);
711
//System.out.println("end ENTITY: " + name);
712
     if (name.equals("[dtd]")) {
713
       processingDTD = false;
714
     }
715
   }
716

    
717
   /**
718
    * SAX Handler that receives notification of element declarations
719
    */
720
   public void elementDecl(String name, String model)
721
                        throws org.xml.sax.SAXException {
722
//System.out.println("ELEMENTDECL: " + name + " " + model);
723
     MetaCatUtil.debugMessage("ELEMENTDECL: " + name + " " + model, 50);
724
   }
725

    
726
   /**
727
    * SAX Handler that receives notification of attribute declarations
728
    */
729
   public void attributeDecl(String eName, String aName,
730
                        String type, String valueDefault, String value)
731
                        throws org.xml.sax.SAXException {
732

    
733
//System.out.println("ATTRIBUTEDECL: " + eName + " "
734
//                        + aName + " " + type + " " + valueDefault + " "
735
//                        + value);
736
     MetaCatUtil.debugMessage("ATTRIBUTEDECL: " + eName + " "
737
                        + aName + " " + type + " " + valueDefault + " "
738
                        + value, 50);
739
   }
740

    
741
   /**
742
    * SAX Handler that receives notification of internal entity declarations
743
    */
744
   public void internalEntityDecl(String name, String value)
745
                        throws org.xml.sax.SAXException {
746
//System.out.println("INTERNENTITYDECL: " + name + " " + value);
747
     MetaCatUtil.debugMessage("INTERNENTITYDECL: " + name + " " + value, 50);
748
   }
749

    
750
   /**
751
    * SAX Handler that receives notification of external entity declarations
752
    */
753
   public void externalEntityDecl(String name, String publicId,
754
                        String systemId)
755
                        throws org.xml.sax.SAXException {
756
//System.out.println("EXTERNENTITYDECL: " + name + " " + publicId
757
//                              + " " + systemId);
758
     MetaCatUtil.debugMessage("EXTERNENTITYDECL: " + name + " " + publicId
759
                              + " " + systemId, 50);
760
     // it processes other external entity, not the DTD;
761
     // it doesn't signal for the DTD here
762
     processingDTD = false;
763
   }
764

    
765
   //
766
   // the next section implements the ErrorHandler interface
767
   //
768

    
769
   /**
770
    * SAX Handler that receives notification of fatal parsing errors
771
    */
772
   public void fatalError(SAXParseException exception) throws SAXException {
773
     MetaCatUtil.debugMessage("FATALERROR: "+exception.getMessage(), 50);
774
     throw (new SAXException("Fatal processing error.", exception));
775
   }
776

    
777
   /**
778
    * SAX Handler that receives notification of recoverable parsing errors
779
    */
780
   public void error(SAXParseException exception) throws SAXException {
781
     MetaCatUtil.debugMessage("ERROR: "+exception.getMessage(), 50);
782
     throw (new SAXException("Processing error.", exception));
783
   }
784

    
785
   /**
786
    * SAX Handler that receives notification of warnings
787
    */
788
   public void warning(SAXParseException exception) throws SAXException {
789
     MetaCatUtil.debugMessage("WARNING: "+exception.getMessage(), 50);
790
     throw (new SAXException("Warning.", exception));
791
   }
792

    
793
   //
794
   // Helper, getter and setter methods
795
   //
796

    
797
   /**
798
    * get the document name
799
    */
800
   public String getDocname() {
801
     return docname;
802
   }
803

    
804
   /**
805
    * get the document processing state
806
    */
807
   public boolean processingDTD() {
808
     return processingDTD;
809
   }
810
   
811
   /* Method to write a text buffer for DBSAXNode*/
812
   protected long writeTextForDBSAXNode(long previousEndNodeId, 
813
                                        StringBuffer strBuffer, DBSAXNode node)
814
                                        throws SAXException
815
   {
816
     long nodeId = previousEndNodeId;
817
     // Check parameter
818
     if ( strBuffer == null || node == null)
819
     {
820
       return nodeId;
821
     }
822
     boolean moredata = true;
823
     String data = null;
824
     int bufferSize = strBuffer.length();
825
     int start = 0;
826
    
827
     // if there are some cotent in buffer, write it
828
     if (bufferSize > 0)
829
     {
830
       MetaCatUtil.debugMessage("Write text into DB", 50);
831
       // This loop deals with the case where there are more characters
832
       // than can fit in a single database text field (limit is
833
       // MAXDATACHARS).  If the text to be inserted exceeds MAXDATACHARS,
834
       // write a series of nodes that are MAXDATACHARS long, and then the
835
       // final node contains the remainder
836
       while (moredata) 
837
       {
838
          bufferSize = strBuffer.length();
839
          if (bufferSize > MAXDATACHARS) 
840
         {
841
            data = strBuffer.substring(start, MAXDATACHARS);
842
            // cut the stringbuffer part that already written into db
843
            strBuffer = strBuffer.delete(start, MAXDATACHARS);
844
          } 
845
          else 
846
          {
847
            data = strBuffer.substring(start, bufferSize);
848
            moredata = false;
849
          }
850
       
851
          // Write the content of the node to the database
852
          nodeId = node.writeChildNodeToDB("TEXT", null, data, docid);
853
        }//while
854
     }//if
855
     return nodeId;
856
   }
857
}
(23-23/58)