ExXmlImport.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
  3. *
  4. * This file is part of the QCAD project.
  5. *
  6. * QCAD is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * QCAD is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with QCAD.
  18. */
  19. include("scripts/EAction.js");
  20. /**
  21. * SAX style XML handler.
  22. */
  23. function MyXmlHandler() {
  24. QXmlDefaultHandler.call(this);
  25. }
  26. MyXmlHandler.prototype = new QXmlDefaultHandler();
  27. /**
  28. * Handle element (tag).
  29. */
  30. MyXmlHandler.prototype.startElement = function(namespaceURI, localName, qName, atts) {
  31. EAction.handleUserMessage("tag: " + localName);
  32. for (var i=0; i<atts.length(); i++) {
  33. EAction.handleUserMessage(" attribute: " + atts.localName(i) + ": " + atts.value(i));
  34. }
  35. return true;
  36. };
  37. MyXmlHandler.prototype.endElement = function(namespaceURI, localName, qName) {
  38. return true;
  39. };
  40. /**
  41. * Handle character data.
  42. */
  43. MyXmlHandler.prototype.characters = function(str) {
  44. EAction.handleUserMessage("CDATA: " + str);
  45. return true;
  46. };
  47. /**
  48. * \ingroup ecma_misc_examples_ioexamples
  49. * \class ExXmlImport
  50. * This action reads an XML file and outputs information to stderr.
  51. * This can be used as starting point for XML importers.
  52. */
  53. function ExXmlImport(guiAction) {
  54. EAction.call(this, guiAction);
  55. }
  56. ExXmlImport.prototype = new EAction();
  57. ExXmlImport.prototype.beginEvent = function() {
  58. EAction.prototype.beginEvent.call(this);
  59. var appWin = EAction.getMainWindow();
  60. var initialPath = QDir.homePath();
  61. var filterAll = qsTr("All Files") + " (*)";
  62. var fileDialog = new QFileDialog(appWin, qsTr("Import XML"), initialPath, "");
  63. fileDialog.setNameFilters([ filterAll ]);
  64. fileDialog.selectNameFilter(filterAll);
  65. fileDialog.setOption(QFileDialog.DontUseNativeDialog, getDontUseNativeDialog());
  66. fileDialog.fileMode = QFileDialog.ExistingFiles;
  67. if (!fileDialog.exec()) {
  68. destr(fileDialog);
  69. EAction.activateMainWindow();
  70. this.terminate();
  71. return;
  72. }
  73. var files = fileDialog.selectedFiles();
  74. if (files.length===0) {
  75. destr(fileDialog);
  76. EAction.activateMainWindow();
  77. this.terminate();
  78. return;
  79. }
  80. destr(fileDialog);
  81. EAction.activateMainWindow();
  82. var fileName = files[0];
  83. //var file = new QFile(fileName);
  84. EAction.handleUserMessage("importing: " + fileName);
  85. //var xmlReader = new QXmlSimpleReader();
  86. //var source = new QXmlInputSource(file);
  87. var handler = new MyXmlHandler();
  88. //xmlReader.setContentHandler(handler);
  89. //var ok = xmlReader.parse(source, false);
  90. var ok = parseXml(fileName, handler);
  91. if (ok) {
  92. EAction.handleUserMessage("import successful");
  93. }
  94. else {
  95. EAction.handleUserMessage("import failed");
  96. }
  97. //file.close();
  98. this.terminate();
  99. };