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