1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
|
/*
Copyright 2011-2016, 2018-2020 Thibaut Paumard
This file is part of Gyoto.
Gyoto is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gyoto is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Gyoto. If not, see <http://www.gnu.org/licenses/>.
*/
#include "GyotoConfig.h"
#ifdef GYOTO_USE_XERCES
#include "GyotoFactory.h"
#include "GyotoFactoryMessenger.h"
#include "GyotoUtils.h"
#include <string>
#include <libgen.h>
#include <unistd.h>
#include <cstdlib>
#include <locale>
// Let's imbue 'C' locale to every stream to make sure decimal_point
// is always actually a '.'
static std::locale Cloc("C");
#include "GyotoMetric.h"
#include "GyotoAstrobj.h"
#include "GyotoSpectrum.h"
#include "GyotoSpectrometer.h"
#include <xercesc/sax/ErrorHandler.hpp>
#include <xercesc/dom/DOMErrorHandler.hpp>
#include <xercesc/sax/SAXParseException.hpp>
#include <xercesc/framework/StdOutFormatTarget.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>
#include <xercesc/framework/MemBufFormatTarget.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>
using namespace Gyoto;
using namespace xercesc;
using namespace std;
// max length should be at least 26: " -0.1234567890123456E+123 \0"
#define dvalLength 30
#define dfmt " %.16g "
// support DBL_MIN, DBL_MAX, and put right format
#define d2txt(txt, val) \
if (val== DBL_MAX) strcpy(txt, "DBL_MAX"); \
else if (val==-DBL_MAX) strcpy(txt, "-DBL_MAX"); \
else if (val== DBL_MIN) strcpy(txt, "DBL_MIN"); \
else if (val==-DBL_MIN) strcpy(txt, "-DBL_MIN"); \
else { \
ostringstream ss; \
ss.imbue(Cloc); \
ss << setprecision(GYOTO_PREC) \
<< setw(GYOTO_WIDTH) << val; \
strcpy(txt, ss.str().c_str()); \
}
#define ifmt " %li "
#define i2txt(txt, val) sprintf( txt, ifmt, val);
class XStr
{
public :
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
XStr(const char* const toTranscode) : fASCIIForm(NULL)
{
// Call the private transcoding method
fUnicodeForm = XMLString::transcode(toTranscode);
}
XStr(const XMLCh* const toTranscode) : fUnicodeForm(NULL)
{
// Call the private transcoding method
fASCIIForm = XMLString::transcode(toTranscode);
}
~XStr()
{
if (fUnicodeForm) XMLString::release(&fUnicodeForm);
if (fASCIIForm) XMLString::release(&fASCIIForm);
}
// -----------------------------------------------------------------------
// Getter methods
// -----------------------------------------------------------------------
const XMLCh* getXForm() const
{
return fUnicodeForm;
}
const char* getCForm() const
{
return fASCIIForm;
}
private :
// -----------------------------------------------------------------------
// Private data members
//
// fUnicodeForm
// This is the Unicode XMLCh format of the string.
// -----------------------------------------------------------------------
XMLCh* fUnicodeForm;
char* fASCIIForm;
};
#define X(str) XStr(str).getXForm()
string Cs (const XMLCh* str) { XStr bidule (str); string truc(bidule.getCForm()); return truc;}
//#define C(str) string(XStr(str).getCForm()).c_str()
#define C(str) Cs(str).c_str()
///// First setup the reporter class. It doesn't need to be in the API.
namespace Gyoto {
class DOMErrorReporter;
}
class Gyoto::DOMErrorReporter : public ErrorHandler
{
public:
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
DOMErrorReporter() :
fSawErrors(false)
{
}
~DOMErrorReporter()
{
}
// -----------------------------------------------------------------------
// Implementation of the error handler interface
// -----------------------------------------------------------------------
void warning(const SAXParseException& toCatch);
void error(const SAXParseException& toCatch);
void fatalError(const SAXParseException& toCatch);
void resetErrors();
// -----------------------------------------------------------------------
// Getter methods
// -----------------------------------------------------------------------
bool getSawErrors() const;
// -----------------------------------------------------------------------
// Private data members
//
// fSawErrors
// This is set if we get any errors, and is queryable via a getter
// method. Its used by the main code to suppress output if there are
// errors.
// -----------------------------------------------------------------------
bool fSawErrors;
};
void DOMErrorReporter::warning(const SAXParseException&)
{
//y_print();
}
void DOMErrorReporter::error(const SAXParseException& toCatch)
{
fSawErrors = true;
GYOTO_ERROR(C(toCatch.getMessage()));
}
void DOMErrorReporter::fatalError(const SAXParseException& toCatch)
{
fSawErrors = true;
GYOTO_ERROR(C(toCatch.getMessage()));
}
void DOMErrorReporter::resetErrors()
{
fSawErrors = false;
}
//// Now for the public API
Factory::Factory(char * data)
: reporter_(NULL), gg_el_(NULL), obj_el_(NULL), ph_el_(NULL),
scenery_(NULL), gg_(NULL), screen_(NULL), obj_(NULL), photon_(NULL),
spectro_(NULL),
filename_("")
{
// Initialize Xerces XML parser
XMLPlatformUtils::Initialize();
parser_ = new XercesDOMParser();
parser_->setValidationScheme(XercesDOMParser::Val_Never);
parser_->setDoNamespaces(true); // optional
DOMErrorReporter *errReporter = new DOMErrorReporter();
reporter_=errReporter;
parser_->setErrorHandler(errReporter);
// Parse file
// If data start with "<?xml", this is an xml buffer.
size_t len=strlen(data);
if (len < 5 || data[0]!='<' || data[1] != '?'
|| data[2]!='x' || data[3]!='m' || data[4]!='l') {
// we are dealing with a file name
if (data[0]=='\\') ++data; // allow escaping
filename_=data;
parser_->parse(data);
} else {
filename_="Gyoto XML data (in memory)";
xercesc::MemBufInputSource
xml_buf(reinterpret_cast<const XMLByte *>(data),
len,
filename_.c_str());
parser_->parse(xml_buf);
}
doc_ = parser_ -> getDocument();
root_ = doc_ -> getDocumentElement();
if (!root_ ) throw(Error( "empty XML document" ));
resolver_=doc_->createNSResolver(root_);
kind_=(C(root_->getTagName()));
}
Factory::~Factory() {
// XMLString::release(&kind_);
if (resolver_) delete resolver_;
if (reporter_) delete reporter_;
if (parser_) delete parser_;
else if (doc_) delete doc_; // parser_ takes care of doc_
// if (impl_) delete impl_; // Terminate takes care of that one
XMLPlatformUtils::Terminate();
gg_=NULL;
obj_=NULL;
scenery_=NULL;
photon_=NULL;
spectro_=NULL;
}
void Factory::setReporter(ErrorHandler* eh) {
reporter_=eh;
parser_->setErrorHandler(eh);
}
DOMElement * Factory::getRoot() { return root_; }
DOMDocument * Factory::getDoc() { return doc_; }
SmartPointer<Gyoto::Metric::Generic> Factory::metric() {
if (!gg_) {
DOMElement *MetricDOM;
if (kind_.compare("Metric")){
DOMXPathResult* result;
result=doc_->evaluate(
X(("/"+kind_+"/Metric").c_str()),
root_,
resolver_,
DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE,
NULL);
if (!result->getSnapshotLength()) {
delete result;
return NULL;
}
MetricDOM = static_cast< xercesc::DOMElement* >(result -> getNodeValue());
delete result;
} else MetricDOM = root_;
vector<string> Plugin = Gyoto::split(C(MetricDOM->getAttribute(X("plugin"))), ",");
string Kind =
C(MetricDOM->getAttribute(X("kind")));
FactoryMessenger fm(this, MetricDOM);
gg_= (*Metric::getSubcontractor(Kind, Plugin))(&fm, Plugin);
}
return gg_;
}
SmartPointer<Gyoto::Astrobj::Generic> Factory::astrobj(){
if (!obj_) {
DOMXPathResult* result;
DOMElement *tmpEl;
if (kind_=="Astrobj") {
obj_el_ = tmpEl = root_;
} else {
result=doc_->evaluate(
X(("/"+kind_+"/Astrobj").c_str()),
root_,
resolver_,
DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE,
NULL);
if (!result->getSnapshotLength()) {
delete result;
return NULL;
}
tmpEl = static_cast< xercesc::DOMElement* >(result -> getNodeValue());
delete result;
}
vector<string> Plugin = split(C(tmpEl->getAttribute(X("plugin"))), ",");
string AstrobjKind =
Cs(tmpEl->getAttribute(X("kind")));
GYOTO_DEBUG_EXPR(AstrobjKind);
FactoryMessenger fm(this, tmpEl);
obj_ = (*Astrobj::getSubcontractor(AstrobjKind, Plugin))(&fm, Plugin);
}
return obj_;
}
SmartPointer<Gyoto::Photon> Factory::photon(){
if (!photon_) {
DOMXPathResult* result;
DOMElement *tmpEl;
if (kind_=="Photon") {
ph_el_ = tmpEl = root_;
} else {
result=doc_->evaluate(
X(("/"+kind_+"/Photon").c_str()),
root_,
resolver_,
DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE,
NULL);
if (!result->getSnapshotLength()) {
delete result;
return NULL;
}
tmpEl = static_cast< xercesc::DOMElement* >(result -> getNodeValue());
delete result;
}
FactoryMessenger fm(this, tmpEl);
photon_ = Photon::Subcontractor(&fm);
}
return photon_;
}
SmartPointer<Gyoto::Spectrum::Generic> Factory::spectrum(){
DOMXPathResult* result;
DOMElement *tmpEl;
if (kind_=="Spectrum") {
tmpEl = root_;
} else {
result=doc_->evaluate(
X(("/"+kind_+"/Spectrum").c_str()),
root_,
resolver_,
DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE,
NULL);
if (!result->getSnapshotLength()) {
delete result;
return NULL;
}
tmpEl = static_cast< xercesc::DOMElement* >(result -> getNodeValue());
delete result;
}
vector<string> Plugin = split(C(tmpEl->getAttribute(X("plugin"))), ",");
string Kind =
Cs(tmpEl->getAttribute(X("kind")));
GYOTO_DEBUG_EXPR(Kind);
FactoryMessenger fm(this, tmpEl);
return (*Spectrum::getSubcontractor(Kind, Plugin))(&fm, Plugin);
}
SmartPointer<Gyoto::Spectrometer::Generic> Factory::spectrometer(){
DOMXPathResult* result;
DOMElement *tmpEl;
if (kind_=="Spectrometer") {
tmpEl = root_;
} else {
result=doc_->evaluate(
X(("/"+kind_+"/Spectrometer").c_str()),
root_,
resolver_,
DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE,
NULL);
if (!result->getSnapshotLength()) {
delete result;
return NULL;
}
tmpEl = static_cast< xercesc::DOMElement* >(result -> getNodeValue());
delete result;
}
vector<string> Plugin = split(C(tmpEl->getAttribute(X("plugin"))), ",");
string Kind =
Cs(tmpEl->getAttribute(X("kind")));
GYOTO_DEBUG_EXPR(Kind);
FactoryMessenger fm(this, tmpEl);
return (*Spectrometer::getSubcontractor(Kind, Plugin))(&fm, Plugin);
}
/// Scenery
SmartPointer<Scenery> Factory::scenery () {
if (!scenery_) {
DOMXPathResult* result;
DOMElement *tmpEl;
result=doc_->evaluate(
X("/Scenery"),
root_,
resolver_,
DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE,
NULL);
tmpEl = static_cast< xercesc::DOMElement* >(result -> getNodeValue());
FactoryMessenger fm(this, tmpEl);
scenery_ = Scenery::Subcontractor(&fm);
delete result;
}
return scenery_;
}
SmartPointer<Gyoto::Screen> Factory::screen(){
if (!screen_) {
DOMXPathResult* result;
DOMElement *ScreenDOM;
result=doc_->evaluate(
X(("/"+kind_+"/Screen").c_str()),
root_,
resolver_,
DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE,
NULL);
if (!result->getSnapshotLength()) {
delete result;
return NULL;
}
ScreenDOM = static_cast< xercesc::DOMElement* >(result -> getNodeValue());
FactoryMessenger fm ( this, ScreenDOM );
screen_ = Screen::Subcontractor(&fm);
delete result;
}
return screen_;
}
const string Factory::kind() { return kind_ ; }
Factory::Factory(SmartPointer<Scenery> sc)
: reporter_(NULL), parser_(NULL), resolver_(NULL),
gg_el_(NULL), obj_el_(NULL), ph_el_(NULL),
scenery_(sc), gg_(sc->metric()),
screen_(NULL), obj_(sc->astrobj()),
photon_(NULL), spectro_(NULL), filename_("")
{
GYOTO_DEBUG << "Initializing XML stuff" << endl;
XMLPlatformUtils::Initialize();
impl_ = DOMImplementationRegistry::getDOMImplementation(X("Core"));
if (!impl_) GYOTO_ERROR("Problem initializing DOMImplementation");
doc_ = impl_->createDocument(
0, // root element namespace URI.
X("Scenery"), // root element name
0); // document type object (DTD).
root_ = doc_->getDocumentElement();
GYOTO_DEBUG << "Creating FactoryMessenger" << endl;
FactoryMessenger fm(this, root_);
GYOTO_DEBUG << "scenery_ -> fillElement(&fm);" << endl;
scenery_ -> fillElement(&fm);
}
Factory::Factory(SmartPointer<Screen> scr)
: reporter_(NULL), parser_(NULL), resolver_(NULL),
gg_el_(NULL), obj_el_(NULL), ph_el_(NULL),
scenery_(NULL), gg_(scr->metric()), screen_(scr), obj_(),
photon_(NULL), spectro_(NULL), filename_("")
{
XMLPlatformUtils::Initialize();
impl_ = DOMImplementationRegistry::getDOMImplementation(X("Core"));
if (!impl_) GYOTO_ERROR("Problem initializing DOMImplementation");
doc_ = impl_->createDocument(
0, // root element namespace URI.
X("Screen"), // root element name
0); // document type object (DTD).
root_ = doc_->getDocumentElement();
FactoryMessenger fm(this, root_);
screen_ -> fillElement(&fm);
}
Factory::Factory(SmartPointer<Metric::Generic> gg)
: reporter_(NULL), parser_(NULL), resolver_(NULL),
gg_el_(NULL), obj_el_(NULL), ph_el_(NULL),
scenery_(NULL), gg_(gg), screen_(NULL), obj_(NULL),
photon_(NULL), spectro_(NULL), filename_("")
{
XMLPlatformUtils::Initialize();
impl_ = DOMImplementationRegistry::getDOMImplementation(X("Core"));
if (!impl_) GYOTO_ERROR("Problem initializing DOMImplementation");
doc_ = impl_->createDocument(
0, // root element namespace URI.
X("Metric"), // root element name
0); // document type object (DTD).
gg_el_ = root_ = doc_->getDocumentElement();
FactoryMessenger fm(this, gg_el_);
gg -> fillElement(&fm);
}
Factory::Factory(SmartPointer<Astrobj::Generic> ao)
: reporter_(NULL), parser_(NULL), resolver_(NULL), gg_el_(NULL),
scenery_(NULL), gg_(NULL), obj_(ao), photon_(NULL),
spectro_(NULL), filename_("")
{
XMLPlatformUtils::Initialize();
impl_ = DOMImplementationRegistry::getDOMImplementation(X("Core"));
if (!impl_) GYOTO_ERROR("Problem initializing DOMImplementation");
doc_ = impl_->createDocument(
0, // root element namespace URI.
X("Astrobj"), // root element name
0); // document type object (DTD).
obj_el_ = root_ = doc_->getDocumentElement();
FactoryMessenger fm(this, obj_el_);
ao -> fillElement(&fm);
}
Factory::Factory(SmartPointer<Spectrum::Generic> sp)
: reporter_(NULL), parser_(NULL), resolver_(NULL), gg_el_(NULL),
scenery_(NULL), gg_(NULL), obj_(NULL), photon_(NULL),
spectro_(NULL), filename_("")
{
XMLPlatformUtils::Initialize();
impl_ = DOMImplementationRegistry::getDOMImplementation(X("Core"));
if (!impl_) GYOTO_ERROR("Problem initializing DOMImplementation");
doc_ = impl_->createDocument(
0, // root element namespace URI.
X("Spectrum"), // root element name
0); // document type object (DTD).
obj_el_ = root_ = doc_->getDocumentElement();
FactoryMessenger fm(this, obj_el_);
sp -> fillElement(&fm);
}
Factory::Factory(SmartPointer<Photon> ph)
: reporter_(NULL), parser_(NULL), resolver_(NULL),
gg_el_(NULL), obj_el_(NULL), ph_el_(NULL),
scenery_(NULL), gg_(ph->metric()), obj_(ph->astrobj()),
photon_(ph), spectro_(NULL), filename_("")
{
XMLPlatformUtils::Initialize();
impl_ = DOMImplementationRegistry::getDOMImplementation(X("Core"));
if (!impl_) GYOTO_ERROR("Problem initializing DOMImplementation");
doc_ = impl_->createDocument(
0, // root element namespace URI.
X("Photon"), // root element name
0); // document type object (DTD).
ph_el_ = root_ = doc_->getDocumentElement();
FactoryMessenger fm(this, root_);
photon_ -> fillElement(&fm);
}
Factory::Factory(SmartPointer<Spectrometer::Generic> sp)
: reporter_(NULL), parser_(NULL), resolver_(NULL),
gg_el_(NULL), obj_el_(NULL), ph_el_(NULL),
scenery_(NULL), gg_(NULL), obj_(NULL),
photon_(NULL), spectro_(sp), filename_("")
{
XMLPlatformUtils::Initialize();
impl_ = DOMImplementationRegistry::getDOMImplementation(X("Core"));
if (!impl_) GYOTO_ERROR("Problem initializing DOMImplementation");
doc_ = impl_->createDocument(
0, // root element namespace URI.
X("Spectrometer"), // root element name
0); // document type object (DTD).
root_ = doc_->getDocumentElement();
FactoryMessenger fm(this, root_);
spectro_ -> fillElement(&fm);
}
void Factory::metric(SmartPointer<Metric::Generic> gg, DOMElement *el) {
if (!gg && !gg_) return;
if (gg_ && gg && gg!= gg_) GYOTO_ERROR("Inconsistent use of Metrics");
if (gg && !gg_el_) {
gg_ = gg;
gg_el_ = doc_->createElement(X("Metric"));
el->appendChild(gg_el_);
FactoryMessenger fm(this, gg_el_);
gg -> fillElement(&fm);
}
}
void Factory::astrobj(SmartPointer<Astrobj::Generic> ao, DOMElement *el) {
GYOTO_DEBUG << endl;
if (!ao && !obj_) return;
if (obj_ && ao && ao!= obj_) GYOTO_ERROR("Inconsistent use of Astrobjs");
if (ao && !obj_el_) {
GYOTO_DEBUG <<"obj_ = ao;" << endl;
obj_ = ao;
GYOTO_DEBUG <<"XML stuff" << endl;
obj_el_ = doc_->createElement(X("Astrobj"));
el->appendChild(obj_el_);
GYOTO_DEBUG <<"XML stuffnew FactoryMessenger" << endl;
FactoryMessenger fm(this, obj_el_);
GYOTO_DEBUG <<"ao -> fillElement(&fm);" << endl;
ao -> fillElement(&fm);
}
}
void Factory::screen(SmartPointer<Screen> scr, DOMElement *el) {
if (!scr && !screen_) return;
if (screen_ && scr && scr!= screen_)
GYOTO_ERROR("Inconsistent use of Screens");
if (scr && !screen_) {
screen_ = scr;
DOMElement * scr_el = doc_->createElement(X("Screen"));
el->appendChild(scr_el);
FactoryMessenger fm(this, scr_el);
scr -> fillElement(&fm);
}
}
void Factory::write(const char* const goutputfile) {
filename_ = goutputfile;
// write file
DOMLSSerializer *theSerializer
= (static_cast<DOMImplementationLS*>(impl_))->createLSSerializer();
DOMConfiguration *serializerConfig
= theSerializer->getDomConfig();
DOMLSOutput *theOutputDesc
= (static_cast<DOMImplementationLS*>(impl_))->createLSOutput();
XMLFormatTarget *myFormTarget;
if (serializerConfig->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint,true))
serializerConfig->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
if (goutputfile)
myFormTarget=new LocalFileFormatTarget(goutputfile);
else
myFormTarget=new StdOutFormatTarget();
theOutputDesc->setByteStream(myFormTarget);
theSerializer->write(doc_, theOutputDesc);
delete myFormTarget;
theOutputDesc->release();
theSerializer->release();
}
string Factory::format() {
// write file
DOMLSSerializer *theSerializer
= (static_cast<DOMImplementationLS*>(impl_))->createLSSerializer();
DOMConfiguration *serializerConfig
= theSerializer->getDomConfig();
DOMLSOutput *theOutputDesc
= (static_cast<DOMImplementationLS*>(impl_))->createLSOutput();
MemBufFormatTarget *myFormTarget = new MemBufFormatTarget();
if (serializerConfig->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint,true))
serializerConfig->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
theOutputDesc->setByteStream(myFormTarget);
theSerializer->write(doc_, theOutputDesc);
string res=(const char*) myFormTarget->getRawBuffer();
delete myFormTarget;
theOutputDesc->release();
theSerializer->release();
return res;
}
void Factory::setParameter(std::string name, DOMElement *pel) {
DOMElement* el = doc_->createElement(X(name.c_str()));
pel -> appendChild(el);
}
void Factory::setParameter(std::string name, double value, DOMElement *pel) {
DOMElement* el = doc_->createElement(X(name.c_str()));
pel -> appendChild(el);
char val_string[dvalLength];
d2txt(val_string,value);
el->appendChild(doc_->createTextNode(X(val_string)));
}
void Factory::setParameter(std::string name, int value, DOMElement *pel) {
DOMElement* el = doc_->createElement(X(name.c_str()));
pel -> appendChild(el);
char val_string[dvalLength];
sprintf( val_string, " %i ", value);
el->appendChild(doc_->createTextNode(X(val_string)));
}
void Factory::setParameter(std::string name, unsigned int value, DOMElement *pel) {
DOMElement* el = doc_->createElement(X(name.c_str()));
pel -> appendChild(el);
char val_string[dvalLength];
sprintf( val_string, " %u ", value);
el->appendChild(doc_->createTextNode(X(val_string)));
}
void Factory::setParameter(std::string name, long value, DOMElement *pel) {
DOMElement* el = doc_->createElement(X(name.c_str()));
pel -> appendChild(el);
char val_string[dvalLength];
sprintf( val_string, " %li ", value);
el->appendChild(doc_->createTextNode(X(val_string)));
}
void Factory::setParameter(std::string name, unsigned long int value, DOMElement *pel) {
DOMElement* el = doc_->createElement(X(name.c_str()));
pel -> appendChild(el);
char val_string[dvalLength];
sprintf( val_string, " %lu ", value);
el->appendChild(doc_->createTextNode(X(val_string)));
}
void Factory::setParameter(std::string name, std::string val, DOMElement *pel) {
DOMElement* el = doc_->createElement(X(name.c_str()));
pel -> appendChild(el);
el->appendChild(doc_->createTextNode(X(val.c_str())));
}
void Factory::setParameter(std::string name, double val[],
size_t n, DOMElement *pel, FactoryMessenger **child){
ostringstream ss;
ss.imbue(Cloc); // set local to 'C'
ss << setprecision(GYOTO_PREC) << setw(GYOTO_WIDTH) << val[0];
for (size_t i=1; i<n; ++i) {
ss << " " << setprecision(GYOTO_PREC) << setw(GYOTO_WIDTH) << val[i];
}
DOMElement* el = doc_->createElement(X(name.c_str()));
pel -> appendChild(el);
el->appendChild( doc_->createTextNode(X(ss.str().c_str())) );
if (child) *child = new FactoryMessenger(this, el);
}
void Factory::setParameter(std::string name,
std::vector<double> const &val,
DOMElement *pel, FactoryMessenger **child){
DOMElement* el = doc_->createElement(X(name.c_str()));
pel -> appendChild(el);
size_t n=val.size();
if (n) {
ostringstream ss;
ss.imbue(Cloc); // set local to 'C'
ss << setprecision(GYOTO_PREC) << setw(GYOTO_WIDTH) << val[0];
for (size_t i=1; i<n; ++i) {
ss << " " << setprecision(GYOTO_PREC) << setw(GYOTO_WIDTH) << val[i];
}
el->appendChild( doc_->createTextNode(X(ss.str().c_str())) );
}
if (child) *child = new FactoryMessenger(this, el);
}
void Factory::setParameter(std::string name,
std::vector<unsigned long> const &val,
DOMElement *pel, FactoryMessenger **child){
DOMElement* el = doc_->createElement(X(name.c_str()));
pel -> appendChild(el);
size_t n=val.size();
if (n) {
ostringstream ss;
ss.imbue(Cloc); // set local to 'C'
ss << val[0];
for (size_t i=1; i<n; ++i) {
ss << " " << val[i];
}
el->appendChild( doc_->createTextNode(X(ss.str().c_str())) );
}
if (child) *child = new FactoryMessenger(this, el);
}
void Factory::setContent(std::string content, DOMElement *el) {
el -> appendChild( doc_->createTextNode( X( content.c_str() ) ) );
}
///////// Factory Messenger Class ///////////
FactoryMessenger::FactoryMessenger(Gyoto::Factory* emp,
xercesc::DOMElement* el) :
employer_(emp), element_(el), curNodeIndex_(0)
{
children_ = element_->getChildNodes();
nodeCount_ = children_->getLength();
}
FactoryMessenger::FactoryMessenger(const FactoryMessenger& fm,
std::string name) :
employer_(fm.employer_), element_(NULL), curNodeIndex_(0)
{
element_ = employer_ -> doc_ -> createElement(X(name.c_str()));
fm.element_ -> appendChild (element_) ;
children_ = element_->getChildNodes();
nodeCount_ = children_->getLength();
}
void FactoryMessenger::reset() {
curNodeIndex_=0;
}
int FactoryMessenger::getNextParameter(std::string* namep,
std::string* contp,
std::string* unitp)
{
GYOTO_DEBUG
<< "namep=" << namep
<< ", contp=" << contp << ": "
<< "*namep=" << *namep
<< ", *contp=" << *contp << endl;
if (curNodeIndex_ >= nodeCount_) return 0;
DOMNode *currentNode = children_->item(curNodeIndex_++);
if( currentNode->getNodeType() && // true is not NULL
currentNode->getNodeType() == DOMNode::ELEMENT_NODE ) // is element
{
// Found node which is an Element. Re-cast node as element
DOMElement *currentElement
= static_cast< xercesc::DOMElement* >( currentNode );
*namep = C(currentElement->getTagName());
*contp = C(currentElement->getTextContent());
if (unitp) *unitp = C(currentElement->getAttribute(X("unit")));
return 1;
}
return getNextParameter(namep, contp, unitp);
}
FactoryMessenger* FactoryMessenger::makeChild(std::string name) {
return new FactoryMessenger(*this, name);
}
void FactoryMessenger::setSelfAttribute(std::string attrname,
std::string attrvalue) {
element_->setAttribute(X(attrname.c_str()), X(attrvalue.c_str()));
}
void FactoryMessenger::setSelfAttribute(std::string attrname,
unsigned long attrvalue) {
char val_string[dvalLength];
sprintf( val_string, "%lu", attrvalue);
element_->setAttribute(X(attrname.c_str()), X(val_string));
}
void FactoryMessenger::setSelfAttribute(std::string attrname,
unsigned int attrvalue) {
setSelfAttribute(attrname, (unsigned long)(attrvalue));
}
void FactoryMessenger::setSelfAttribute(std::string attrname,
double attrvalue) {
char val_string[dvalLength];
d2txt(val_string, attrvalue);
element_->setAttribute(X(attrname.c_str()), X(val_string));
}
string FactoryMessenger::getAttribute(std::string attrname) const {
DOMNode *currentNode = children_->item(curNodeIndex_-1);
DOMElement *currentElement
= static_cast< xercesc::DOMElement* >( currentNode );
return Cs(currentElement->getAttribute(X(attrname.c_str())));
}
string FactoryMessenger::getSelfAttribute(std::string attrname) const {
return Cs(element_->getAttribute(X(attrname.c_str())));
}
string FactoryMessenger::getFullContent() const {
return Cs(element_->getTextContent());
}
void FactoryMessenger::setFullContent(std::string content) {
employer_ -> setContent (content, element_) ;
}
FactoryMessenger* FactoryMessenger::getChild() const {
DOMNode *currentNode = children_->item(curNodeIndex_-1);
DOMElement *currentElement
= static_cast< xercesc::DOMElement* >( currentNode );
return new FactoryMessenger(employer_, currentElement);
}
void FactoryMessenger::astrobj(SmartPointer<Astrobj::Generic> gg) {
employer_ -> astrobj (gg, element_);
}
void FactoryMessenger::screen(SmartPointer<Screen> gg) {
employer_ -> screen (gg, element_);
}
void FactoryMessenger::metric(SmartPointer<Metric::Generic> gg) {
employer_ -> metric (gg, element_);
}
SmartPointer<Metric::Generic> FactoryMessenger::metric() {
return employer_ -> metric ();
}
SmartPointer<Screen> FactoryMessenger::screen() {
return employer_ -> screen ();
}
SmartPointer<Photon> FactoryMessenger::photon() {
return employer_ -> photon ();
}
SmartPointer<Astrobj::Generic> FactoryMessenger::astrobj() {
return employer_ -> astrobj ();
}
void FactoryMessenger::setParameter(std::string name){
employer_ -> setParameter(name, element_);
}
void FactoryMessenger::setParameter(std::string name, double value){
employer_ -> setParameter(name, value, element_);
}
void FactoryMessenger::setParameter(std::string name, int value){
employer_ -> setParameter(name, value, element_);
}
void FactoryMessenger::setParameter(std::string name, long value){
employer_ -> setParameter(name, value, element_);
}
void FactoryMessenger::setParameter(std::string name, unsigned int value){
employer_ -> setParameter(name, value, element_);
}
void FactoryMessenger::setParameter(std::string name, unsigned long value){
employer_ -> setParameter(name, value, element_);
}
void FactoryMessenger::setParameter(std::string name, std::string value){
employer_ -> setParameter(name, value, element_);
}
void FactoryMessenger::setParameter(std::string name, double val[], size_t n,
FactoryMessenger **child){
employer_ -> setParameter(name, val, n, element_, child);
}
void FactoryMessenger::setParameter(std::string name,
std::vector<double> const &val,
FactoryMessenger **child){
employer_ -> setParameter(name, val, element_, child);
}
void FactoryMessenger::setParameter(std::string name,
std::vector<unsigned long> const &val,
FactoryMessenger **child){
employer_ -> setParameter(name, val, element_, child);
}
size_t FactoryMessenger::parseArray(std::string content, double val[], size_t max_tokens)
{
char const * const delim = " \t\n" ;
char const * const c_content=content.c_str();
size_t len=strlen(c_content);
if (len==0) return 0;
size_t n=0;
char * const tc = new char[len+1];
memcpy(tc, c_content, len+1);
char * sub = strtok(tc, delim);
while (sub && n<max_tokens) {
val[n++] = Gyoto::atof(sub);
sub = strtok(NULL, delim);
}
if (sub) n=max_tokens+1;
delete [] tc;
return n;
}
std::vector<double> FactoryMessenger::parseArray(std::string content)
{
std::vector<double> result;
char const * const delim = " \t\n" ;
char const * const c_content=content.c_str();
size_t len=strlen(c_content);
if (len==0) return result;
char * const tc = new char[len+1];
memcpy(tc, c_content, len+1);
char * sub = strtok(tc, delim);
while (sub) {
result.push_back(Gyoto::atof(sub));
sub = strtok(NULL, delim);
}
delete [] tc;
return result;
}
std::vector<unsigned long> FactoryMessenger::parseArrayULong(std::string content)
{
std::vector<unsigned long> result;
char const * const delim = " \t\n" ;
char const * const c_content=content.c_str();
size_t len=strlen(c_content);
if (len==0) return result;
char * const tc = new char[len+1];
memcpy(tc, c_content, len+1);
char * sub = strtok(tc, delim);
while (sub) {
result.push_back(atol(sub));
sub = strtok(NULL, delim);
}
delete [] tc;
return result;
}
std::string FactoryMessenger::fullPath(std::string fname) {
return employer_ -> fullPath(fname);
}
std::string Factory::fullPath(std::string fname) {
GYOTO_DEBUG << endl;
if (!fname.compare(0, 1, "/")) return fname; // fname is already absolute
string fpath = "";
string xmlpath="", curwd="";
{
// Make sure we call dirname and getcwd correctly and free memory.
// We do this in a block to scope the temporary variables.
char * xmlfile = strdup(filename_.c_str()); // why strdup? because
xmlpath = dirname(xmlfile); // dirname may modify xmlfile.
free (xmlfile); xmlfile = NULL;
char * cwd = getcwd(NULL, 0);
curwd = cwd;
free (cwd); cwd = NULL;
}
string prefix="`pwd`/";
if (fname.compare(0, prefix.size(), prefix)) {
// fname does not start with "`pwd`/":
// it is relative to xmlpath
if (xmlpath.compare(0, 1, "/")) fpath = curwd + "/" ;
fpath += xmlpath + "/";
fpath += fname;
} else {
// fname starts with "`pwd`/": relative to working directory
fpath = curwd + "/";
fpath += fname.substr(prefix.size());
}
GYOTO_DEBUG << "returns " << fpath << endl;
return fpath;
}
#endif
|