Gyoto
Public Member Functions | Protected Attributes | Private Member Functions | Friends | List of all members
Gyoto::Factory Class Reference

XML input/output. More...

#include <GyotoFactory.h>

Public Member Functions

 Factory (char *filename)
 Constructor for reading a file. More...
 
 Factory (SmartPointer< Scenery > sc)
 Constructor for saving (or printing) a Scenery.
 
 Factory (SmartPointer< Metric::Generic > gg)
 Constructor for saving (or printing) a Metric.
 
 Factory (SmartPointer< Astrobj::Generic > ao)
 Constructor for saving (or printing) an Astrobj.
 
 Factory (SmartPointer< Spectrum::Generic > sp)
 Constructor for saving (or printing) a Spectrum.
 
 Factory (SmartPointer< Screen > screen)
 Constructor for saving (or printing) a Screen.
 
 Factory (SmartPointer< Photon > photon)
 Constructor for saving (or printing) a Photon.
 
 Factory (SmartPointer< Spectrometer::Generic > Spectrometer)
 Constructor for saving (or printing) a Spectrometer.
 
 ~Factory ()
 Destructor.
 
const std::string kind ()
 Get Factory::kind_.
 
Gyoto::SmartPointer< Gyoto::Sceneryscenery ()
 Find Scenery element, instantiate it and get it. More...
 
Gyoto::SmartPointer< Gyoto::Metric::Genericmetric ()
 Find Metric element, instantiate it and get it. More...
 
Gyoto::SmartPointer< Gyoto::Screenscreen ()
 Find Screen element, instantiate it and get it. More...
 
Gyoto::SmartPointer< Gyoto::Astrobj::Genericastrobj ()
 Find Astrobj element, instantiate it and get it. More...
 
Gyoto::SmartPointer< Gyoto::Photonphoton ()
 Find Photon element, instantiate it and get it. More...
 
Gyoto::SmartPointer< Gyoto::Spectrum::Genericspectrum ()
 Find Photon element, instantiate it and get it. More...
 
Gyoto::SmartPointer< Gyoto::Spectrometer::Genericspectrometer ()
 Find Spectrometer element, instantiate it and get it. More...
 
void write (const char *const fname=0)
 Write constructed XML representation to file.
 
std::string format ()
 Get constructed XML representation as std::string.
 
void metric (SmartPointer< Metric::Generic > gg, xercesc::DOMElement *el)
 Set Metric for this document. More...
 
void astrobj (SmartPointer< Astrobj::Generic > ao, xercesc::DOMElement *el)
 Set Astrobj for this document. More...
 
void screen (SmartPointer< Screen > scr, xercesc::DOMElement *el)
 Set Screen for this document. More...
 
void setContent (std::string content, xercesc::DOMElement *el)
 Set text content of XML element.
 
void setParameter (std::string name, xercesc::DOMElement *pel)
 Create new XML element without content. More...
 
void setParameter (std::string name, double value, xercesc::DOMElement *pel)
 Create new XML element with double value. More...
 
void setParameter (std::string name, int value, xercesc::DOMElement *pel)
 Create new XML element with integer value. More...
 
void setParameter (std::string name, unsigned int value, xercesc::DOMElement *pel)
 Create new XML element with integer value. More...
 
void setParameter (std::string name, long value, xercesc::DOMElement *pel)
 Create new XML element with integer value. More...
 
void setParameter (std::string name, unsigned long value, xercesc::DOMElement *pel)
 Create new XML element with integer value. More...
 
void setParameter (std::string name, std::string value, xercesc::DOMElement *pel)
 Create new XML element with string content. More...
 
void setParameter (std::string name, double val[], size_t nelem, xercesc::DOMElement *pel, FactoryMessenger **child=NULL)
 Create new XML element with array content. More...
 
void setParameter (std::string name, std::vector< double > const &val, xercesc::DOMElement *pel, FactoryMessenger **child=NULL)
 
void setParameter (std::string name, std::vector< unsigned long > const &val, xercesc::DOMElement *pel, FactoryMessenger **child=NULL)
 
std::string fullPath (std::string relpath)
 Transform relative path into absolute path. More...
 

Protected Attributes

xercesc::ErrorHandler * reporter_
 Xerces error handler.
 
xercesc::DOMDocument * doc_
 The document being read or written.
 
xercesc::DOMElement * root_
 Root element in Factory::doc_.
 
xercesc::XercesDOMParser * parser_
 Xerces parser.
 
xercesc::DOMXPathNSResolver * resolver_
 Xerces resolver.
 
xercesc::DOMImplementation * impl_
 Xerces implementation.
 
xercesc::DOMElement * gg_el_
 XML element representing the Metric.
 
xercesc::DOMElement * obj_el_
 XML element representing the Astrobj.
 
xercesc::DOMElement * ph_el_
 XML element representing the Photon.
 
SmartPointer< Sceneryscenery_
 The Scenery read from or written to Factory::doc_.
 
SmartPointer< Metric::Genericgg_
 The Metric read from or written to Factory::doc_.
 
SmartPointer< Screenscreen_
 The Screen read from or written to Factory::doc_.
 
SmartPointer< Astrobj::Genericobj_
 The Astrobj read from or written to Factory::doc_.
 
SmartPointer< Photonphoton_
 The Photon read from or written to Factory::doc_.
 
SmartPointer< Spectrometer::Genericspectro_
 The Spectrometer read from or written to Factory::doc_.
 
std::string filename_
 XML file name, if actually reading from or writting to file.
 
std::string kind_
 Kind of root element (Scenery, Metric etc.)
 

Private Member Functions

void setReporter (xercesc::ErrorHandler *)
 Set Xerces reporter.
 
xercesc::DOMElement * getRoot ()
 Get Factory::root_.
 
xercesc::DOMDocument * getDoc ()
 Get Factory::doc_.
 

Friends

class Gyoto::FactoryMessenger
 

Detailed Description

XML input/output.

The Factory is responsible from building objects from their XML description, and from saving an XML description of existing objects. Since the Factory doesn't know how to build the variety of objects available in Gyoto and in external plug-ins, the Factory orders Metric, Astrobj and Spectrum objects from registered subcontractors (see SmartPointee::Subcontractor_t). The factory an the various subcontractors communicate through a FactoryMessenger.

To read an XML file, you simply create an instance of the Factory with a filename, and get whichever object type you are interested in:

Gyoto::Factory * factory = new Gyoto::Factory("some/input/file.xml");
const std::string kind = factory->kind();
if (kind.compare("Scenery")) Gyoto::throwError("I wan't a Scenery");
delete factory; factory=NULL;

or, for a single object and without checking the kind (kind()) first:

Gyoto::SmartPointer<Gyoto::Scenery> scenery = Factory("some/input/file.xml").scenery();

Writing an object to a file is even easier. Assuming "object" below is a Gyoto::SmartPointer<class> where "class" is one of Scenery, Metric::Generic, Astrobj::Generic, Spectrum::Generic, Screen, Photon or Spectrometer:

Gyoto::Factory * factory = new Gyoto::Factory(object);
factory -> write("some/output/file.xml");
delete factory; factory=NULL;

or, for short:

Gyoto::Factory(object).write("some/output/file.xml");

You can also directly display the object to stdout:

std::cout << Gyoto::Factory(object).format() << std::endl;

Constructor & Destructor Documentation

◆ Factory()

Gyoto::Factory::Factory ( char *  filename)

Constructor for reading a file.

If filename starts with "<?xml", it is interpreted as XML data and parsed directly (with no file access). It is possible to parse a file that actually starts with those unlikely characters by escaping the first with '\'. If filename starts with '\', this single character is always stripped.

Member Function Documentation

◆ astrobj() [1/2]

Gyoto::SmartPointer<Gyoto::Astrobj::Generic> Gyoto::Factory::astrobj ( )

Find Astrobj element, instantiate it and get it.

Astrobj may be either the root element or directly within the root element.

◆ astrobj() [2/2]

void Gyoto::Factory::astrobj ( SmartPointer< Astrobj::Generic ao,
xercesc::DOMElement *  el 
)

Set Astrobj for this document.

If called several times for the same document, the astrobj SmartPointers must point to the same instance or an error will be thrown using Gyoto::throwError().

◆ fullPath()

std::string Gyoto::Factory::fullPath ( std::string  relpath)

Transform relative path into absolute path.

relpath is interpreted as follows:

If it starts with "/", it is interpreted as an absolute path and is returned as is.

If it is prefixed with "`pwd`/", the rest of relpath is interpreted as relative to the current working directory, i.e. fullPath() tries to mimic how the shell would expand relpath into an absolute path.

In any other circumstance, relpath is interpreted to relative to the directory where the XML file resides.

Parameters
relpathPath specification.
Returns
Absolute path to same file.

◆ metric() [1/2]

Gyoto::SmartPointer<Gyoto::Metric::Generic> Gyoto::Factory::metric ( )

Find Metric element, instantiate it and get it.

Metric may be either the root element or directly within the root element.

◆ metric() [2/2]

void Gyoto::Factory::metric ( SmartPointer< Metric::Generic gg,
xercesc::DOMElement *  el 
)

Set Metric for this document.

If called several times for the same document, the metric SmartPointers must point to the same instance or an error will be thrown using Gyoto::throwError().

◆ photon()

Gyoto::SmartPointer<Gyoto::Photon> Gyoto::Factory::photon ( )

Find Photon element, instantiate it and get it.

Photon may be either the root element or directly within the root element.

◆ scenery()

Gyoto::SmartPointer<Gyoto::Scenery> Gyoto::Factory::scenery ( )

Find Scenery element, instantiate it and get it.

Scenery must be the root element. scenery() will call metric(), astrobj() and screen().

◆ screen() [1/2]

Gyoto::SmartPointer<Gyoto::Screen> Gyoto::Factory::screen ( )

Find Screen element, instantiate it and get it.

Screen may be either the root element or directly within the root element.

◆ screen() [2/2]

void Gyoto::Factory::screen ( SmartPointer< Screen scr,
xercesc::DOMElement *  el 
)

Set Screen for this document.

If called several times for the same document, the screen SmartPointers must point to the same instance or an error will be thrown using Gyoto::throwError().

◆ setParameter() [1/8]

void Gyoto::Factory::setParameter ( std::string  name,
xercesc::DOMElement *  pel 
)

Create new XML element without content.

E.g.

<OpticallyThin/>
Parameters
nameXML entity name.
pelParent XML element.

◆ setParameter() [2/8]

void Gyoto::Factory::setParameter ( std::string  name,
double  value,
xercesc::DOMElement *  pel 
)

Create new XML element with double value.

E.g.

<Radius> 2. </Radius>
Parameters
nameXML entity name.
valueEntity content.
pelParent XML element.

◆ setParameter() [3/8]

void Gyoto::Factory::setParameter ( std::string  name,
int  value,
xercesc::DOMElement *  pel 
)

Create new XML element with integer value.

E.g.

<IntParameter> 7 </IntParameter>
Parameters
nameXML entity name.
valueEntity content.
pelParent XML element.

◆ setParameter() [4/8]

void Gyoto::Factory::setParameter ( std::string  name,
unsigned int  value,
xercesc::DOMElement *  pel 
)

Create new XML element with integer value.

E.g.

<IntParameter> 7 </IntParameter>
Parameters
nameXML entity name.
valueEntity content.
pelParent XML element.

◆ setParameter() [5/8]

void Gyoto::Factory::setParameter ( std::string  name,
long  value,
xercesc::DOMElement *  pel 
)

Create new XML element with integer value.

E.g.

<IntParameter> 7 </IntParameter>
Parameters
nameXML entity name.
valueEntity content.
pelParent XML element.

◆ setParameter() [6/8]

void Gyoto::Factory::setParameter ( std::string  name,
unsigned long  value,
xercesc::DOMElement *  pel 
)

Create new XML element with integer value.

E.g.

<IntParameter> 7 </IntParameter>
Parameters
nameXML entity name.
valueEntity content.
pelParent XML element.

◆ setParameter() [7/8]

void Gyoto::Factory::setParameter ( std::string  name,
std::string  value,
xercesc::DOMElement *  pel 
)

Create new XML element with string content.

E.g.

<StringParameter> Text </StringParameter>

Any parameter can acually be set this way if total control over Text formatting is wished for.

Parameters
nameXML entity name.
valueEntity content.
pelParent XML element.

◆ setParameter() [8/8]

void Gyoto::Factory::setParameter ( std::string  name,
double  val[],
size_t  nelem,
xercesc::DOMElement *  pel,
FactoryMessenger **  child = NULL 
)

Create new XML element with array content.

E.g.

<Position> 0. 10. 3.14. 0. </Position>
Parameters
[in]nameXML entity name.
[in]valEntity content.
[in]nelemNumber of elements in val to output.
[in]pelParent XML element.
[out]childIf not NULL, set to a new Gyoto::FactoryMessenger pointing to the new element. It must be deleted later.

◆ spectrometer()

Gyoto::SmartPointer<Gyoto::Spectrometer::Generic> Gyoto::Factory::spectrometer ( )

Find Spectrometer element, instantiate it and get it.

Spectrometer may be either the root element or directly within the root element.

◆ spectrum()

Gyoto::SmartPointer<Gyoto::Spectrum::Generic> Gyoto::Factory::spectrum ( )

Find Photon element, instantiate it and get it.

Photon may be either the root element or directly within the root element.


The documentation for this class was generated from the following file: