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