core/load_xml_handle.h

00001 /* gld_loadHndl.h
00002  * 
00003  * Authors:
00004  *  Matthew Hauer <matthew.hauer@pnl.gov>, 6 Nov 07 -
00005  *
00006  * Versions:
00007  *  1.0 - MH - initial version
00008  *
00009  * Credits:
00010  *  adapted from SAX2PrintHndl.h
00011  *
00012  *  @file load_xml_handle.h
00013  *  @addtogroup load XML file loader
00014  *  @ingroup core
00015  *
00016  */
00017 
00018 #include    <xercesc/sax2/DefaultHandler.hpp>
00019 #include    <xercesc/framework/XMLFormatter.hpp>
00020 #include <stdio.h>
00021 #include <string.h>
00022 #include <wchar.h>
00023 #include <vector>
00024 
00025 extern "C" {
00026     #include "class.h"
00027     #include "load.h"
00028     #include "globals.h"
00029     #include "module.h"
00030     #include "convert.h"
00031     #include "random.h"
00032     #include "output.h"
00033     #include "unit.h"
00034 }
00035 #include "gridlabd.h"
00036 
00037 XERCES_CPP_NAMESPACE_USE
00038 
00039 using std::vector;
00040 
00041 typedef enum {
00042     EMPTY = 0,
00043     LOAD,           //  got load tag & not in a module
00044     MODULE_STATE,   //  in a module
00045     MODULE_PROP,    //  setting a module property
00046     OBJECT_STATE,   //  setting up an object
00047     OBJECT_PROP,    //  setting an object property
00048     GLOBAL_STATE,   //  setting up for global variables
00049     GLOBAL_PROP,    //  setting a global variable
00050     CLOCK_STATE,
00051     CLOCK_PROP
00052 } gld_state;
00053 
00054 class gldStack{
00055 public:
00056     gldStack(){next = NULL; clear();}
00057     gldStack(gldStack *ptr){next = ptr; clear();}
00058     ~gldStack(){if(next != NULL) delete next;}
00059 
00060     void clear(){object_type[0] = 0; object_id[0] = 0; object_name[0] = 0; keyword[0] = 0;}
00061     char object_type[64];
00062     char object_id[64];
00063     char object_name[64];
00064     char keyword[64];
00065     gldStack *next;
00066 };
00067 
00068 class gld_loadHndl : public DefaultHandler, public XMLFormatTarget {
00069 public:
00070     gld_loadHndl();
00071     gld_loadHndl(const char* const, const XMLFormatter::UnRepFlags, const bool);
00072     ~gld_loadHndl();
00073 
00074     bool did_load(){return load_state;}
00075 
00076     void writeChars(const XMLByte* const toWrite);
00077     void writeChars(const XMLByte* const toWrite, const unsigned int count, XMLFormatter* const formatter);
00078 
00079     void setDocumentLocator(const Locator *const locator);
00080     void endDocument();
00081     void endElement(const XMLCh* const uri, const XMLCh* const localname, const XMLCh* const qname);
00082     void characters(const XMLCh* const chars, const unsigned int length);
00083     void ignorableWhitespace(const XMLCh* const chars, const unsigned int length);
00084     void processingInstruction(const XMLCh* const target, const XMLCh* const data);
00085     void startDocument();
00086     void startElement(const XMLCh* const uri, const XMLCh* const localname, const XMLCh* const qname, const Attributes& attributes);
00087 
00088     void warning(const SAXParseException& exc);
00089     void error(const SAXParseException& exc);
00090     void fatalError(const SAXParseException& exc);
00091 
00092     void notationDecl(const XMLCh* const name, const XMLCh* const publicId, const XMLCh* const systemId);
00093     /* void unparsedEntityDecl(const XMLCh* const name, const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const notationName); */
00094 
00095     char *build_object_vect(int start, int end);
00096     void parse_property(char *buffer);
00097 private :
00098     XMLFormatter    fFormatter;
00099     bool            fExpandNS ;
00100 
00101     gld_state   stack_state;
00102     Locator const *locator;
00103     char    errmsg[1024];
00104     MODULE *module;
00105     CLASS *oclass;
00106     OBJECT *obj;
00107     PROPERTY *prop;
00108     char propname[256];
00109 
00110     char *read_module_prop(char *buffer, size_t len);
00111     char *read_global_prop(char *buffer, size_t len);
00112     char *read_object_prop(char *buffer, size_t len);
00113     char *read_clock_prop(char *buffer, size_t len);
00114 
00115     char *start_element_empty(char *buffer, size_t len, const Attributes& attributes);
00116     char *start_element_load(char *buffer, size_t len, const Attributes& attributes);
00117     char *start_element_module(char *buffer, size_t len, const Attributes& attributes);
00118     char *start_element_module_prop(char *buffer, size_t len, const Attributes& attributes);
00119     char *start_element_module_build_object(const Attributes &attributes);
00120     char *start_element_object(char *buffer, size_t len, const Attributes& attributes);
00121     char *start_element_object_prop(char *buffer, size_t len, const Attributes& attributes);
00122     char *start_element_global(char *buffer, size_t len, const Attributes& attributes);
00123     char *start_element_global_prop(char *buffer, size_t len, const Attributes& attributes);
00124     char *start_element_clock(char *buffer, size_t len, const Attributes& attributes);
00125     char *start_element_clock_prop(char *buffer, size_t len, const Attributes& attributes);
00126 
00127     char *end_element_empty(char *buffer, size_t len);
00128     char *end_element_load(char *buffer, size_t len);
00129     char *end_element_module(char *buffer, size_t len);
00130     char *end_element_module_prop(char *buffer, size_t len);
00131     char *end_element_object(char *buffer, size_t len);
00132     char *end_element_object_prop(char *buffer, size_t len);
00133     char *end_element_global(char *buffer, size_t len);
00134     char *end_element_global_prop(char *buffer, size_t len);
00135     char *end_element_clock(char *buffer, size_t len);
00136     char *end_element_clock_prop(char *buffer, size_t len);
00137 
00138     int depth;
00139 
00140     char module_name[64];
00141     /* char obj_id[64];
00142     char obj_type[64];
00143     char keyword[64]; */
00144 
00145     int first, last;
00146     bool load_ready;
00147     bool load_state;
00148     int object_count, class_count;
00149 
00150     vector<OBJECT *> obj_vect;
00151     gldStack *stack_ptr;
00152 };

GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy