Ejemplo n.º 1
0
void XMLTokenizer::finish()
{
    // parse xml file
    XMLHandler handler(m_doc,m_view);
    QXmlInputSource source;
    source.setData(m_xmlCode);
    QXmlSimpleReader reader;
    reader.setContentHandler( &handler );
    reader.setLexicalHandler( &handler );
    reader.setErrorHandler( &handler );
    reader.setDeclHandler( &handler );
    reader.setDTDHandler( &handler );
    bool ok = reader.parse( source );
    // ### handle exceptions inserting nodes
    if (!ok) {
	kdDebug(6036) << "Error during XML parsing: " << handler.errorProtocol() << endl;

	int exceptioncode;
	while (m_doc->hasChildNodes())
	    static_cast<NodeImpl*>(m_doc)->removeChild(m_doc->firstChild(),exceptioncode);
	
	// construct a HTML page giving the error message
	// ### for multiple error messages, display the code for each
	QTextIStream stream(&m_xmlCode);
	unsigned long lineno;
	for (lineno = 0; lineno < handler.errorLine-1; lineno++)
	  stream.readLine();
	QString line = stream.readLine();
	
	m_doc->appendChild(m_doc->createElementNS("http://www.w3.org/1999/xhtml","html"),exceptioncode);
	NodeImpl *body = m_doc->createElementNS("http://www.w3.org/1999/xhtml","body");
	m_doc->firstChild()->appendChild(body,exceptioncode);
	
	NodeImpl *h1 = m_doc->createElementNS("http://www.w3.org/1999/xhtml","h1");
	body->appendChild(h1,exceptioncode);
	h1->appendChild(m_doc->createTextNode(i18n("XML parsing error")),exceptioncode);
	h1->renderer()->close();
	
	body->appendChild(m_doc->createTextNode(handler.errorProtocol()),exceptioncode);
	body->appendChild(m_doc->createElementNS("http://www.w3.org/1999/xhtml","hr"),exceptioncode);
	NodeImpl *pre = m_doc->createElementNS("http://www.w3.org/1999/xhtml","pre");
	body->appendChild(pre,exceptioncode);
	pre->appendChild(m_doc->createTextNode(line+"\n"),exceptioncode);
	
	unsigned long colno;
	QString indent = "";
	for (colno = 0; colno < handler.errorCol-1; colno++)
	    indent += " ";
	
	pre->appendChild(m_doc->createTextNode(indent+"^"),exceptioncode);
	pre->renderer()->close();
	
	body->renderer()->close();
	m_doc->applyChanges();
	m_doc->updateRendering();
		
	end();
    }
    else {
	addScripts(m_doc);
	m_scriptsIt = new QListIterator<HTMLScriptElementImpl>(m_scripts);
	executeScripts();
    }

}