DDXParser.h

Go to the documentation of this file.
00001 
00002 // -*- mode: c++; c-basic-offset:4 -*-
00003 
00004 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
00005 // Access Protocol.
00006 
00007 // Copyright (c) 2003 OPeNDAP, Inc.
00008 // Author: James Gallagher <jgallagher@opendap.org>
00009 //
00010 // This library is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU Lesser General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2.1 of the License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 //
00024 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
00025 
00026 #ifndef ddx_parser_h
00027 #define ddx_parser_h
00028 
00029 #include <string>
00030 #include <map>
00031 #include <stack>
00032 
00033 #include <libxml/parserInternals.h>
00034 
00035 #ifndef ddx_exceptions_h
00036 #include "DDXExceptions.h"
00037 #endif
00038 
00039 #ifndef _dds_h
00040 #include "DDS.h"
00041 #endif
00042 
00043 #ifndef _basetype_h
00044 #include "BaseType.h"
00045 #endif
00046 
00047 #ifndef base_type_factory_h
00048 #include "BaseTypeFactory.h"
00049 #endif
00050 
00076 class DDXParser
00077 {
00078 private:
00081     enum ParseState {
00082         parser_start,
00083 
00084         inside_dataset,
00085 
00086         inside_attribute_container,
00087         inside_attribute,
00088         inside_attribute_value,
00089 
00090         inside_alias,
00091 
00092         // This covers Byte, ..., Url.
00093         inside_simple_type,
00094 
00095         inside_array,
00096         inside_dimension,
00097 
00098         inside_grid,
00099         inside_map,
00100 
00101         inside_structure,
00102         inside_sequence,
00103 
00104         inside_blob_href,
00105 
00106         parser_unknown,
00107         parser_error
00108     };
00109 
00110     BaseTypeFactory *d_factory;
00111 
00112     // These stacks hold the state of the parse as it progresses.
00113     stack<ParseState> s; // Current parse state
00114     stack<BaseType*> bt_stack; // current variable(s)
00115     stack<AttrTable*> at_stack; // current attribute table
00116 
00117     // These are used for processing errors.
00118     string error_msg;  // Error message(s), if any.
00119     xmlParserCtxtPtr ctxt; // used for error msg line numbers
00120 
00121     // The results of the parse operation are stored in these fields.
00122     DDS *dds;   // dump DDX here
00123     string *blob_href;  // put href to blob here
00124 
00125     // These hold temporary values read during the parse.
00126     string dods_attr_name; // DAP2 attributes, not XML attributes
00127     string dods_attr_type; // ... not XML ...
00128     string char_data;  // char data in value elements; null after use
00129     map<string, string> attributes; // dump XML attributes here
00130 
00131     // These are kind of silly...
00132     void set_state(DDXParser::ParseState state);
00133     DDXParser::ParseState get_state() const;
00134     void pop_state();
00135 
00136     // Glue for the BaseTypeFactory class.
00137     BaseType *factory(Type t, const string &name);
00138 
00139     // Common cleanup code for intern() and intern_stream()
00140     void cleanup_parse(xmlParserCtxtPtr &context) const;
00141 
00148     void transfer_attrs(const char **attrs);
00149     bool check_required_attribute(const string &attr);
00150 
00151     void process_attribute_element(const char **attrs);
00152     void process_attribute_alias(const char **attrs);
00153 
00154     void process_variable(Type t, ParseState s, const char **attrs);
00155 
00156     void process_dimension(const char **attrs);
00157     void process_blob(const char **attrs);
00158 
00159     bool is_attribute_or_alias(const char *name, const char **attrs);
00160     bool is_variable(const char *name, const char **attrs);
00161 
00162     void finish_variable(const char *tag, Type t, const char *expected);
00164 
00166     DDXParser()
00167     {}
00168 
00169 public:
00170     DDXParser(BaseTypeFactory *factory) : d_factory(factory)
00171     {}
00172 
00173     void intern(const string &document, DDS *dest_dds, string *blob);
00174     void intern_stream(FILE *in, DDS *dds, string *blob);
00175 
00176     static void ddx_start_document(DDXParser *parser);
00177     static void ddx_end_document(DDXParser *parser);
00178     static void ddx_start_element(DDXParser *parser, const char *name,
00179                                   const char **attrs);
00180     static void ddx_end_element(DDXParser *parser, const char *name);
00181     static void characters(DDXParser *parser, const xmlChar *ch, int len);
00182     static xmlEntityPtr ddx_get_entity(DDXParser *parser,
00183                                        const xmlChar *name);
00184     static void ddx_fatal_error(DDXParser *parser, const char *msg, ...);
00185 };
00186 
00187 #endif // ddx_parser_h

Generated on Wed Jun 27 12:56:39 2007 for libdap++ by  doxygen 1.4.7