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-09-08 16:07:37 -0700 (Thu, 08 Sep 2005) $'
12
 * '$Revision: 2580 $'
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.xml.sax.Attributes;
45
import org.xml.sax.SAXException;
46
import org.xml.sax.SAXParseException;
47
import org.xml.sax.ext.DeclHandler;
48
import org.xml.sax.ext.LexicalHandler;
49
import org.xml.sax.helpers.DefaultHandler;
50

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

    
59
    protected boolean atFirstElement;
60

    
61
    protected boolean processingDTD;
62

    
63
    protected String docname = null;
64

    
65
    protected String doctype;
66

    
67
    protected String systemid;
68

    
69
    private boolean stackCreated = false;
70

    
71
    protected Stack nodeStack;
72

    
73
    protected Vector nodeIndex;
74

    
75
    protected DBConnection connection = null;
76

    
77
    protected DocumentImpl currentDocument;
78

    
79
    protected DBSAXNode rootNode;
80

    
81
    protected String action = null;
82

    
83
    protected String docid = null;
84

    
85
    protected String revision = null;
86

    
87
    protected String user = null;
88

    
89
    protected String[] groups = null;
90

    
91
    protected String pub = null;
92

    
93
    protected Thread xmlIndex;
94

    
95
    private boolean endDocument = false;
96

    
97
    protected int serverCode = 1;
98

    
99
    protected Hashtable namespaces = new Hashtable();
100

    
101
    protected boolean hitTextNode = false; // a flag to hit text node
102

    
103
    // a buffer to keep all text nodes for same element
104
    // it is for element was splited
105
    protected StringBuffer textBuffer = new StringBuffer();
106

    
107
    protected Stack textBufferStack = new Stack();
108

    
109
    protected static final int MAXDATACHARS = 4000;
110

    
111
    //protected static final int MAXDATACHARS = 50;
112
    protected static final long INDEXDELAY = 10000;
113

    
114
    // methods writeChildNodeToDB, setAttribute, setNamespace,
115
    // writeTextForDBSAXNode will increase endNodeId.
116
    protected long endNodeId = -1; // The end node id for a substree
117
    // DOCTITLE attr cleared from the db
118
    //   private static final int MAXTITLELEN = 1000;
119

    
120
    //HandlerTriple stuff
121
    TripleCollection tripleList = new TripleCollection();
122

    
123
    Triple currentTriple = new Triple();
124

    
125
    boolean startParseTriple = false;
126

    
127
    boolean hasTriple = false;
128

    
129
    public static final String ECOGRID = "ecogrid://";
130

    
131
    /**
132
     * Construct an instance of the handler class
133
     *
134
     * @param conn the JDBC connection to which information is written
135
     */
136
    private DBSAXHandler(DBConnection conn)
137
    {
138
        this.connection = conn;
139
        this.atFirstElement = true;
140
        this.processingDTD = false;
141

    
142
        // Create the stack for keeping track of node context
143
        // if it doesn't already exist
144
        if (!stackCreated) {
145
            nodeStack = new Stack();
146
            nodeIndex = new Vector();
147
            stackCreated = true;
148
        }
149
    }
150

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

    
208
    /** SAX Handler that receives notification of beginning of the document */
209
    public void startDocument() throws SAXException
210
    {
211
        MetaCatUtil.debugMessage("start Document", 50);
212

    
213
        // Create the document node representation as root
214
        rootNode = new DBSAXNode(connection, this.docid);
215
        // Add the node to the stack, so that any text data can be
216
        // added as it is encountered
217
        nodeStack.push(rootNode);
218
    }
219

    
220
    /** SAX Handler that receives notification of end of the document */
221
    public void endDocument() throws SAXException
222
    {
223
        MetaCatUtil.debugMessage("end Document", 50);
224
        // Starting new thread for writing XML Index.
225
        // It calls the run method of the thread.
226

    
227
        //if it is data package insert triple into relationtion table;
228
        if (doctype != null
229
                && MetaCatUtil.getOptionList(
230
                        MetaCatUtil.getOption("packagedoctype")).contains(
231
                        doctype) && hasTriple) {
232
            try {
233
                //initial handler and write into relationdb
234
                RelationHandler handler = new RelationHandler(docid, doctype,
235
                        connection, tripleList);
236
            } catch (Exception e) {
237
                MetaCatUtil.debugMessage(
238
                        "Failed to write triples into relation table"
239
                                + e.getMessage(), 30);
240
                throw new SAXException(
241
                        "Failed to write triples into relation table "
242
                                + e.getMessage());
243
            }
244
        }
245
    }
246

    
247
    /** SAX Handler that is called at the start of Namespace */
248
    public void startPrefixMapping(String prefix, String uri)
249
            throws SAXException
250
    {
251
        MetaCatUtil.debugMessage("NAMESPACE", 50);
252

    
253
        namespaces.put(prefix, uri);
254
    }
255

    
256
    /** SAX Handler that is called at the start of each XML element */
257
    public void startElement(String uri, String localName, String qName,
258
            Attributes atts) throws SAXException
259
    {
260
        // for element <eml:eml...> qname is "eml:eml", local name is "eml"
261
        // for element <acl....> both qname and local name is "eml"
262
        // uri is namesapce
263
        MetaCatUtil.debugMessage("Start ELEMENT(qName) " + qName, 50);
264
        MetaCatUtil.debugMessage("Start ELEMENT(localName) " + localName, 50);
265
        MetaCatUtil.debugMessage("Start ELEMENT(uri) " + uri, 50);
266

    
267
        DBSAXNode parentNode = null;
268
        DBSAXNode currentNode = null;
269

    
270
        // Get a reference to the parent node for the id
271
        try {
272
            parentNode = (DBSAXNode) nodeStack.peek();
273
        } catch (EmptyStackException e) {
274
            parentNode = null;
275
        }
276

    
277
        // If hit a text node, we need write this text for current's parent
278
        // node
279
        // This will happend if the element is mixted
280
        if (hitTextNode && parentNode != null) {
281
            // write the textbuffer into db for parent node.
282
            endNodeId = writeTextForDBSAXNode(endNodeId, textBuffer, parentNode);
283
            // rest hitTextNode
284
            hitTextNode = false;
285
            // reset textbuffer
286
            textBuffer = null;
287
            textBuffer = new StringBuffer();
288

    
289
        }
290

    
291
        // Document representation that points to the root document node
292
        if (atFirstElement) {
293
            atFirstElement = false;
294
            // If no DOCTYPE declaration: docname = root element
295
            // doctype = root element name or name space
296
            if (docname == null) {
297
                docname = localName;
298
                // if uri isn't null doctype = uri(namespace)
299
                // othewise root element
300
                if (uri != null && !(uri.trim()).equals("")) {
301
                    doctype = uri;
302
                } else {
303
                    doctype = docname;
304
                }
305
                MetaCatUtil.debugMessage("DOCNAME-a: " + docname, 30);
306
                MetaCatUtil.debugMessage("DOCTYPE-a: " + doctype, 30);
307
            } else if (doctype == null) {
308
                // because docname is not null and it is declared in dtd
309
                // so could not be in schema, no namespace
310
                doctype = docname;
311
                MetaCatUtil.debugMessage("DOCTYPE-b: " + doctype, 30);
312
            }
313
            rootNode.writeNodename(docname);
314
            try {
315
                // for validated XML Documents store a reference to XML DB
316
                // Catalog
317
                // Because this is select statement and it needn't to roll back
318
                // if
319
                // insert document action fialed.
320
                // In order to decrease DBConnection usage count, we get a new
321
                // dbconnection from pool
322
                String catalogid = null;
323
                DBConnection dbConn = null;
324
                int serialNumber = -1;
325

    
326
                if (systemid != null) {
327
                    try {
328
                        // Get dbconnection
329
                        dbConn = DBConnectionPool
330
                                .getDBConnection("DBSAXHandler.startElement");
331
                        serialNumber = dbConn.getCheckOutSerialNumber();
332

    
333
                        Statement stmt = dbConn.createStatement();
334
                        ResultSet rs = stmt
335
                                .executeQuery("SELECT catalog_id FROM xml_catalog "
336
                                        + "WHERE entry_type = 'DTD' "
337
                                        + "AND public_id = '" + doctype + "'");
338
                        boolean hasRow = rs.next();
339
                        if (hasRow) {
340
                            catalogid = rs.getString(1);
341
                        }
342
                        stmt.close();
343
                    }//try
344
                    finally {
345
                        // Return dbconnection
346
                        DBConnectionPool.returnDBConnection(dbConn,
347
                                serialNumber);
348
                    }//finally
349
                }
350

    
351
                //create documentImpl object by the constructor which can
352
                // specify
353
                //the revision
354
                currentDocument = new DocumentImpl(connection, rootNode
355
                        .getNodeID(), docname, doctype, docid, revision,
356
                        action, user, this.pub, catalogid, this.serverCode);
357

    
358
            } catch (Exception ane) {
359
                throw (new SAXException("Error in DBSaxHandler.startElement "
360
                        + action, ane));
361
            }
362
        }
363

    
364
        // Create the current node representation
365
        currentNode = new DBSAXNode(connection, qName, localName, parentNode,
366
                currentDocument.getRootNodeID(), docid, currentDocument
367
                        .getDoctype());
368

    
369
        // Add all of the namespaces
370
        String prefix;
371
        String nsuri;
372
        Enumeration prefixes = namespaces.keys();
373
        while (prefixes.hasMoreElements()) {
374
            prefix = (String) prefixes.nextElement();
375
            nsuri = (String) namespaces.get(prefix);
376
            currentNode.setNamespace(prefix, nsuri, docid);
377
        }
378
        namespaces = null;
379
        namespaces = new Hashtable();
380

    
381
        // Add all of the attributes
382
        for (int i = 0; i < atts.getLength(); i++) {
383
            String attributeName = atts.getQName(i);
384
            String attributeValue = atts.getValue(i);
385
            endNodeId = currentNode.setAttribute(attributeName, attributeValue,
386
                    docid);
387

    
388
            // To handle name space and schema location if the attribute name
389
            // is
390
            // xsi:schemaLocation. If the name space is in not in catalog table
391
            // it will be regeistered.
392
            if (attributeName != null
393
                    && attributeName
394
                            .indexOf(MetaCatServlet.SCHEMALOCATIONKEYWORD) != -1) {
395
                SchemaLocationResolver resolver = new SchemaLocationResolver(
396
                        attributeValue);
397
                resolver.resolveNameSpace();
398

    
399
            }
400
        }
401

    
402
        // Add the node to the stack, so that any text data can be
403
        // added as it is encountered
404
        nodeStack.push(currentNode);
405
        // Add the node to the vector used by thread for writing XML Index
406
        nodeIndex.addElement(currentNode);
407
        // start parsing triple
408
        if (doctype != null
409
                && MetaCatUtil.getOptionList(
410
                        MetaCatUtil.getOption("packagedoctype")).contains(
411
                        doctype) && localName.equals("triple")) {
412
            startParseTriple = true;
413
            hasTriple = true;
414
            currentTriple = new Triple();
415
        }
416
    }
417

    
418
    public void runIndexingThread(){
419
        boolean useXMLIndex =
420
            (new Boolean(MetaCatUtil.getOption("usexmlindex"))).booleanValue();
421
        if (useXMLIndex) {
422
            try {
423
                xmlIndex.start();
424
            } catch (NullPointerException e) {
425
                xmlIndex = null;
426
                MetaCatUtil.debugMessage("Error in DBSAXHandler.runIndexingThread() "
427
                        + e.getMessage(), 20);
428
            }
429
        }
430
    }
431
    
432
    /*
433
     * Run a separate thread to build the XML index for this document.  This
434
     * thread is run asynchronously in order to more quickly return control to
435
     * the submitting user.  The run method checks to see if the document has
436
     * been fully inserted before trying to update the xml_index table.
437
     */
438
    public void run()
439
    {
440
        try {
441
            // stop 5 second
442
            Thread.sleep(5000);
443
            //make sure record is done
444
            checkDocumentTable();
445
            // Build the index for this document
446
            currentDocument.buildIndex();
447
        } catch (Exception e) {
448
            MetaCatUtil.debugMessage("Error in DBSAXHandler.run "
449
                    + e.getMessage(), 30);
450
        }
451
    }
452

    
453
    /*
454
     * method to make sure insert is finished before create index table If new
455
     * version of record is in xml_documents every thing will be fine
456
     */
457
    private void checkDocumentTable() throws Exception
458
    {
459

    
460
        DBConnection dbConn = null;
461
        int serialNumber = -1;
462

    
463
        try {
464
            // Opening separate db connection for writing XML Index
465
            dbConn = DBConnectionPool
466
                    .getDBConnection("DBSAXHandler.checkDocumentTable");
467
            serialNumber = dbConn.getCheckOutSerialNumber();
468

    
469
            // the following while loop construct checks to make sure that
470
            // the docid of the document that we are trying to index is already
471
            // in the xml_documents table. if this is not the case, the foreign
472
            // key relationship between xml_documents and xml_index is
473
            // temporarily broken causing multiple problems.
474
            boolean inxmldoc = false;
475
            long startTime = System.currentTimeMillis();
476
            while (!inxmldoc) {
477
                String xmlDocumentsCheck = "select distinct docid from xml_documents"
478
                        + " where docid ='"
479
                        + docid
480
                        + "' and "
481
                        + " rev ='"
482
                        + revision + "'";
483

    
484
                PreparedStatement xmlDocCheck = dbConn
485
                        .prepareStatement(xmlDocumentsCheck);
486
                // Increase usage count
487
                dbConn.increaseUsageCount(1);
488
                xmlDocCheck.execute();
489
                ResultSet doccheckRS = xmlDocCheck.getResultSet();
490
                boolean tableHasRows = doccheckRS.next();
491
                if (tableHasRows) {
492
                    MetaCatUtil.debugMessage(
493
                            "=========== found the correct document", 35);
494
                    inxmldoc = true;
495
                }
496
                doccheckRS.close();
497
                xmlDocCheck.close();
498
                // make sure the while loop will be ended in reseaonable time
499
                long stopTime = System.currentTimeMillis();
500
                if ((stopTime - startTime) > INDEXDELAY) { throw new Exception(
501
                        "Couldn't find the docid for index build in "
502
                                + "reseaonable time!"); }
503
            }//while
504
        } catch (Exception e) {
505
            try {
506
                dbConn.rollback();
507
                //dbconn.close();
508
            } catch (SQLException sqle) {
509
            }
510
            MetaCatUtil.debugMessage("Error in DBSAXHandler.checkDocumentTable "
511
                    + e.getMessage(), 30);
512

    
513
        } finally {
514
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
515
        }//finally
516

    
517
    }
518

    
519
    /** SAX Handler that is called for each XML text node */
520
    public void characters(char[] cbuf, int start, int len) throws SAXException
521
    {
522
        MetaCatUtil.debugMessage("CHARACTERS", 50);
523
        // buffer all text nodes for same element. This is for text was splited
524
        // into different nodes
525
        textBuffer.append(new String(cbuf, start, len));
526
        // set hittextnode true
527
        hitTextNode = true;
528
        // if text buffer .size is greater than max, write it to db.
529
        // so we can save memory
530
        if (textBuffer.length() > MAXDATACHARS) {
531
            MetaCatUtil.debugMessage("Write text into DB in charaters"
532
                    + " when text buffer size is greater than maxmum number",
533
                    50);
534
            DBSAXNode currentNode = (DBSAXNode) nodeStack.peek();
535
            endNodeId = writeTextForDBSAXNode(endNodeId, textBuffer,
536
                    currentNode);
537
            textBuffer = null;
538
            textBuffer = new StringBuffer();
539
        }
540
    }
541

    
542
    /**
543
     * SAX Handler that is called for each XML text node that is Ignorable
544
     * white space
545
     */
546
    public void ignorableWhitespace(char[] cbuf, int start, int len)
547
            throws SAXException
548
    {
549
        // When validation is turned "on", white spaces are reported here
550
        // When validation is turned "off" white spaces are not reported here,
551
        // but through characters() callback
552
        MetaCatUtil.debugMessage("IGNORABLEWHITESPACE", 50);
553

    
554
        DBSAXNode currentNode = (DBSAXNode) nodeStack.peek();
555
        String data = null;
556
        int leftover = len;
557
        int offset = start;
558
        boolean moredata = true;
559

    
560
        // This loop deals with the case where there are more characters
561
        // than can fit in a single database text field (limit is
562
        // MAXDATACHARS). If the text to be inserted exceeds MAXDATACHARS,
563
        // write a series of nodes that are MAXDATACHARS long, and then the
564
        // final node contains the remainder
565
        while (moredata) {
566
            if (leftover > MAXDATACHARS) {
567
                data = new String(cbuf, offset, MAXDATACHARS);
568
                leftover -= MAXDATACHARS;
569
                offset += MAXDATACHARS;
570
            } else {
571
                data = new String(cbuf, offset, leftover);
572
                moredata = false;
573
            }
574

    
575
            // Write the content of the node to the database
576
            endNodeId = currentNode.writeChildNodeToDB("TEXT", null, data,
577
                    docid);
578
        }
579
    }
580

    
581
    /**
582
     * SAX Handler called once for each processing instruction found: node that
583
     * PI may occur before or after the root element.
584
     */
585
    public void processingInstruction(String target, String data)
586
            throws SAXException
587
    {
588
        MetaCatUtil.debugMessage("PI", 50);
589
        DBSAXNode currentNode = (DBSAXNode) nodeStack.peek();
590
        endNodeId = currentNode.writeChildNodeToDB("PI", target, data, docid);
591
    }
592

    
593
    /** SAX Handler that is called at the end of each XML element */
594
    public void endElement(String uri, String localName, String qName)
595
            throws SAXException
596
    {
597
        MetaCatUtil.debugMessage("End ELEMENT " + qName, 50);
598

    
599
        // write buffered text nodes into db (so no splited)
600
        DBSAXNode currentNode = (DBSAXNode) nodeStack.peek();
601

    
602
        // If before the end element, the parser hit text nodes and store them
603
        // into the buffer, write the buffer to data base. The reason we put
604
        // write database here is for xerces some time split text node
605
        if (hitTextNode) {
606
            MetaCatUtil.debugMessage("Write text into DB in End Element", 50);
607
            endNodeId = writeTextForDBSAXNode(endNodeId, textBuffer,
608
                    currentNode);
609

    
610
            //if it is triple parsing process
611
            if (startParseTriple) {
612

    
613
                String content = textBuffer.toString().trim();
614
                if (localName.equals("subject")) { //get the subject content
615
                    currentTriple.setSubject(content);
616
                } else if (localName.equals("relationship")) { //get the
617
                                                               // relationship
618
                                                               // content
619
                    currentTriple.setRelationship(content);
620
                } else if (localName.equals("object")) { //get the object
621
                                                         // content
622
                    currentTriple.setObject(content);
623
                }
624
            }
625

    
626
        }//if
627

    
628
        //set hitText false
629
        hitTextNode = false;
630
        // reset textbuff
631
        textBuffer = null;
632
        textBuffer = new StringBuffer();
633

    
634
        // Get the node from the stack
635
        currentNode = (DBSAXNode) nodeStack.pop();
636
        //finishing parsing single triple
637
        if (startParseTriple && localName.equals("triple")) {
638
            // add trip to triple collection
639
            tripleList.addTriple(currentTriple);
640
            //rest variable
641
            currentTriple = null;
642
            startParseTriple = false;
643
        }
644
    }
645

    
646
    //
647
    // the next section implements the LexicalHandler interface
648
    //
649

    
650
    /** SAX Handler that receives notification of DOCTYPE. Sets the DTD */
651
    public void startDTD(String name, String publicId, String systemId)
652
            throws SAXException
653
    {
654
        docname = name;
655
        doctype = publicId;
656
        systemid = systemId;
657

    
658
        processingDTD = true;
659
        DBSAXNode currentNode = (DBSAXNode) nodeStack.peek();
660
        //create a DTD node and write docname,publicid and system id into db
661
        // we don't put the dtd node into node stack
662
        DBSAXNode dtdNode = new DBSAXNode(connection, name, publicId, systemId,
663
                currentNode, currentNode.getRootNodeID(), docid);
664
        MetaCatUtil.debugMessage("Start DTD", 50);
665
        MetaCatUtil.debugMessage("Setting processingDTD to true", 50);
666
        MetaCatUtil.debugMessage("DOCNAME: " + docname, 50);
667
        MetaCatUtil.debugMessage("DOCTYPE: " + doctype, 50);
668
        MetaCatUtil.debugMessage("  SYSID: " + systemid, 50);
669
    }
670

    
671
    /**
672
     * SAX Handler that receives notification of end of DTD
673
     */
674
    public void endDTD() throws SAXException
675
    {
676

    
677
        processingDTD = false;
678
        MetaCatUtil.debugMessage("Setting processingDTD to false", 50);
679
        MetaCatUtil.debugMessage("end DTD", 50);
680
    }
681

    
682
    /**
683
     * SAX Handler that receives notification of comments in the DTD
684
     */
685
    public void comment(char[] ch, int start, int length) throws SAXException
686
    {
687
        MetaCatUtil.debugMessage("COMMENT", 50);
688
        if (!processingDTD) {
689
            DBSAXNode currentNode = (DBSAXNode) nodeStack.peek();
690
            endNodeId = currentNode.writeChildNodeToDB("COMMENT", null,
691
                    new String(ch, start, length), docid);
692
        }
693
    }
694

    
695
    /**
696
     * SAX Handler that receives notification of the start of CDATA sections
697
     */
698
    public void startCDATA() throws SAXException
699
    {
700
        MetaCatUtil.debugMessage("start CDATA", 50);
701
    }
702

    
703
    /**
704
     * SAX Handler that receives notification of the end of CDATA sections
705
     */
706
    public void endCDATA() throws SAXException
707
    {
708
        MetaCatUtil.debugMessage("end CDATA", 50);
709
    }
710

    
711
    /**
712
     * SAX Handler that receives notification of the start of entities
713
     */
714
    public void startEntity(String name) throws SAXException
715
    {
716
        MetaCatUtil.debugMessage("start ENTITY: " + name, 50);
717
        //System.out.println("start ENTITY: " + name);
718
        if (name.equals("[dtd]")) {
719
            processingDTD = true;
720
        }
721
    }
722

    
723
    /**
724
     * SAX Handler that receives notification of the end of entities
725
     */
726
    public void endEntity(String name) throws SAXException
727
    {
728
        MetaCatUtil.debugMessage("end ENTITY: " + name, 50);
729
        //System.out.println("end ENTITY: " + name);
730
        if (name.equals("[dtd]")) {
731
            processingDTD = false;
732
        }
733
    }
734

    
735
    /**
736
     * SAX Handler that receives notification of element declarations
737
     */
738
    public void elementDecl(String name, String model)
739
            throws org.xml.sax.SAXException
740
    {
741
        //System.out.println("ELEMENTDECL: " + name + " " + model);
742
        MetaCatUtil.debugMessage("ELEMENTDECL: " + name + " " + model, 50);
743
    }
744

    
745
    /**
746
     * SAX Handler that receives notification of attribute declarations
747
     */
748
    public void attributeDecl(String eName, String aName, String type,
749
            String valueDefault, String value) throws org.xml.sax.SAXException
750
    {
751

    
752
        //System.out.println("ATTRIBUTEDECL: " + eName + " "
753
        //                        + aName + " " + type + " " + valueDefault + " "
754
        //                        + value);
755
        MetaCatUtil.debugMessage("ATTRIBUTEDECL: " + eName + " " + aName + " "
756
                + type + " " + valueDefault + " " + value, 50);
757
    }
758

    
759
    /**
760
     * SAX Handler that receives notification of internal entity declarations
761
     */
762
    public void internalEntityDecl(String name, String value)
763
            throws org.xml.sax.SAXException
764
    {
765
        //System.out.println("INTERNENTITYDECL: " + name + " " + value);
766
        MetaCatUtil.debugMessage("INTERNENTITYDECL: " + name + " " + value, 50);
767
    }
768

    
769
    /**
770
     * SAX Handler that receives notification of external entity declarations
771
     */
772
    public void externalEntityDecl(String name, String publicId, String systemId)
773
            throws org.xml.sax.SAXException
774
    {
775
        //System.out.println("EXTERNENTITYDECL: " + name + " " + publicId
776
        //                              + " " + systemId);
777
        MetaCatUtil.debugMessage("EXTERNENTITYDECL: " + name + " " + publicId
778
                + " " + systemId, 50);
779
        // it processes other external entity, not the DTD;
780
        // it doesn't signal for the DTD here
781
        processingDTD = false;
782
    }
783

    
784
    //
785
    // the next section implements the ErrorHandler interface
786
    //
787

    
788
    /**
789
     * SAX Handler that receives notification of fatal parsing errors
790
     */
791
    public void fatalError(SAXParseException exception) throws SAXException
792
    {
793
        MetaCatUtil.debugMessage("FATALERROR: " + exception.getMessage(), 50);
794
        throw (new SAXException("Fatal processing error.", exception));
795
    }
796

    
797
    /**
798
     * SAX Handler that receives notification of recoverable parsing errors
799
     */
800
    public void error(SAXParseException exception) throws SAXException
801
    {
802
        MetaCatUtil.debugMessage("ERROR: " + exception.getMessage(), 50);
803
        throw (new SAXException("Error in processing EML.", exception));
804
    }
805

    
806
    /**
807
     * SAX Handler that receives notification of warnings
808
     */
809
    public void warning(SAXParseException exception) throws SAXException
810
    {
811
        MetaCatUtil.debugMessage("WARNING: " + exception.getMessage(), 50);
812
        throw (new SAXException("Warning.", exception));
813
    }
814

    
815
    //
816
    // Helper, getter and setter methods
817
    //
818

    
819
    /**
820
     * get the document name
821
     */
822
    public String getDocname()
823
    {
824
        return docname;
825
    }
826

    
827
    /**
828
     * get the document processing state
829
     */
830
    public boolean processingDTD()
831
    {
832
        return processingDTD;
833
    }
834

    
835
    /* Method to write a text buffer for DBSAXNode */
836
    protected long writeTextForDBSAXNode(long previousEndNodeId,
837
            StringBuffer strBuffer, DBSAXNode node) throws SAXException
838
    {
839
        long nodeId = previousEndNodeId;
840
        // Check parameter
841
        if (strBuffer == null || node == null) { return nodeId; }
842
        boolean moredata = true;
843
        String data = null;
844

    
845
        String normalizedData = strBuffer.toString();
846
        strBuffer = new StringBuffer(MetaCatUtil.normalize(normalizedData));
847

    
848
        int bufferSize = strBuffer.length();
849
        int start = 0;
850

    
851
        // if there are some cotent in buffer, write it
852
        if (bufferSize > 0) {
853
            MetaCatUtil.debugMessage("Write text into DB", 50);
854
            // This loop deals with the case where there are more characters
855
            // than can fit in a single database text field (limit is
856
            // MAXDATACHARS). If the text to be inserted exceeds MAXDATACHARS,
857
            // write a series of nodes that are MAXDATACHARS long, and then the
858
            // final node contains the remainder
859
            while (moredata) {
860
                bufferSize = strBuffer.length();
861
                if (bufferSize > MAXDATACHARS) {
862
                    data = strBuffer.substring(start, MAXDATACHARS);
863
                    // cut the stringbuffer part that already written into db
864
                    strBuffer = strBuffer.delete(start, MAXDATACHARS);
865
                } else {
866
                    data = strBuffer.substring(start, bufferSize);
867
                    moredata = false;
868
                }
869

    
870
                // Write the content of the node to the database
871
                nodeId = node.writeChildNodeToDB("TEXT", null, data, docid);
872
            }//while
873
        }//if
874
        return nodeId;
875
    }
876
}
(23-23/63)