Changeset View
Changeset View
Standalone View
Standalone View
src/metadata/xliffextractor.cpp
Show All 22 Lines | |||||
23 | #include "xliffextractor.h" | 23 | #include "xliffextractor.h" | ||
24 | 24 | | |||
25 | #include <QFile> | 25 | #include <QFile> | ||
26 | #include <QXmlInputSource> | 26 | #include <QXmlInputSource> | ||
27 | 27 | | |||
28 | #include "lokalize_debug.h" | 28 | #include "lokalize_debug.h" | ||
29 | #include "catalog/catalog.h" | 29 | #include "catalog/catalog.h" | ||
30 | 30 | | |||
31 | XliffExtractor::XliffExtractor() | | |||
32 | { | | |||
33 | } | | |||
34 | | ||||
35 | class XliffHandler: public QXmlDefaultHandler | 31 | class XliffHandler: public QXmlDefaultHandler | ||
36 | { | 32 | { | ||
37 | public: | 33 | public: | ||
38 | XliffHandler() | 34 | XliffHandler() | ||
39 | : total(0) | 35 | : total(0) | ||
40 | , untranslated(0) | 36 | , untranslated(0) | ||
41 | , fuzzy(0) | 37 | , fuzzy(0) | ||
42 | , fuzzy_reviewer(0) | 38 | , fuzzy_reviewer(0) | ||
▲ Show 20 Lines • Show All 96 Lines • ▼ Show 20 Line(s) | |||||
139 | 135 | | |||
140 | bool XliffHandler::characters(const QString& ch) | 136 | bool XliffHandler::characters(const QString& ch) | ||
141 | { | 137 | { | ||
142 | charCount += ch.length(); | 138 | charCount += ch.length(); | ||
143 | return true; | 139 | return true; | ||
144 | } | 140 | } | ||
145 | 141 | | |||
146 | 142 | | |||
147 | void XliffExtractor::extract(const QString& filePath, FileMetaData& m) | 143 | FileMetaData XliffExtractor::extract(const QString& filePath) | ||
148 | { | 144 | { | ||
149 | QFile file(filePath); | 145 | QFile file(filePath); | ||
150 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) | 146 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { | ||
151 | return; | 147 | return {}; | ||
148 | } | ||||
152 | 149 | | |||
153 | QXmlInputSource source(&file); | 150 | QXmlInputSource source(&file); | ||
154 | QXmlSimpleReader xmlReader; | 151 | QXmlSimpleReader xmlReader; | ||
155 | XliffHandler handler; | 152 | XliffHandler handler; | ||
156 | xmlReader.setContentHandler(&handler); | 153 | xmlReader.setContentHandler(&handler); | ||
157 | 154 | | |||
158 | bool ok = xmlReader.parse(source); | 155 | bool ok = xmlReader.parse(source); | ||
159 | 156 | | |||
160 | if (!ok) | 157 | if (!ok) | ||
161 | qCDebug(LOKALIZE_LOG) << "Parsing failed."; | 158 | qCDebug(LOKALIZE_LOG) << "Parsing failed."; | ||
162 | 159 | | |||
163 | 160 | | |||
164 | //TODO WordCount | 161 | //TODO WordCount | ||
162 | FileMetaData m; | ||||
165 | m.fuzzy = handler.fuzzy; | 163 | m.fuzzy = handler.fuzzy; | ||
166 | m.translated = handler.total - handler.untranslated - handler.fuzzy; | 164 | m.translated = handler.total - handler.untranslated - handler.fuzzy; | ||
167 | m.untranslated = handler.untranslated; | 165 | m.untranslated = handler.untranslated; | ||
168 | m.filePath = filePath; | 166 | m.filePath = filePath; | ||
169 | 167 | | |||
170 | //qCDebug(LOKALIZE_LOG)<<"parsed"<<filePath<<m.fuzzy<<m.translated<<m.untranslated<<handler.fuzzy_approver<<handler.fuzzy_reviewer; | 168 | //qCDebug(LOKALIZE_LOG)<<"parsed"<<filePath<<m.fuzzy<<m.translated<<m.untranslated<<handler.fuzzy_approver<<handler.fuzzy_reviewer; | ||
171 | Q_ASSERT(m.fuzzy >= 0 && m.untranslated >= 0 && handler.total >= 0); | 169 | Q_ASSERT(m.fuzzy >= 0 && m.untranslated >= 0 && handler.total >= 0); | ||
172 | 170 | | |||
173 | m.translated_approver = handler.total - handler.untranslated - handler.fuzzy_approver; | 171 | m.translated_approver = handler.total - handler.untranslated - handler.fuzzy_approver; | ||
174 | m.translated_reviewer = handler.total - handler.untranslated - handler.fuzzy_reviewer; | 172 | m.translated_reviewer = handler.total - handler.untranslated - handler.fuzzy_reviewer; | ||
175 | m.fuzzy_approver = handler.fuzzy_approver; | 173 | m.fuzzy_approver = handler.fuzzy_approver; | ||
176 | m.fuzzy_reviewer = handler.fuzzy_reviewer; | 174 | m.fuzzy_reviewer = handler.fuzzy_reviewer; | ||
177 | 175 | | |||
178 | m.lastTranslator = handler.lastTranslator.length() ? handler.lastTranslator : handler.lastTranslator_fallback; | 176 | m.lastTranslator = handler.lastTranslator.length() ? handler.lastTranslator : handler.lastTranslator_fallback; | ||
179 | m.translationDate = handler.lastDate.isValid() ? handler.lastDate.toString(Qt::ISODate) : handler.lastDateString_fallback; | 177 | m.translationDate = handler.lastDate.isValid() ? handler.lastDate.toString(Qt::ISODate) : handler.lastDateString_fallback; | ||
178 | | ||||
179 | return m; | ||||
180 | } | 180 | } |