streamlet-examples/xpath.cpp
branchv_0
changeset 70 018e2609f5bb
parent 68 5d3d57d9c323
child 74 a2aa84f310a5
equal deleted inserted replaced
69:52f837fbb216 70:018e2609f5bb
    60 		if (mode == Mode::BOOLEAN) return BOOLEAN;
    60 		if (mode == Mode::BOOLEAN) return BOOLEAN;
    61 		else if (mode == Mode::LINE_NUMBER) return INTEGER;
    61 		else if (mode == Mode::LINE_NUMBER) return INTEGER;
    62 		else return STRING;
    62 		else return STRING;
    63 	}
    63 	}
    64 
    64 
    65 	// TODO: should not be done in particular streamlets but in the worker
       
    66 	std::wstring toNullValue(std::wstring type) {
       
    67 		if (type == BOOLEAN) return L"false";
       
    68 		else if (type == INTEGER) return L"0";
       
    69 		else return L"";
       
    70 	}
       
    71 
       
    72 	class XPathAttribute {
    65 	class XPathAttribute {
    73 	public:
    66 	public:
    74 
    67 
    75 		std::wstring name;
    68 		std::wstring name;
    76 		std::wstring xpath;
    69 		std::wstring xpath;
   116 					result = convertor.from_bytes(root->eval_to_string(xpath, ns));
   109 					result = convertor.from_bytes(root->eval_to_string(xpath, ns));
   117 				} else if (xpathAttribute.mode == Mode::BOOLEAN) {
   110 				} else if (xpathAttribute.mode == Mode::BOOLEAN) {
   118 					result = root->eval_to_boolean(xpath, ns) ? L"true" : L"false";
   111 					result = root->eval_to_boolean(xpath, ns) ? L"true" : L"false";
   119 				} else if (xpathAttribute.mode == Mode::LINE_NUMBER) {
   112 				} else if (xpathAttribute.mode == Mode::LINE_NUMBER) {
   120 					xmlpp::NodeSet attributeNodes = root->find(xpath, ns);
   113 					xmlpp::NodeSet attributeNodes = root->find(xpath, ns);
   121 					if (attributeNodes.size()) {
   114 					if (attributeNodes.size()) result = std::to_wstring(attributeNodes[0]->get_line());
   122 						result = std::to_wstring(attributeNodes[0]->get_line());
   115 					else isNull = true;
   123 					} else {
       
   124 						result = L"0";
       
   125 						isNull = true;
       
   126 					}
       
   127 				} else if (xpathAttribute.mode == Mode::XPATH) {
   116 				} else if (xpathAttribute.mode == Mode::XPATH) {
   128 					xmlpp::NodeSet attributeNodes = root->find(xpath, ns);
   117 					xmlpp::NodeSet attributeNodes = root->find(xpath, ns);
   129 					if (attributeNodes.size()) {
   118 					if (attributeNodes.size()) result = convertor.from_bytes(attributeNodes[0]->get_path());
   130 						result = convertor.from_bytes(attributeNodes[0]->get_path());
   119 					else isNull = true;
   131 					} else {
       
   132 						result = L"";
       
   133 						isNull = true;
       
   134 					}
       
   135 				} else if (xpathAttribute.mode == Mode::RAW_XML) {
   120 				} else if (xpathAttribute.mode == Mode::RAW_XML) {
   136 					throw std::logic_error("Raw XML mode is not yet implemented."); // TODO: implement also RAW_XML
   121 					throw std::logic_error("Raw XML mode is not yet implemented."); // TODO: implement also RAW_XML
   137 				} else {
   122 				} else {
   138 					throw std::logic_error("Unsupported mode."); // should never happer
   123 					throw std::logic_error("Unsupported mode."); // should never happer
   139 				}
   124 				}
   140 
   125 
   141 				oa.push_back({result, false});
   126 				oa.push_back({result, isNull});
   142 			}
   127 			}
   143 		} catch (xmlpp::parse_error& e) {
   128 		} catch (xmlpp::parse_error& e) {
   144 			for (XPathAttribute xpathAttribute : xpathAttributes) oa.push_back({toNullValue(toType(xpathAttribute.mode)), true});
   129 			for (XPathAttribute xpathAttribute : xpathAttributes) oa.push_back({L"", true});
   145 			// invalid XML → xmlpp::parse_error → just skip this file
   130 			// invalid XML → xmlpp::parse_error → just skip this file
   146 			// invalid XPath → xmlpp::exception → failure
   131 			// invalid XPath → xmlpp::exception → failure
   147 		}
   132 		}
   148 
   133 
   149 
   134