File: igstkTrackerDataLoggerConfigurationXMLFileReader.cxx

package info (click to toggle)
igstk 4.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 38,980 kB
  • sloc: cpp: 86,267; xml: 96; makefile: 75; python: 38
file content (279 lines) | stat: -rwxr-xr-x 8,833 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*=========================================================================

  Program:   Image Guided Surgery Software Toolkit
  Module:    $RCSfile: igstkTrackerDataLoggerConfigurationXMLFileReader.cxx,v $
  Language:  C++
  Date:      $Date: 2011-02-04 22:42:16 $
  Version:   $Revision: 1.1 $

  Copyright (c) ISC  Insight Software Consortium.  All rights reserved.
  See IGSTKCopyright.txt or http://www.igstk.org/copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#include <itksys/SystemTools.hxx>
#include <algorithm>

#include "igstkTrackerDataLoggerConfigurationXMLFileReader.h"

namespace igstk
{

int 
TrackerDataLoggerConfigurationXMLFileReader::CanReadFile(const char* name)
{
  //check that we have a valid file name, it is not a directory, and the 
  //file exists (has length greater than zero)
  if(itksys::SystemTools::FileLength( name ) == 0 ||
     itksys::SystemTools::FileIsDirectory( name ))
    {
    return 0;
    }
  return 1;
}


void 
TrackerDataLoggerConfigurationXMLFileReader::StartElement( const char * name, 
                                                         const char **atts )
{
  this->m_CurrentTagData.clear();

  //check for the root of the xml tree
  if( itksys::SystemTools::Strucmp( name, "tracking_system" ) == 0 ) 
    {
    //there can only be one "tracking_system" per xml file 
    if( HaveConfigurationData() )
      {
      throw FileFormatException(
      std::string( "The tag \"tracking_system\" appears multiple times, \
        should only appear once." ) );
      }
    this->m_ReadingTrackerConfigurationLogger = true;
    }
  else if( itksys::SystemTools::Strucmp( name, "tool" ) == 0 )
    {
    if( !m_ReadingTrackerConfigurationLogger )
      throw FileFormatException( "Tag not nested correctly in xml structure." );
    if( m_ReadingToolConfiguration )
      throw FileFormatException( "Self nested \"tool\" tag not allowed." );
    else
      {
      ProcessToolAttributes( atts );
      this->m_ReadingToolConfiguration = true;
      }
    }
  else if( itksys::SystemTools::Strucmp( name, "name" ) == 0 ||
           itksys::SystemTools::Strucmp( name, "save_to" ) == 0 )
    {
    if( !m_ReadingToolConfiguration )
      {
      throw FileFormatException( "Tag not nested correctly in xml structure." );
      }
    }
}


void 
TrackerDataLoggerConfigurationXMLFileReader::EndElement( const char *name )
{
                         //trim the tag's data string and check that it
                         //isn't empty (empty tags are not allowed)
  size_t startpos = this->m_CurrentTagData.find_first_not_of(" \t\n\r");
  size_t endpos = this->m_CurrentTagData.find_last_not_of(" \t\n\r");   
  if( startpos == std::string::npos || endpos == std::string::npos )
    {
    this->m_CurrentTagData.clear();
    }
  else
    {
    this->m_CurrentTagData = this->m_CurrentTagData.substr( startpos, 
                                                            endpos-startpos+1 );
    }
           //replace all newlines ('\n') and carriage returns ('\r') with space
  std::replace( this->m_CurrentTagData.begin(), 
                this->m_CurrentTagData.end(), 
                '\n', ' ' );
  std::replace( this->m_CurrentTagData.begin(), 
                this->m_CurrentTagData.end(), 
                '\r', ' ' );
                    
  if( this->m_CurrentTagData.empty() )
    {
    std::ostringstream outStr; 
    outStr<<"Empty tag ("<<name<<") not allowed in tracker settings";
    throw FileFormatException( outStr.str() );
    }
  
  if( itksys::SystemTools::Strucmp( name, "tracking_system" ) == 0 )
    {
    this->m_ReadingTrackerConfigurationLogger = false;
                 //see that we got all the data, perhaps replace with a more
                //specific message with regard to the missing tag(s)
    if( !HaveConfigurationData() )
      {
      throw FileFormatException( std::string(
        "Missing information in xml file." ) );
      }
    return;
    }
  
  if( m_ReadingToolConfiguration &&
      itksys::SystemTools::Strucmp( name, "name" ) == 0 )
    {
    ProcessToolName();
    }

  if( m_ReadingToolConfiguration &&
      itksys::SystemTools::Strucmp( name, "save_to" ) == 0 )
    {
    ProcessToolOutputFile();
    }

  if( itksys::SystemTools::Strucmp( name, "tool" ) == 0 )
    {
    this->m_ReadingToolConfiguration = false;
    ProcessToolData();
    }
}


void 
TrackerDataLoggerConfigurationXMLFileReader::CharacterDataHandler( 
const char *inData, int inLength )
{
  this->m_CurrentTagData.append( inData, inLength );
}


void 
TrackerDataLoggerConfigurationXMLFileReader::ProcessToolName() 
throw ( FileFormatException )
{
  //see DEVICE_NAME http://www.na-mic.org/Wiki/index.php//Protocol
  const unsigned int DEVICE_NAME_MAX_LENGTH = 20;

  if( this->m_CurrentTagData.size() > DEVICE_NAME_MAX_LENGTH )
    {
    std::ostringstream outStr; 
    outStr<<"Device name ("<<this->m_CurrentTagData<<") too long, ";
    outStr<<" maximal length is "<<DEVICE_NAME_MAX_LENGTH;
    throw FileFormatException( outStr.str() );
    }
  this->m_CurrentToolName = this->m_CurrentTagData;
}


void 
TrackerDataLoggerConfigurationXMLFileReader::ProcessToolAttributes( 
const char **atts )
{
  //by default the tool isn't a reference
  this->m_CurrentToolIsReference = false;

  //go over all the attribute-value pairs
  for( int i=0; atts[i] != NULL; i+=2 ) 
    {
    if( itksys::SystemTools::Strucmp( atts[i], "usage" ) == 0 &&
        itksys::SystemTools::Strucmp( atts[i+1], "reference" ) == 0 )
        this->m_CurrentToolIsReference = true;
    }
}


void 
TrackerDataLoggerConfigurationXMLFileReader::ProcessToolOutputFile() 
throw ( FileFormatException )
{
  FileNameDataType outputFileData = this->m_CurrentTagData;
  //only add if not a duplicate outputFile, this is a naive test that can be
  //bypassed easily by the user. It is here to prevent copy paste errors in the
  //xml file.
  std::vector<FileNameDataType>::iterator it, end;
  it = this->m_CurrentOutputFileNames.begin();
  end = this->m_CurrentOutputFileNames.end();
  for(; it!=end; it++)
    {
    if( (*it) == outputFileData )
      {
      return;
      }
    }
  this->m_CurrentOutputFileNames.push_back( outputFileData ); 
}


void 
TrackerDataLoggerConfigurationXMLFileReader::ProcessToolData() 
throw ( FileFormatException )
{
  if( this->m_CurrentToolName.empty() )
    {
    throw FileFormatException( "\"name\" tag missing for one of the tools." );
    }
  if( this->m_CurrentOutputFileNames.empty()  && !this->m_CurrentToolIsReference )
    { 
    throw FileFormatException( "\"save_to\" tag missing for one of the tools." );
    }
  LoggerDataType::const_iterator it, end;
  it = this->m_ToolNamesAndOutputFileNames.begin();
  end = this->m_ToolNamesAndOutputFileNames.end();

  //check that both the name and outputFile data are unique, don't send
  //two sets of tool info to the same output file.
  for(; it !=end; it++ )
    {
    if( it->first == this->m_CurrentToolName )
      {
      throw FileFormatException( "Non unique tool name given." );
      }
    std::vector<FileNameDataType>::const_iterator it1, it2, end1, end2;
    it1 = this->m_CurrentOutputFileNames.begin();
    end1 = this->m_CurrentOutputFileNames.end();
    for(; it1 != end1; it1++ )
      {
      it2 = it->second.begin();
      end2 = it->second.end();
      for(; it2 != end2; it2++ )
        {
          if ( (*it1).compare(*it2)  == 0 )
          {
          std::ostringstream msg;
          msg<<"Tools '"<<it->first<<"' and '"<<this->m_CurrentToolName;
          msg<<"' send data to the same output file.";
          throw FileFormatException( msg.str() );
          }
        }
      }
    }
  this->m_ToolNamesAndOutputFileNames.insert( 
    std::pair<std::string, std::vector<FileNameDataType> >
    ( this->m_CurrentToolName, this->m_CurrentOutputFileNames ) );
  this->m_CurrentOutputFileNames.clear();
  this->m_CurrentToolName.clear();
}


bool 
TrackerDataLoggerConfigurationXMLFileReader::HaveConfigurationData()
{
  //check that we finished reading the configuration and that we have at 
  //least one tool with output file defined. 
  return ( !this->m_ReadingTrackerConfigurationLogger &&
            this->m_ToolNamesAndOutputFileNames.size() != 0 );
}

void 
TrackerDataLoggerConfigurationXMLFileReader::
  GetToolConfigurationData(
  LoggerDataType &toolNamesAndOutputFiles )
{
  toolNamesAndOutputFiles.clear();
  toolNamesAndOutputFiles.insert( this->m_ToolNamesAndOutputFileNames.begin(), 
                                  this->m_ToolNamesAndOutputFileNames.end() );
}

} //namespace