#include //注,解析未完善,仅解析上海深圳所需!!!,喜欢大而全的可以去用xerces来解析。
namespace QuickFAST
{
namespace Codecs
{
//具体的解析函数
bool parse(SegmentBodyPtr body,pugi::xml_node &node_item);
bool parseOp(FieldInstructionPtr field,pugi::xml_node &node_item)
{
pugi::xml_node nodeop=node_item.first_child();
if(nodeop.empty())
return true;
FieldOpPtr op;
std::string opname=nodeop.name();
if(opname=="increment")
op.reset(new FieldOpIncrement());
else if(opname=="default")
op.reset(new FieldOpDefault());
else if(opname=="copy")
op.reset(new FieldOpCopy());
else if(opname=="delta")
op.reset(new FieldOpDelta());
else if(opname=="constant")
{
op.reset(new FieldOpConstant());
}
else
{
return true;
}
std::string dictionary=nodeop.attribute("dictionary").value();
if(!dictionary.empty())
{
op->setDictionaryName(dictionary);
}
std::string key=nodeop.attribute("key").value();;
if(!key.empty())
{
op->setKey(key);
std::string nsKey=nodeop.attribute("nskey").value();
if(!nsKey.empty())
{
op->setKeyNamespace(nsKey);
}
}
std::string value=nodeop.attribute("value").value();
if(!value.empty())
{
op->setValue(value);
}
std::string pmapBitStr=nodeop.attribute("pmap").value();
if(!pmapBitStr.empty())
{
size_t pmapBit = atoi(pmapBitStr.c_str());
op->setPMapBit(pmapBit);
}
field->setFieldOp(op);
return true;
}
bool parseLength(std::string &tag,SegmentBodyPtr body,pugi::xml_node &node_item)
{
std::string name=node_item.attribute("name").value();
std::string ns=node_item.attribute("ns").value();
FieldInstructionPtr field(new FieldInstructionUInt32(name, ns));
std::string id=node_item.attribute("id").value();
if (!id.empty())
{
field->setId(atoi(id.c_str()));
}
std::string presence=node_item.attribute("presence").value();
if(!presence.empty())
{
field->setPresence(presence == "mandatory");
}
parseOp(field,node_item);
body->allowLengthField();
body->addLengthInstruction(field);
return true;
}
bool parseInt8(std::string &tag,SegmentBodyPtr body,pugi::xml_node &node_item)
{
std::string name=node_item.attribute("name").value();
std::string ns=node_item.attribute("ns").value();
FieldInstructionPtr field(new FieldInstructionInt8(name, ns));
std::string id=node_item.attribute("id").value();
if (!id.empty())
{
field->setId(atoi(id.c_str()));
}
std::string presence=node_item.attribute("presence").value();
if(!presence.empty())
{
field->setPresence(presence == "mandatory");
}
parseOp(field,node_item);
body->addInstruction(field);
return true;
}
bool parseUInt8(std::string &tag,SegmentBodyPtr body,pugi::xml_node &node_item)
{
std::string name=node_item.attribute("name").value();
std::string ns=node_item.attribute("ns").value();
FieldInstructionPtr field(new FieldInstructionUInt8(name, ns));
std::string id=node_item.attribute("id").value();
if (!id.empty())
{
field->setId(atoi(id.c_str()));
}
std::string presence=node_item.attribute("presence").value();
if(!presence.empty())
{
field->setPresence(presence == "mandatory");
}
parseOp(field,node_item);
body->addInstruction(field);
return true;
}
//16
bool parseInt16(std::string &tag,SegmentBodyPtr body,pugi::xml_node &node_item)
{
std::string name=node_item.attribute("name").value();
std::string ns=node_item.attribute("ns").value();
FieldInstructionPtr field(new FieldInstructionInt16(name, ns));
std::string id=node_item.attribute("id").value();
if (!id.empty())
{
field->setId(atoi(id.c_str()));
}
std::string presence=node_item.attribute("presence").value();
if(!presence.empty())
{
field->setPresence(presence == "mandatory");
}
parseOp(field,node_item);
body->addInstruction(field);
return true;
}
bool parseUInt16(std::string &tag,SegmentBodyPtr body,pugi::xml_node &node_item)
{
std::string name=node_item.attribute("name").value();
std::string ns=node_item.attribute("ns").value();
FieldInstructionPtr field(new FieldInstructionUInt16(name, ns));
std::string id=node_item.attribute("id").value();
if (!id.empty())
{
field->setId(atoi(id.c_str()));
}
std::string presence=node_item.attribute("presence").value();
if(!presence.empty())
{
field->setPresence(presence == "mandatory");
}
parseOp(field,node_item);
body->addInstruction(field);
return true;
}
//32
bool parseInt32(std::string &tag,SegmentBodyPtr body,pugi::xml_node &node_item)
{
std::string name=node_item.attribute("name").value();
std::string ns=node_item.attribute("ns").value();
FieldInstructionPtr field(new FieldInstructionInt32(name, ns));
std::string id=node_item.attribute("id").value();
if (!id.empty())
{
field->setId(atoi(id.c_str()));
}
std::string presence=node_item.attribute("presence").value();
if(!presence.empty())
{
field->setPresence(presence == "mandatory");
}
parseOp(field,node_item);
body->addInstruction(field);
return true;
}
bool parseUInt32(std::string &tag,SegmentBodyPtr body,pugi::xml_node &node_item)
{
std::string name=node_item.attribute("name").value();
std::string ns=node_item.attribute("ns").value();
FieldInstructionPtr field(new FieldInstructionUInt32(name, ns));
std::string id=node_item.attribute("id").value();
if (!id.empty())
{
field->setId(atoi(id.c_str()));
}
std::string presence=node_item.attribute("presence").value();
if(!presence.empty())
{
field->setPresence(presence == "mandatory");
}
parseOp(field,node_item);
body->addInstruction(field);
return true;
}
//64
bool parseInt64(std::string &tag,SegmentBodyPtr body,pugi::xml_node &node_item)
{
std::string name=node_item.attribute("name").value();
std::string ns=node_item.attribute("ns").value();
FieldInstructionPtr field(new FieldInstructionInt64(name, ns));
std::string id=node_item.attribute("id").value();
if (!id.empty())
{
field->setId(atoi(id.c_str()));
}
std::string presence=node_item.attribute("presence").value();
if(!presence.empty())
{
field->setPresence(presence == "mandatory");
}
parseOp(field,node_item);
body->addInstruction(field);
return true;
}
bool parseUInt64(std::string &tag,SegmentBodyPtr body,pugi::xml_node &node_item)
{
std::string name=node_item.attribute("name").value();
std::string ns=node_item.attribute("ns").value();
FieldInstructionPtr field(new FieldInstructionUInt64(name, ns));
std::string id=node_item.attribute("id").value();
if (!id.empty())
{
field->setId(atoi(id.c_str()));
}
std::string presence=node_item.attribute("presence").value();
if(!presence.empty())
{
field->setPresence(presence == "mandatory");
}
parseOp(field,node_item);
body->addInstruction(field);
return true;
}
bool parseDecimal(std::string &tag,SegmentBodyPtr body,pugi::xml_node &node_item)
{
std::string name=node_item.attribute("name").value();
std::string ns=node_item.attribute("ns").value();
FieldInstructionPtr field(new FieldInstructionDecimal(name, ns));
std::string id=node_item.attribute("id").value();
if (!id.empty())
{
field->setId(atoi(id.c_str()));
}
std::string presence=node_item.attribute("presence").value();
if(!presence.empty())
{
field->setPresence(presence == "mandatory");
}
parseOp(field,node_item);
body->addInstruction(field);
return true;
}
bool parseString(std::string &tag,SegmentBodyPtr body,pugi::xml_node &node_item)
{
std::string name=node_item.attribute("name").value();
std::string ns=node_item.attribute("ns").value();
std::string charset = "ascii";
if(!node_item.attribute("charset").empty())
charset=node_item.attribute("charset").value();
FieldInstructionPtr field;
if(charset == "unicode")
{
field.reset(new FieldInstructionUtf8(name, ns));
}
else
{
field.reset(new FieldInstructionAscii(name, ns));
}
std::string id=node_item.attribute("id").value();
if (!id.empty())
{
field->setId(atoi(id.c_str()));
}
std::string presence=node_item.attribute("presence").value();
if(!presence.empty())
{
field->setPresence(presence == "mandatory");
}
parseOp(field,node_item);
body->addInstruction(field);
return true;
}
bool parseSequence(std::string &tag,SegmentBodyPtr body,pugi::xml_node &node_item)
{
std::string name=node_item.attribute("name").value();
std::string ns=node_item.attribute("ns").value();
FieldInstructionPtr field(new FieldInstructionSequence(name, ns));
std::string id=node_item.attribute("id").value();
if (!id.empty())
{
field->setId(atoi(id.c_str()));
}
std::string presence=node_item.attribute("presence").value();
bool mandatory = false;
if(!presence.empty())
{
mandatory = (presence == "mandatory");
field->setPresence(mandatory);
}
std::string dictionary=node_item.attribute("dictionary").value();
if(!dictionary.empty())
{
field->setDictionaryName(dictionary);
}
SegmentBodyPtr seqbody(new SegmentBody);
field->setSegmentBody(seqbody);
for(pugi::xml_node node_subitem=node_item.first_child();!node_subitem.empty();node_subitem=node_subitem.next_sibling())
{
//parse_template(node_item,templateRegistry);
std::string itemname=node_subitem.name();
parse(seqbody,node_subitem);
int xxx=1;
}
seqbody->allowLengthField();
seqbody->setMandatoryLength(mandatory);
body->addInstruction(field);
return true;
}
bool parseGroup(std::string &tag,SegmentBodyPtr body,pugi::xml_node &node_item)
{
std::string name=node_item.attribute("name").value();
std::string ns=node_item.attribute("ns").value();
FieldInstructionPtr field(new FieldInstructionGroup(name, ns));
std::string id=node_item.attribute("id").value();
if (!id.empty())
{
field->setId(atoi(id.c_str()));
}
std::string presence=node_item.attribute("presence").value();
bool mandatory = false;
if(!presence.empty())
{
mandatory = (presence == "mandatory");
field->setPresence(mandatory);
}
std::string dictionary=node_item.attribute("dictionary").value();
if(!dictionary.empty())
{
field->setDictionaryName(dictionary);
}
SegmentBodyPtr seqbody(new SegmentBody);
field->setSegmentBody(seqbody);
for(pugi::xml_node node_subitem=node_item.first_child();!node_subitem.empty();node_subitem=node_subitem.next_sibling())
{
//parse_template(node_item,templateRegistry);
std::string itemname=node_subitem.name();
parse(seqbody,node_subitem);
int xxx=1;
}
seqbody->allowLengthField();
seqbody->setMandatoryLength(mandatory);
body->addInstruction(field);
return true;
}
bool parse(SegmentBodyPtr body,pugi::xml_node &node_item)
{
//解析子函数
std::string tag=node_item.name();
if (tag == "int8")
{
parseInt8(tag, body,node_item);
}
else if (tag == "uInt8")
{
parseUInt8(tag, body,node_item);
}
else if (tag == "int16")
{
parseInt16(tag, body,node_item);
}
else if (tag == "uInt16")
{
parseUInt16(tag, body,node_item);
}
else if (tag == "int32")
{
parseInt32(tag, body,node_item);
}
else if (tag == "uInt32")
{
parseUInt32(tag, body,node_item);
}
else if (tag == "int64")
{
parseInt64(tag, body,node_item);
}
else if (tag == "uInt64")
{
parseUInt64(tag, body,node_item);
}
else if (tag == "decimal")
{
parseDecimal(tag, body,node_item);
}
else if (tag == "string")
{
parseString(tag, body,node_item);
}
else if (tag == "sequence")
{
parseSequence(tag, body,node_item);
}
else if(tag=="group")
{
parseGroup(tag,body,node_item);
}
else if(tag=="length")
{
parseLength(tag,body,node_item);
}
else
{
int xxsd=1;
}
return true;
}
bool parse_template(pugi::xml_node &node_template,TemplateRegistryPtr registry_)
{
TemplatePtr target(new Template);
target->setTemplateName(node_template.attribute("name").value());
std::string ns=node_template.attribute("ns").value();
if(!ns.empty())
{
target->setNamespace(ns);
}
std::string templateNs=node_template.attribute("templateNs").value();
if(!templateNs.empty())
{
target->setTemplateNamespace(templateNs);
}
std::string id=node_template.attribute("id").value();
if (!id.empty())
{
target->setId(
boost::lexical_cast(id)
);
}
std::string dictionary=node_template.attribute("dictionary").value();
if (!dictionary.empty())
{
target->setDictionaryName(dictionary);
}
bool ignore=node_template.attribute("ignore").as_bool();
target->setIgnore(ignore);
//解析具体的字段
for(pugi::xml_node node_item=node_template.first_child();!node_item.empty();node_item=node_item.next_sibling())
{
//parse_template(node_item,templateRegistry);
std::string itemname=node_item.name();
parse(target,node_item);
int xxx=1;
}
registry_->addTemplate(target);
return true;
}
TemplateRegistryPtr parse_template_file(const char *file)
{
pugi::xml_document doc;
if(!doc.load_file(file))
{
throw 0;
}
pugi::xml_node root_node=doc.document_element();
if(root_node.empty())
{
throw 1;
}
TemplateRegistryPtr templateRegistry(
new TemplateRegistry
);
for(pugi::xml_node node_template=root_node.first_child();!node_template.empty();node_template=node_template.next_sibling())
{
parse_template(node_template,templateRegistry);
}
templateRegistry->finalize();
return templateRegistry;
}
}
}