Revision 1411
Added by Jing Tao almost 22 years ago
src/edu/ucsb/nceas/metacat/DBSAXHandler.java | ||
---|---|---|
71 | 71 |
private boolean endDocument = false; |
72 | 72 |
protected int serverCode = 1; |
73 | 73 |
protected Hashtable namespaces = new Hashtable(); |
74 |
protected boolean hitTextNode = false; // a flag to hit text node |
|
75 |
// a buffer to keep all text nodes for same element |
|
76 |
// it is for element was splited |
|
77 |
protected StringBuffer textBuffer = new StringBuffer(); |
|
78 |
protected Stack textBufferStack = new Stack(); |
|
74 | 79 |
|
75 | 80 |
protected static final int MAXDATACHARS = 4000; |
76 | 81 |
protected static final long INDEXDELAY = 10000; |
... | ... | |
194 | 199 |
MetaCatUtil.debugMessage("Start ELEMENT(localName) " + localName, 50); |
195 | 200 |
MetaCatUtil.debugMessage("Start ELEMENT(uri) " + uri, 50); |
196 | 201 |
|
197 |
|
|
198 | 202 |
DBSAXNode parentNode = null; |
199 | 203 |
DBSAXNode currentNode = null; |
200 | 204 |
|
... | ... | |
204 | 208 |
} catch (EmptyStackException e) { |
205 | 209 |
parentNode = null; |
206 | 210 |
} |
211 |
|
|
212 |
// If hit a text node, we need write this text for current's parent node |
|
213 |
// This will happend if the element is mixted |
|
214 |
if (hitTextNode && parentNode != null) |
|
215 |
{ |
|
216 |
// write the textbuffer into db for parent node. |
|
217 |
writeTextForDBSAXNode(textBuffer, parentNode); |
|
218 |
// rest hitTextNode |
|
219 |
hitTextNode =false; |
|
220 |
// reset textbuffer |
|
221 |
textBuffer = null; |
|
222 |
textBuffer = new StringBuffer(); |
|
223 |
|
|
224 |
} |
|
207 | 225 |
|
208 | 226 |
// Document representation that points to the root document node |
209 | 227 |
if (atFirstElement) |
... | ... | |
449 | 467 |
|
450 | 468 |
|
451 | 469 |
/** SAX Handler that is called for each XML text node */ |
452 |
public void characters(char[] cbuf, int start, int len) throws SAXException { |
|
470 |
public void characters(char[] cbuf, int start, int len) throws SAXException |
|
471 |
{ |
|
453 | 472 |
MetaCatUtil.debugMessage("CHARACTERS", 50); |
454 |
DBSAXNode currentNode = (DBSAXNode)nodeStack.peek(); |
|
455 |
String data = null; |
|
456 |
int leftover = len; |
|
457 |
int offset = start; |
|
458 |
boolean moredata = true; |
|
473 |
// buffer all text nodes for same element. This is for text was splited |
|
474 |
// into different nodes |
|
475 |
textBuffer.append(new String(cbuf, start,len)); |
|
476 |
// set hittextnode true |
|
477 |
hitTextNode = true; |
|
478 |
} |
|
459 | 479 |
|
460 |
// This loop deals with the case where there are more characters |
|
461 |
// than can fit in a single database text field (limit is |
|
462 |
// MAXDATACHARS). If the text to be inserted exceeds MAXDATACHARS, |
|
463 |
// write a series of nodes that are MAXDATACHARS long, and then the |
|
464 |
// final node contains the remainder |
|
465 |
while (moredata) { |
|
466 |
if (leftover > MAXDATACHARS) { |
|
467 |
data = new String(cbuf, offset, MAXDATACHARS); |
|
468 |
leftover -= MAXDATACHARS; |
|
469 |
offset += MAXDATACHARS; |
|
470 |
} else { |
|
471 |
data = new String(cbuf, offset, leftover); |
|
472 |
moredata = false; |
|
473 |
} |
|
474 |
|
|
475 |
// Write the content of the node to the database |
|
476 |
currentNode.writeChildNodeToDB("TEXT", null, data, docid); |
|
477 |
} |
|
478 |
} |
|
479 |
|
|
480 | 480 |
/** |
481 | 481 |
* SAX Handler that is called for each XML text node that is |
482 | 482 |
* Ignorable white space |
... | ... | |
530 | 530 |
public void endElement(String uri, String localName, |
531 | 531 |
String qName) throws SAXException { |
532 | 532 |
MetaCatUtil.debugMessage("End ELEMENT " + qName, 50); |
533 |
|
|
534 |
// write buffered text nodes into db (so no splited) |
|
535 |
DBSAXNode currentNode = (DBSAXNode)nodeStack.peek(); |
|
536 |
|
|
537 |
// If before the end element, the parser hit text nodes and store them |
|
538 |
// into the buffer, write the buffer to data base. The reason we put |
|
539 |
// write database here is for xerces some time split text node |
|
540 |
if (hitTextNode) |
|
541 |
{ |
|
542 |
MetaCatUtil.debugMessage("Write text into DB in End Element", 50); |
|
543 |
writeTextForDBSAXNode(textBuffer, currentNode); |
|
544 |
}//if |
|
545 |
|
|
546 |
//set hitText false |
|
547 |
hitTextNode = false; |
|
548 |
// reset textbuff |
|
549 |
textBuffer = null; |
|
550 |
textBuffer = new StringBuffer(); |
|
533 | 551 |
|
534 | 552 |
// Get the node from the stack |
535 |
DBSAXNode currentNode = (DBSAXNode)nodeStack.pop();
|
|
553 |
currentNode = (DBSAXNode)nodeStack.pop(); |
|
536 | 554 |
} |
537 | 555 |
|
538 | 556 |
// |
... | ... | |
705 | 723 |
public boolean processingDTD() { |
706 | 724 |
return processingDTD; |
707 | 725 |
} |
726 |
|
|
727 |
/* Method to write a text buffer for DBSAXNode*/ |
|
728 |
protected void writeTextForDBSAXNode(StringBuffer strBuffer, DBSAXNode node) |
|
729 |
throws SAXException |
|
730 |
{ |
|
731 |
// Check parameter |
|
732 |
if ( strBuffer == null || node == null) |
|
733 |
{ |
|
734 |
return; |
|
735 |
} |
|
736 |
boolean moredata = true; |
|
737 |
String data = null; |
|
738 |
int bufferSize = strBuffer.length(); |
|
739 |
int start = 0; |
|
740 |
|
|
741 |
// if there are some cotent in buffer, write it |
|
742 |
if (bufferSize > 0) |
|
743 |
{ |
|
744 |
MetaCatUtil.debugMessage("Write text into DB", 50); |
|
745 |
// This loop deals with the case where there are more characters |
|
746 |
// than can fit in a single database text field (limit is |
|
747 |
// MAXDATACHARS). If the text to be inserted exceeds MAXDATACHARS, |
|
748 |
// write a series of nodes that are MAXDATACHARS long, and then the |
|
749 |
// final node contains the remainder |
|
750 |
while (moredata) |
|
751 |
{ |
|
752 |
bufferSize = strBuffer.length(); |
|
753 |
if (bufferSize > MAXDATACHARS) |
|
754 |
{ |
|
755 |
data = strBuffer.substring(start, MAXDATACHARS); |
|
756 |
// cut the stringbuffer part that already written into db |
|
757 |
strBuffer = strBuffer.delete(start, MAXDATACHARS); |
|
758 |
} |
|
759 |
else |
|
760 |
{ |
|
761 |
data = strBuffer.substring(start, bufferSize); |
|
762 |
moredata = false; |
|
763 |
} |
|
764 |
|
|
765 |
// Write the content of the node to the database |
|
766 |
node.writeChildNodeToDB("TEXT", null, data, docid); |
|
767 |
}//while |
|
768 |
}//if |
|
769 |
} |
|
708 | 770 |
} |
Also available in: Unified diff
Add the code the handle text node was splitted