| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 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 tools applications 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 "qt3to4.h" | 
|---|
| 43 |  | 
|---|
| 44 | #include <QApplication> | 
|---|
| 45 | #include <QDir> | 
|---|
| 46 | #include <QFile> | 
|---|
| 47 | #include <QHash> | 
|---|
| 48 | #include <QLibraryInfo> | 
|---|
| 49 | #include <QXmlDefaultHandler> | 
|---|
| 50 | #include <QXmlSimpleReader> | 
|---|
| 51 |  | 
|---|
| 52 | QT_BEGIN_NAMESPACE | 
|---|
| 53 |  | 
|---|
| 54 | class ContentHandler: public QXmlDefaultHandler | 
|---|
| 55 | { | 
|---|
| 56 | public: | 
|---|
| 57 | typedef QPair<QString, QString> Rule; | 
|---|
| 58 | typedef QList<Rule> RuleList; | 
|---|
| 59 |  | 
|---|
| 60 | public: | 
|---|
| 61 | ContentHandler() {} | 
|---|
| 62 |  | 
|---|
| 63 | inline RuleList renamedHeaders() const | 
|---|
| 64 | { return rules(QString::fromLatin1("RenamedHeader")); } | 
|---|
| 65 |  | 
|---|
| 66 | inline RuleList renamedClasses() const | 
|---|
| 67 | { return rules(QString::fromLatin1("RenamedClass")); } | 
|---|
| 68 |  | 
|---|
| 69 | inline RuleList renamedEnums() const | 
|---|
| 70 | { return rules(QString::fromLatin1("RenamedEnumvalue")); } | 
|---|
| 71 |  | 
|---|
| 72 | inline RuleList rules(const QString &kind) const | 
|---|
| 73 | { return m_rules.value(kind); } | 
|---|
| 74 |  | 
|---|
| 75 | virtual bool startDocument() | 
|---|
| 76 | { | 
|---|
| 77 | m_rules.clear(); | 
|---|
| 78 | m_state.current.clear(); | 
|---|
| 79 | m_state.kind.clear(); | 
|---|
| 80 | m_state.q3.clear(); | 
|---|
| 81 | m_state.q4.clear(); | 
|---|
| 82 |  | 
|---|
| 83 | return true; | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | virtual bool startElement(const QString &, const QString &, const QString &qName, const QXmlAttributes &attrs) | 
|---|
| 87 | { | 
|---|
| 88 | if (qName == QLatin1String("item")) { | 
|---|
| 89 | m_state.kind = attrs.value(QLatin1String("Type")); | 
|---|
| 90 |  | 
|---|
| 91 | m_state.current.clear(); | 
|---|
| 92 | m_state.q3.clear(); | 
|---|
| 93 | m_state.q4.clear(); | 
|---|
| 94 |  | 
|---|
| 95 | if (!m_rules.contains(m_state.kind)) | 
|---|
| 96 | m_rules[m_state.kind].clear(); | 
|---|
| 97 |  | 
|---|
| 98 | return true; | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | return true; // ### only if it is a valid tag | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 |  | 
|---|
| 105 | virtual bool endElement(const QString &, const QString &, const QString &qName) | 
|---|
| 106 | { | 
|---|
| 107 | if (qName == QLatin1String("Qt3")) { | 
|---|
| 108 | m_state.q3 = m_state.current.trimmed(); | 
|---|
| 109 | } else if (qName == QLatin1String("Qt4")) { | 
|---|
| 110 | m_state.q4 = m_state.current.trimmed(); | 
|---|
| 111 | } else if (qName == QLatin1String("item") | 
|---|
| 112 | && (m_state.kind == QLatin1String("RenamedHeader") | 
|---|
| 113 | || m_state.kind == QLatin1String("RenamedEnumvalue") | 
|---|
| 114 | || m_state.kind == QLatin1String("RenamedClass"))) { | 
|---|
| 115 | Rule r(m_state.q3, m_state.q4); | 
|---|
| 116 | m_rules[m_state.kind].append(r); | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | m_state.current.clear(); | 
|---|
| 120 | return true; | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | virtual bool characters(const QString &ch) | 
|---|
| 124 | { m_state.current += ch; return true; } | 
|---|
| 125 |  | 
|---|
| 126 | virtual bool error(const QXmlParseException &e) | 
|---|
| 127 | { Q_UNUSED(e); Q_ASSERT(0); return true; } | 
|---|
| 128 |  | 
|---|
| 129 | virtual bool fatalError(const QXmlParseException &e) | 
|---|
| 130 | { Q_UNUSED(e); Q_ASSERT(0); return true; } | 
|---|
| 131 |  | 
|---|
| 132 | virtual bool warning(const QXmlParseException &e) | 
|---|
| 133 | { Q_UNUSED(e); Q_ASSERT(0); return true; } | 
|---|
| 134 |  | 
|---|
| 135 | private: | 
|---|
| 136 | QHash<QString, RuleList> m_rules; | 
|---|
| 137 |  | 
|---|
| 138 | struct state | 
|---|
| 139 | { | 
|---|
| 140 | QString current; | 
|---|
| 141 | QString kind; | 
|---|
| 142 | QString q3; | 
|---|
| 143 | QString q4; | 
|---|
| 144 | } m_state; | 
|---|
| 145 | }; | 
|---|
| 146 |  | 
|---|
| 147 | void Porting::readXML(RuleList *renamedHeaders, RuleList *renamedClasses, RuleList *renamedEnums) | 
|---|
| 148 | { | 
|---|
| 149 | QString fileName = QLatin1String("q3porting.xml"); | 
|---|
| 150 | QString filePath; | 
|---|
| 151 | //check QLibraryInfo::DataPath/filename | 
|---|
| 152 | filePath = QDir::cleanPath(QLibraryInfo::location(QLibraryInfo::DataPath) + QLatin1Char('/') + fileName)  ; | 
|---|
| 153 |  | 
|---|
| 154 | //check QLibraryInfo::PrefixPath/tools/porting/src/filename | 
|---|
| 155 | if (!QFile::exists(filePath)) | 
|---|
| 156 | filePath = QDir::cleanPath(QLibraryInfo::location(QLibraryInfo::PrefixPath) + QLatin1String("/tools/porting/src/") + fileName); | 
|---|
| 157 |  | 
|---|
| 158 | // for bootstrapping, look in the rules file in a location | 
|---|
| 159 | // relative to where it would be in the source tree | 
|---|
| 160 | if (!QFile::exists(filePath)) | 
|---|
| 161 | filePath = QDir::cleanPath(QApplication::instance()->applicationDirPath() + QLatin1String("/../tools/porting/src/") + fileName); | 
|---|
| 162 |  | 
|---|
| 163 | if (!QFile::exists(filePath)) { | 
|---|
| 164 | fprintf(stderr, "Error: Could not find rules file: %s\n", fileName.toLatin1().constData()); | 
|---|
| 165 | Q_ASSERT(0); | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | ContentHandler handler; | 
|---|
| 169 |  | 
|---|
| 170 | QXmlSimpleReader reader; | 
|---|
| 171 | reader.setContentHandler(&handler); | 
|---|
| 172 | reader.setErrorHandler(&handler); | 
|---|
| 173 |  | 
|---|
| 174 | QFile file(filePath); | 
|---|
| 175 | file.open(QFile::ReadOnly); | 
|---|
| 176 |  | 
|---|
| 177 | QXmlInputSource source(file); | 
|---|
| 178 | bool ok = reader.parse(&source); | 
|---|
| 179 | Q_UNUSED(ok); | 
|---|
| 180 | Q_ASSERT(ok); | 
|---|
| 181 |  | 
|---|
| 182 | if (renamedHeaders) | 
|---|
| 183 | *renamedHeaders = handler.renamedHeaders(); | 
|---|
| 184 | if (renamedClasses) | 
|---|
| 185 | *renamedClasses = handler.renamedClasses(); | 
|---|
| 186 | if (renamedEnums) | 
|---|
| 187 | *renamedEnums = handler.renamedEnums(); | 
|---|
| 188 |  | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | Porting::Porting() | 
|---|
| 192 | { | 
|---|
| 193 | readXML(&m_renamedHeaders, &m_renamedClasses, &m_renamedEnums); | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | int Porting::findRule(const RuleList &rules, const QString &q3) | 
|---|
| 197 | { | 
|---|
| 198 | for (int i=0; i<rules.count(); ++i) | 
|---|
| 199 | if (rules.at(i).first == q3) | 
|---|
| 200 | return i; | 
|---|
| 201 | return -1; | 
|---|
| 202 | } | 
|---|
| 203 |  | 
|---|
| 204 | QString Porting::renameHeader(const QString &headerName) const | 
|---|
| 205 | { | 
|---|
| 206 | int index = findRule(m_renamedHeaders, headerName); | 
|---|
| 207 | return index == -1 ? headerName : m_renamedHeaders.at(index).second; | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 | QString Porting::renameClass(const QString &className) const | 
|---|
| 211 | { | 
|---|
| 212 | if (className == QLatin1String("QSplitter"))  // We don't want a Q3Splitter, ever! | 
|---|
| 213 | return className; | 
|---|
| 214 |  | 
|---|
| 215 | int index = findRule(m_renamedClasses, className); | 
|---|
| 216 | return index == -1 ? className : m_renamedClasses.at(index).second; | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 | QString Porting::renameEnumerator(const QString &enumName) const | 
|---|
| 220 | { | 
|---|
| 221 | int index = findRule(m_renamedEnums, enumName); | 
|---|
| 222 | return index == -1 ? QString() : m_renamedEnums.at(index).second; | 
|---|
| 223 | } | 
|---|
| 224 |  | 
|---|
| 225 | QT_END_NAMESPACE | 
|---|