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: sgarg $'
11
 *     '$Date: 2005-11-02 10:31:32 -0800 (Wed, 02 Nov 2005) $'
12
 * '$Revision: 2715 $'
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.PreparedStatement;
32
import java.sql.ResultSet;
33
import java.sql.SQLException;
34
import java.sql.Statement;
35
import java.util.EmptyStackException;
36
import java.util.Enumeration;
37
import java.util.Hashtable;
38
import java.util.Stack;
39
import java.util.Vector;
40

    
41
import edu.ucsb.nceas.morpho.datapackage.Triple;
42
import edu.ucsb.nceas.morpho.datapackage.TripleCollection;
43

    
44
import org.apache.log4j.Logger;
45
import org.xml.sax.Attributes;
46
import org.xml.sax.SAXException;
47
import org.xml.sax.SAXParseException;
48
import org.xml.sax.ext.DeclHandler;
49
import org.xml.sax.ext.LexicalHandler;
50
import org.xml.sax.helpers.DefaultHandler;
51

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

    
60
    protected boolean atFirstElement;
61

    
62
    protected boolean processingDTD;
63

    
64
    protected String docname = null;
65

    
66
    protected String doctype;
67
    
68
    protected String catalogid = null;
69

    
70
    protected String systemid;
71

    
72
    private boolean stackCreated = false;
73

    
74
    protected Stack nodeStack;
75

    
76
    protected Vector nodeIndex;
77

    
78
    protected DBConnection connection = null;
79

    
80
    protected DocumentImpl currentDocument;
81
    
82
    protected String createDate = null;
83
    
84
    protected String updateDate = null;
85

    
86
    protected DBSAXNode rootNode;
87

    
88
    protected String action = null;
89

    
90
    protected String docid = null;
91

    
92
    protected String revision = null;
93

    
94
    protected String user = null;
95

    
96
    protected String[] groups = null;
97

    
98
    protected String pub = null;
99

    
100
    protected Thread xmlIndex;
101

    
102
    private boolean endDocument = false;
103

    
104
    protected int serverCode = 1;
105

    
106
    protected Hashtable namespaces = new Hashtable();
107

    
108
    protected boolean hitTextNode = false; // a flag to hit text node
109

    
110
    // a buffer to keep all text nodes for same element
111
    // it is for element was splited
112
    protected StringBuffer textBuffer = new StringBuffer();
113

    
114
    protected Stack textBufferStack = new Stack();
115

    
116
    protected static final int MAXDATACHARS = 4000;
117

    
118
    //protected static final int MAXDATACHARS = 50;
119
    protected static final long INDEXDELAY = 10000;
120

    
121
    // methods writeChildNodeToDB, setAttribute, setNamespace,
122
    // writeTextForDBSAXNode will increase endNodeId.
123
    protected long endNodeId = -1; // The end node id for a substree
124
    // DOCTITLE attr cleared from the db
125
    //   private static final int MAXTITLELEN = 1000;
126
    
127
    private boolean isRevisionDoc  = false;
128

    
129
    //HandlerTriple stuff
130
    TripleCollection tripleList = new TripleCollection();
131

    
132
    Triple currentTriple = new Triple();
133

    
134
    boolean startParseTriple = false;
135

    
136
    boolean hasTriple = false;
137

    
138
    public static final String ECOGRID = "ecogrid://";
139

    
140
    private Logger logMetacat = Logger.getLogger(DBSAXHandler.class);
141

    
142
    /**
143
     * Construct an instance of the handler class
144
     *
145
     * @param conn the JDBC connection to which information is written
146
     */
147
    private DBSAXHandler(DBConnection conn, String createDate, String updateDate)
148
    {
149
        this.connection = conn;
150
        this.atFirstElement = true;
151
        this.processingDTD = false;
152
        this.createDate = createDate;
153
        this.updateDate = updateDate;
154

    
155
        // Create the stack for keeping track of node context
156
        // if it doesn't already exist
157
        if (!stackCreated) {
158
            nodeStack = new Stack();
159
            nodeIndex = new Vector();
160
            stackCreated = true;
161
        }
162
    }
163

    
164
    /**
165
     * Construct an instance of the handler class
166
     *
167
     * @param conn the JDBC connection to which information is written
168
     * @param action - "INSERT" or "UPDATE"
169
     * @param docid to be inserted or updated into JDBC connection
170
     * @param user the user connected to MetaCat servlet and owns the document
171
     * @param groups the groups to which user belongs
172
     * @param pub flag for public "read" access on document
173
     * @param serverCode the serverid from xml_replication on which this
174
     *            document resides.
175
     *
176
     */
177
/* TODO excise this constructor because not used anywhere in project
178
    public DBSAXHandler(DBConnection conn, String action, String docid,
179
            String user, String[] groups, String pub, int serverCode)
180
    {
181
        this(conn);
182
        this.action = action;
183
        this.docid = docid;
184
        this.user = user;
185
        this.groups = groups;
186
        this.pub = pub;
187
        this.serverCode = serverCode;
188
        this.xmlIndex = new Thread(this);
189
    }
190
*/
191
    /**
192
     * Construct an instance of the handler class In this constructor, user can
193
     * specify the version need to upadate
194
     *
195
     * @param conn the JDBC connection to which information is written
196
     * @param action - "INSERT" or "UPDATE"
197
     * @param docid to be inserted or updated into JDBC connection
198
     * @param revision, the user specified the revision need to be update
199
     * @param user the user connected to MetaCat servlet and owns the document
200
     * @param groups the groups to which user belongs
201
     * @param pub flag for public "read" access on document
202
     * @param serverCode the serverid from xml_replication on which this
203
     *            document resides.
204
     *
205
     */
206
    public DBSAXHandler(DBConnection conn, String action, String docid,
207
            String revision, String user, String[] groups, String pub,
208
            int serverCode, String createDate, String updateDate)
209
    {
210
        this(conn, createDate, updateDate);
211
        this.action = action;
212
        this.docid = docid;
213
        this.revision = revision;
214
        this.user = user;
215
        this.groups = groups;
216
        this.pub = pub;
217
        this.serverCode = serverCode;
218
        this.xmlIndex = new Thread(this);
219
    }
220

    
221
    /** SAX Handler that receives notification of beginning of the document */
222
    public void startDocument() throws SAXException
223
    {
224
        logMetacat.info("start Document");
225

    
226
        // Create the document node representation as root
227
        rootNode = new DBSAXNode(connection, this.docid);
228
        // Add the node to the stack, so that any text data can be
229
        // added as it is encountered
230
        nodeStack.push(rootNode);
231
    }
232

    
233
    /** SAX Handler that receives notification of end of the document */
234
    public void endDocument() throws SAXException
235
    {
236
        logMetacat.info("end Document");
237
        // Starting new thread for writing XML Index.
238
        // It calls the run method of the thread.
239

    
240
        //if it is data package insert triple into relationtion table;
241
        if (doctype != null
242
                && MetaCatUtil.getOptionList(
243
                        MetaCatUtil.getOption("packagedoctype")).contains(
244
                        doctype) && hasTriple && !isRevisionDoc) {
245
            try {
246
                //initial handler and write into relationdb only for xml-documents
247
                if (!isRevisionDoc)
248
                {
249
                  RelationHandler handler = new RelationHandler(docid, doctype,
250
                        connection, tripleList);
251
                }
252
            } catch (Exception e) {
253
                logMetacat.error(
254
                        "Failed to write triples into relation table"
255
                                + e.getMessage());
256
                throw new SAXException(
257
                        "Failed to write triples into relation table "
258
                                + e.getMessage());
259
            }
260
        }
261
    }
262

    
263
    /** SAX Handler that is called at the start of Namespace */
264
    public void startPrefixMapping(String prefix, String uri)
265
            throws SAXException
266
    {
267
        logMetacat.info("NAMESPACE");
268

    
269
        namespaces.put(prefix, uri);
270
    }
271

    
272
    /** SAX Handler that is called at the start of each XML element */
273
    public void startElement(String uri, String localName, String qName,
274
            Attributes atts) throws SAXException
275
    {
276
        // for element <eml:eml...> qname is "eml:eml", local name is "eml"
277
        // for element <acl....> both qname and local name is "eml"
278
        // uri is namesapce
279
        logMetacat.info("Start ELEMENT(qName) " + qName);
280
        logMetacat.info("Start ELEMENT(localName) " + localName);
281
        logMetacat.info("Start ELEMENT(uri) " + uri);
282

    
283
        DBSAXNode parentNode = null;
284
        DBSAXNode currentNode = null;
285

    
286
        // Get a reference to the parent node for the id
287
        try {
288
            
289
            parentNode = (DBSAXNode) nodeStack.peek();
290
        } catch (EmptyStackException e) {
291
            parentNode = null;
292
        }
293

    
294
        // If hit a text node, we need write this text for current's parent
295
        // node
296
        // This will happend if the element is mixted
297
        if (hitTextNode && parentNode != null) {
298
            // write the textbuffer into db for parent node.
299
            endNodeId = writeTextForDBSAXNode(endNodeId, textBuffer, parentNode);
300
            // rest hitTextNode
301
            hitTextNode = false;
302
            // reset textbuffer
303
            textBuffer = null;
304
            textBuffer = new StringBuffer();
305
           
306
        }
307
        
308
        // Document representation that points to the root document node
309
        if (atFirstElement) {
310
            atFirstElement = false;
311
            // If no DOCTYPE declaration: docname = root element
312
            // doctype = root element name or name space
313
            if (docname == null) {
314
                docname = localName;
315
                // if uri isn't null doctype = uri(namespace)
316
                // othewise root element
317
                if (uri != null && !(uri.trim()).equals("")) {
318
                    doctype = uri;
319
                } else {
320
                    doctype = docname;
321
                }
322
                logMetacat.info("DOCNAME-a: " + docname);
323
                logMetacat.info("DOCTYPE-a: " + doctype);
324
            } else if (doctype == null) {
325
                // because docname is not null and it is declared in dtd
326
                // so could not be in schema, no namespace
327
                doctype = docname;
328
                logMetacat.info("DOCTYPE-b: " + doctype);
329
            }
330
           
331
            rootNode.writeNodename(docname);
332
          
333
            try {
334
                // for validated XML Documents store a reference to XML DB
335
                // Catalog
336
                // Because this is select statement and it needn't to roll back
337
                // if
338
                // insert document action fialed.
339
                // In order to decrease DBConnection usage count, we get a new
340
                // dbconnection from pool
341
               
342
                DBConnection dbConn = null;
343
                int serialNumber = -1;
344
               
345
                if (systemid != null) {
346
                    try {
347
                        // Get dbconnection
348
                        dbConn = DBConnectionPool
349
                                .getDBConnection("DBSAXHandler.startElement");
350
                        serialNumber = dbConn.getCheckOutSerialNumber();
351

    
352
                        Statement stmt = dbConn.createStatement();
353
                        ResultSet rs = stmt
354
                                .executeQuery("SELECT catalog_id FROM xml_catalog "
355
                                        + "WHERE entry_type = 'DTD' "
356
                                        + "AND public_id = '" + doctype + "'");
357
                        boolean hasRow = rs.next();
358
                        if (hasRow) {
359
                            catalogid = rs.getString(1);
360
                        }
361
                        stmt.close();
362
                    }//try
363
                    finally {
364
                        // Return dbconnection
365
                        DBConnectionPool.returnDBConnection(dbConn,
366
                                serialNumber);
367
                    }//finally
368
                }
369

    
370
                //create documentImpl object by the constructor which can
371
                // specify
372
                //the revision
373
              
374
                if (!isRevisionDoc)
375
                {
376
                  //System.out.println("here!!!!!!!!!!!3");
377
                  currentDocument = new DocumentImpl(connection, rootNode
378
                        .getNodeID(), docname, doctype, docid, revision,
379
                        action, user, this.pub, catalogid, this.serverCode, 
380
                        createDate, updateDate);
381
                  //System.out.println("here!!!!!!!!!!!4");
382
                }
383
               
384

    
385
            } catch (Exception ane) {
386
                ane.printStackTrace();
387
                throw (new SAXException("Error in DBSaxHandler.startElement "
388
                        + action, ane));
389
            }
390
        }
391

    
392
        // Create the current node representation
393
        currentNode = new DBSAXNode(connection, qName, localName,
394
                parentNode, rootNode.getNodeID(), docid, doctype);
395

    
396
        // Add all of the namespaces
397
        String prefix;
398
        String nsuri;
399
        Enumeration prefixes = namespaces.keys();
400
        while (prefixes.hasMoreElements()) {
401
            prefix = (String) prefixes.nextElement();
402
            nsuri = (String) namespaces.get(prefix);
403
            currentNode.setNamespace(prefix, nsuri, docid);
404
        }
405
        namespaces = null;
406
        namespaces = new Hashtable();
407

    
408
        // Add all of the attributes
409
        for (int i = 0; i < atts.getLength(); i++) {
410
            String attributeName = atts.getQName(i);
411
            String attributeValue = atts.getValue(i);
412
            endNodeId = currentNode.setAttribute(attributeName, attributeValue,
413
                    docid);
414

    
415
            // To handle name space and schema location if the attribute name
416
            // is
417
            // xsi:schemaLocation. If the name space is in not in catalog table
418
            // it will be regeistered.
419
            if (attributeName != null
420
                    && attributeName
421
                            .indexOf(MetaCatServlet.SCHEMALOCATIONKEYWORD) != -1) {
422
                SchemaLocationResolver resolver = new SchemaLocationResolver(
423
                        attributeValue);
424
                resolver.resolveNameSpace();
425

    
426
            }
427
        }
428

    
429
        // Add the node to the stack, so that any text data can be
430
        // added as it is encountered
431
        nodeStack.push(currentNode);
432
        // Add the node to the vector used by thread for writing XML Index
433
        nodeIndex.addElement(currentNode);
434
        // start parsing triple
435
        if (doctype != null
436
                && MetaCatUtil.getOptionList(
437
                        MetaCatUtil.getOption("packagedoctype")).contains(
438
                        doctype) && localName.equals("triple")) {
439
            startParseTriple = true;
440
            hasTriple = true;
441
            currentTriple = new Triple();
442
        }
443
    }
444

    
445
    public void runIndexingThread(){
446
    	
447
        boolean useXMLIndex =
448
            (new Boolean(MetaCatUtil.getOption("usexmlindex"))).booleanValue();
449
        if (useXMLIndex && !isRevisionDoc) {
450
            try {
451
                xmlIndex.start();
452
            } catch (NullPointerException e) {
453
                xmlIndex = null;
454
                logMetacat.error("Error in DBSAXHandler.runIndexingThread() "
455
                        + e.getMessage());
456
            }
457
        }
458
    }
459
    
460
    /*
461
     * Run a separate thread to build the XML index for this document.  This
462
     * thread is run asynchronously in order to more quickly return control to
463
     * the submitting user.  The run method checks to see if the document has
464
     * been fully inserted before trying to update the xml_index table.
465
     */
466
    public void run()
467
    {
468
        try {
469
            if (!isRevisionDoc)
470
            {
471
             // stop 5 second
472
             Thread.sleep(5000);
473
             //make sure record is done
474
             checkDocumentTable();
475
             // Build the index for this document
476
             currentDocument.buildIndex();
477
            }
478
        } catch (Exception e) {
479
            logMetacat.error("Error in DBSAXHandler.run "
480
                    + e.getMessage());
481
        }
482
    }
483

    
484
    /*
485
     * method to make sure insert is finished before create index table If new
486
     * version of record is in xml_documents every thing will be fine
487
     */
488
    private void checkDocumentTable() throws Exception
489
    {
490

    
491
        DBConnection dbConn = null;
492
        int serialNumber = -1;
493

    
494
        try {
495
            // Opening separate db connection for writing XML Index
496
            dbConn = DBConnectionPool
497
                    .getDBConnection("DBSAXHandler.checkDocumentTable");
498
            serialNumber = dbConn.getCheckOutSerialNumber();
499

    
500
            // the following while loop construct checks to make sure that
501
            // the docid of the document that we are trying to index is already
502
            // in the xml_documents table. if this is not the case, the foreign
503
            // key relationship between xml_documents and xml_index is
504
            // temporarily broken causing multiple problems.
505
            boolean inxmldoc = false;
506
            long startTime = System.currentTimeMillis();
507
            while (!inxmldoc) {
508
                String xmlDocumentsCheck = "select distinct docid from xml_documents"
509
                        + " where docid ='"
510
                        + docid
511
                        + "' and "
512
                        + " rev ='"
513
                        + revision + "'";
514

    
515
                PreparedStatement xmlDocCheck = dbConn
516
                        .prepareStatement(xmlDocumentsCheck);
517
                // Increase usage count
518
                dbConn.increaseUsageCount(1);
519
                xmlDocCheck.execute();
520
                ResultSet doccheckRS = xmlDocCheck.getResultSet();
521
                boolean tableHasRows = doccheckRS.next();
522
                if (tableHasRows) {
523
                    logMetacat.info(
524
                            "=========== found the correct document");
525
                    inxmldoc = true;
526
                }
527
                doccheckRS.close();
528
                xmlDocCheck.close();
529
                // make sure the while loop will be ended in reseaonable time
530
                long stopTime = System.currentTimeMillis();
531
                if ((stopTime - startTime) > INDEXDELAY) { 
532
                  	logMetacat.warn("Couldn't find the docid for index build in "
533
                            + "reseaonable time!");
534
                	throw new Exception(
535
                        "Couldn't find the docid for index build in "
536
                                + "reseaonable time!"); 
537
                }
538
            }//while
539
        } catch (Exception e) {
540
            try {
541
                dbConn.rollback();
542
                //dbconn.close();
543
            } catch (SQLException sqle) {
544
            }
545
            logMetacat.error("Error in DBSAXHandler.checkDocumentTable "
546
                    + e.getMessage());
547

    
548
        } finally {
549
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
550
        }//finally
551

    
552
    }
553

    
554
    /** SAX Handler that is called for each XML text node */
555
    public void characters(char[] cbuf, int start, int len) throws SAXException
556
    {
557
        logMetacat.info("CHARACTERS");
558
        // buffer all text nodes for same element. This is for text was splited
559
        // into different nodes
560
        textBuffer.append(new String(cbuf, start, len));
561
        // set hittextnode true
562
        hitTextNode = true;
563
        // if text buffer .size is greater than max, write it to db.
564
        // so we can save memory
565
        if (textBuffer.length() > MAXDATACHARS) {
566
            logMetacat.info("Write text into DB in charaters"
567
                    + " when text buffer size is greater than maxmum number");
568
            DBSAXNode currentNode = (DBSAXNode) nodeStack.peek();
569
            endNodeId = writeTextForDBSAXNode(endNodeId, textBuffer,
570
                    currentNode);
571
            textBuffer = null;
572
            textBuffer = new StringBuffer();
573
        }
574
    }
575

    
576
    /**
577
     * SAX Handler that is called for each XML text node that is Ignorable
578
     * white space
579
     */
580
    public void ignorableWhitespace(char[] cbuf, int start, int len)
581
            throws SAXException
582
    {
583
        // When validation is turned "on", white spaces are reported here
584
        // When validation is turned "off" white spaces are not reported here,
585
        // but through characters() callback
586
        logMetacat.info("IGNORABLEWHITESPACE");
587

    
588
        DBSAXNode currentNode = (DBSAXNode) nodeStack.peek();
589
        String data = null;
590
        int leftover = len;
591
        int offset = start;
592
        boolean moredata = true;
593

    
594
        // This loop deals with the case where there are more characters
595
        // than can fit in a single database text field (limit is
596
        // MAXDATACHARS). If the text to be inserted exceeds MAXDATACHARS,
597
        // write a series of nodes that are MAXDATACHARS long, and then the
598
        // final node contains the remainder
599
        while (moredata) {
600
            if (leftover > MAXDATACHARS) {
601
                data = new String(cbuf, offset, MAXDATACHARS);
602
                leftover -= MAXDATACHARS;
603
                offset += MAXDATACHARS;
604
            } else {
605
                data = new String(cbuf, offset, leftover);
606
                moredata = false;
607
            }
608

    
609
            // Write the content of the node to the database
610
            endNodeId = currentNode.writeChildNodeToDB("TEXT", null, data,
611
                    docid);
612
        }
613
    }
614

    
615
    /**
616
     * SAX Handler called once for each processing instruction found: node that
617
     * PI may occur before or after the root element.
618
     */
619
    public void processingInstruction(String target, String data)
620
            throws SAXException
621
    {
622
        logMetacat.info("PI");
623
        DBSAXNode currentNode = (DBSAXNode) nodeStack.peek();
624
        endNodeId = currentNode.writeChildNodeToDB("PI", target, data, docid);
625
    }
626

    
627
    /** SAX Handler that is called at the end of each XML element */
628
    public void endElement(String uri, String localName, String qName)
629
            throws SAXException
630
    {
631
        logMetacat.info("End ELEMENT " + qName);
632

    
633
        // write buffered text nodes into db (so no splited)
634
        DBSAXNode currentNode = (DBSAXNode) nodeStack.peek();
635

    
636
        // If before the end element, the parser hit text nodes and store them
637
        // into the buffer, write the buffer to data base. The reason we put
638
        // write database here is for xerces some time split text node
639
        if (hitTextNode) {
640
            logMetacat.info("Write text into DB in End Element");
641
            endNodeId = writeTextForDBSAXNode(endNodeId, textBuffer,
642
                    currentNode);
643

    
644
            //if it is triple parsing process
645
            if (startParseTriple) {
646

    
647
                String content = textBuffer.toString().trim();
648
                if (localName.equals("subject")) { //get the subject content
649
                    currentTriple.setSubject(content);
650
                } else if (localName.equals("relationship")) { //get the
651
                                                               // relationship
652
                                                               // content
653
                    currentTriple.setRelationship(content);
654
                } else if (localName.equals("object")) { //get the object
655
                                                         // content
656
                    currentTriple.setObject(content);
657
                }
658
            }
659

    
660
        }//if
661

    
662
        //set hitText false
663
        hitTextNode = false;
664
        // reset textbuff
665
        textBuffer = null;
666
        textBuffer = new StringBuffer();
667

    
668
        // Get the node from the stack
669
        currentNode = (DBSAXNode) nodeStack.pop();
670
        //finishing parsing single triple
671
        if (startParseTriple && localName.equals("triple")) {
672
            // add trip to triple collection
673
            tripleList.addTriple(currentTriple);
674
            //rest variable
675
            currentTriple = null;
676
            startParseTriple = false;
677
        }
678
    }
679

    
680
    //
681
    // the next section implements the LexicalHandler interface
682
    //
683

    
684
    /** SAX Handler that receives notification of DOCTYPE. Sets the DTD */
685
    public void startDTD(String name, String publicId, String systemId)
686
            throws SAXException
687
    {
688
        docname = name;
689
        doctype = publicId;
690
        systemid = systemId;
691

    
692
        processingDTD = true;
693
        DBSAXNode currentNode = (DBSAXNode) nodeStack.peek();
694
        //create a DTD node and write docname,publicid and system id into db
695
        // we don't put the dtd node into node stack
696
        DBSAXNode dtdNode = new DBSAXNode(connection, name, publicId, systemId,
697
                currentNode, currentNode.getRootNodeID(), docid);
698
        logMetacat.info("Start DTD");
699
        logMetacat.info("Setting processingDTD to true");
700
        logMetacat.info("DOCNAME: " + docname);
701
        logMetacat.info("DOCTYPE: " + doctype);
702
        logMetacat.info("  SYSID: " + systemid);
703
    }
704

    
705
    /**
706
     * SAX Handler that receives notification of end of DTD
707
     */
708
    public void endDTD() throws SAXException
709
    {
710

    
711
        processingDTD = false;
712
        logMetacat.info("Setting processingDTD to false");
713
        logMetacat.info("end DTD");
714
    }
715

    
716
    /**
717
     * SAX Handler that receives notification of comments in the DTD
718
     */
719
    public void comment(char[] ch, int start, int length) throws SAXException
720
    {
721
        logMetacat.info("COMMENT");
722
        if (!processingDTD) {
723
            DBSAXNode currentNode = (DBSAXNode) nodeStack.peek();
724
            endNodeId = currentNode.writeChildNodeToDB("COMMENT", null,
725
                    new String(ch, start, length), docid);
726
        }
727
    }
728

    
729
    /**
730
     * SAX Handler that receives notification of the start of CDATA sections
731
     */
732
    public void startCDATA() throws SAXException
733
    {
734
        logMetacat.info("start CDATA");
735
    }
736

    
737
    /**
738
     * SAX Handler that receives notification of the end of CDATA sections
739
     */
740
    public void endCDATA() throws SAXException
741
    {
742
        logMetacat.info("end CDATA");
743
    }
744

    
745
    /**
746
     * SAX Handler that receives notification of the start of entities
747
     */
748
    public void startEntity(String name) throws SAXException
749
    {
750
        logMetacat.info("start ENTITY: " + name);
751
        //System.out.println("start ENTITY: " + name);
752
        if (name.equals("[dtd]")) {
753
            processingDTD = true;
754
        }
755
    }
756

    
757
    /**
758
     * SAX Handler that receives notification of the end of entities
759
     */
760
    public void endEntity(String name) throws SAXException
761
    {
762
        logMetacat.info("end ENTITY: " + name);
763
        //System.out.println("end ENTITY: " + name);
764
        if (name.equals("[dtd]")) {
765
            processingDTD = false;
766
        }
767
    }
768

    
769
    /**
770
     * SAX Handler that receives notification of element declarations
771
     */
772
    public void elementDecl(String name, String model)
773
            throws org.xml.sax.SAXException
774
    {
775
        //System.out.println("ELEMENTDECL: " + name + " " + model);
776
        logMetacat.info("ELEMENTDECL: " + name + " " + model);
777
    }
778

    
779
    /**
780
     * SAX Handler that receives notification of attribute declarations
781
     */
782
    public void attributeDecl(String eName, String aName, String type,
783
            String valueDefault, String value) throws org.xml.sax.SAXException
784
    {
785

    
786
        //System.out.println("ATTRIBUTEDECL: " + eName + " "
787
        //                        + aName + " " + type + " " + valueDefault + " "
788
        //                        + value);
789
        logMetacat.info("ATTRIBUTEDECL: " + eName + " " + aName + " "
790
                + type + " " + valueDefault + " " + value);
791
    }
792

    
793
    /**
794
     * SAX Handler that receives notification of internal entity declarations
795
     */
796
    public void internalEntityDecl(String name, String value)
797
            throws org.xml.sax.SAXException
798
    {
799
        //System.out.println("INTERNENTITYDECL: " + name + " " + value);
800
        logMetacat.info("INTERNENTITYDECL: " + name + " " + value);
801
    }
802

    
803
    /**
804
     * SAX Handler that receives notification of external entity declarations
805
     */
806
    public void externalEntityDecl(String name, String publicId, String systemId)
807
            throws org.xml.sax.SAXException
808
    {
809
        //System.out.println("EXTERNENTITYDECL: " + name + " " + publicId
810
        //                              + " " + systemId);
811
        logMetacat.info("EXTERNENTITYDECL: " + name + " " + publicId
812
                + " " + systemId);
813
        // it processes other external entity, not the DTD;
814
        // it doesn't signal for the DTD here
815
        processingDTD = false;
816
    }
817

    
818
    //
819
    // the next section implements the ErrorHandler interface
820
    //
821

    
822
    /**
823
     * SAX Handler that receives notification of fatal parsing errors
824
     */
825
    public void fatalError(SAXParseException exception) throws SAXException
826
    {
827
        logMetacat.fatal("FATALERROR: " + exception.getMessage());
828
        throw (new SAXException("Fatal processing error.", exception));
829
    }
830

    
831
    /**
832
     * SAX Handler that receives notification of recoverable parsing errors
833
     */
834
    public void error(SAXParseException exception) throws SAXException
835
    {
836
        logMetacat.error("ERROR: " + exception.getMessage());
837
        throw (new SAXException("Error in processing EML.", exception));
838
    }
839

    
840
    /**
841
     * SAX Handler that receives notification of warnings
842
     */
843
    public void warning(SAXParseException exception) throws SAXException
844
    {
845
        logMetacat.warn("WARNING: " + exception.getMessage());
846
        throw (new SAXException("Warning.", exception));
847
    }
848

    
849
    //
850
    // Helper, getter and setter methods
851
    //
852

    
853
    /**
854
     * get the document name
855
     */
856
    public String getDocname()
857
    {
858
        return docname;
859
    }
860

    
861
    /**
862
     * get the document processing state
863
     */
864
    public boolean processingDTD()
865
    {
866
        return processingDTD;
867
    }
868
    
869
    
870
    /**
871
     * get the the is revision doc
872
     * @return
873
     */
874
    public boolean getIsRevisionDoc()
875
    {
876
        return isRevisionDoc;
877
    }
878
    
879
    /**
880
     * Set the the handler is for revisionDoc
881
     * @param isRevisionDoc
882
     */
883
    public void setIsRevisionDoc(boolean isRevisionDoc)
884
    {
885
       this.isRevisionDoc = isRevisionDoc;   
886
    }
887

    
888
    /* Method to write a text buffer for DBSAXNode */
889
    protected long writeTextForDBSAXNode(long previousEndNodeId,
890
            StringBuffer strBuffer, DBSAXNode node) throws SAXException
891
    {
892
        long nodeId = previousEndNodeId;
893
        // Check parameter
894
        if (strBuffer == null || node == null) { return nodeId; }
895
        boolean moredata = true;
896
        String data = null;
897

    
898
        String normalizedData = strBuffer.toString();
899
        strBuffer = new StringBuffer(MetaCatUtil.normalize(normalizedData));
900

    
901
        int bufferSize = strBuffer.length();
902
        int start = 0;
903

    
904
        // if there are some cotent in buffer, write it
905
        if (bufferSize > 0) {
906
            logMetacat.info("Write text into DB");
907
            // This loop deals with the case where there are more characters
908
            // than can fit in a single database text field (limit is
909
            // MAXDATACHARS). If the text to be inserted exceeds MAXDATACHARS,
910
            // write a series of nodes that are MAXDATACHARS long, and then the
911
            // final node contains the remainder
912
            while (moredata) {
913
                bufferSize = strBuffer.length();
914
                if (bufferSize > MAXDATACHARS) {
915
                    data = strBuffer.substring(start, MAXDATACHARS);
916
                    // cut the stringbuffer part that already written into db
917
                    strBuffer = strBuffer.delete(start, MAXDATACHARS);
918
                } else {
919
                    data = strBuffer.substring(start, bufferSize);
920
                    moredata = false;
921
                }
922

    
923
                // Write the content of the node to the database
924
                nodeId = node.writeChildNodeToDB("TEXT", null, data, docid);
925
            }//while
926
        }//if
927
        return nodeId;
928
    }
929
    
930
    public long getRootNodeId()
931
    {
932
        return rootNode.getNodeID();
933
    }
934
    
935
    public String getDocumentType()
936
    {
937
        return doctype;
938
    }
939
    
940
    public String getDocumentName()
941
    {
942
        return docname;
943
    }
944
    
945
    public String getCatalogId()
946
    {
947
        return catalogid;
948
    }
949
}
(23-23/63)