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-10-10 11:06:55 -0700 (Mon, 10 Oct 2005) $'
12
 * '$Revision: 2663 $'
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) { throw new Exception(
532
                        "Couldn't find the docid for index build in "
533
                                + "reseaonable time!"); }
534
            }//while
535
        } catch (Exception e) {
536
            try {
537
                dbConn.rollback();
538
                //dbconn.close();
539
            } catch (SQLException sqle) {
540
            }
541
            logMetacat.error("Error in DBSAXHandler.checkDocumentTable "
542
                    + e.getMessage());
543

    
544
        } finally {
545
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
546
        }//finally
547

    
548
    }
549

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

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

    
584
        DBSAXNode currentNode = (DBSAXNode) nodeStack.peek();
585
        String data = null;
586
        int leftover = len;
587
        int offset = start;
588
        boolean moredata = true;
589

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

    
605
            // Write the content of the node to the database
606
            endNodeId = currentNode.writeChildNodeToDB("TEXT", null, data,
607
                    docid);
608
        }
609
    }
610

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

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

    
629
        // write buffered text nodes into db (so no splited)
630
        DBSAXNode currentNode = (DBSAXNode) nodeStack.peek();
631

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

    
640
            //if it is triple parsing process
641
            if (startParseTriple) {
642

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

    
656
        }//if
657

    
658
        //set hitText false
659
        hitTextNode = false;
660
        // reset textbuff
661
        textBuffer = null;
662
        textBuffer = new StringBuffer();
663

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

    
676
    //
677
    // the next section implements the LexicalHandler interface
678
    //
679

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

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

    
701
    /**
702
     * SAX Handler that receives notification of end of DTD
703
     */
704
    public void endDTD() throws SAXException
705
    {
706

    
707
        processingDTD = false;
708
        logMetacat.info("Setting processingDTD to false");
709
        logMetacat.info("end DTD");
710
    }
711

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

    
725
    /**
726
     * SAX Handler that receives notification of the start of CDATA sections
727
     */
728
    public void startCDATA() throws SAXException
729
    {
730
        logMetacat.info("start CDATA");
731
    }
732

    
733
    /**
734
     * SAX Handler that receives notification of the end of CDATA sections
735
     */
736
    public void endCDATA() throws SAXException
737
    {
738
        logMetacat.info("end CDATA");
739
    }
740

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

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

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

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

    
782
        //System.out.println("ATTRIBUTEDECL: " + eName + " "
783
        //                        + aName + " " + type + " " + valueDefault + " "
784
        //                        + value);
785
        logMetacat.info("ATTRIBUTEDECL: " + eName + " " + aName + " "
786
                + type + " " + valueDefault + " " + value);
787
    }
788

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

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

    
814
    //
815
    // the next section implements the ErrorHandler interface
816
    //
817

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

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

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

    
845
    //
846
    // Helper, getter and setter methods
847
    //
848

    
849
    /**
850
     * get the document name
851
     */
852
    public String getDocname()
853
    {
854
        return docname;
855
    }
856

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

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

    
894
        String normalizedData = strBuffer.toString();
895
        strBuffer = new StringBuffer(MetaCatUtil.normalize(normalizedData));
896

    
897
        int bufferSize = strBuffer.length();
898
        int start = 0;
899

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

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