src/lib/GenericASN1ContentHandler.h
branchv_0
changeset 40 85b6f13f1088
parent 39 6ef41443211e
child 41 12acb6c02d32
equal deleted inserted replaced
39:6ef41443211e 40:85b6f13f1088
    20 #include <sstream>
    20 #include <sstream>
    21 #include <iomanip>
    21 #include <iomanip>
    22 
    22 
    23 #include "ASN1ContentHandler.h"
    23 #include "ASN1ContentHandler.h"
    24 #include "XMLContentHandler.h"
    24 #include "XMLContentHandler.h"
       
    25 #include "uri.h"
    25 
    26 
    26 namespace relpipe {
    27 namespace relpipe {
    27 namespace in {
    28 namespace in {
    28 namespace asn1 {
    29 namespace asn1 {
    29 namespace lib {
    30 namespace lib {
    34  * Does not need to know anything about the model/schema – works with generic events (nodes).
    35  * Does not need to know anything about the model/schema – works with generic events (nodes).
    35  */
    36  */
    36 class GenericASN1ContentHandler : public ASN1ContentHandler {
    37 class GenericASN1ContentHandler : public ASN1ContentHandler {
    37 private:
    38 private:
    38 	XMLContentHandlerProxy handlers;
    39 	XMLContentHandlerProxy handlers;
       
    40 
       
    41 	std::string rootName = "asn1";
       
    42 	bool treeWithNamespaces = false;
       
    43 
       
    44 	/**
       
    45 	 * TODO: use a common method
       
    46 	 */
       
    47 	bool parseBoolean(const std::string& value) {
       
    48 		if (value == "true") return true;
       
    49 		else if (value == "false") return false;
       
    50 		else throw std::invalid_argument(std::string("Unable to parse boolean value: ") + value + " (expecting true or false)");
       
    51 	}
    39 
    52 
    40 	std::vector<std::string> getCommonAttributes(const Header& header, std::vector<std::string> attributes = {}) {
    53 	std::vector<std::string> getCommonAttributes(const Header& header, std::vector<std::string> attributes = {}) {
    41 		std::string tag = std::to_string(header.tag);
    54 		std::string tag = std::to_string(header.tag);
    42 		std::string tagClass = std::to_string((uint64_t) header.tagClass);
    55 		std::string tagClass = std::to_string((uint64_t) header.tagClass);
    43 		std::string pc = std::to_string((uint8_t) header.pc);
    56 		std::string pc = std::to_string((uint8_t) header.pc);
    78 	}
    91 	}
    79 
    92 
    80 	virtual ~GenericASN1ContentHandler() {
    93 	virtual ~GenericASN1ContentHandler() {
    81 	}
    94 	}
    82 
    95 
       
    96 	bool setOption(const std::string& uri, const std::string& value) override {
       
    97 		if (uri == xml::RootName) rootName = value;
       
    98 		else if (uri == xml::TreeWithNamespaces) treeWithNamespaces = parseBoolean(value);
       
    99 		else if (uri == xml::TreeStyle && value == "standard"); // the only style currently supported
       
   100 		else if (uri == xml::TreeStyle && value == "literal") throw std::invalid_argument("Tree style 'literal' is not yet supported"); // will require ASN.1 schema, might be implemented in another class
       
   101 		else if (uri == xml::TreeStyle) throw std::invalid_argument("Unsupported tree-style: " + value);
       
   102 		else return false;
       
   103 
       
   104 		return true;
       
   105 	}
       
   106 
    83 	void writeStreamStart() override {
   107 	void writeStreamStart() override {
    84 		handlers.writeStartElement("asn1");
   108 		if (treeWithNamespaces) handlers.writeStartElement(rootName,{"xmlns", xml::XMLNS}); // TODO: actual namespace instead of a mere attribute
       
   109 		else handlers.writeStartElement(rootName);
    85 	}
   110 	}
    86 
   111 
    87 	void writeStreamEnd() override {
   112 	void writeStreamEnd() override {
    88 		handlers.writeEndElement();
   113 		handlers.writeEndElement();
    89 	}
   114 	}