src/lib/GenericASN1ContentHandler.h
branchv_0
changeset 41 12acb6c02d32
parent 40 85b6f13f1088
equal deleted inserted replaced
40:85b6f13f1088 41:12acb6c02d32
    37 class GenericASN1ContentHandler : public ASN1ContentHandler {
    37 class GenericASN1ContentHandler : public ASN1ContentHandler {
    38 private:
    38 private:
    39 	XMLContentHandlerProxy handlers;
    39 	XMLContentHandlerProxy handlers;
    40 
    40 
    41 	std::string rootName = "asn1";
    41 	std::string rootName = "asn1";
       
    42 	std::string bitStringSymbol0 = "0";
       
    43 	std::string bitStringSymbol1 = "1";
    42 	bool treeWithNamespaces = false;
    44 	bool treeWithNamespaces = false;
    43 
    45 
    44 	/**
    46 	/**
    45 	 * TODO: use a common method
    47 	 * TODO: use a common method
    46 	 */
    48 	 */
    96 	bool setOption(const std::string& uri, const std::string& value) override {
    98 	bool setOption(const std::string& uri, const std::string& value) override {
    97 		if (uri == xml::RootName) rootName = value;
    99 		if (uri == xml::RootName) rootName = value;
    98 		else if (uri == xml::TreeWithNamespaces) treeWithNamespaces = parseBoolean(value);
   100 		else if (uri == xml::TreeWithNamespaces) treeWithNamespaces = parseBoolean(value);
    99 		else if (uri == xml::TreeStyle && value == "standard"); // the only style currently supported
   101 		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
   102 		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
       
   103 		else if (uri == xml::BitStringSymbol0) bitStringSymbol0 = value;
       
   104 		else if (uri == xml::BitStringSymbol1) bitStringSymbol1 = value;
   101 		else if (uri == xml::TreeStyle) throw std::invalid_argument("Unsupported tree-style: " + value);
   105 		else if (uri == xml::TreeStyle) throw std::invalid_argument("Unsupported tree-style: " + value);
   102 		else return false;
   106 		else return false;
   103 
   107 
   104 		return true;
   108 		return true;
   105 	}
   109 	}
   163 		handlers.writeEndElement();
   167 		handlers.writeEndElement();
   164 	}
   168 	}
   165 
   169 
   166 	void writeBitString(const Header& header, std::vector<bool> value) override {
   170 	void writeBitString(const Header& header, std::vector<bool> value) override {
   167 		std::stringstream bits;
   171 		std::stringstream bits;
   168 		for (bool b : value) bits << (int) b;
   172 		for (bool b : value) bits << (b ? bitStringSymbol1 : bitStringSymbol0);
   169 		// for (bool b : value) bits << (b ? ':' : '.'); // TODO: configurable true/false symbols?
       
   170 		handlers.writeStartElement("bit-string", getCommonAttributes(header,{"length", std::to_string(value.size())}));
   173 		handlers.writeStartElement("bit-string", getCommonAttributes(header,{"length", std::to_string(value.size())}));
   171 		handlers.writeCharacters(bits.str());
   174 		handlers.writeCharacters(bits.str());
   172 		handlers.writeEndElement();
   175 		handlers.writeEndElement();
   173 	}
   176 	}
   174 
   177