reader.setContentHandler( &parser );
reader.parse( &source );
+ if( parser.isValid() ) {
+ board = parser.getBoard();
+ } else {
+ qDebug("inValid");
+ }
return board;
}
bool Board::test()
{
- Board::fromXml( "<board>\n<url>http://pc.2ch.net/linux/</url>\n<name>Linux</name>\n</board>\n" );
+ Board board = Board::fromXml( "<board>\n<url>http://pc.2ch.net/linux/</url>\n<name>Linux</name>\n</board>\n" );
+ if( board.url().prettyURL() != "http://pc.2ch.net/linux/" ) qDebug("inValid: line=%d", __LINE__);
+ if( board.name() != "Linux" ) qDebug("inValid: line=%d", __LINE__);
+
+ board = Board::fromXml( " <board>\n <url>http://pc.2ch.net/linux/</url>\n <name>Linux</name>\n </board>\n" );
+ if( board.url().prettyURL() != "http://pc.2ch.net/linux/" ) qDebug("inValid: line=%d", __LINE__);
+ if( board.name() != "Linux" ) qDebug("inValid: line=%d", __LINE__);
+
return true;
}
-bool BoardXmlParser::startElement( const QString& namespaceURI, const QString& localName, const QString& qName, const QXmlAttributes& atts )
+bool BoardXmlParser::startElement( const QString& namespaceURI, const QString& localName, const QString& qName, const QXmlAttributes& )
{
- qDebug("startElement url='%s', local='%s', qName='%s'", namespaceURI.latin1(), localName.latin1(), qName.latin1() );
+// qDebug("startElement url='%s', local='%s', qName='%s'", namespaceURI.latin1(), localName.latin1(), qName.latin1() );
+ if( m_inBoard == true ) {
+ if( localName == "url" ) {
+ m_inURL = true;
+ } else if( localName == "name" ) {
+ m_inName = true;
+ } else {
+ // error
+ return false;
+ }
+ } else {
+ if( localName == "board" ) {
+ m_inBoard = true;
+ } else {
+ // error
+ return false;
+ }
+ }
return true;
}
bool BoardXmlParser::endElement( const QString& namespaceURI, const QString& localName, const QString& qName )
{
- qDebug("endElement url='%s', local='%s', qName='%s'", namespaceURI.latin1(), localName.latin1(), qName.latin1() );
+// qDebug("endElement url='%s', local='%s', qName='%s'", namespaceURI.latin1(), localName.latin1(), qName.latin1() );
+ if( localName == "board" ) {
+ m_inBoard = false;
+ m_board = Kita::Board( KURL( m_urlStr ), m_nameStr );
+ m_isValid = true;
+ // create board;
+ } else if( localName == "url" ) {
+ m_inURL = false;
+ } else if( localName == "name" ) {
+ m_inName = false;
+ } else {
+ // error
+ return false;
+ }
return true;
}
bool BoardXmlParser::characters( const QString& ch )
{
- qDebug("characters ch='%s'", ch.latin1() );
+// qDebug("characters ch='%s'", ch.latin1() );
+
+ if( m_inName == true ) {
+ m_nameStr = ch;
+ } else if( m_inURL == true ) {
+ m_urlStr = ch;
+ } else {
+ // ignore
+ }
return true;
}
class BoardXmlParser : public QXmlDefaultHandler
{
+ QString m_nameStr;
+ QString m_urlStr;
+
+ Board m_board;
+ bool m_inBoard;
+ bool m_inName;
+ bool m_inURL;
+ bool m_isValid;
public:
- BoardXmlParser() {};
+ BoardXmlParser() : m_inBoard(false), m_inName(false), m_inURL(false), m_isValid(false) {};
~BoardXmlParser() {};
bool startElement( const QString& namespaceURI, const QString& localName, const QString& qName, const QXmlAttributes& atts );
bool endElement( const QString& namespaceURI, const QString& localName, const QString& qName );
bool characters( const QString& ch );
+ bool isValid() const { return m_isValid; }
+ Board getBoard() const { return m_board; }
};
};