33 #include "Constructor.h"
38 #include <AttrTable.h>
40 #include "BESDapResponse.h"
41 #include "BESDataDDSResponse.h"
42 #include "BESDDSResponse.h"
44 #include "BESInternalError.h"
47 #include "NCMLDebug.h"
54 const std::string NCMLUtil::WHITESPACE =
" \t\n";
56 int NCMLUtil::tokenize(
const string& str, vector<string>& tokens,
const string& delimiters)
58 BESDEBUG(
"ncml",
"NCMLUtil::tokenize value of str:" << str << endl);
63 string::size_type lastPos = str.find_first_not_of(delimiters, 0);
65 string::size_type pos = str.find_first_of(delimiters, lastPos);
68 while (string::npos != pos || string::npos != lastPos) {
70 tokens.push_back(str.substr(lastPos, pos - lastPos));
73 lastPos = str.find_first_not_of(delimiters, pos);
75 pos = str.find_first_of(delimiters, lastPos);
80 int NCMLUtil::tokenizeChars(
const string& str, vector<string>& tokens)
84 for (
unsigned int i = 0; i < str.size(); ++i) {
87 tokens.push_back(val);
92 bool NCMLUtil::isAscii(
const string& str)
94 string::const_iterator endIt = str.end();
95 for (string::const_iterator it = str.begin(); it != endIt; ++it) {
103 bool NCMLUtil::isAllWhitespace(
const string& str)
105 return (str.find_first_not_of(
" \t\n") == string::npos);
108 void NCMLUtil::trimLeft(std::string& input,
const std::string& trimChars )
110 size_t firstValid = input.find_first_not_of(trimChars);
111 input.erase(0, firstValid);
117 void NCMLUtil::trimRight(std::string& input,
const std::string& trimChars )
119 size_t lastValid = input.find_last_not_of(trimChars);
120 if (lastValid != string::npos) {
121 input.erase(lastValid + 1, string::npos);
125 void NCMLUtil::trimAll(std::vector<std::string>& tokens,
const std::string& trimChars )
127 unsigned int num = tokens.size();
128 for (
unsigned int i = 0; i < num; ++i) {
129 trim(tokens[i], trimChars);
133 bool NCMLUtil::toUnsignedInt(
const std::string& stringVal,
unsigned int& oVal)
137 istringstream iss(stringVal);
139 if (iss.fail() || (stringVal[0] ==
'-')
158 has_dap2_attributes(AttrTable &a)
160 for (AttrTable::Attr_iter i = a.attr_begin(), e = a.attr_end(); i != e; ++i) {
161 if (a.get_attr_type(i) != Attr_container) {
164 else if (has_dap2_attributes(*a.get_attr_table(i))) {
182 has_dap2_attributes(BaseType *btp)
184 if (btp->get_attr_table().get_size() && has_dap2_attributes(btp->get_attr_table())) {
188 Constructor *cons =
dynamic_cast<Constructor *
>(btp);
190 Grid* grid =
dynamic_cast<Grid*
>(btp);
192 return has_dap2_attributes(grid->get_array());
195 for (Constructor::Vars_iter i = cons->var_begin(), e = cons->var_end(); i != e; i++) {
196 if (has_dap2_attributes(*i))
return true;
209 static void populateAttrTableForContainerVariableRecursive(AttrTable* dasTable, Constructor* consVar)
214 if(!has_dap2_attributes(consVar))
218 Grid* grid =
dynamic_cast<Grid*
>(consVar);
223 BESDEBUG(
"ncml", __func__ <<
"() The variable " << grid->name() <<
" is a Grid. So, we promote the Grid Array AttrTable content to the DAS container for Grid " << grid->name() << endl);
224 Array *gArray = grid->get_array();
225 AttrTable arrayAT = gArray->get_attr_table();
226 for( AttrTable::Attr_iter atIter = arrayAT.attr_begin(); atIter!=arrayAT.attr_end(); ++atIter){
227 AttrType type = arrayAT.get_attr_type(atIter);
228 string childName = arrayAT.get_name(atIter);
229 if (type == Attr_container){
230 BESDEBUG(
"ncml", __func__ <<
"() Adding child AttrTable '" << childName <<
"' to Grid " << grid->name() << endl);
231 dasTable->append_container(
new AttrTable(*arrayAT.get_attr_table(atIter)), childName);
234 vector<string>* pAttrTokens = arrayAT.get_attr_vector(atIter);
236 BESDEBUG(
"ncml", __func__ <<
"() Adding child Attrbute '" << childName <<
"' to Grid " << grid->name() << endl);
237 dasTable->append_attr(childName, AttrType_to_String(type), pAttrTokens);
243 BESDEBUG(
"ncml", __func__ <<
"() Adding attribute tables for children of a Constructor type variable " << consVar->name() << endl);
244 Constructor::Vars_iter endIt = consVar->var_end();
245 for (Constructor::Vars_iter it = consVar->var_begin(); it != endIt; ++it) {
249 if(has_dap2_attributes(var)){
250 BESDEBUG(
"ncml", __func__ <<
"() Adding attribute table for var: " << var->name() << endl);
252 AttrTable* newTable =
new AttrTable(var->get_attr_table());
254 dasTable->append_container(newTable, var->name());
257 if (var->is_constructor_type()) {
258 Constructor* child =
dynamic_cast<Constructor*
>(var);
262 BESDEBUG(
"ncml", __func__ <<
"() Var " << child->name() <<
" is Constructor type, recursively adding attribute tables" << endl);
263 populateAttrTableForContainerVariableRecursive(newTable, child);
267 BESDEBUG(
"ncml", __func__ <<
"() Variable '" << var->name() <<
"' has no dap2 attributes,. Skipping."<< endl);
274 void NCMLUtil::populateDASFromDDS(DAS* das,
const DDS& dds_const)
276 BESDEBUG(
"ncml",
"Populating a DAS from a DDS...." << endl);
284 DDS& dds =
const_cast<DDS&
>(dds_const);
288 if (dds.container()) {
289 BESDEBUG(
"ncml", __func__ <<
"() Got unexpected container " << dds.container_name() <<
" and is failing." << endl);
290 throw BESInternalError(
"Unexpected Container Error creating DAS from DDS in NCMLHandler", __FILE__, __LINE__);
295 *(das->get_top_level_attributes()) = dds.get_attr_table();
300 DDS::Vars_iter endIt = dds.var_end();
301 for (DDS::Vars_iter it = dds.var_begin(); it != endIt; ++it) {
307 if(has_dap2_attributes(var)){
308 BESDEBUG(
"ncml",
"Adding attribute table for variable: " << var->name() << endl);
309 AttrTable* clonedVarTable =
new AttrTable(var->get_attr_table());
310 VALID_PTR(clonedVarTable);
311 das->add_table(var->name(), clonedVarTable);
314 if (var->is_constructor_type()) {
315 Constructor* consVar =
dynamic_cast<Constructor*
>(var);
319 populateAttrTableForContainerVariableRecursive(clonedVarTable, consVar);
323 BESDEBUG(
"ncml", __func__ <<
"() Variable '" << var->name() <<
"' has no dap2 attributes,. Skipping."<< endl);
330 void NCMLUtil::copyVariablesAndAttributesInto(DDS* dds_out,
const DDS& dds_in)
335 if (dds_out == &dds_in) {
340 DDS& dds =
const_cast<DDS&
>(dds_in);
343 dds_out->get_attr_table() = dds.get_attr_table();
348 for (DDS::Vars_iter i = dds.var_begin(); i != dds.var_end(); ++i) {
349 dds_out->add_var(*i);
361 pDDS = pDDXResponse->
get_dds();
363 else if (pDataDDSResponse) {
364 pDDS = pDataDDSResponse->get_dds();
384 void NCMLUtil::hackGlobalAttributesForDAP2(libdap::AttrTable &global_attributes,
385 const std::string &global_container_name)
387 if (global_container_name.empty())
return;
396 bool simple_attribute_found =
false;
397 AttrTable::Attr_iter i = global_attributes.attr_begin();
398 while (!simple_attribute_found && i != global_attributes.attr_end()) {
399 if (!global_attributes.is_container(i)) simple_attribute_found =
true;
404 if (!simple_attribute_found)
return;
407 bool only_simple_attributes =
true;
408 i = global_attributes.attr_begin();
409 while (only_simple_attributes && i != global_attributes.attr_end()) {
410 if (global_attributes.is_container(i))
411 only_simple_attributes =
false;
419 if (only_simple_attributes)
421 AttrTable *new_global_attr_container =
new AttrTable();
422 AttrTable *new_attr_container = new_global_attr_container->append_container(global_container_name);
423 *new_attr_container = global_attributes;
424 global_attributes = *new_global_attr_container;
430 AttrTable *new_attr_container = global_attributes.find_container(global_container_name);
431 if (!new_attr_container) new_attr_container = global_attributes.append_container(global_container_name);
434 i = global_attributes.attr_begin();
435 while (i != global_attributes.attr_end()) {
436 if (!global_attributes.is_container(i)) {
437 new_attr_container->append_attr(global_attributes.get_name(i), global_attributes.get_type(i),
438 global_attributes.get_attr_vector(i));
445 i = global_attributes.attr_begin();
446 while (i != global_attributes.attr_end()) {
447 if (!global_attributes.is_container(i)) {
448 global_attributes.del_attr(global_attributes.get_name(i));
450 i = global_attributes.attr_begin();
460 void NCMLUtil::setVariableNameProperly(libdap::BaseType* pVar,
const std::string& name)
463 pVar->set_name(name);
465 BaseType* pTemplate = pVar->var();
467 pTemplate->set_name(name);
Holds a DDS object within the BES.
Represents an OPeNDAP DAP response object within the BES.
Represents an OPeNDAP DataDDS DAP2 data object within the BES.
exception thrown if internal error encountered
NcML Parser for adding/modifying/removing metadata (attributes) to existing local datasets using NcML...