PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/wso2-wsf-cpp-src-2.1.0/wsf_c/axis2c/axiom/test/xpath/test_xpath.c

#
C | 513 lines | 375 code | 98 blank | 40 comment | 86 complexity | 6683adcdd869cadb86097e0afe10cbc3 MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause, CPL-1.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /*
  18. Compiling For Windows:
  19. cl.exe /nologo /D "WIN32" /D "_WINDOWS" /D "_MBCS" /I %WSFCPP_HOME%\include /c *.c
  20. link.exe /LIBPATH:%WSFCPP_HOME%\lib axutil.lib axiom.lib axis2_parser.lib axis2_engine.lib /OUT:test.exe *.obj
  21. */
  22. #include <stdio.h>
  23. #include <axiom.h>
  24. #include <axis2_util.h>
  25. #include <axiom_soap.h>
  26. #include <axiom_xpath.h>
  27. /* Function headers */
  28. axiom_node_t *build_test_xml(const axutil_env_t *env);
  29. void output_results(const axutil_env_t *env, axiom_xpath_result_t *xpath_result);
  30. axiom_node_t *read_test_xml(const axutil_env_t *env, char *file_name);
  31. void output_results(const axutil_env_t *env,
  32. axiom_xpath_result_t *xpath_result);
  33. void evaluate(const axutil_env_t *env,
  34. axiom_xpath_context_t *context,
  35. axis2_char_t *expr_str);
  36. void evaluate_expressions(const axutil_env_t *env,
  37. axiom_xpath_context_t *context,
  38. char *file_name);
  39. void add_namespaces(const axutil_env_t *env,
  40. axiom_xpath_context_t *context,
  41. char *file_name);
  42. int readline(FILE *fin, char *str);
  43. FILE *fcor;
  44. /*FILE *ftemp;*/
  45. int main(int argc, char *argv[])
  46. {
  47. axiom_node_t *test_tree = NULL;
  48. axis2_char_t *test_tree_str;
  49. char *xml_file = "test.xml";
  50. char *xpath_file = "test.xpath";
  51. char *ns_file = "test.ns";
  52. char *cor_file = "results.txt";
  53. axiom_xpath_context_t *context = NULL;
  54. /* Create environment */
  55. axutil_env_t *env =
  56. axutil_env_create_all("xpath_test.log", AXIS2_LOG_LEVEL_TRACE);
  57. /* Set input file */
  58. if (argc > 1)
  59. {
  60. printf("Usage: test [xml_file xpath_file namespaces_file results_file]\n\n");
  61. printf("\tLook at the example test case:");
  62. printf(" test.xml, test.xpath test.ns\n\n");
  63. if(argc > 4)
  64. {
  65. xml_file = argv[1];
  66. xpath_file = argv[2];
  67. ns_file = argv[3];
  68. cor_file = argv[4];
  69. }
  70. }
  71. /*Create the request */
  72. /* test_tree = build_test_xml(env); */
  73. test_tree = read_test_xml(env, (axis2_char_t *)xml_file);
  74. fcor = fopen(cor_file, "r");
  75. /*ftemp = fopen("temp.txt", "w");*/
  76. if (!fcor)
  77. {
  78. printf("Error opening file: %s\n", cor_file);
  79. }
  80. if (test_tree)
  81. {
  82. test_tree_str = axiom_node_to_string(test_tree, env);
  83. printf("\nTesting XML\n-----------\n\"%s\"\n\n\n", test_tree_str);
  84. /* Create XPath Context */
  85. context = axiom_xpath_context_create(env, test_tree);
  86. /* Namespaces */
  87. add_namespaces(env, context, ns_file);
  88. evaluate_expressions(env, context, xpath_file);
  89. test_tree_str = axiom_node_to_string(test_tree, env);
  90. printf("\n\nFinal XML\n-----------\n\"%s\"\n\n\n", test_tree_str);
  91. }
  92. /* Freeing memory */
  93. if (context)
  94. {
  95. axiom_xpath_free_context(env, context);
  96. }
  97. if (test_tree)
  98. {
  99. axiom_node_free_tree(test_tree, env);
  100. }
  101. if (env)
  102. {
  103. axutil_env_free((axutil_env_t *) env);
  104. }
  105. if(fcor)
  106. {
  107. fclose(fcor);
  108. }
  109. return 0;
  110. }
  111. int readline(FILE *fin, char *str)
  112. {
  113. int i;
  114. for (i = 0; 1; i++)
  115. {
  116. str[i] = (char)getc(fin);
  117. if (str[i] == '\n' || str[i] == '\r' || str[i] == EOF)
  118. {
  119. break;
  120. }
  121. }
  122. str[i] = '\0';
  123. return i;
  124. }
  125. void add_namespaces(const axutil_env_t *env,
  126. axiom_xpath_context_t *context,
  127. char *file_name)
  128. {
  129. FILE *fin = NULL;
  130. char prefix[1024];
  131. char uri[1024];
  132. axiom_namespace_t *ns;
  133. fin = fopen(file_name, "r");
  134. if (!fin)
  135. {
  136. printf("Error opening file: %s\n", file_name);
  137. return;
  138. }
  139. /* Compiling XPath expression */
  140. while (1)
  141. {
  142. if (readline(fin, prefix) == 0)
  143. {
  144. break;
  145. }
  146. if (readline(fin, uri) == 0)
  147. {
  148. break;
  149. }
  150. ns = axiom_namespace_create(
  151. env, (axis2_char_t *)uri, (axis2_char_t *)prefix);
  152. if (ns)
  153. {
  154. axiom_xpath_register_namespace(context, ns);
  155. }
  156. }
  157. fclose(fin);
  158. }
  159. void evaluate_expressions(
  160. const axutil_env_t *env,
  161. axiom_xpath_context_t *context,
  162. char *file_name)
  163. {
  164. FILE *fin = NULL;
  165. char str[1024];
  166. fin = fopen(file_name, "r");
  167. if (!fin)
  168. {
  169. printf("Error opening file: %s\n", file_name);
  170. return;
  171. }
  172. /* Compiling XPath expression */
  173. while (1)
  174. {
  175. if (readline(fin, str) == 0)
  176. {
  177. break;
  178. }
  179. if (str[0] == '#')
  180. {
  181. printf("\n\n%s", str + 1);
  182. continue;
  183. }
  184. evaluate(env, context, (axis2_char_t *)str);
  185. }
  186. fclose(fin);
  187. }
  188. void evaluate(
  189. const axutil_env_t *env,
  190. axiom_xpath_context_t *context,
  191. axis2_char_t *expr_str)
  192. {
  193. axiom_xpath_expression_t *expr = NULL;
  194. axiom_xpath_result_t *result = NULL;
  195. printf("\nCompiling XPath expression: \"%s\" ...\n", expr_str);
  196. expr = axiom_xpath_compile_expression(env, expr_str);
  197. if (!expr)
  198. {
  199. printf("Compilation error.");
  200. printf("Please check the systax of the XPath query.\n");
  201. return;
  202. }
  203. /* Evaluating XPath expression */
  204. printf("Evaluating...\n");
  205. result = axiom_xpath_evaluate(context, expr);
  206. if (!result)
  207. {
  208. printf("An error occured while evaluating the expression.\n");
  209. axiom_xpath_free_expression(env, expr);
  210. return;
  211. }
  212. if (result->flag == AXIOM_XPATH_ERROR_STREAMING_NOT_SUPPORTED)
  213. {
  214. printf("Streaming not supported.\n");
  215. axiom_xpath_free_expression(env, expr);
  216. return;
  217. }
  218. output_results(env, result);
  219. if (result)
  220. {
  221. axiom_xpath_free_result(env, result);
  222. }
  223. if (expr)
  224. {
  225. axiom_xpath_free_expression(env, expr);
  226. }
  227. }
  228. int compare_result(axis2_char_t *rs)
  229. {
  230. int i;
  231. char c;
  232. /*fprintf(ftemp, "%s#", rs);*/
  233. if(!fcor)
  234. {
  235. return 0;
  236. }
  237. for(i = 0; 1; i++)
  238. {
  239. while(rs[i] == ' ' || rs[i] == '\n' || rs[i] == '\r' || rs[i] == '\t')
  240. {
  241. i++;
  242. }
  243. do
  244. {
  245. c = (char)getc(fcor);
  246. }
  247. while(c == ' ' || c == '\n' || c == '\r' || c == '\t');
  248. if(c == '#' || c == EOF)
  249. {
  250. break;
  251. }
  252. if(c != rs[i])
  253. {
  254. while(c != '#' && c != EOF)
  255. {
  256. c = getc(fcor);
  257. }
  258. return 0;
  259. }
  260. }
  261. return rs[i] == '\0';
  262. }
  263. void output_results(const axutil_env_t *env,
  264. axiom_xpath_result_t *xpath_result)
  265. {
  266. axiom_xpath_result_node_t *xpath_result_node;
  267. axiom_node_t *result_node;
  268. axis2_char_t *result_str;
  269. axis2_char_t result_set[100000];
  270. axis2_char_t temp_res[100000];
  271. axiom_element_t *ele;
  272. int i;
  273. result_set[0] = '\n';
  274. result_set[1] = '\0';
  275. for (i = 0; i < axutil_array_list_size(xpath_result->nodes, env); i++)
  276. {
  277. xpath_result_node = axutil_array_list_get(xpath_result->nodes, env, i);
  278. if (xpath_result_node->type == AXIOM_XPATH_TYPE_NODE)
  279. {
  280. result_node = xpath_result_node->value;
  281. ele = (axiom_element_t *)axiom_node_get_data_element(
  282. result_node, env);
  283. if (ele)
  284. {
  285. /*result_str = axiom_element_get_text(ele, env, result_node);*/
  286. result_str = axiom_node_to_string(result_node, env);
  287. sprintf(temp_res, "\"%s\"\n", result_str);
  288. strcat(result_set, temp_res);
  289. }
  290. }
  291. else if (xpath_result_node->type == AXIOM_XPATH_TYPE_ATTRIBUTE)
  292. {
  293. result_str =
  294. axiom_attribute_get_localname(xpath_result_node->value, env);
  295. sprintf(temp_res, "%s = ", result_str);
  296. strcat(result_set, temp_res);
  297. result_str =
  298. axiom_attribute_get_value(xpath_result_node->value, env);
  299. sprintf(temp_res, "\"%s\"\n", result_str);
  300. strcat(result_set, temp_res);
  301. }
  302. else if (xpath_result_node->type == AXIOM_XPATH_TYPE_NAMESPACE)
  303. {
  304. result_str =
  305. axiom_namespace_get_prefix(xpath_result_node->value, env);
  306. sprintf(temp_res, "%s = ", result_str);
  307. strcat(result_set, temp_res);
  308. result_str =
  309. axiom_namespace_get_uri(xpath_result_node->value, env);
  310. sprintf(temp_res, "\"%s\"\n", result_str);
  311. strcat(result_set, temp_res);
  312. }
  313. else if (xpath_result_node->type == AXIOM_XPATH_TYPE_BOOLEAN)
  314. {
  315. sprintf(temp_res, "\"%s\"\n",
  316. (*(axis2_bool_t *)xpath_result_node->value) ? "true" : "false");
  317. strcat(result_set, temp_res);
  318. }
  319. else if (xpath_result_node->type == AXIOM_XPATH_TYPE_TEXT)
  320. {
  321. sprintf(temp_res, "\"%s\"\n",
  322. (axis2_char_t *)xpath_result_node->value);
  323. strcat(result_set, temp_res);
  324. }
  325. else if (xpath_result_node->type == AXIOM_XPATH_TYPE_NUMBER)
  326. {
  327. sprintf(temp_res, "\"%lf\"\n",
  328. *(double *)xpath_result_node->value);
  329. strcat(result_set, temp_res);
  330. }
  331. }
  332. if(compare_result(result_set))
  333. {
  334. printf("Test case passed\n");
  335. }
  336. else
  337. {
  338. printf("Failed test case!\nOutput\n%s\n", result_set);
  339. }
  340. }
  341. axiom_node_t *read_test_xml(const axutil_env_t *env, axis2_char_t *file_name)
  342. {
  343. axiom_xml_reader_t *reader = NULL;
  344. axiom_stax_builder_t *builder = NULL;
  345. axiom_document_t *document = NULL;
  346. axiom_node_t *root = NULL;
  347. /* Create parser */
  348. reader = axiom_xml_reader_create_for_file(env, file_name, NULL);
  349. if (!reader)
  350. {
  351. printf("Error creating pullparser");
  352. return NULL;
  353. }
  354. /* Create axiom_stax_builder */
  355. builder = axiom_stax_builder_create(env, reader);
  356. if (!builder)
  357. {
  358. printf("Error creating pull parser");
  359. return NULL;
  360. }
  361. /* Create an om document */
  362. document = axiom_stax_builder_get_document(builder, env);
  363. if (!document)
  364. {
  365. printf("Error creating document");
  366. return NULL;
  367. }
  368. /* Get root element */
  369. root = axiom_document_get_root_element(document, env);
  370. if (!root)
  371. {
  372. printf("Root element null ");
  373. axiom_stax_builder_free(builder, env);
  374. return NULL;
  375. }
  376. while (axiom_document_build_next(document, env));
  377. return root;
  378. }
  379. axiom_node_t * build_test_xml(const axutil_env_t *env)
  380. {
  381. axiom_node_t *main_node;
  382. axiom_node_t *order_node;
  383. axiom_node_t *values_node;
  384. axiom_node_t *value_node;
  385. axiom_node_t *grandchild;
  386. axis2_char_t value_str[255];
  387. axiom_element_t *order_ele;
  388. axiom_element_t *values_ele;
  389. axiom_element_t *value_ele;
  390. axiom_element_t *grandchild_ele;
  391. axiom_attribute_t *attribute;
  392. int i, N = 20;
  393. axiom_namespace_t *ns1 =
  394. axiom_namespace_create(env, "http://xpath/test", "ns1");
  395. axiom_element_create(env, NULL, "test", ns1, &main_node);
  396. order_ele =
  397. axiom_element_create(env, main_node, "node1", NULL, &order_node);
  398. axiom_element_set_text(order_ele, env, "10", order_node);
  399. attribute =
  400. axiom_attribute_create(env, "attr1", "attribute_value_1", NULL);
  401. axiom_element_add_attribute(order_ele, env, attribute, order_node);
  402. values_ele =
  403. axiom_element_create(env, main_node, "node2", NULL, &values_node);
  404. for (i = 0 ; i < N; i++)
  405. {
  406. sprintf(value_str, "%d", i + 1);
  407. value_ele =
  408. axiom_element_create(env, values_node, "child", NULL, &value_node);
  409. axiom_element_set_text(value_ele, env, value_str, value_node);
  410. grandchild_ele =
  411. axiom_element_create(env, value_node, "grandchild", NULL, &grandchild);
  412. axiom_element_set_text(grandchild_ele, env, value_str /*"3 rd level"*/, grandchild);
  413. }
  414. return main_node;
  415. }