[15] | 1 | /*************************************************************************** |
---|
| 2 | tmdmcreator.cpp |
---|
| 3 | ------------------- |
---|
| 4 | copyright : (C) 2013 CREALP Lucien Schreiber |
---|
| 5 | email : lucien.schreiber at crealp dot vs dot ch |
---|
| 6 | ***************************************************************************/ |
---|
| 7 | |
---|
| 8 | /*************************************************************************** |
---|
| 9 | * * |
---|
| 10 | * This program is free software; you can redistribute it and/or modify * |
---|
| 11 | * it under the terms of the GNU General Public License as published by * |
---|
| 12 | * the Free Software Foundation; either version 2 of the License, or * |
---|
| 13 | * (at your option) any later version. * |
---|
| 14 | * * |
---|
| 15 | ***************************************************************************/ |
---|
| 16 | |
---|
| 17 | #include "tmdmcreator.h" |
---|
[16] | 18 | #include "tmdmcopier.h" |
---|
[17] | 19 | #include "tmdmprocessor.h" |
---|
[15] | 20 | |
---|
[16] | 21 | TmDmCreator::TmDmCreator() { |
---|
[15] | 22 | } |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | |
---|
[16] | 26 | TmDmCreator::~TmDmCreator() { |
---|
[15] | 27 | } |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | |
---|
[16] | 31 | void TmDmCreator::SetBaseSQL(wxFileName value) { |
---|
[15] | 32 | m_FileNameBaseSQL = value; |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | |
---|
[16] | 37 | void TmDmCreator::SetUserSQL(wxFileName value) { |
---|
[15] | 38 | m_FileNameUserSQL = value; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | |
---|
[16] | 43 | void TmDmCreator::SetUserContent(wxFileName value) { |
---|
[15] | 44 | m_FileNameUserContent = value; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | |
---|
[16] | 49 | void TmDmCreator::SetOutSQL(wxFileName value) { |
---|
[15] | 50 | m_FileNameOutSQL = value; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | |
---|
[16] | 54 | bool TmDmCreator::_CheckExistsAndExt(const wxFileName & filename, const wxString & extension, wxArrayString * errormsg){ |
---|
| 55 | wxASSERT(errormsg); |
---|
| 56 | if (filename.Exists() == false) { |
---|
| 57 | errormsg->Add(wxString::Format(_("File: %s didn't exists!"), filename.GetFullPath())); |
---|
| 58 | return false; |
---|
| 59 | } |
---|
| 60 | if (filename.GetExt().Lower() != extension) { |
---|
| 61 | errormsg->Add(wxString::Format(_("Wrong extension for: %s (expected: %s)"), filename.GetFullPath(), extension)); |
---|
| 62 | return false; |
---|
| 63 | } |
---|
| 64 | return true; |
---|
| 65 | } |
---|
[15] | 66 | |
---|
[16] | 67 | |
---|
| 68 | |
---|
| 69 | bool TmDmCreator::CheckFiles(wxArrayString & errormsg) { |
---|
| 70 | errormsg.Clear(); |
---|
| 71 | if (_CheckExistsAndExt(m_FileNameBaseSQL, _T("sql"), &errormsg) == false) { |
---|
| 72 | return false; |
---|
| 73 | } |
---|
| 74 | if (_CheckExistsAndExt(m_FileNameUserSQL, _T("sql"), &errormsg) == false) { |
---|
| 75 | return false; |
---|
| 76 | } |
---|
| 77 | if (_CheckExistsAndExt(m_FileNameUserContent, _T("txt"), &errormsg) == false) { |
---|
| 78 | return false; |
---|
| 79 | } |
---|
| 80 | if (m_FileNameOutSQL.Exists()) { |
---|
| 81 | errormsg.Add(wxString::Format(_("output file: %s allready exists!"), m_FileNameOutSQL.GetFullPath())); |
---|
| 82 | return false; |
---|
| 83 | } |
---|
| 84 | if (m_FileNameOutSQL.IsDirWritable() == false){ |
---|
| 85 | errormsg.Add(wxString::Format(_T("Writing not permitted into %s"), m_FileNameOutSQL.GetPath())); |
---|
| 86 | return false; |
---|
| 87 | } |
---|
| 88 | return true; |
---|
[15] | 89 | } |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | |
---|
[16] | 93 | bool TmDmCreator::ProcessFiles(wxArrayString & errorsmsg) { |
---|
| 94 | errorsmsg.Clear(); |
---|
[17] | 95 | // Copy structure |
---|
[18] | 96 | { // needed to destroy the copier before processing layers |
---|
| 97 | TmDmCopier myCopier(m_FileNameOutSQL); |
---|
| 98 | if (myCopier.CopyFrom(m_FileNameBaseSQL) == false) { |
---|
| 99 | errorsmsg.Add(wxString::Format(_("Copying: %s failed!"), m_FileNameBaseSQL.GetFullPath())); |
---|
| 100 | return false; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | if (myCopier.CopyFrom(m_FileNameUserSQL) == false) { |
---|
| 104 | errorsmsg.Add(wxString::Format(_("Copying: %s failed!"), m_FileNameUserSQL.GetFullPath())); |
---|
| 105 | return false; |
---|
| 106 | } |
---|
[16] | 107 | } |
---|
[18] | 108 | |
---|
[17] | 109 | // Process layers |
---|
[19] | 110 | TmDmProcessorSimple myProc(m_FileNameUserContent, m_FileNameOutSQL); |
---|
| 111 | int myThematicLayersStart = myProc.FindBlock(_T("thematic_layers")); |
---|
[17] | 112 | if (myThematicLayersStart == wxNOT_FOUND) { |
---|
| 113 | errorsmsg.Add(wxString::Format(_("'thematic_layers' field not found in %s"), m_FileNameUserContent.GetFullPath())); |
---|
| 114 | return false; |
---|
| 115 | } |
---|
[19] | 116 | if (myProc.ProcessBlock(myThematicLayersStart, _T("thematic_layers"))==false) { |
---|
[17] | 117 | errorsmsg.Add(wxString::Format(_("Processing 'thematic_layers' failed in %s"), m_FileNameUserContent.GetFullPath())); |
---|
| 118 | return false; |
---|
| 119 | } |
---|
[19] | 120 | |
---|
| 121 | // Process objects |
---|
| 122 | int myObjectStart = myProc.FindBlock(_T("dmn_layer_object")); |
---|
| 123 | if (myObjectStart == wxNOT_FOUND) { |
---|
| 124 | errorsmsg.Add(wxString::Format(_("'dmn_layer_object' field not found in %s"), m_FileNameUserContent.GetFullPath())); |
---|
| 125 | return false; |
---|
| 126 | } |
---|
| 127 | if (myProc.ProcessBlock(myObjectStart, _T("dmn_layer_object"))==false) { |
---|
| 128 | errorsmsg.Add(wxString::Format(_("Processing 'dmn_layer_object' failed in %s"), m_FileNameUserContent.GetFullPath())); |
---|
| 129 | return false; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | // Process attributs |
---|
| 133 | TmDmProcessorAttributs myProcAttributs (m_FileNameUserContent, m_FileNameOutSQL); |
---|
| 134 | int myAttributStart = myProcAttributs.FindBlock(_T("attributs")); |
---|
| 135 | if (myAttributStart == wxNOT_FOUND) { |
---|
| 136 | errorsmsg.Add(wxString::Format(_("'attributs' field not found in %s"), m_FileNameUserContent.GetFullPath())); |
---|
| 137 | return false; |
---|
| 138 | } |
---|
| 139 | if (myProcAttributs.ProcessBlock(myAttributStart, wxEmptyString)==false) { |
---|
| 140 | errorsmsg.Add(wxString::Format(_("Processing 'attributs' failed in %s"), m_FileNameUserContent.GetFullPath())); |
---|
| 141 | return false; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | |
---|
[16] | 145 | return true; |
---|
[15] | 146 | } |
---|
| 147 | |
---|