txload.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* === This file is part of Calamares - <https://github.com/calamares> ===
  2. *
  3. * Copyright 2018, Adriaan de Groot <groot@kde.org>
  4. *
  5. * Calamares is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Calamares is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Calamares. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <QCoreApplication>
  19. #include <QDebug>
  20. #include <QFile>
  21. #include <QList>
  22. #include <QDomDocument>
  23. bool load_file(const char* filename, QDomDocument& doc)
  24. {
  25. QFile file(filename);
  26. QString err;
  27. int err_line, err_column;
  28. if (!file.open(QIODevice::ReadOnly))
  29. {
  30. qDebug() << "Could not open" << filename;
  31. return false;
  32. }
  33. QByteArray ba( file.read(1024 * 1024) );
  34. qDebug() << "Read" << ba.length() << "bytes from" << filename;
  35. if (!doc.setContent(ba, &err, &err_line, &err_column)) {
  36. qDebug() << "Could not read" << filename << ':' << err_line << ':' << err_column << ' ' << err;
  37. file.close();
  38. return false;
  39. }
  40. file.close();
  41. return true;
  42. }
  43. QDomElement find_context(QDomDocument& doc, const QString& name)
  44. {
  45. QDomElement top = doc.documentElement();
  46. QDomNode n = top.firstChild();
  47. while (!n.isNull()) {
  48. if (n.isElement()) {
  49. QDomElement e = n.toElement();
  50. if ( ( e.tagName() == "context" ) && ( e.firstChildElement( "name" ).text() == name ) )
  51. return e;
  52. }
  53. n = n.nextSibling();
  54. }
  55. return QDomElement();
  56. }
  57. QDomElement find_message(QDomElement& context, const QString& source)
  58. {
  59. QDomNode n = context.firstChild();
  60. while (!n.isNull()) {
  61. if (n.isElement()) {
  62. QDomElement e = n.toElement();
  63. if ( e.tagName() == "message" )
  64. {
  65. QString msource = e.firstChildElement( "source" ).text();
  66. if ( msource == source )
  67. return e;
  68. }
  69. }
  70. n = n.nextSibling();
  71. }
  72. return QDomElement();
  73. }
  74. bool merge_into(QDomElement& master, QDomElement& sub)
  75. {
  76. QDomNode n = sub.firstChild();
  77. while (!n.isNull()) {
  78. if (n.isElement()) {
  79. QDomElement e = n.toElement();
  80. if ( e.tagName() == "message" )
  81. {
  82. QString source = e.firstChildElement( "source" ).text();
  83. QString translation = e.firstChildElement( "translation" ).text();
  84. QDomElement masterTranslation = find_message( master, source );
  85. if ( masterTranslation.isNull() )
  86. {
  87. qDebug() << "No master translation for" << source;
  88. return false;
  89. }
  90. QString msource = masterTranslation.firstChildElement( "source" ).text();
  91. QString mtranslation = masterTranslation.firstChildElement( "translation" ).text();
  92. if ( source != msource )
  93. {
  94. qDebug() << "Mismatch for messages\n" << source << '\n' << msource;
  95. return false;
  96. }
  97. if ( !translation.isEmpty() && ( translation != mtranslation ) )
  98. {
  99. qDebug() << "\n\n\nSource:" << source << "\nTL1:" << mtranslation << "\nTL2:" << translation;
  100. }
  101. }
  102. }
  103. n = n.nextSibling();
  104. }
  105. return true;
  106. }
  107. bool merge_into(QDomDocument& master, QDomElement& context)
  108. {
  109. QDomElement name = context.firstChildElement( "name" );
  110. if ( name.isNull() )
  111. return false;
  112. QString contextname = name.text();
  113. QDomElement masterContext = find_context( master, contextname );
  114. if ( masterContext.isNull() )
  115. {
  116. qDebug() << "Master document has no context" << contextname;
  117. return false;
  118. }
  119. return merge_into( masterContext, context );
  120. }
  121. bool merge_into(QDomDocument& master, QDomDocument& sub)
  122. {
  123. QDomElement top = sub.documentElement();
  124. QDomNode n = top.firstChild();
  125. while (!n.isNull()) {
  126. if (n.isElement()) {
  127. QDomElement e = n.toElement();
  128. if ( e.tagName() == "context" )
  129. if ( !merge_into( master, e ) )
  130. return false;
  131. }
  132. n = n.nextSibling();
  133. }
  134. return true;
  135. }
  136. int main(int argc, char** argv)
  137. {
  138. QCoreApplication a(argc, argv);
  139. if (argc < 2)
  140. return 1;
  141. QDomDocument doc("master");
  142. if ( !load_file(argv[1], doc) )
  143. return 1;
  144. for (int i = 2; i < argc; ++i)
  145. {
  146. QDomDocument subdoc("sub");
  147. if ( !load_file(argv[i], subdoc) )
  148. return 1;
  149. if ( !merge_into( doc, subdoc ) )
  150. return 1;
  151. }
  152. QString outfilename( argv[1] );
  153. outfilename.append( ".new" );
  154. QFile outfile(outfilename);
  155. if (!outfile.open(QIODevice::WriteOnly))
  156. {
  157. qDebug() << "Could not open" << outfilename;
  158. return 1;
  159. }
  160. outfile.write( doc.toString(4).toUtf8() );
  161. outfile.close();
  162. return 0;
  163. }