00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "xmlparser.hpp"
00021 #include <QtCore/QDebug>
00022
00023 QE_BEGIN_NAMESPACE
00024
00025 XmlParser::XmlParser()
00026 {
00027 clearLastError();
00028
00029
00030 m_ext.append("xml");
00031 m_ext.append("html");
00032 }
00033
00034 bool XmlParser::parse( const QString &xmlData)
00035 {
00036 bool parsingOk = false;
00037 QString *errorMsg = new QString();
00038 int *errorLine = new int(), *errorColumn = new int();
00039 if ( m_xmlDoc.setContent( xmlData, true, errorMsg, errorLine, errorColumn ) )
00040 {
00041 parsingOk = true;
00042 }
00043 else
00044 {
00045 m_lastError = QString("Erreur lors du parsing : %1 (ligne : %2, colonne : %3")
00046 .arg(*errorMsg)
00047 .arg(*errorLine)
00048 .arg(*errorColumn);
00049 }
00050
00051 return parsingOk;
00052 }
00053
00054 bool XmlParser::parse( QFile &xmlFile)
00055 {
00056 bool parsingOk = false;
00057 QString tmpFilename = xmlFile.fileName();
00058 QString tmpExt = tmpFilename.right(tmpFilename.length() - (tmpFilename.lastIndexOf('.') + 1));
00059
00060 if ( m_ext.contains( tmpExt, Qt::CaseInsensitive ) )
00061 {
00062 if (xmlFile.open(QIODevice::ReadOnly))
00063 {
00064 QString *errorMsg = new QString();
00065 int *errorLine = new int(), *errorColumn = new int();
00066 if ( m_xmlDoc.setContent( &xmlFile, true, errorMsg, errorLine, errorColumn ) )
00067 {
00068 parsingOk = true;
00069 }
00070 else
00071 {
00072 m_lastError = QString("Erreur lors du parsing du fichier %1 : %2 (ligne : %3, colonne : %4")
00073 .arg(tmpFilename)
00074 .arg(*errorMsg)
00075 .arg(*errorLine)
00076 .arg(*errorColumn);
00077 }
00078 xmlFile.close();
00079 }
00080 else
00081 {
00082 m_lastError = QString("Impossible de lire le fichier : %1").arg(tmpFilename);
00083 }
00084 }
00085 else
00086 {
00087 m_lastError = QString("Extension incorrecte pour le fichier (%1), les extensions valides sont : ").arg(tmpFilename);
00088 for ( int i = 0; i < m_ext.size(); ++i )
00089 {
00090 m_lastError += m_ext.at(i);
00091 if( i != (m_ext.size() - 1) )
00092 {
00093 m_lastError += ", ";
00094 }
00095 }
00096 }
00097
00098 return parsingOk;
00099 }
00100
00101 QString XmlParser::lastError() const
00102 {
00103 return m_lastError;
00104 }
00105
00106 void XmlParser::clearLastError()
00107 {
00108 m_lastError = "Aucune erreur";
00109 }
00110
00111 QE_END_NAMESPACE