void Bank::load( const string& path, bool append ) { if( path.empty() ) return; if( append == false ) { clear(); path_ = path; } try { Document doc = Document( path ); doc.LoadFile(); Element* bankElement = doc.FirstChildElement( "Bank", true ); if( append == false ) { bankElement->GetAttribute( "name", &name_ ); bankElement->GetAttribute( "program", &programNum_ ); } Iterator< Element > it( "Program" ); for( it = it.begin( bankElement ); it != it.end(); it++ ) { Element* programElement = it.Get(); Program* program = new Program(); try { readProgram( programElement, program ); push_back( program ); } catch( const exception& e ) { // parse error, skip program TRACE( e.what() ); } } if( programNum_ >= (INT32)size() ) programNum_ = 0; if( append == false && programNum_ < (INT32)size() ) // create current program currentProgram_ = *at( programNum_ ); } catch( const exception& e ) // file error, use default program { TRACE( e.what() ); newBank( "New Bank.nexus", "New Bank", false ); } }
void XmlParser::parseXmlString( std::string &xml, RadioRepresentation &radio) { try{ Document doc; doc.Parse(xml); //Pull out the root element Element* head = doc.FirstChildElement(); string headValue = head->Value(); if( headValue == "softwareradio") { readSoftwareRadio(*head, radio); }else{ throw XmlParsingException("The top element of the xml configuration must be softwareradio."); } //Instruct the radio description to build a graph of the radio radio.buildGraphs(); } catch( Exception& ex ) { throw XmlParsingException(ex.what()); } }
void Bank::readProgram( const Document& doc, Program* program ) { Element* programElement = doc.FirstChildElement( "Program", true ); readProgram( programElement, program ); }
tsdb::RecordParser* record_parser_from_xml(const std::string parse_instruction_filename, tsdb::Timeseries* out_ts) { using namespace std; using namespace ticpp; Document doc = Document(parse_instruction_filename); doc.LoadFile(); cout << "Loaded '" << parse_instruction_filename << "'." << endl; cout << "Creating parser..." << endl; /* Create the RecordParser */ tsdb::RecordParser* recordparser = new tsdb::RecordParser(); recordparser->setRecordStructure(out_ts->structure().get()); Iterator<Element> child; string name; string value; auto_ptr<Node> delimparser; for(child = child.begin(doc.FirstChildElement()); child != child.end(); child++) { child->GetValue(&value); if(value == "delimparser") { delimparser = child->Clone(); break; } } string delim = delimparser->ToElement()->GetAttribute("field_delim"); if(delim == "") { delim = ","; } string escape = delimparser->ToElement()->GetAttribute("escape_chars"); if(escape == "") { escape="\\"; } string quote = delimparser->ToElement()->GetAttribute("quote_chars"); if(quote == "") { quote="\"'"; } string mode = delimparser->ToElement()->GetAttribute("parse_mode"); if(mode == "extended") { recordparser->setSimpleParse(false); recordparser->setDelimiter(delim); recordparser->setEscapeCharacter(escape); recordparser->setQuoteCharacter(quote); cout << " - field delimiter(s): '" << delim << "'" << endl; cout << " - quote character(s): '" << quote << "'" << endl; cout << " - escape character(s): '" << escape << "'" << endl; } else { recordparser->setSimpleParse(true); recordparser->setDelimiter(delim.substr(0,1)); cout << " - field delimiter: '" << delim.substr(0,1) << "'" << endl; } cout << " Processing parser elements:" << endl; /* Loop through the RecordParser and look for TokenFilters or FieldParsers */ typedef boost::tokenizer< boost::escaped_list_separator<char> > Tokenizer; string tokens,comparison,format_string,type,missing_token_replacement; bool missing_tokens_ok = false; vector<string> vec; vector<size_t> apply_to_tokens; size_t i; for(child = child.begin(delimparser.get()); child != child.end(); child++) { child->GetValue(&value); if(value == "tokenfilter") { tokens = child->GetAttribute("tokens"); comparison = child->GetAttribute("comparison"); value = child->GetAttribute("value"); Tokenizer tok(tokens); vec.assign(tok.begin(),tok.end()); apply_to_tokens.clear(); for(i=0;i<vec.size();i++) { apply_to_tokens.push_back(atoi(vec.at(i).c_str())); } /* Write out some information */ cout << " - TokenFilter:" << endl; cout << " apply to tokens: ("; for(i=0;i<apply_to_tokens.size();) { cout << apply_to_tokens.at(i); i++; if(i<apply_to_tokens.size()) { cout << ","; } } cout << ")" << endl; if(comparison == "NE") { recordparser->addTokenFilter(new tsdb::TokenFilter(apply_to_tokens, tsdb::TokenFilter::NOT_EQUAL_TO, value)); cout << " comparison: NOT_EQUAL_TO" << endl; } else if(comparison == "EQ") { recordparser->addTokenFilter(new tsdb::TokenFilter(apply_to_tokens, tsdb::TokenFilter::EQUAL_TO, value)); cout << " comparison: EQUAL_TO" << endl; } else { cout << " comparison not recognized!" << endl; throw(runtime_error("comparison operator in TokenFilter not recognized")); } cout << " value: '" << value << "'" << endl; } else if(value == "fieldparser") { tokens = child->GetAttribute("tokens"); missing_tokens_ok=false; Tokenizer tok(tokens); vec.assign(tok.begin(),tok.end()); apply_to_tokens.clear(); for(i=0;i<vec.size();i++) { apply_to_tokens.push_back(atoi(vec.at(i).c_str())); } /* Write out some information */ cout << " - FieldParser:" << endl; cout << " apply to tokens: ("; for(i=0;i<apply_to_tokens.size();) { cout << apply_to_tokens.at(i); i++; if(i<apply_to_tokens.size()) { cout << ","; } } cout << ")" << endl; name = child->GetAttribute("name"); format_string = child->GetAttribute("format_string"); type = child->GetAttribute("type"); if(child->HasAttribute("missing_token_replacement")) { missing_token_replacement = child->GetAttribute("missing_token_replacement"); missing_tokens_ok = true; } if(type == "timestamp") { recordparser->addFieldParser(new tsdb::TimestampFieldParser(apply_to_tokens, format_string, name)); cout << " type: Timestamp" << endl; cout << " format string: '" << format_string << "'" << endl; } else if(type == "string") { recordparser->addFieldParser(new tsdb::StringFieldParser(apply_to_tokens, name)); cout << " type: String" << endl; } else if(type == "int32") { recordparser->addFieldParser(new tsdb::Int32FieldParser(apply_to_tokens.at(0), name)); cout << " type: Int32" << endl; } else if(type == "int8") { recordparser->addFieldParser(new tsdb::Int8FieldParser(apply_to_tokens.at(0), name)); cout << " type: Int8" << endl; } else if(type == "double") { recordparser->addFieldParser(new tsdb::DoubleFieldParser(apply_to_tokens.at(0), name)); cout << " type: Double" << endl; } else if(type == "char") { recordparser->addFieldParser(new tsdb::CharFieldParser(apply_to_tokens.at(0), name)); cout << " type: Char" << endl; } else { cout << " type: not recognized!" << endl; throw(runtime_error("type in FieldParser not recognized")); } if(missing_tokens_ok) { recordparser->fieldParsers().back()->setMissingTokenReplacement(missing_token_replacement); cout << " missing_token_replacement: '" << missing_token_replacement << "'" << endl; } cout << " name: '" << name << "'" << endl; } } return recordparser; cout << " finished processing parser elements." << endl; }