Line data Source code
1 :
2 : /*
3 : * ALMA - Atacama Large Millimeter Array
4 : * (c) European Southern Observatory, 2002
5 : * (c) Associated Universities Inc., 2002
6 : * Copyright by ESO (in the framework of the ALMA collaboration),
7 : * Copyright by AUI (in the framework of the ALMA collaboration),
8 : * All rights reserved.
9 : *
10 : * This library is free software; you can redistribute it and/or
11 : * modify it under the terms of the GNU Lesser General Public
12 : * License as published by the Free software Foundation; either
13 : * version 2.1 of the License, or (at your option) any later version.
14 : *
15 : * This library is distributed in the hope that it will be useful,
16 : * but WITHOUT ANY WARRANTY, without even the implied warranty of
17 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 : * Lesser General Public License for more details.
19 : *
20 : * You should have received a copy of the GNU Lesser General Public
21 : * License along with this library; if not, write to the Free Software
22 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 : * MA 02111-1307 USA
24 : *
25 : * Warning!
26 : * --------------------------------------------------------------------
27 : * | This is generated code! Do not modify this file. |
28 : * | If you do, all changes will be lost when the file is re-generated. |
29 : * --------------------------------------------------------------------
30 : *
31 : * File SysCalTable.cpp
32 : */
33 : #include <alma/ASDM/ConversionException.h>
34 : #include <alma/ASDM/DuplicateKey.h>
35 : #include <alma/ASDM/OutOfBoundsException.h>
36 :
37 : using asdm::ConversionException;
38 : using asdm::DuplicateKey;
39 : using asdm::OutOfBoundsException;
40 :
41 : #include <alma/ASDM/ASDM.h>
42 : #include <alma/ASDM/SysCalTable.h>
43 : #include <alma/ASDM/SysCalRow.h>
44 : #include <alma/ASDM/Parser.h>
45 :
46 : using asdm::ASDM;
47 : using asdm::SysCalTable;
48 : using asdm::SysCalRow;
49 : using asdm::Parser;
50 :
51 : #include <iostream>
52 : #include <fstream>
53 : #include <iterator>
54 : #include <sstream>
55 : #include <set>
56 : #include <algorithm>
57 : using namespace std;
58 :
59 : #include <alma/ASDM/Misc.h>
60 : using namespace asdm;
61 :
62 : #include <libxml/parser.h>
63 : #include <libxml/tree.h>
64 :
65 : #ifndef WITHOUT_BOOST
66 : #include "boost/filesystem/operations.hpp"
67 : #include <boost/algorithm/string.hpp>
68 : #else
69 : #include <sys/stat.h>
70 : #endif
71 :
72 : namespace asdm {
73 : // The name of the entity we will store in this table.
74 : static string entityNameOfSysCal = "SysCal";
75 :
76 : // An array of string containing the names of the columns of this table.
77 : // The array is filled in the order : key, required value, optional value.
78 : //
79 : static string attributesNamesOfSysCal_a[] = {
80 :
81 : "antennaId"
82 : ,
83 : "spectralWindowId"
84 : ,
85 : "timeInterval"
86 : ,
87 : "feedId"
88 :
89 :
90 : , "numReceptor"
91 :
92 : , "numChan"
93 :
94 :
95 : , "tcalFlag"
96 :
97 : , "tcalSpectrum"
98 :
99 : , "trxFlag"
100 :
101 : , "trxSpectrum"
102 :
103 : , "tskyFlag"
104 :
105 : , "tskySpectrum"
106 :
107 : , "tsysFlag"
108 :
109 : , "tsysSpectrum"
110 :
111 : , "tantFlag"
112 :
113 : , "tantSpectrum"
114 :
115 : , "tantTsysFlag"
116 :
117 : , "tantTsysSpectrum"
118 :
119 : , "phaseDiffFlag"
120 :
121 : , "phaseDiffSpectrum"
122 :
123 : };
124 :
125 : // A vector of string whose content is a copy of the strings in the array above.
126 : //
127 : static vector<string> attributesNamesOfSysCal_v (attributesNamesOfSysCal_a, attributesNamesOfSysCal_a + sizeof(attributesNamesOfSysCal_a) / sizeof(attributesNamesOfSysCal_a[0]));
128 :
129 : // An array of string containing the names of the columns of this table.
130 : // The array is filled in the order where the names would be read by default in the XML header of a file containing
131 : // the table exported in binary mode.
132 : //
133 : static string attributesNamesInBinOfSysCal_a[] = {
134 :
135 : "antennaId" , "spectralWindowId" , "timeInterval" , "feedId" , "numReceptor" , "numChan"
136 : ,
137 : "tcalFlag" , "tcalSpectrum" , "trxFlag" , "trxSpectrum" , "tskyFlag" , "tskySpectrum" , "tsysFlag" , "tsysSpectrum" , "tantFlag" , "tantSpectrum" , "tantTsysFlag" , "tantTsysSpectrum" , "phaseDiffFlag" , "phaseDiffSpectrum"
138 :
139 : };
140 :
141 : // A vector of string whose content is a copy of the strings in the array above.
142 : //
143 : static vector<string> attributesNamesInBinOfSysCal_v(attributesNamesInBinOfSysCal_a, attributesNamesInBinOfSysCal_a + sizeof(attributesNamesInBinOfSysCal_a) / sizeof(attributesNamesInBinOfSysCal_a[0]));
144 :
145 :
146 : // The array of attributes (or column) names that make up key key.
147 : //
148 : string keyOfSysCal_a[] = {
149 :
150 : "antennaId"
151 : ,
152 : "spectralWindowId"
153 : ,
154 : "timeInterval"
155 : ,
156 : "feedId"
157 :
158 : };
159 :
160 : // A vector of strings which are copies of those stored in the array above.
161 : vector<string> keyOfSysCal_v(keyOfSysCal_a, keyOfSysCal_a + sizeof(keyOfSysCal_a) / sizeof(keyOfSysCal_a[0]));
162 :
163 : /**
164 : * Return the list of field names that make up key key
165 : * as a const reference to a vector of strings.
166 : */
167 0 : const vector<string>& SysCalTable::getKeyName() {
168 0 : return keyOfSysCal_v;
169 : }
170 :
171 :
172 118 : SysCalTable::SysCalTable(ASDM &c) : container(c) {
173 :
174 : // Define a default entity.
175 118 : entity.setEntityId(EntityId("uid://X0/X0/X0"));
176 118 : entity.setEntityIdEncrypted("na");
177 118 : entity.setEntityTypeName("SysCalTable");
178 118 : entity.setEntityVersion("1");
179 118 : entity.setInstanceVersion("1");
180 :
181 : // Archive XML
182 118 : archiveAsBin = true;
183 :
184 : // File XML
185 118 : fileAsBin = true;
186 :
187 : // By default the table is considered as present in memory
188 118 : presentInMemory = true;
189 :
190 : // By default there is no load in progress
191 118 : loadInProgress = false;
192 118 : }
193 :
194 : /**
195 : * A destructor for SysCalTable.
196 : */
197 236 : SysCalTable::~SysCalTable() {
198 46416 : for (unsigned int i = 0; i < privateRows.size(); i++)
199 46298 : delete(privateRows.at(i));
200 236 : }
201 :
202 : /**
203 : * Container to which this table belongs.
204 : */
205 125 : ASDM &SysCalTable::getContainer() const {
206 125 : return container;
207 : }
208 :
209 : /**
210 : * Return the number of rows in the table.
211 : */
212 131 : unsigned int SysCalTable::size() const {
213 131 : if (presentInMemory)
214 98 : return privateRows.size();
215 : else
216 33 : return declaredSize;
217 : }
218 :
219 : /**
220 : * Return the name of this table.
221 : */
222 441 : string SysCalTable::getName() const {
223 441 : return entityNameOfSysCal;
224 : }
225 :
226 : /**
227 : * Return the name of this table.
228 : */
229 0 : string SysCalTable::name() {
230 0 : return entityNameOfSysCal;
231 : }
232 :
233 : /**
234 : * Return the the names of the attributes (or columns) of this table.
235 : */
236 0 : const vector<string>& SysCalTable::getAttributesNames() { return attributesNamesOfSysCal_v; }
237 :
238 : /**
239 : * Return the the names of the attributes (or columns) of this table as they appear by default
240 : * in an binary export of this table.
241 : */
242 0 : const vector<string>& SysCalTable::defaultAttributesNamesInBin() { return attributesNamesInBinOfSysCal_v; }
243 :
244 : /**
245 : * Return this table's Entity.
246 : */
247 79 : Entity SysCalTable::getEntity() const {
248 79 : return entity;
249 : }
250 :
251 : /**
252 : * Set this table's Entity.
253 : */
254 43 : void SysCalTable::setEntity(Entity e) {
255 43 : this->entity = e;
256 43 : }
257 :
258 : //
259 : // ====> Row creation.
260 : //
261 :
262 : /**
263 : * Create a new row.
264 : */
265 11296 : SysCalRow *SysCalTable::newRow() {
266 11296 : return new SysCalRow (*this);
267 : }
268 :
269 :
270 : /**
271 : * Create a new row initialized to the specified values.
272 : * @return a pointer on the created and initialized row.
273 :
274 : * @param antennaId
275 :
276 : * @param spectralWindowId
277 :
278 : * @param timeInterval
279 :
280 : * @param feedId
281 :
282 : * @param numReceptor
283 :
284 : * @param numChan
285 :
286 : */
287 33410 : SysCalRow* SysCalTable::newRow(Tag antennaId, Tag spectralWindowId, ArrayTimeInterval timeInterval, int feedId, int numReceptor, int numChan){
288 33410 : SysCalRow *row = new SysCalRow(*this);
289 :
290 33410 : row->setAntennaId(antennaId);
291 :
292 33410 : row->setSpectralWindowId(spectralWindowId);
293 :
294 33410 : row->setTimeInterval(timeInterval);
295 :
296 33410 : row->setFeedId(feedId);
297 :
298 33410 : row->setNumReceptor(numReceptor);
299 :
300 33410 : row->setNumChan(numChan);
301 :
302 33410 : return row;
303 : }
304 :
305 :
306 :
307 0 : SysCalRow* SysCalTable::newRow(SysCalRow* row) {
308 0 : return new SysCalRow(*this, row);
309 : }
310 :
311 : //
312 : // Append a row to its table.
313 : //
314 :
315 :
316 :
317 :
318 :
319 : /**
320 : * Returns a string built by concatenating the ascii representation of the
321 : * parameters values suffixed with a "_" character.
322 : */
323 44706 : string SysCalTable::Key(Tag antennaId, Tag spectralWindowId, int feedId) {
324 44706 : ostringstream ostrstr;
325 : ostrstr
326 :
327 44706 : << antennaId.toString() << "_"
328 :
329 89412 : << spectralWindowId.toString() << "_"
330 :
331 44706 : << feedId << "_"
332 :
333 : ;
334 89412 : return ostrstr.str();
335 44706 : }
336 :
337 :
338 :
339 33410 : SysCalRow* SysCalTable::add(SysCalRow* x) {
340 33410 : ArrayTime startTime = x->getTimeInterval().getStart();
341 :
342 : /*
343 : * Is there already a context for this combination of not temporal
344 : * attributes ?
345 : */
346 : string k = Key(
347 66820 : x->getAntennaId()
348 : ,
349 0 : x->getSpectralWindowId()
350 : ,
351 : x->getFeedId()
352 66820 : );
353 :
354 33410 : if (context.find(k) == context.end()) {
355 : // There is not yet a context ...
356 : // Create and initialize an entry in the context map for this combination....
357 626 : TIME_ROWS v;
358 626 : context[k] = v;
359 626 : }
360 :
361 66820 : return insertByStartTime(x, context[k]);
362 33410 : }
363 :
364 :
365 :
366 :
367 11296 : void SysCalTable::addWithoutCheckingUnique(SysCalRow * x) {
368 11296 : SysCalRow * dummy = checkAndAdd(x, true); // We require the check for uniqueness to be skipped.
369 : // by passing true in the second parameter
370 : // whose value by default is false.
371 : // this statement is never executed, but it hides the unused return value from the compiler to silence that warning.
372 : if (false) cout << (unsigned long long) dummy;
373 11296 : }
374 :
375 :
376 :
377 :
378 : //
379 : // A private method to append a row to its table, used by input conversion
380 : // methods, with row uniqueness.
381 : //
382 :
383 :
384 :
385 :
386 :
387 :
388 :
389 :
390 :
391 11296 : SysCalRow* SysCalTable::checkAndAdd(SysCalRow* x, bool /* skipCheckUniqueness */ ) {
392 : string keystr = Key(
393 22592 : x->getAntennaId()
394 : ,
395 0 : x->getSpectralWindowId()
396 : ,
397 : x->getFeedId()
398 22592 : );
399 11296 : if (context.find(keystr) == context.end()) {
400 2228 : vector<SysCalRow *> v;
401 2228 : context[keystr] = v;
402 2228 : }
403 :
404 11296 : vector<SysCalRow*>& found = context.find(keystr)->second;
405 22592 : return insertByStartTime(x, found);
406 11296 : }
407 :
408 :
409 :
410 :
411 :
412 :
413 : //
414 : // A private method to brutally append a row to its table, without checking for row uniqueness.
415 : //
416 :
417 1592 : void SysCalTable::append(SysCalRow *x) {
418 1592 : privateRows.push_back(x);
419 1592 : x->isAdded(true);
420 1592 : }
421 :
422 :
423 :
424 :
425 :
426 0 : vector<SysCalRow *> SysCalTable::get() {
427 0 : checkPresenceInMemory();
428 0 : return privateRows;
429 : }
430 :
431 78 : const vector<SysCalRow *>& SysCalTable::get() const {
432 78 : const_cast<SysCalTable&>(*this).checkPresenceInMemory();
433 78 : return privateRows;
434 : }
435 :
436 :
437 :
438 :
439 :
440 :
441 :
442 :
443 :
444 :
445 0 : vector<SysCalRow *> *SysCalTable::getByContext(Tag antennaId, Tag spectralWindowId, int feedId) {
446 : //if (getContainer().checkRowUniqueness() == false)
447 : //throw IllegalAccessException ("The method 'getByContext' can't be called because the dataset has been built without checking the row uniqueness.", "SysCalTable");
448 :
449 0 : checkPresenceInMemory();
450 0 : string k = Key(antennaId, spectralWindowId, feedId);
451 :
452 0 : if (context.find(k) == context.end()) return 0;
453 0 : else return &(context[k]);
454 0 : }
455 :
456 :
457 :
458 :
459 :
460 :
461 :
462 :
463 :
464 :
465 : /*
466 : ** Returns a SysCalRow* given a key.
467 : ** @return a pointer to the row having the key whose values are passed as parameters, or 0 if
468 : ** no row exists for that key.
469 : **
470 : */
471 :
472 :
473 0 : SysCalRow* SysCalTable::getRowByKey(Tag antennaId, Tag spectralWindowId, ArrayTimeInterval timeInterval, int feedId) {
474 0 : checkPresenceInMemory();
475 0 : string keystr = Key(antennaId, spectralWindowId, feedId);
476 0 : vector<SysCalRow *> row;
477 :
478 0 : if ( context.find(keystr) == context.end()) return 0;
479 :
480 0 : row = context[keystr];
481 :
482 : // Is the vector empty...impossible in principle !
483 0 : if (row.size() == 0) return 0;
484 :
485 : // Only one element in the vector
486 0 : if (row.size() == 1) {
487 0 : SysCalRow* r = row.at(0);
488 0 : if ( r->getTimeInterval().contains(timeInterval.getStart()))
489 0 : return r;
490 : else
491 0 : return 0;
492 : }
493 :
494 : // Optimizations
495 0 : SysCalRow* last = row.at(row.size()-1);
496 0 : if (timeInterval.getStart().get() >= (last->getTimeInterval().getStart().get()+last->getTimeInterval().getDuration().get())) return 0;
497 :
498 0 : SysCalRow* first = row.at(0);
499 0 : if (timeInterval.getStart().get() < first->getTimeInterval().getStart().get()) return 0;
500 :
501 :
502 : // More than one row
503 : // Let's use a dichotomy method for the general case..
504 0 : int k0 = 0;
505 0 : int k1 = row.size() - 1;
506 0 : SysCalRow* r = 0;
507 0 : while (k0!=k1) {
508 :
509 : // Is the start time contained in the time interval of row #k0
510 0 : r = row.at(k0);
511 0 : if (r->getTimeInterval().contains(timeInterval.getStart())) return r;
512 :
513 : // Is the start contained in the time interval of row #k1
514 0 : r = row.at(k1);
515 0 : if (r->getTimeInterval().contains(timeInterval.getStart())) return r;
516 :
517 : // Are the rows #k0 and #k1 consecutive
518 : // Then we know for sure that there is no row containing the start of timeInterval
519 0 : if (k1==(k0+1)) return 0;
520 :
521 : // Proceed to the next step of dichotomy.
522 0 : r = row.at((k0+k1)/2);
523 0 : if ( timeInterval.getStart().get() <= r->getTimeInterval().getStart().get())
524 0 : k1 = (k0 + k1) / 2;
525 : else
526 0 : k0 = (k0 + k1) / 2;
527 : }
528 0 : return 0;
529 0 : }
530 :
531 :
532 :
533 :
534 :
535 :
536 :
537 :
538 :
539 : #ifndef WITHOUT_ACS
540 : using asdmIDL::SysCalTableIDL;
541 : #endif
542 :
543 : #ifndef WITHOUT_ACS
544 : // Conversion Methods
545 :
546 : SysCalTableIDL *SysCalTable::toIDL() {
547 : SysCalTableIDL *x = new SysCalTableIDL ();
548 : unsigned int nrow = size();
549 : x->row.length(nrow);
550 : vector<SysCalRow*> v = get();
551 : for (unsigned int i = 0; i < nrow; ++i) {
552 : //x->row[i] = *(v[i]->toIDL());
553 : v[i]->toIDL(x->row[i]);
554 : }
555 : return x;
556 : }
557 :
558 : void SysCalTable::toIDL(asdmIDL::SysCalTableIDL& x) const {
559 : unsigned int nrow = size();
560 : x.row.length(nrow);
561 : vector<SysCalRow*> v = get();
562 : for (unsigned int i = 0; i < nrow; ++i) {
563 : v[i]->toIDL(x.row[i]);
564 : }
565 : }
566 : #endif
567 :
568 : #ifndef WITHOUT_ACS
569 : void SysCalTable::fromIDL(SysCalTableIDL x) {
570 : unsigned int nrow = x.row.length();
571 : for (unsigned int i = 0; i < nrow; ++i) {
572 : SysCalRow *tmp = newRow();
573 : tmp->setFromIDL(x.row[i]);
574 : // checkAndAdd(tmp);
575 : add(tmp);
576 : }
577 : }
578 : #endif
579 :
580 :
581 0 : string SysCalTable::toXML() {
582 0 : string buf;
583 :
584 0 : buf.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> ");
585 0 : buf.append("<SysCalTable xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:syscal=\"http://Alma/XASDM/SysCalTable\" xsi:schemaLocation=\"http://Alma/XASDM/SysCalTable http://almaobservatory.org/XML/XASDM/4/SysCalTable.xsd\" schemaVersion=\"4\" schemaRevision=\"-1\">\n");
586 :
587 0 : buf.append(entity.toXML());
588 0 : string s = container.getEntity().toXML();
589 : // Change the "Entity" tag to "ContainerEntity".
590 0 : buf.append("<Container" + s.substr(1,s.length() - 1)+" ");
591 0 : vector<SysCalRow*> v = get();
592 0 : for (unsigned int i = 0; i < v.size(); ++i) {
593 : try {
594 0 : buf.append(v[i]->toXML());
595 0 : } catch (const NoSuchRow &e) {
596 0 : }
597 0 : buf.append(" ");
598 : }
599 0 : buf.append("</SysCalTable> ");
600 0 : return buf;
601 0 : }
602 :
603 :
604 0 : string SysCalTable::getVersion() const {
605 0 : return version;
606 : }
607 :
608 :
609 29 : void SysCalTable::fromXML(string& tableInXML) {
610 : //
611 : // Look for a version information in the schemaVersion of the XML
612 : //
613 : xmlDoc *doc;
614 : #if LIBXML_VERSION >= 20703
615 29 : doc = xmlReadMemory(tableInXML.data(), tableInXML.size(), "XMLTableHeader.xml", NULL, XML_PARSE_NOBLANKS|XML_PARSE_HUGE);
616 : #else
617 : doc = xmlReadMemory(tableInXML.data(), tableInXML.size(), "XMLTableHeader.xml", NULL, XML_PARSE_NOBLANKS);
618 : #endif
619 29 : if ( doc == NULL )
620 0 : throw ConversionException("Failed to parse the xmlHeader into a DOM structure.", "SysCal");
621 :
622 29 : xmlNode* root_element = xmlDocGetRootElement(doc);
623 29 : if ( root_element == NULL || root_element->type != XML_ELEMENT_NODE )
624 0 : throw ConversionException("Failed to retrieve the root element in the DOM structure.", "SysCal");
625 :
626 29 : xmlChar * propValue = xmlGetProp(root_element, (const xmlChar *) "schemaVersion");
627 29 : if ( propValue != 0 ) {
628 29 : version = string( (const char*) propValue);
629 29 : xmlFree(propValue);
630 : }
631 :
632 29 : Parser xml(tableInXML);
633 29 : if (!xml.isStr("<SysCalTable"))
634 0 : error();
635 : // cout << "Parsing a SysCalTable" << endl;
636 58 : string s = xml.getElement("<Entity","/>");
637 29 : if (s.length() == 0)
638 0 : error();
639 29 : Entity e;
640 29 : e.setFromXML(s);
641 29 : if (e.getEntityTypeName() != "SysCalTable")
642 0 : error();
643 29 : setEntity(e);
644 : // Skip the container's entity; but, it has to be there.
645 29 : s = xml.getElement("<ContainerEntity","/>");
646 29 : if (s.length() == 0)
647 0 : error();
648 :
649 : // Get each row in the table.
650 29 : s = xml.getElementContent("<row>","</row>");
651 : SysCalRow *row;
652 29 : if (getContainer().checkRowUniqueness()) {
653 : try {
654 0 : while (s.length() != 0) {
655 0 : row = newRow();
656 0 : row->setFromXML(s);
657 0 : checkAndAdd(row);
658 0 : s = xml.getElementContent("<row>","</row>");
659 : }
660 :
661 : }
662 0 : catch (const DuplicateKey &e1) {
663 0 : throw ConversionException(e1.getMessage(),"SysCalTable");
664 0 : }
665 0 : catch (const UniquenessViolationException &e1) {
666 0 : throw ConversionException(e1.getMessage(),"SysCalTable");
667 0 : }
668 0 : catch (...) {
669 : // cout << "Unexpected error in SysCalTable::checkAndAdd called from SysCalTable::fromXML " << endl;
670 0 : }
671 : }
672 : else {
673 : try {
674 11325 : while (s.length() != 0) {
675 11296 : row = newRow();
676 11296 : row->setFromXML(s);
677 11296 : addWithoutCheckingUnique(row);
678 11296 : s = xml.getElementContent("<row>","</row>");
679 : }
680 : }
681 0 : catch (const DuplicateKey &e1) {
682 0 : throw ConversionException(e1.getMessage(),"SysCalTable");
683 0 : }
684 0 : catch (...) {
685 : // cout << "Unexpected error in SysCalTable::addWithoutCheckingUnique called from SysCalTable::fromXML " << endl;
686 0 : }
687 : }
688 :
689 :
690 29 : if (!xml.isStr("</SysCalTable>"))
691 0 : error();
692 :
693 : //Does not change the convention defined in the model.
694 : //archiveAsBin = false;
695 : //fileAsBin = false;
696 :
697 : // clean up the xmlDoc pointer
698 29 : if ( doc != NULL ) xmlFreeDoc(doc);
699 :
700 29 : }
701 :
702 :
703 0 : void SysCalTable::error() {
704 0 : throw ConversionException("Invalid xml document","SysCal");
705 : }
706 :
707 :
708 26 : string SysCalTable::MIMEXMLPart(const asdm::ByteOrder* byteOrder) {
709 52 : string UID = getEntity().getEntityId().toString();
710 26 : string withoutUID = UID.substr(6);
711 52 : string containerUID = getContainer().getEntity().getEntityId().toString();
712 26 : ostringstream oss;
713 26 : oss << "<?xml version='1.0' encoding='ISO-8859-1'?>";
714 26 : oss << "\n";
715 26 : oss << "<SysCalTable xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:syscal=\"http://Alma/XASDM/SysCalTable\" xsi:schemaLocation=\"http://Alma/XASDM/SysCalTable http://almaobservatory.org/XML/XASDM/4/SysCalTable.xsd\" schemaVersion=\"4\" schemaRevision=\"-1\">\n";
716 26 : oss<< "<Entity entityId='"<<UID<<"' entityIdEncrypted='na' entityTypeName='SysCalTable' schemaVersion='1' documentVersion='1'/>\n";
717 26 : oss<< "<ContainerEntity entityId='"<<containerUID<<"' entityIdEncrypted='na' entityTypeName='ASDM' schemaVersion='1' documentVersion='1'/>\n";
718 26 : oss << "<BulkStoreRef file_id='"<<withoutUID<<"' byteOrder='"<<byteOrder->toString()<<"' />\n";
719 26 : oss << "<Attributes>\n";
720 :
721 26 : oss << "<antennaId/>\n";
722 26 : oss << "<spectralWindowId/>\n";
723 26 : oss << "<timeInterval/>\n";
724 26 : oss << "<feedId/>\n";
725 26 : oss << "<numReceptor/>\n";
726 26 : oss << "<numChan/>\n";
727 :
728 26 : oss << "<tcalFlag/>\n";
729 26 : oss << "<tcalSpectrum/>\n";
730 26 : oss << "<trxFlag/>\n";
731 26 : oss << "<trxSpectrum/>\n";
732 26 : oss << "<tskyFlag/>\n";
733 26 : oss << "<tskySpectrum/>\n";
734 26 : oss << "<tsysFlag/>\n";
735 26 : oss << "<tsysSpectrum/>\n";
736 26 : oss << "<tantFlag/>\n";
737 26 : oss << "<tantSpectrum/>\n";
738 26 : oss << "<tantTsysFlag/>\n";
739 26 : oss << "<tantTsysSpectrum/>\n";
740 26 : oss << "<phaseDiffFlag/>\n";
741 26 : oss << "<phaseDiffSpectrum/>\n";
742 26 : oss << "</Attributes>\n";
743 26 : oss << "</SysCalTable>\n";
744 :
745 52 : return oss.str();
746 26 : }
747 :
748 13 : string SysCalTable::toMIME(const asdm::ByteOrder* byteOrder) {
749 13 : EndianOSStream eoss(byteOrder);
750 :
751 26 : string UID = getEntity().getEntityId().toString();
752 :
753 : // The MIME Header
754 13 : eoss <<"MIME-Version: 1.0";
755 13 : eoss << "\n";
756 13 : eoss << "Content-Type: Multipart/Related; boundary='MIME_boundary'; type='text/xml'; start= '<header.xml>'";
757 13 : eoss <<"\n";
758 13 : eoss <<"Content-Description: Correlator";
759 13 : eoss <<"\n";
760 13 : eoss <<"alma-uid:" << UID;
761 13 : eoss <<"\n";
762 13 : eoss <<"\n";
763 :
764 : // The MIME XML part header.
765 13 : eoss <<"--MIME_boundary";
766 13 : eoss <<"\n";
767 13 : eoss <<"Content-Type: text/xml; charset='ISO-8859-1'";
768 13 : eoss <<"\n";
769 13 : eoss <<"Content-Transfer-Encoding: 8bit";
770 13 : eoss <<"\n";
771 13 : eoss <<"Content-ID: <header.xml>";
772 13 : eoss <<"\n";
773 13 : eoss <<"\n";
774 :
775 : // The MIME XML part content.
776 13 : eoss << MIMEXMLPart(byteOrder);
777 :
778 : // The MIME binary part header
779 13 : eoss <<"--MIME_boundary";
780 13 : eoss <<"\n";
781 13 : eoss <<"Content-Type: binary/octet-stream";
782 13 : eoss <<"\n";
783 13 : eoss <<"Content-ID: <content.bin>";
784 13 : eoss <<"\n";
785 13 : eoss <<"\n";
786 :
787 : // The MIME binary content
788 13 : entity.toBin(eoss);
789 13 : container.getEntity().toBin(eoss);
790 13 : eoss.writeInt((int) privateRows.size());
791 28383 : for (unsigned int i = 0; i < privateRows.size(); i++) {
792 28370 : privateRows.at(i)->toBin(eoss);
793 : }
794 :
795 : // The closing MIME boundary
796 13 : eoss << "\n--MIME_boundary--";
797 13 : eoss << "\n";
798 :
799 26 : return eoss.str();
800 13 : }
801 :
802 :
803 4 : void SysCalTable::setFromMIME(const string & mimeMsg) {
804 4 : string xmlPartMIMEHeader = "Content-ID: <header.xml>\n\n";
805 :
806 4 : string binPartMIMEHeader = "--MIME_boundary\nContent-Type: binary/octet-stream\nContent-ID: <content.bin>\n\n";
807 :
808 : // Detect the XML header.
809 4 : string::size_type loc0 = mimeMsg.find(xmlPartMIMEHeader, 0);
810 4 : if ( loc0 == string::npos) {
811 : // let's try with CRLFs
812 0 : xmlPartMIMEHeader = "Content-ID: <header.xml>\r\n\r\n";
813 0 : loc0 = mimeMsg.find(xmlPartMIMEHeader, 0);
814 0 : if ( loc0 == string::npos )
815 0 : throw ConversionException("Failed to detect the beginning of the XML header", "SysCal");
816 : }
817 :
818 4 : loc0 += xmlPartMIMEHeader.size();
819 :
820 : // Look for the string announcing the binary part.
821 4 : string::size_type loc1 = mimeMsg.find( binPartMIMEHeader, loc0 );
822 :
823 4 : if ( loc1 == string::npos ) {
824 0 : throw ConversionException("Failed to detect the beginning of the binary part", "SysCal");
825 : }
826 :
827 : //
828 : // Extract the xmlHeader and analyze it to find out what is the byte order and the sequence
829 : // of attribute names.
830 : //
831 4 : string xmlHeader = mimeMsg.substr(loc0, loc1-loc0);
832 : xmlDoc *doc;
833 4 : doc = xmlReadMemory(xmlHeader.data(), xmlHeader.size(), "BinaryTableHeader.xml", NULL, XML_PARSE_NOBLANKS);
834 4 : if ( doc == NULL )
835 0 : throw ConversionException("Failed to parse the xmlHeader into a DOM structure.", "SysCal");
836 :
837 : // This vector will be filled by the names of all the attributes of the table
838 : // in the order in which they are expected to be found in the binary representation.
839 : //
840 4 : vector<string> attributesSeq;
841 :
842 4 : xmlNode* root_element = xmlDocGetRootElement(doc);
843 4 : if ( root_element == NULL || root_element->type != XML_ELEMENT_NODE )
844 0 : throw ConversionException("Failed to parse the xmlHeader into a DOM structure.", "SysCal");
845 :
846 4 : const ByteOrder* byteOrder=0;
847 4 : if ( string("ASDMBinaryTable").compare((const char*) root_element->name) == 0) {
848 : // Then it's an "old fashioned" MIME file for tables.
849 : // Just try to deserialize it with Big_Endian for the bytes ordering.
850 0 : byteOrder = asdm::ByteOrder::Big_Endian;
851 :
852 : //
853 : // Let's consider a default order for the sequence of attributes.
854 : //
855 :
856 :
857 0 : attributesSeq.push_back("antennaId") ;
858 :
859 0 : attributesSeq.push_back("spectralWindowId") ;
860 :
861 0 : attributesSeq.push_back("timeInterval") ;
862 :
863 0 : attributesSeq.push_back("feedId") ;
864 :
865 0 : attributesSeq.push_back("numReceptor") ;
866 :
867 0 : attributesSeq.push_back("numChan") ;
868 :
869 :
870 0 : attributesSeq.push_back("tcalFlag") ;
871 :
872 0 : attributesSeq.push_back("tcalSpectrum") ;
873 :
874 0 : attributesSeq.push_back("trxFlag") ;
875 :
876 0 : attributesSeq.push_back("trxSpectrum") ;
877 :
878 0 : attributesSeq.push_back("tskyFlag") ;
879 :
880 0 : attributesSeq.push_back("tskySpectrum") ;
881 :
882 0 : attributesSeq.push_back("tsysFlag") ;
883 :
884 0 : attributesSeq.push_back("tsysSpectrum") ;
885 :
886 0 : attributesSeq.push_back("tantFlag") ;
887 :
888 0 : attributesSeq.push_back("tantSpectrum") ;
889 :
890 0 : attributesSeq.push_back("tantTsysFlag") ;
891 :
892 0 : attributesSeq.push_back("tantTsysSpectrum") ;
893 :
894 0 : attributesSeq.push_back("phaseDiffFlag") ;
895 :
896 0 : attributesSeq.push_back("phaseDiffSpectrum") ;
897 :
898 :
899 :
900 :
901 : // And decide that it has version == "2"
902 0 : version = "2";
903 : }
904 4 : else if (string("SysCalTable").compare((const char*) root_element->name) == 0) {
905 : // It's a new (and correct) MIME file for tables.
906 : //
907 : // 1st ) Look for a BulkStoreRef element with an attribute byteOrder.
908 : //
909 4 : xmlNode* bulkStoreRef = 0;
910 4 : xmlNode* child = root_element->children;
911 :
912 4 : if (xmlHasProp(root_element, (const xmlChar*) "schemaVersion")) {
913 4 : xmlChar * value = xmlGetProp(root_element, (const xmlChar *) "schemaVersion");
914 4 : version = string ((const char *) value);
915 4 : xmlFree(value);
916 : }
917 :
918 : // Skip the two first children (Entity and ContainerEntity).
919 4 : bulkStoreRef = (child == 0) ? 0 : ( (child->next) == 0 ? 0 : child->next->next );
920 :
921 4 : if ( bulkStoreRef == 0 || (bulkStoreRef->type != XML_ELEMENT_NODE) || (string("BulkStoreRef").compare((const char*) bulkStoreRef->name) != 0))
922 0 : throw ConversionException ("Could not find the element '/SysCalTable/BulkStoreRef'. Invalid XML header '"+ xmlHeader + "'.", "SysCal");
923 :
924 : // We found BulkStoreRef, now look for its attribute byteOrder.
925 4 : _xmlAttr* byteOrderAttr = 0;
926 8 : for (struct _xmlAttr* attr = bulkStoreRef->properties; attr; attr = attr->next)
927 8 : if (string("byteOrder").compare((const char*) attr->name) == 0) {
928 4 : byteOrderAttr = attr;
929 4 : break;
930 : }
931 :
932 4 : if (byteOrderAttr == 0)
933 0 : throw ConversionException("Could not find the element '/SysCalTable/BulkStoreRef/@byteOrder'. Invalid XML header '" + xmlHeader +"'.", "SysCal");
934 :
935 4 : string byteOrderValue = string((const char*) byteOrderAttr->children->content);
936 4 : if (!(byteOrder = asdm::ByteOrder::fromString(byteOrderValue)))
937 0 : throw ConversionException("No valid value retrieved for the element '/SysCalTable/BulkStoreRef/@byteOrder'. Invalid XML header '" + xmlHeader + "'.", "SysCal");
938 :
939 : //
940 : // 2nd) Look for the Attributes element and grab the names of the elements it contains.
941 : //
942 4 : xmlNode* attributes = bulkStoreRef->next;
943 4 : if ( attributes == 0 || (attributes->type != XML_ELEMENT_NODE) || (string("Attributes").compare((const char*) attributes->name) != 0))
944 0 : throw ConversionException ("Could not find the element '/SysCalTable/Attributes'. Invalid XML header '"+ xmlHeader + "'.", "SysCal");
945 :
946 4 : xmlNode* childOfAttributes = attributes->children;
947 :
948 84 : while ( childOfAttributes != 0 && (childOfAttributes->type == XML_ELEMENT_NODE) ) {
949 80 : attributesSeq.push_back(string((const char*) childOfAttributes->name));
950 80 : childOfAttributes = childOfAttributes->next;
951 : }
952 4 : }
953 : // Create an EndianISStream from the substring containing the binary part.
954 4 : EndianISStream eiss(mimeMsg.substr(loc1+binPartMIMEHeader.size()), byteOrder);
955 :
956 4 : entity = Entity::fromBin((EndianIStream&) eiss);
957 :
958 : // We do nothing with that but we have to read it.
959 4 : Entity containerEntity = Entity::fromBin((EndianIStream&) eiss);
960 :
961 : // Let's read numRows but ignore it and rely on the value specified in the ASDM.xml file.
962 4 : int numRows = ((EndianIStream&) eiss).readInt();
963 4 : if ((numRows != -1) // Then these are *not* data produced at the EVLA.
964 4 : && ((unsigned int) numRows != this->declaredSize )) { // Then the declared size (in ASDM.xml) is not equal to the one
965 : // written into the binary representation of the table.
966 0 : cout << "The a number of rows ('"
967 : << numRows
968 0 : << "') declared in the binary representation of the table is different from the one declared in ASDM.xml ('"
969 0 : << this->declaredSize
970 0 : << "'). I'll proceed with the value declared in ASDM.xml"
971 0 : << endl;
972 : }
973 :
974 4 : if (getContainer().checkRowUniqueness()) {
975 : try {
976 0 : for (uint32_t i = 0; i < this->declaredSize; i++) {
977 0 : SysCalRow* aRow = SysCalRow::fromBin((EndianIStream&) eiss, *this, attributesSeq);
978 0 : checkAndAdd(aRow);
979 : }
980 : }
981 0 : catch (const DuplicateKey &e) {
982 0 : throw ConversionException("Error while writing binary data , the message was "
983 0 : + e.getMessage(), "SysCal");
984 0 : }
985 0 : catch (const TagFormatException &e) {
986 0 : throw ConversionException("Error while reading binary data , the message was "
987 0 : + e.getMessage(), "SysCal");
988 0 : }
989 : }
990 : else {
991 1596 : for (uint32_t i = 0; i < this->declaredSize; i++) {
992 1592 : SysCalRow* aRow = SysCalRow::fromBin((EndianIStream&) eiss, *this, attributesSeq);
993 1592 : append(aRow);
994 : }
995 : }
996 : //Does not change the convention defined in the model.
997 : //archiveAsBin = true;
998 : //fileAsBin = true;
999 4 : if ( doc != NULL ) xmlFreeDoc(doc);
1000 :
1001 4 : }
1002 :
1003 0 : void SysCalTable::setUnknownAttributeBinaryReader(const string& attributeName, BinaryAttributeReaderFunctor* barFctr) {
1004 : //
1005 : // Is this attribute really unknown ?
1006 : //
1007 0 : for (vector<string>::const_iterator iter = attributesNamesOfSysCal_v.begin(); iter != attributesNamesOfSysCal_v.end(); iter++) {
1008 0 : if ((*iter).compare(attributeName) == 0)
1009 0 : throw ConversionException("the attribute '"+attributeName+"' is known you can't override the way it's read in the MIME binary file containing the table.", "SysCal");
1010 : }
1011 :
1012 : // Ok then register the functor to activate when an unknown attribute is met during the reading of a binary table?
1013 0 : unknownAttributes2Functors[attributeName] = barFctr;
1014 0 : }
1015 :
1016 0 : BinaryAttributeReaderFunctor* SysCalTable::getUnknownAttributeBinaryReader(const string& attributeName) const {
1017 0 : map<string, BinaryAttributeReaderFunctor*>::const_iterator iter = unknownAttributes2Functors.find(attributeName);
1018 0 : return (iter == unknownAttributes2Functors.end()) ? 0 : iter->second;
1019 : }
1020 :
1021 :
1022 13 : void SysCalTable::toFile(string directory) {
1023 13 : if (!directoryExists(directory.c_str()) &&
1024 0 : !createPath(directory.c_str())) {
1025 0 : throw ConversionException("Could not create directory " , directory);
1026 : }
1027 :
1028 13 : string fileName = directory + "/SysCal.xml";
1029 13 : ofstream tableout(fileName.c_str(),ios::out|ios::trunc);
1030 13 : if (tableout.rdstate() == ostream::failbit)
1031 0 : throw ConversionException("Could not open file " + fileName + " to write ", "SysCal");
1032 13 : if (fileAsBin)
1033 13 : tableout << MIMEXMLPart();
1034 : else
1035 0 : tableout << toXML() << endl;
1036 13 : tableout.close();
1037 13 : if (tableout.rdstate() == ostream::failbit)
1038 0 : throw ConversionException("Could not close file " + fileName, "SysCal");
1039 :
1040 13 : if (fileAsBin) {
1041 : // write the bin serialized
1042 13 : string fileName = directory + "/SysCal.bin";
1043 13 : ofstream tableout(fileName.c_str(),ios::out|ios::trunc);
1044 13 : if (tableout.rdstate() == ostream::failbit)
1045 0 : throw ConversionException("Could not open file " + fileName + " to write ", "SysCal");
1046 13 : tableout << toMIME() << endl;
1047 13 : tableout.close();
1048 13 : if (tableout.rdstate() == ostream::failbit)
1049 0 : throw ConversionException("Could not close file " + fileName, "SysCal");
1050 13 : }
1051 13 : }
1052 :
1053 :
1054 33 : void SysCalTable::setFromFile(const string& directory) {
1055 : #ifndef WITHOUT_BOOST
1056 : if (boost::filesystem::exists(boost::filesystem::path(uniqSlashes(directory + "/SysCal.xml"))))
1057 : setFromXMLFile(directory);
1058 : else if (boost::filesystem::exists(boost::filesystem::path(uniqSlashes(directory + "/SysCal.bin"))))
1059 : setFromMIMEFile(directory);
1060 : #else
1061 : // alternative in Misc.h
1062 33 : if (file_exists(uniqSlashes(directory + "/SysCal.xml")))
1063 33 : setFromXMLFile(directory);
1064 0 : else if (file_exists(uniqSlashes(directory + "/SysCal.bin")))
1065 0 : setFromMIMEFile(directory);
1066 : #endif
1067 : else
1068 0 : throw ConversionException("No file found for the SysCal table", "SysCal");
1069 33 : }
1070 :
1071 :
1072 4 : void SysCalTable::setFromMIMEFile(const string& directory) {
1073 4 : string tablePath ;
1074 :
1075 4 : tablePath = directory + "/SysCal.bin";
1076 4 : ifstream tablefile(tablePath.c_str(), ios::in|ios::binary);
1077 4 : if (!tablefile.is_open()) {
1078 0 : throw ConversionException("Could not open file " + tablePath, "SysCal");
1079 : }
1080 : // Read in a stringstream.
1081 4 : stringstream ss; ss << tablefile.rdbuf();
1082 :
1083 4 : if (tablefile.rdstate() == istream::failbit || tablefile.rdstate() == istream::badbit) {
1084 0 : throw ConversionException("Error reading file " + tablePath,"SysCal");
1085 : }
1086 :
1087 : // And close.
1088 4 : tablefile.close();
1089 4 : if (tablefile.rdstate() == istream::failbit)
1090 0 : throw ConversionException("Could not close file " + tablePath,"SysCal");
1091 :
1092 4 : setFromMIME(ss.str());
1093 4 : }
1094 : /*
1095 : void SysCalTable::openMIMEFile (const string& directory) {
1096 :
1097 : // Open the file.
1098 : string tablePath ;
1099 : tablePath = directory + "/SysCal.bin";
1100 : ifstream tablefile(tablePath.c_str(), ios::in|ios::binary);
1101 : if (!tablefile.is_open())
1102 : throw ConversionException("Could not open file " + tablePath, "SysCal");
1103 :
1104 : // Locate the xmlPartMIMEHeader.
1105 : string xmlPartMIMEHeader = "CONTENT-ID: <HEADER.XML>\n\n";
1106 : CharComparator comparator;
1107 : istreambuf_iterator<char> BEGIN(tablefile.rdbuf());
1108 : istreambuf_iterator<char> END;
1109 : istreambuf_iterator<char> it = search(BEGIN, END, xmlPartMIMEHeader.begin(), xmlPartMIMEHeader.end(), comparator);
1110 : if (it == END)
1111 : throw ConversionException("failed to detect the beginning of the XML header", "SysCal");
1112 :
1113 : // Locate the binaryPartMIMEHeader while accumulating the characters of the xml header.
1114 : string binPartMIMEHeader = "--MIME_BOUNDARY\nCONTENT-TYPE: BINARY/OCTET-STREAM\nCONTENT-ID: <CONTENT.BIN>\n\n";
1115 : string xmlHeader;
1116 : CharCompAccumulator compaccumulator(&xmlHeader, 100000);
1117 : ++it;
1118 : it = search(it, END, binPartMIMEHeader.begin(), binPartMIMEHeader.end(), compaccumulator);
1119 : if (it == END)
1120 : throw ConversionException("failed to detect the beginning of the binary part", "SysCal");
1121 :
1122 : cout << xmlHeader << endl;
1123 : //
1124 : // We have the xmlHeader , let's parse it.
1125 : //
1126 : xmlDoc *doc;
1127 : doc = xmlReadMemory(xmlHeader.data(), xmlHeader.size(), "BinaryTableHeader.xml", NULL, XML_PARSE_NOBLANKS);
1128 : if ( doc == NULL )
1129 : throw ConversionException("Failed to parse the xmlHeader into a DOM structure.", "SysCal");
1130 :
1131 : // This vector will be filled by the names of all the attributes of the table
1132 : // in the order in which they are expected to be found in the binary representation.
1133 : //
1134 : vector<string> attributesSeq(attributesNamesInBinOfSysCal_v);
1135 :
1136 : xmlNode* root_element = xmlDocGetRootElement(doc);
1137 : if ( root_element == NULL || root_element->type != XML_ELEMENT_NODE )
1138 : throw ConversionException("Failed to parse the xmlHeader into a DOM structure.", "SysCal");
1139 :
1140 : const ByteOrder* byteOrder=0;
1141 : if ( string("ASDMBinaryTable").compare((const char*) root_element->name) == 0) {
1142 : // Then it's an "old fashioned" MIME file for tables.
1143 : // Just try to deserialize it with Big_Endian for the bytes ordering.
1144 : byteOrder = asdm::ByteOrder::Big_Endian;
1145 :
1146 : // And decide that it has version == "2"
1147 : version = "2";
1148 : }
1149 : else if (string("SysCalTable").compare((const char*) root_element->name) == 0) {
1150 : // It's a new (and correct) MIME file for tables.
1151 : //
1152 : // 1st ) Look for a BulkStoreRef element with an attribute byteOrder.
1153 : //
1154 : xmlNode* bulkStoreRef = 0;
1155 : xmlNode* child = root_element->children;
1156 :
1157 : if (xmlHasProp(root_element, (const xmlChar*) "schemaVersion")) {
1158 : xmlChar * value = xmlGetProp(root_element, (const xmlChar *) "schemaVersion");
1159 : version = string ((const char *) value);
1160 : xmlFree(value);
1161 : }
1162 :
1163 : // Skip the two first children (Entity and ContainerEntity).
1164 : bulkStoreRef = (child == 0) ? 0 : ( (child->next) == 0 ? 0 : child->next->next );
1165 :
1166 : if ( bulkStoreRef == 0 || (bulkStoreRef->type != XML_ELEMENT_NODE) || (string("BulkStoreRef").compare((const char*) bulkStoreRef->name) != 0))
1167 : throw ConversionException ("Could not find the element '/SysCalTable/BulkStoreRef'. Invalid XML header '"+ xmlHeader + "'.", "SysCal");
1168 :
1169 : // We found BulkStoreRef, now look for its attribute byteOrder.
1170 : _xmlAttr* byteOrderAttr = 0;
1171 : for (struct _xmlAttr* attr = bulkStoreRef->properties; attr; attr = attr->next)
1172 : if (string("byteOrder").compare((const char*) attr->name) == 0) {
1173 : byteOrderAttr = attr;
1174 : break;
1175 : }
1176 :
1177 : if (byteOrderAttr == 0)
1178 : throw ConversionException("Could not find the element '/SysCalTable/BulkStoreRef/@byteOrder'. Invalid XML header '" + xmlHeader +"'.", "SysCal");
1179 :
1180 : string byteOrderValue = string((const char*) byteOrderAttr->children->content);
1181 : if (!(byteOrder = asdm::ByteOrder::fromString(byteOrderValue)))
1182 : throw ConversionException("No valid value retrieved for the element '/SysCalTable/BulkStoreRef/@byteOrder'. Invalid XML header '" + xmlHeader + "'.", "SysCal");
1183 :
1184 : //
1185 : // 2nd) Look for the Attributes element and grab the names of the elements it contains.
1186 : //
1187 : xmlNode* attributes = bulkStoreRef->next;
1188 : if ( attributes == 0 || (attributes->type != XML_ELEMENT_NODE) || (string("Attributes").compare((const char*) attributes->name) != 0))
1189 : throw ConversionException ("Could not find the element '/SysCalTable/Attributes'. Invalid XML header '"+ xmlHeader + "'.", "SysCal");
1190 :
1191 : xmlNode* childOfAttributes = attributes->children;
1192 :
1193 : while ( childOfAttributes != 0 && (childOfAttributes->type == XML_ELEMENT_NODE) ) {
1194 : attributesSeq.push_back(string((const char*) childOfAttributes->name));
1195 : childOfAttributes = childOfAttributes->next;
1196 : }
1197 : }
1198 : // Create an EndianISStream from the substring containing the binary part.
1199 : EndianIFStream eifs(&tablefile, byteOrder);
1200 :
1201 : entity = Entity::fromBin((EndianIStream &) eifs);
1202 :
1203 : // We do nothing with that but we have to read it.
1204 : Entity containerEntity = Entity::fromBin((EndianIStream &) eifs);
1205 :
1206 : // Let's read numRows but ignore it and rely on the value specified in the ASDM.xml file.
1207 : int numRows = eifs.readInt();
1208 : if ((numRows != -1) // Then these are *not* data produced at the EVLA.
1209 : && ((unsigned int) numRows != this->declaredSize )) { // Then the declared size (in ASDM.xml) is not equal to the one
1210 : // written into the binary representation of the table.
1211 : cout << "The a number of rows ('"
1212 : << numRows
1213 : << "') declared in the binary representation of the table is different from the one declared in ASDM.xml ('"
1214 : << this->declaredSize
1215 : << "'). I'll proceed with the value declared in ASDM.xml"
1216 : << endl;
1217 : }
1218 : // clean up xmlDoc pointer
1219 : if ( doc != NULL ) xmlFreeDoc(doc);
1220 : }
1221 : */
1222 :
1223 :
1224 33 : void SysCalTable::setFromXMLFile(const string& directory) {
1225 33 : string tablePath ;
1226 :
1227 33 : tablePath = directory + "/SysCal.xml";
1228 :
1229 : /*
1230 : ifstream tablefile(tablePath.c_str(), ios::in|ios::binary);
1231 : if (!tablefile.is_open()) {
1232 : throw ConversionException("Could not open file " + tablePath, "SysCal");
1233 : }
1234 : // Read in a stringstream.
1235 : stringstream ss;
1236 : ss << tablefile.rdbuf();
1237 :
1238 : if (tablefile.rdstate() == istream::failbit || tablefile.rdstate() == istream::badbit) {
1239 : throw ConversionException("Error reading file '" + tablePath + "'", "SysCal");
1240 : }
1241 :
1242 : // And close
1243 : tablefile.close();
1244 : if (tablefile.rdstate() == istream::failbit)
1245 : throw ConversionException("Could not close file '" + tablePath + "'", "SysCal");
1246 :
1247 : // Let's make a string out of the stringstream content and empty the stringstream.
1248 : string xmlDocument = ss.str(); ss.str("");
1249 :
1250 : // Let's make a very primitive check to decide
1251 : // whether the XML content represents the table
1252 : // or refers to it via a <BulkStoreRef element.
1253 : */
1254 :
1255 33 : string xmlDocument;
1256 : try {
1257 33 : xmlDocument = getContainer().getXSLTransformer()(tablePath);
1258 33 : if (getenv("ASDM_DEBUG")) cout << "About to read " << tablePath << endl;
1259 : }
1260 0 : catch (const XSLTransformerException &e) {
1261 0 : throw ConversionException("Caugth an exception whose message is '" + e.getMessage() + "'.", "SysCal");
1262 0 : }
1263 :
1264 33 : if (xmlDocument.find("<BulkStoreRef") != string::npos)
1265 4 : setFromMIMEFile(directory);
1266 : else
1267 29 : fromXML(xmlDocument);
1268 33 : }
1269 :
1270 :
1271 :
1272 :
1273 :
1274 :
1275 :
1276 :
1277 :
1278 : /**
1279 : * Insert a SysCalRow* in a vector of SysCalRow* so that it's ordered by ascending start time.
1280 : *
1281 : * @param SysCalRow* x . The pointer to be inserted.
1282 : * @param vector <SysCalRow*>& row. A reference to the vector where to insert x.
1283 : *
1284 : */
1285 44706 : SysCalRow* SysCalTable::insertByStartTime(SysCalRow* x, vector<SysCalRow*>& row) {
1286 :
1287 44706 : vector <SysCalRow*>::iterator theIterator;
1288 :
1289 44706 : ArrayTime start = x->timeInterval.getStart();
1290 :
1291 : // Is the row vector empty ?
1292 44706 : if (row.size() == 0) {
1293 2854 : row.push_back(x);
1294 2854 : privateRows.push_back(x);
1295 2854 : x->isAdded(true);
1296 2854 : return x;
1297 : }
1298 :
1299 : // Optimization for the case of insertion by ascending time.
1300 41852 : SysCalRow* last = *(row.end()-1);
1301 :
1302 41852 : if ( start > last->timeInterval.getStart() ) {
1303 : //
1304 : // Modify the duration of last if and only if the start time of x
1305 : // is located strictly before the end time of last.
1306 : //
1307 41852 : if ( start < (last->timeInterval.getStart() + last->timeInterval.getDuration()))
1308 5616 : last->timeInterval.setDuration(start - last->timeInterval.getStart());
1309 41852 : row.push_back(x);
1310 41852 : privateRows.push_back(x);
1311 41852 : x->isAdded(true);
1312 41852 : return x;
1313 : }
1314 :
1315 : // Optimization for the case of insertion by descending time.
1316 0 : SysCalRow* first = *(row.begin());
1317 :
1318 0 : if ( start < first->timeInterval.getStart() ) {
1319 : //
1320 : // Modify the duration of x if and only if the start time of first
1321 : // is located strictly before the end time of x.
1322 : //
1323 0 : if ( first->timeInterval.getStart() < (start + x->timeInterval.getDuration()) )
1324 0 : x->timeInterval.setDuration(first->timeInterval.getStart() - start);
1325 0 : row.insert(row.begin(), x);
1326 0 : privateRows.push_back(x);
1327 0 : x->isAdded(true);
1328 0 : return x;
1329 : }
1330 :
1331 : // Case where x has to be inserted inside row; let's use a dichotomy
1332 : // method to find the insertion index.
1333 0 : unsigned int k0 = 0;
1334 0 : unsigned int k1 = row.size() - 1;
1335 :
1336 0 : while (k0 != (k1 - 1)) {
1337 0 : if (start == row[k0]->timeInterval.getStart()) {
1338 0 : if (row[k0]->equalByRequiredValue(x))
1339 0 : return row[k0];
1340 : else
1341 0 : throw DuplicateKey("DuplicateKey exception in ", "SysCalTable");
1342 : }
1343 0 : else if (start == row[k1]->timeInterval.getStart()) {
1344 0 : if (row[k1]->equalByRequiredValue(x))
1345 0 : return row[k1];
1346 : else
1347 0 : throw DuplicateKey("DuplicateKey exception in ", "SysCalTable");
1348 : }
1349 : else {
1350 0 : if (start <= row[(k0+k1)/2]->timeInterval.getStart())
1351 0 : k1 = (k0 + k1) / 2;
1352 : else
1353 0 : k0 = (k0 + k1) / 2;
1354 : }
1355 : }
1356 :
1357 0 : if (start == row[k0]->timeInterval.getStart()) {
1358 0 : if (row[k0]->equalByRequiredValue(x))
1359 0 : return row[k0];
1360 : else
1361 0 : throw DuplicateKey("DuplicateKey exception in ", "SysCalTable");
1362 : }
1363 0 : else if (start == row[k1]->timeInterval.getStart()) {
1364 0 : if (row[k1]->equalByRequiredValue(x))
1365 0 : return row[k1];
1366 : else
1367 0 : throw DuplicateKey("DuplicateKey exception in ", "SysCalTable");
1368 : }
1369 :
1370 0 : row[k0]->timeInterval.setDuration(start-row[k0]->timeInterval.getStart());
1371 0 : x->timeInterval.setDuration(row[k0+1]->timeInterval.getStart() - start);
1372 0 : row.insert(row.begin()+(k0+1), x);
1373 0 : privateRows.push_back(x);
1374 0 : x->isAdded(true);
1375 0 : return x;
1376 44706 : }
1377 :
1378 :
1379 :
1380 :
1381 :
1382 : } // End namespace asdm
1383 :
|