PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ivef-sdk/schema2code/src/mainapp.cpp

http://ivef-sdk.googlecode.com/
C++ | 142 lines | 90 code | 21 blank | 31 comment | 15 complexity | b406aca3f5be64cd5d9e22b5439fa2ae MD5 | raw file
  1. /*
  2. * MainApp.cpp
  3. *
  4. * schema2code is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * schema2code is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * Created by Lukassen on 11/06/08.
  15. * Copyright 2008
  16. *
  17. */
  18. #include <cstdlib>
  19. #include "mainapp.h"
  20. #define STRINGIFY(x) XSTRINGIFY(x)
  21. #define XSTRINGIFY(x) #x
  22. MainApp::MainApp( int & argc, char ** argv )
  23. :QApplication(argc, argv, false) {
  24. // handle the commandline args
  25. // add default command line m_options
  26. m_options.append( CmdLineOption( CmdLineOption::BOOLEAN, "help", "show this information and exit." ) );
  27. m_options.append( CmdLineOption( CmdLineOption::TEXT, "file", "read from XSD file" ) );
  28. m_options.append( CmdLineOption( CmdLineOption::TEXT, "out", "target dir (default is file)" ) );
  29. m_options.append( CmdLineOption( CmdLineOption::BOOLEAN, "version", "show version information and exit." ) );
  30. m_options.append( CmdLineOption( CmdLineOption::TEXT, "prefix", "a prefix for the classes" ) );
  31. m_options.append( CmdLineOption( CmdLineOption::BOOLEAN, "nons", "do not use name space (for c++ qt)" ) );
  32. m_options.append( CmdLineOption( CmdLineOption::BOOLEAN, "qt", "generate qt cpp files (default)." ) );
  33. m_options.append( CmdLineOption( CmdLineOption::BOOLEAN, "php", "generate php files." ) );
  34. // m_options.append( CmdLineOption( CmdLineOption::BOOLEAN, "cpp", "generate cpp files" ) );
  35. // m_options.append( CmdLineOption( CmdLineOption::BOOLEAN, "cs", "generate c# files" ) );
  36. m_options.append( CmdLineOption( CmdLineOption::BOOLEAN, "java", "generate java files" ) );
  37. m_options.append( CmdLineOption( CmdLineOption::BOOLEAN, "objc", "generate objective-C files" ) );
  38. m_options.append( CmdLineOption( CmdLineOption::BOOLEAN, "proto", "generate google protocol buffer interface" ) );
  39. // parse command line m_options
  40. m_options.parse( argc, argv );
  41. // is there a request for some version info?
  42. if ( m_options.getBoolean( "version" ) ) {
  43. std::cout << "\n schema2code " << STRINGIFY(VERSION)
  44. << "\n----------------------------------------\n\nCopyright "
  45. << QDate::currentDate().year() << "\n" << std::endl;
  46. std::exit(0);
  47. }
  48. // setup the file handler
  49. m_reader = new QXmlSimpleReader();
  50. m_handler = new Handler();
  51. // setup the parser
  52. m_reader->setContentHandler(m_handler);
  53. m_reader->setErrorHandler(m_handler);
  54. // startup timer, to allow the event loop to start
  55. QTimer *timer = new QTimer( 0 ); // we leak one timer here, is acceptable
  56. timer->setInterval( 100 );
  57. timer->setSingleShot( true );
  58. connect( timer, SIGNAL( timeout() ), this, SLOT( slotStart() ) );
  59. timer->start();
  60. }
  61. void MainApp::slotStart( void ) {
  62. std::cout << "schema2code started" << std::endl;
  63. QString fileName("");
  64. if (m_options.getText("file", fileName)) {
  65. // read from file
  66. QFile *file = new QFile(fileName);
  67. // test if we can read it
  68. if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) {
  69. std::cout << QString("schema2code error opening file: %1").arg(fileName).toLatin1().data() << std::endl;
  70. std::exit(-1);
  71. }
  72. // and the input
  73. QXmlInputSource inputXML(file);
  74. m_reader->parse(inputXML); // presume we can read the whole file at once
  75. CodeGen *generator = 0;
  76. if ( m_options.getBoolean( "java" ) ) {
  77. std::cout << "\n---------------------------------------------------------------------\n" << std::endl;
  78. std::cout << "Generating JAVA sources \n" << std::endl;
  79. std::cout << "---------------------------------------------------------------------\n" << std::endl;
  80. generator = new CodeGenJava();
  81. } else if ( m_options.getBoolean( "cs" ) ) {
  82. std::cout << "schema2code: sorry not implemented yet" << std::endl;
  83. std::exit(-1);
  84. } else if ( m_options.getBoolean( "objc" ) ) {
  85. std::cout << "\n---------------------------------------------------------------------\n" << std::endl;
  86. std::cout << "Generating Objective-C sources \n" << std::endl;
  87. std::cout << "---------------------------------------------------------------------\n" << std::endl;
  88. generator = new CodeGenObjC();
  89. } else if ( m_options.getBoolean( "php" ) ) {
  90. std::cout << "\n---------------------------------------------------------------------\n" << std::endl;
  91. std::cout << "Generating PHP5 sources \n" << std::endl;
  92. std::cout << "---------------------------------------------------------------------\n" << std::endl;
  93. generator = new CodeGenPHP();
  94. } else if ( m_options.getBoolean( "proto" ) ) {
  95. std::cout << "\n---------------------------------------------------------------------\n" << std::endl;
  96. std::cout << "Generating Protocol Buffer file \n" << std::endl;
  97. std::cout << "---------------------------------------------------------------------\n" << std::endl;
  98. generator = new CodeGenPB();
  99. } else if ( m_options.getBoolean( "cpp" ) ) {
  100. std::cout << "schema2code: sorry not implemented yet" << std::endl;
  101. std::exit(-1);
  102. } else {
  103. std::cout << "\n---------------------------------------------------------------------\n" << std::endl;
  104. std::cout << "Generating Qt sources \n" << std::endl;
  105. std::cout << "---------------------------------------------------------------------\n" << std::endl;
  106. generator = new CodeGenQT(); // default to Qt
  107. }
  108. QString prefix("");
  109. m_options.getText("prefix", prefix); // get an optional prefix
  110. // set properties and go
  111. generator->setObjects(m_handler->objects());
  112. generator->setPrefix(prefix);
  113. QString outDir(fileName); // probably need to create it first
  114. m_options.getText("out", outDir);
  115. generator->setOutputDir(outDir);
  116. generator->setNamespace( !m_options.getBoolean( "nons" ) );
  117. generator->go();
  118. // we are finished
  119. std::exit(0);
  120. }
  121. }