| 1 | // XLIFFParser.cxx -- parse an XLIFF 1.2 XML file |
| 2 | //// |
| 3 | // Copyright (C) 2018 James Turner <[email protected]> |
| 4 | // |
| 5 | // This program is free software; you can redistribute it and/or |
| 6 | // modify it under the terms of the GNU General Public License as |
| 7 | // published by the Free Software Foundation; either version 2 of the |
| 8 | // License, or (at your option) any later version. |
| 9 | // |
| 10 | // This program is distributed in the hope that it will be useful, but |
| 11 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | // General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU General Public License |
| 16 | // along with this program; if not, write to the Free Software |
| 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | |
| 19 | #include "config.h" |
| 20 | |
| 21 | #include "XLIFFParser.hxx" |
| 22 | |
| 23 | #include <cstring> |
| 24 | #include <string> |
| 25 | |
| 26 | // simgear |
| 27 | #include <simgear/debug/logstream.hxx> |
| 28 | #include <simgear/props/props.hxx> |
| 29 | #include <simgear/misc/strutils.hxx> |
| 30 | |
| 31 | using namespace flightgear; |
| 32 | |
| 33 | XLIFFParser::XLIFFParser(SGPropertyNode_ptr lroot) : |
| 34 | _localeRoot(lroot) |
| 35 | { |
| 36 | |
| 37 | } |
| 38 | |
| 39 | void XLIFFParser::startXML() |
| 40 | { |
| 41 | |
| 42 | } |
| 43 | |
| 44 | void XLIFFParser::endXML() |
| 45 | { |
| 46 | |
| 47 | } |
| 48 | |
| 49 | void XLIFFParser::startElement(const char *name, const XMLAttributes &atts) |
| 50 | { |
| 51 | _text.clear(); |
| 52 | std::string tag(name); |
| 53 | if (tag == "trans-unit") { |
| 54 | _unitId = atts.getValue("id"); |
| 55 | if (_unitId.empty()) { |
| 56 | SG_LOG(SG_GENERAL, SG_WARN, "XLIFF trans-unit with missing ID: line " |
| 57 | << getLine() << " of " << getPath()); |
| 58 | } |
| 59 | |
| 60 | _source.clear(); |
| 61 | _target.clear(); |
| 62 | } else if (tag == "group") { |
| 63 | _resource = atts.getValue("resname"); |
| 64 | if (_resource.empty()) { |
| 65 | SG_LOG(SG_GENERAL, SG_WARN, "XLIFF group with missing resname: line " |
| 66 | << getLine() << " of " << getPath()); |
| 67 | } else { |
| 68 | _resourceNode = _localeRoot->getChild(_resource, 0, true /* create */); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void XLIFFParser::endElement(const char* name) |
| 74 | { |
| 75 | std::string tag(name); |
| 76 | if (tag == "source") { |
| 77 | _source = _text; |
| 78 | } else if (tag == "target") { |
| 79 | _target = _text; |
| 80 | } else if (tag == "trans-unit") { |
| 81 | finishTransUnit(); |
| 82 | } else if (tag == "group") { |
| 83 | _resource.erase(); |
| 84 | _resourceNode.reset(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void XLIFFParser::finishTransUnit() |
| 89 | { |
| 90 | if (!_resourceNode) { |
| 91 | SG_LOG(SG_GENERAL, SG_WARN, "XLIFF trans-unit without enclosing resource group: line " |
| 92 | << getLine() << " of " << getPath()); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | if (_target.empty()) { |
| 97 | return; // skip missing translations |
| 98 | } |
| 99 | |
| 100 | const auto slashPos = _unitId.find('/'); |
| 101 | const auto indexPos = _unitId.find(':'); |
| 102 | |
| 103 | if (slashPos == std::string::npos) { |
| 104 | SG_LOG(SG_GENERAL, SG_WARN, "XLIFF trans-unit id without resource: '" << |
| 105 | _unitId << "' at line " << getLine() << " of " << getPath()); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | const auto res = _unitId.substr(0, slashPos); |
| 110 | if (res != _resource) { |
| 111 | // this implies the <group> node resname doesn't match the |
| 112 | // id resource prefix. For now just warn and skip, we could decide |
| 113 | // that one or the other takes precedence here? |
| 114 | SG_LOG(SG_GENERAL, SG_WARN, "XLIFF trans-unit with inconsistent resource: line " |
| 115 | << getLine() << " of " << getPath()); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | const auto id = _unitId.substr(slashPos + 1, indexPos - (slashPos + 1)); |
| 120 | const int index = std::stoi(_unitId.substr(indexPos+1)); |
| 121 | _resourceNode->getNode(id, index, true)->setStringValue(_target); |
| 122 | } |
| 123 | |
| 124 | void XLIFFParser::data (const char * s, int len) |
| 125 | { |
| 126 | _text += std::string(s, static_cast<size_t>(len)); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | void XLIFFParser::pi (const char * target, const char * data) |
| 131 | { |
| 132 | SG_UNUSED(target); |
| 133 | SG_UNUSED(data); |
| 134 | //cout << "Processing instruction " << target << ' ' << data << endl; |
| 135 | } |
| 136 | |
| 137 | void XLIFFParser::warning (const char * message, int line, int column) { |
| 138 | SG_LOG(SG_GENERAL, SG_WARN, "Warning: " << message << " (" << line << ',' << column << ')'); |
| 139 | } |