ox 1.2.4 → 1.2.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of ox might be problematic. Click here for more details.

Files changed (5) hide show
  1. data/ext/ox/dump.c +20 -10
  2. data/ext/ox/ox.c +18 -1
  3. data/lib/ox/version.rb +1 -1
  4. data/test/func.rb +10 -0
  5. metadata +2 -2
data/ext/ox/dump.c CHANGED
@@ -65,13 +65,14 @@ typedef struct _Out {
65
65
  unsigned long circ_cnt;
66
66
  int indent;
67
67
  int depth; // used by dumpHash
68
+ Options opts;
68
69
  } *Out;
69
70
 
70
71
  static void dump_obj_to_xml(VALUE obj, Options copts, Out out);
71
72
 
72
- static void dump_first_obj(VALUE obj, Options copts, Out out);
73
+ static void dump_first_obj(VALUE obj, Out out);
73
74
  static void dump_obj(ID aid, VALUE obj, unsigned int depth, Out out);
74
- static void dump_gen_doc(VALUE obj, unsigned int depth, Options copts, Out out);
75
+ static void dump_gen_doc(VALUE obj, unsigned int depth, Out out);
75
76
  static void dump_gen_element(VALUE obj, unsigned int depth, Out out);
76
77
  static int dump_gen_attr(VALUE key, VALUE value, Out out);
77
78
  static int dump_gen_nodes(VALUE obj, unsigned int depth, Out out);
@@ -389,8 +390,9 @@ dump_time_xsd(Out out, VALUE obj) {
389
390
  }
390
391
 
391
392
  static void
392
- dump_first_obj(VALUE obj, Options copts, Out out) {
393
+ dump_first_obj(VALUE obj, Out out) {
393
394
  char buf[128];
395
+ Options copts = out->opts;
394
396
  int cnt;
395
397
 
396
398
  if (Yes == copts->with_xml) {
@@ -622,7 +624,7 @@ dump_obj(ID aid, VALUE obj, unsigned int depth, Out out) {
622
624
  if (ox_document_clas == clas) {
623
625
  e.type = RawCode;
624
626
  out->w_start(out, &e);
625
- dump_gen_doc(obj, depth + 1, 0, out);
627
+ dump_gen_doc(obj, depth + 1, out);
626
628
  out->w_end(out, &e);
627
629
  } else if (ox_element_clas == clas) {
628
630
  e.type = RawCode;
@@ -730,7 +732,14 @@ dump_obj(ID aid, VALUE obj, unsigned int depth, Out out) {
730
732
  break;
731
733
  }
732
734
  default:
733
- rb_raise(rb_eNotImpError, "Failed to dump %s Object (%02x)\n", rb_class2name(rb_obj_class(obj)), rb_type(obj));
735
+ if (StrictEffort == out->opts->effort) {
736
+ rb_raise(rb_eNotImpError, "Failed to dump %s Object (%02x)\n",
737
+ rb_class2name(rb_obj_class(obj)), rb_type(obj));
738
+ } else {
739
+ e.type = NilClassCode; e.clas.len = 0; e.clas.str = 0;
740
+ e.closed = 1;
741
+ out->w_start(out, &e);
742
+ }
734
743
  break;
735
744
  }
736
745
  }
@@ -751,18 +760,18 @@ dump_hash(VALUE key, VALUE value, Out out) {
751
760
  }
752
761
 
753
762
  static void
754
- dump_gen_doc(VALUE obj, unsigned int depth, Options copts, Out out) {
763
+ dump_gen_doc(VALUE obj, unsigned int depth, Out out) {
755
764
  VALUE attrs = rb_attr_get(obj, attributes_id);
756
765
  VALUE nodes = rb_attr_get(obj, nodes_id);
757
766
 
758
- if (Yes == copts->with_xml) {
767
+ if (Yes == out->opts->with_xml) {
759
768
  dump_value(out, "<?xml", 5);
760
769
  if (Qnil != attrs) {
761
770
  rb_hash_foreach(attrs, dump_gen_attr, (VALUE)out);
762
771
  }
763
772
  dump_value(out, "?>", 2);
764
773
  }
765
- if (Yes == copts->with_instruct) {
774
+ if (Yes == out->opts->with_instruct) {
766
775
  if (out->buf < out->cur) {
767
776
  dump_value(out, "\n<?ox version=\"1.0\" mode=\"generic\"?>", 36);
768
777
  } else {
@@ -915,18 +924,19 @@ dump_obj_to_xml(VALUE obj, Options copts, Out out) {
915
924
  out->cur = out->buf;
916
925
  out->circ_cache = 0;
917
926
  out->circ_cnt = 0;
927
+ out->opts = copts;
918
928
  if (Yes == copts->circular) {
919
929
  ox_cache8_new(&out->circ_cache);
920
930
  }
921
931
  out->indent = copts->indent;
922
932
  if (ox_document_clas == clas) {
923
- dump_gen_doc(obj, -1, copts, out);
933
+ dump_gen_doc(obj, -1, out);
924
934
  } else if (ox_element_clas == clas) {
925
935
  dump_gen_element(obj, 0, out);
926
936
  } else {
927
937
  out->w_start = dump_start;
928
938
  out->w_end = dump_end;
929
- dump_first_obj(obj, copts, out);
939
+ dump_first_obj(obj, out);
930
940
  }
931
941
  dump_value(out, "\n", 1);
932
942
  }
data/ext/ox/ox.c CHANGED
@@ -462,6 +462,17 @@ parse_dump_options(VALUE ropts, Options copts) {
462
462
  }
463
463
  strncpy(copts->encoding, StringValuePtr(v), sizeof(copts->encoding) - 1);
464
464
  }
465
+ if (Qnil != (v = rb_hash_lookup(ropts, effort_sym))) {
466
+ if (auto_define_sym == v) {
467
+ copts->effort = AutoEffort;
468
+ } else if (tolerant_sym == v) {
469
+ copts->effort = TolerantEffort;
470
+ } else if (strict_sym == v) {
471
+ copts->effort = StrictEffort;
472
+ } else {
473
+ rb_raise(rb_eArgError, ":effort must be :strict, :tolerant, or :auto_define.\n");
474
+ }
475
+ }
465
476
  for (o = ynos; 0 != o->attr; o++) {
466
477
  if (Qnil != (v = rb_hash_lookup(ropts, o->sym))) {
467
478
  VALUE c = rb_obj_class(v);
@@ -486,6 +497,9 @@ parse_dump_options(VALUE ropts, Options copts) {
486
497
  * [:indent] number of spaces to use as the standard indention, default: 2
487
498
  * [:xsd_date] use XSD date format if true, default: false
488
499
  * [:circular] allow circular references, default: false
500
+ * [:effort] effort to use when an undumpable object (e.g., IO) is encountered, default: :strict
501
+ * [:strict] raise an NotImplementedError if an undumpable object is encountered
502
+ * [:tolerant] replaces undumplable objects with nil
489
503
  */
490
504
  static VALUE
491
505
  dump(int argc, VALUE *argv, VALUE self) {
@@ -510,13 +524,16 @@ dump(int argc, VALUE *argv, VALUE self) {
510
524
 
511
525
  /* call-seq: to_file(file_path, obj, options)
512
526
  *
513
- * Dumps and Object to the specified file.
527
+ * Dumps an Object to the specified file.
514
528
  * [file_path] file path to write the XML document to
515
529
  * [obj] Object to serialize as an XML document String
516
530
  * [options] formating options
517
531
  * [:indent] number of spaces to use as the standard indention, default: 2
518
532
  * [:xsd_date] use XSD date format if true, default: false
519
533
  * [:circular] allow circular references, default: false
534
+ * [:effort] effort to use when an undumpable object (e.g., IO) is encountered, default: :strict
535
+ * [:strict] raise an NotImplementedError if an undumpable object is encountered
536
+ * [:tolerant] replaces undumplable objects with nil
520
537
  */
521
538
  static VALUE
522
539
  to_file(int argc, VALUE *argv, VALUE self) {
data/lib/ox/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
2
  module Ox
3
3
  # Current version of the module.
4
- VERSION = '1.2.4'
4
+ VERSION = '1.2.5'
5
5
  end
data/test/func.rb CHANGED
@@ -304,6 +304,16 @@ class Func < ::Test::Unit::TestCase
304
304
  assert_equal("test", obj)
305
305
  end
306
306
 
307
+ def test_IO
308
+ f = File.open("func.rb", "r")
309
+ assert_raise(NotImplementedError) {
310
+ xml = Ox.dump(f, effort: :strict)
311
+ }
312
+ xml = Ox.dump(f, effort: :tolerant)
313
+ obj = Ox.load(xml, mode: :object) # should convert it to an object
314
+ assert_equal(nil, obj)
315
+ end
316
+
307
317
  end
308
318
 
309
319
  class Bag
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: ox
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.2.4
5
+ version: 1.2.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Peter Ohler
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-27 00:00:00 Z
13
+ date: 2011-07-29 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: A fast XML parser and object serializer that uses only standard C lib.