| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at qt-info@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include <QtCore>
|
|---|
| 43 |
|
|---|
| 44 | void parseHtmlFile(QTextStream &out, const QString &fileName) {
|
|---|
| 45 | QFile file(fileName);
|
|---|
| 46 |
|
|---|
| 47 | out << "Analysis of HTML file: " << fileName << endl;
|
|---|
| 48 |
|
|---|
| 49 | if (!file.open(QIODevice::ReadOnly)) {
|
|---|
| 50 | out << " Couldn't open the file." << endl << endl << endl;
|
|---|
| 51 | return;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | //! [0]
|
|---|
| 55 | QXmlStreamReader reader(&file);
|
|---|
| 56 | //! [0]
|
|---|
| 57 |
|
|---|
| 58 | //! [1]
|
|---|
| 59 | int paragraphCount = 0;
|
|---|
| 60 | QStringList links;
|
|---|
| 61 | QString title;
|
|---|
| 62 | while (!reader.atEnd()) {
|
|---|
| 63 | reader.readNext();
|
|---|
| 64 | if (reader.isStartElement()) {
|
|---|
| 65 | if (reader.name() == "title")
|
|---|
| 66 | title = reader.readElementText();
|
|---|
| 67 | else if(reader.name() == "a")
|
|---|
| 68 | links.append(reader.attributes().value("href").toString());
|
|---|
| 69 | else if(reader.name() == "p")
|
|---|
| 70 | ++paragraphCount;
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 | //! [1]
|
|---|
| 74 |
|
|---|
| 75 | //! [2]
|
|---|
| 76 | if (reader.hasError()) {
|
|---|
| 77 | out << " The HTML file isn't well-formed: " << reader.errorString()
|
|---|
| 78 | << endl << endl << endl;
|
|---|
| 79 | return;
|
|---|
| 80 | }
|
|---|
| 81 | //! [2]
|
|---|
| 82 |
|
|---|
| 83 | out << " Title: \"" << title << "\"" << endl
|
|---|
| 84 | << " Number of paragraphs: " << paragraphCount << endl
|
|---|
| 85 | << " Number of links: " << links.size() << endl
|
|---|
| 86 | << " Showing first few links:" << endl;
|
|---|
| 87 |
|
|---|
| 88 | while(links.size() > 5)
|
|---|
| 89 | links.removeLast();
|
|---|
| 90 |
|
|---|
| 91 | foreach(QString link, links)
|
|---|
| 92 | out << " " << link << endl;
|
|---|
| 93 | out << endl << endl;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | int main(int argc, char **argv)
|
|---|
| 97 | {
|
|---|
| 98 | // intialize QtCore application
|
|---|
| 99 | QCoreApplication app(argc, argv);
|
|---|
| 100 |
|
|---|
| 101 | // get a list of all html files in the current directory
|
|---|
| 102 | QStringList filter;
|
|---|
| 103 | filter << "*.htm";
|
|---|
| 104 | filter << "*.html";
|
|---|
| 105 | QStringList htmlFiles = QDir::current().entryList(filter, QDir::Files);
|
|---|
| 106 |
|
|---|
| 107 | QTextStream out(stdout);
|
|---|
| 108 |
|
|---|
| 109 | if (htmlFiles.isEmpty()) {
|
|---|
| 110 | out << "No html files available.";
|
|---|
| 111 | return 1;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | // parse each html file and write the result to file/stream
|
|---|
| 115 | foreach(QString file, htmlFiles)
|
|---|
| 116 | parseHtmlFile(out, file);
|
|---|
| 117 |
|
|---|
| 118 | return 0;
|
|---|
| 119 | }
|
|---|