Parse an XML document.
The parser can use this method to instruct this configuration
to begin parsing an XML document from any valid input source
(a character stream, a byte stream, or a URI).
Parsers may not invoke this method while a parse is in progress.
Once a parse is complete, the parser may then parse another XML
document.
This method is synchronous: it will not return until parsing
has ended. If a client application wants to terminate
parsing early, it should throw an exception.
//
// XMLParserConfiguration methods
//
// get reader
openInputSourceStream(source);
Reader reader = source.getCharacterStream();
if (reader == null) {
InputStream stream = source.getByteStream();
reader = new InputStreamReader(stream);
}
BufferedReader bufferedReader = new BufferedReader(reader);
// start document
if (fDocumentHandler != null) {
fDocumentHandler.startDocument(null, "UTF-8", new NamespaceSupport(), null);
fDocumentHandler.xmlDecl("1.0", "UTF-8", "true", null);
fDocumentHandler.doctypeDecl("csv", null, null, null);
}
if (fDTDHandler != null) {
fDTDHandler.startDTD(null, null);
fDTDHandler.elementDecl("csv", "(row)*", null);
fDTDHandler.elementDecl("row", "(col)*", null);
fDTDHandler.elementDecl("col", "(#PCDATA)", null);
}
if (fDTDContentModelHandler != null) {
fDTDContentModelHandler.startContentModel("csv", null);
fDTDContentModelHandler.startGroup(null);
fDTDContentModelHandler.element("row", null);
fDTDContentModelHandler.endGroup(null);
short csvOccurs = XMLDTDContentModelHandler.OCCURS_ZERO_OR_MORE;
fDTDContentModelHandler.occurrence(csvOccurs, null);
fDTDContentModelHandler.endContentModel(null);
fDTDContentModelHandler.startContentModel("row", null);
fDTDContentModelHandler.startGroup(null);
fDTDContentModelHandler.element("col", null);
fDTDContentModelHandler.endGroup(null);
short rowOccurs = XMLDTDContentModelHandler.OCCURS_ZERO_OR_MORE;
fDTDContentModelHandler.occurrence(rowOccurs, null);
fDTDContentModelHandler.endContentModel(null);
fDTDContentModelHandler.startContentModel("col", null);
fDTDContentModelHandler.startGroup(null);
fDTDContentModelHandler.pcdata(null);
fDTDContentModelHandler.endGroup(null);
fDTDContentModelHandler.endContentModel(null);
}
if (fDTDHandler != null) {
fDTDHandler.endDTD(null);
}
if (fDocumentHandler != null) {
fDocumentHandler.startElement(CSV, EMPTY_ATTRS, null);
}
// read lines
String line;
while ((line = bufferedReader.readLine()) != null) {
if (fDocumentHandler != null) {
fDocumentHandler.ignorableWhitespace(NEWLINE_ONE_SPACE, null);
fDocumentHandler.startElement(ROW, EMPTY_ATTRS, null);
StringTokenizer tokenizer = new StringTokenizer(line, ",");
while (tokenizer.hasMoreTokens()) {
fDocumentHandler.ignorableWhitespace(NEWLINE_TWO_SPACES, null);
fDocumentHandler.startElement(COL, EMPTY_ATTRS, null);
String token = tokenizer.nextToken();
fStringBuffer.clear();
fStringBuffer.append(token);
fDocumentHandler.characters(fStringBuffer, null);
fDocumentHandler.endElement(COL, null);
}
fDocumentHandler.ignorableWhitespace(NEWLINE_ONE_SPACE, null);
fDocumentHandler.endElement(ROW, null);
}
}
bufferedReader.close();
// end document
if (fDocumentHandler != null) {
fDocumentHandler.ignorableWhitespace(NEWLINE, null);
fDocumentHandler.endElement(CSV, null);
fDocumentHandler.endDocument(null);
}