casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TBParser.h
Go to the documentation of this file.
1 //# TBParser.h: Parses the XMLDriver-generated XML into data in a TBTable.
2 //# Copyright (C) 2005
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id: $
27 #ifndef TBPARSER_H_
28 #define TBPARSER_H_
29 
30 #include <xercesc/dom/DOM.hpp>
31 #include <xercesc/sax2/DefaultHandler.hpp>
32 #include <xercesc/sax2/Attributes.hpp>
33 #include <xercesc/sax2/SAX2XMLReader.hpp>
34 
36 
37 #include <casa/BasicSL/String.h>
38 
39 #include <vector>
40 #include <sstream>
41 #include <map>
42 
43 using namespace xercesc;
44 
45 namespace casa {
46 
47 //# Forward Declarations
48 class TBField;
49 class TBKeyword;
50 class XMLtoken;
51 class TableParams;
52 class TBData;
53 
54 // <summary>
55 // Parses the XMLDriver-generated XML into data in a TBTable.
56 // </summary>
57 //
58 // <synopsis>
59 // TBParser is an abstract superclass for any implementing subclass to parse
60 // a casacore::String containing XML. A TBParser keeps a reference to the table
61 // parameters so that the table data can be directly imported. Note: the
62 // TBParser is not used for the "Direct" table driver, which is the default.
63 // Currently the user is unable to selected an "XML" table driver, which means
64 // TBParsers are not used.
65 // </synopsis>
66 
67 class TBParser {
68 public:
69  // Constructor which takes a TableParams argument to store references to
70  // the table parameters.
71  TBParser(TableParams* tp);
72 
73  virtual ~TBParser();
74 
75 
76  std::vector<std::vector<casacore::String>*>* getData() { return &data; }
77 
78  // Set whether the TBParser should print debug information or not.
79  void setPrintDebug(bool pdb);
80 
81 
82  // Any subclass must implement the parse() method. Parses the given String
83  // into the table parameters and returns a Result indicating whether the
84  // parsing succeeded or not. If parsedata is true, the table data is
85  // parsed, otherwise just table meta-data like keywords is parsed.
86  virtual Result parse(casacore::String* xml, bool parsedata = true) = 0;
87 
88 protected:
89  // Is true if this table allows for the insertion of rows, false otherwise.
90  bool& insertRow;
91 
92  // Is true if this table allows for the deletion of rows, false otherwise.
93  bool& removeRow;
94 
95  // Holds the table data.
96  std::vector<std::vector<casacore::String>*> data;
97 
98  // Holds the "real" table data.
99  std::vector<std::vector<TBData*>*>& data2;
100 
101  // Holds the table fields.
102  std::vector<TBField*>& fields;
103 
104  // Holds the table keywords.
105  std::vector<TBKeyword*>& keywords;
106 
107  // Holds the list of the number of rows for each subtable.
108  std::vector<int>& subtableRows;
109 
110  // Holds the total number of rows in the table.
111  int& totalRows;
112 
113  // Holds the number of rows currently loaded in the table.
115 
116  // Is true if debug information should be printed, false otherwise.
118 };
119 
120 // <summary>
121 // TBParser subclass that uses a "home" parsing method.
122 // </summary>
123 //
124 // <synopsis>
125 // TBHomeParser is a subclass of TBParser that implements all the parsing
126 // methods itself using casacore::String methods. It is somewhat slow and its use is
127 // not recommended.
128 // </synopsis>
129 
130 class TBHomeParser : public TBParser {
131 public:
132  // Constructor that take the table parameters.
134 
135  virtual ~TBHomeParser();
136 
137 
138  // Implements TBParser::parse(). Parses the casacore::String into XMLtokens and then
139  // parses the table information from the XMLtokens.
140  Result parse(casacore::String* xml, bool parsedata = true);
141 
142 private:
143  // All parsed XMLtokens that had a tag name of TBConstants::XML_FIELD.
144  std::vector<XMLtoken*> xfields;
145 
146  // All parsed XMLtokens that had a tag name of TBConstants::XML_KEYWORD.
147  std::vector<XMLtoken*> xkeywords;
148 
149  // All parsed XMLtokens that had a tag name of TBConstants::XML_COLUMNKW.
150  std::map<casacore::String, std::vector<XMLtoken*>*> xcolkeywords;
151 
152 
153  // Recursively parses a XMLtoken from the given String. The level
154  // parameter is used to properly add tabs to the debug information.
155  XMLtoken* parseToken(casacore::String* xml, int level);
156 
157  // Parses XML attributes from the given casacore::String into the given token. The
158  // level parameter is used to properly add tabs to the debug information.
159  void parseAttributes(XMLtoken* token, casacore::String* attrPtr, int level);
160 
161  // Parses XML content (<tag>content</tag>) from the given casacore::String into the
162  // given token. The level parameter is used to properly add tabs to the
163  // debug information.
164  void parseContent(XMLtoken* token, casacore::String* contentPtr, int level);
165 
166  // Given an XMLtoken tree, parse the table information from it. If
167  // parsedata is true the table data is parsed, otherwise just the
168  // meta-information like keywords is parsed.
169  bool parseXMLtable(XMLtoken* t, bool parsedata);
170 };
171 
172 // <summary>
173 // TBParser subclass that uses a DOM parser in the XERCES library.
174 // </summary>
175 //
176 // <synopsis>
177 // TBXercesDOMParser is a subclass of TBParser that implements all the parsing
178 // methods using a XERCES DOM parser. Although the actual parsing happens
179 // quickly, deciphering table data from the parsed XML is somewhat slow and
180 // thus the use of TBXercesDOMParser is not recommended.
181 // </synopsis>
182 
183 class TBXercesDOMParser : public TBParser {
184 public:
185  // Constructor that takes the table parameters.
187 
188  virtual ~TBXercesDOMParser();
189 
190 
191  // Implements TBParser::parse(). The casacore::String is parsed into DOMElements and
192  // then the table information is parsed from the DOMElements.
193  Result parse(casacore::String* xml, bool parsedata = true);
194 
195 private:
196  // First level parsing method that takes the top-level element and
197  // parses it.
198  Result parseXML(const DOMElement* element, bool parsedata);
199 
200  // Second level parsing method that takes the TABLE element and parses
201  // the table out of it.
202  Result parseTable(const DOMElement* element, bool parsedata);
203 
204  // Third level parsing method that takes the TABLEDATA element and
205  // parses the table data out of it.
206  Result parseTableData(const DOMElement* element);
207 };
208 
209 // <summary>
210 // TBParser subclass that uses a SAX parser in the XERCES library.
211 // </summary>
212 //
213 // <synopsis>
214 // TBXercesSAXParser is a subclass of TBParser that implements all the parsing
215 // methods using a XERCES SAX parser. If XML parsing is required, the
216 // TBXercesSAXParser is recommended for its (relative) speed.
217 // TBXercesSAXParser also implements xerces::DefaultHandler since SAX uses
218 // event-driven parsing.
219 // </synopsis>
220 
221 class TBXercesSAXParser : public TBParser, public DefaultHandler {
222 public:
223  // Constructor that takes the table parameters.
225 
226  virtual ~TBXercesSAXParser();
227 
228 
229  // Implements TBParser::parse(). Parses the casacore::String into the table data
230  // serially using event-driven SAX parsing.
231  Result parse(casacore::String* xml, bool parsedata = true);
232 
233  // Implements DefaultHandler::startDocument().
234  void startDocument();
235 
236  // Implements DefaultHandler::endDocument().
237  void endDocument();
238 
239  // Implements DefaultHandler::startElement().
240  void startElement(const XMLCh* const uri, const XMLCh* const localname,
241  const XMLCh* const qname, const Attributes& attrs);
242 
243  // Implements DefaultHandler::endElement().
244  void endElement(const XMLCh* const uri, const XMLCh* const localname,
245  const XMLCh* const qname);
246 
247  // Implements DefaultHandler::characters().
248  void characters(const XMLCh* const chars, const unsigned int length);
249 
250 private:
251  // SAX reader.
252  SAX2XMLReader* reader;
253 
254  // Flag indicating whether the parsing is currently in a <TD> tag or not.
255  bool inTD;
256 
257  // The current row of table data being parsed.
258  std::vector<casacore::String>* row;
259 
260  // Keeps all non-XML or extra text.
261  std::stringstream extraText;
262 
263  // Indicates whether the parsing is valid or not.
264  bool valid;
265 
266  // Keep all parsed column keywords.
267  std::map<int, std::vector<TBKeyword*>*> colkws;
268 
269  // Is true if the table data should be parsed, false otherwise.
270  bool parsedata;
271 };
272 
273 }
274 
275 #endif /* TBPARSER_H_ */
bool & insertRow
Is true if this table allows for the insertion of rows, false otherwise.
Definition: TBParser.h:90
SAX2XMLReader * reader
SAX reader.
Definition: TBParser.h:252
Convenience class for a casacore::String/bool tuple.
Definition: TBConstants.h:93
std::map< int, std::vector< TBKeyword * > * > colkws
Keep all parsed column keywords.
Definition: TBParser.h:267
std::vector< XMLtoken * > xkeywords
All parsed XMLtokens that had a tag name of TBConstants::XML_KEYWORD.
Definition: TBParser.h:147
std::vector< casacore::String > * row
The current row of table data being parsed.
Definition: TBParser.h:258
Representation of a single XML token.
Definition: XMLtoken.h:47
ABSTRACT CLASSES Deliberately vague to be general enough to allow for many different types of data
Definition: PlotData.h:48
std::vector< TBField * > & fields
Holds the table fields.
Definition: TBParser.h:102
std::stringstream extraText
Keeps all non-XML or extra text.
Definition: TBParser.h:261
Parameters that define all table data and meta-deta.
Definition: TBTable.h:90
bool inTD
Flag indicating whether the parsing is currently in a tag or not.
Definition: TBParser.h:255
TBParser subclass that uses a DOM parser in the XERCES library.
Definition: TBParser.h:183
bool valid
Indicates whether the parsing is valid or not.
Definition: TBParser.h:264
std::vector< int > & subtableRows
Holds the list of the number of rows for each subtable.
Definition: TBParser.h:108
LatticeExprNode length(const LatticeExprNode &expr, const LatticeExprNode &axis)
2-argument function to get the length of an axis.
std::map< casacore::String, std::vector< XMLtoken * > * > xcolkeywords
All parsed XMLtokens that had a tag name of TBConstants::XML_COLUMNKW.
Definition: TBParser.h:150
int & totalRows
Holds the total number of rows in the table.
Definition: TBParser.h:111
bool printdebug
Is true if debug information should be printed, false otherwise.
Definition: TBParser.h:117
std::vector< TBKeyword * > & keywords
Holds the table keywords.
Definition: TBParser.h:105
std::vector< XMLtoken * > xfields
All parsed XMLtokens that had a tag name of TBConstants::XML_FIELD.
Definition: TBParser.h:144
std::vector< std::vector< casacore::String > * > * getData()
Definition: TBParser.h:76
std::vector< std::vector< TBData * > * > & data2
Holds the &quot;real&quot; table data.
Definition: TBParser.h:99
bool parsedata
Is true if the table data should be parsed, false otherwise.
Definition: TBParser.h:270
Parses the XMLDriver-generated XML into data in a TBTable.
Definition: TBParser.h:67
int & loadedRows
Holds the number of rows currently loaded in the table.
Definition: TBParser.h:114
String: the storage and methods of handling collections of characters.
Definition: String.h:223
TBParser subclass that uses a SAX parser in the XERCES library.
Definition: TBParser.h:221
std::vector< std::vector< casacore::String > * > data
Holds the table data.
Definition: TBParser.h:96
bool & removeRow
Is true if this table allows for the deletion of rows, false otherwise.
Definition: TBParser.h:93
TBParser subclass that uses a &quot;home&quot; parsing method.
Definition: TBParser.h:130