psych 2.1.0 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +14 -0
  3. data/.travis.yml +1 -10
  4. data/Gemfile +3 -0
  5. data/Mavenfile +7 -0
  6. data/Rakefile +27 -119
  7. data/bin/console +7 -0
  8. data/bin/setup +6 -0
  9. data/ext/java/PsychEmitter.java +345 -0
  10. data/ext/java/PsychLibrary.java +93 -0
  11. data/ext/java/PsychParser.java +399 -0
  12. data/ext/java/PsychToRuby.java +79 -0
  13. data/ext/java/PsychYamlTree.java +55 -0
  14. data/ext/psych/.gitignore +11 -0
  15. data/lib/psych.rb +2 -2
  16. data/lib/psych/scalar_scanner.rb +1 -1
  17. data/lib/psych/visitors/to_ruby.rb +2 -2
  18. data/psych.gemspec +39 -0
  19. metadata +21 -84
  20. data/.autotest +0 -18
  21. data/Manifest.txt +0 -114
  22. data/test/psych/handlers/test_recorder.rb +0 -26
  23. data/test/psych/helper.rb +0 -122
  24. data/test/psych/json/test_stream.rb +0 -110
  25. data/test/psych/nodes/test_enumerable.rb +0 -44
  26. data/test/psych/test_alias_and_anchor.rb +0 -97
  27. data/test/psych/test_array.rb +0 -58
  28. data/test/psych/test_boolean.rb +0 -37
  29. data/test/psych/test_class.rb +0 -37
  30. data/test/psych/test_coder.rb +0 -207
  31. data/test/psych/test_date_time.rb +0 -39
  32. data/test/psych/test_deprecated.rb +0 -215
  33. data/test/psych/test_document.rb +0 -47
  34. data/test/psych/test_emitter.rb +0 -111
  35. data/test/psych/test_encoding.rb +0 -269
  36. data/test/psych/test_exception.rb +0 -158
  37. data/test/psych/test_hash.rb +0 -95
  38. data/test/psych/test_json_tree.rb +0 -66
  39. data/test/psych/test_merge_keys.rb +0 -181
  40. data/test/psych/test_nil.rb +0 -19
  41. data/test/psych/test_null.rb +0 -20
  42. data/test/psych/test_numeric.rb +0 -46
  43. data/test/psych/test_object.rb +0 -45
  44. data/test/psych/test_object_references.rb +0 -72
  45. data/test/psych/test_omap.rb +0 -76
  46. data/test/psych/test_parser.rb +0 -340
  47. data/test/psych/test_psych.rb +0 -184
  48. data/test/psych/test_safe_load.rb +0 -98
  49. data/test/psych/test_scalar.rb +0 -12
  50. data/test/psych/test_scalar_scanner.rb +0 -107
  51. data/test/psych/test_serialize_subclasses.rb +0 -39
  52. data/test/psych/test_set.rb +0 -50
  53. data/test/psych/test_stream.rb +0 -94
  54. data/test/psych/test_string.rb +0 -227
  55. data/test/psych/test_struct.rb +0 -50
  56. data/test/psych/test_symbol.rb +0 -26
  57. data/test/psych/test_tainted.rb +0 -131
  58. data/test/psych/test_to_yaml_properties.rb +0 -64
  59. data/test/psych/test_tree_builder.rb +0 -80
  60. data/test/psych/test_yaml.rb +0 -1293
  61. data/test/psych/test_yamldbm.rb +0 -193
  62. data/test/psych/test_yamlstore.rb +0 -86
  63. data/test/psych/visitors/test_depth_first.rb +0 -50
  64. data/test/psych/visitors/test_emitter.rb +0 -145
  65. data/test/psych/visitors/test_to_ruby.rb +0 -332
  66. data/test/psych/visitors/test_yaml_tree.rb +0 -180
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b60d3bf3233df7c3abe9184e81b79eed85129ba1
4
- data.tar.gz: b06bdbf39cd19feaf29b19d5fb49f2d0ae6cfe0e
3
+ metadata.gz: f2e70b3ff8828fe0241a7047bdd843ad39f669c7
4
+ data.tar.gz: ec14c0a1d14bb917e2b43701d97c23ff32c037d2
5
5
  SHA512:
6
- metadata.gz: d9a6914218ecaece3214bb8698a6589bb67b3ded99be295fee23894be97e40dac2c67ca730c4c8c7bf12bc89e68ec7974132adb3a81f6f8f54ae8507fda324a6
7
- data.tar.gz: 105c008b421b79781e25f041a4f5d9ddc99fbf79b8a0db1fad1329abee305387fd10156612279daf0bdcaa4baaf1c13382cef0164833ca9e4fc52448e87060c4
6
+ metadata.gz: 24c746dc58c84d37adf9e1660c002d3882bbd4ae81bf5f800094959593c0b06ca1d246c563e95ed40e4459d3fa179260f8358ce1dd6a57d9b44d2187b8a52401
7
+ data.tar.gz: 01df0982d787422a5cd43ff68adc5dca474d0e7b5012205095f43ccc04d55c5b80d3f112da68c46617c5b587631307fb1e75e61e6acfafe0cee3f378afa6ab7d
@@ -0,0 +1,14 @@
1
+ *.swp
2
+ *.bundle
3
+ *.jar
4
+ *.class
5
+ .mvn
6
+ /.bundle/
7
+ /.yardoc
8
+ /Gemfile.lock
9
+ /_yardoc/
10
+ /coverage/
11
+ /doc/
12
+ /pkg/
13
+ /spec/reports/
14
+ /tmp/
@@ -4,13 +4,4 @@ rvm:
4
4
  - 2.2
5
5
  - 2.3.0
6
6
  - ruby-head
7
- - rbx-2
8
- before_script:
9
- - gem install hoe
10
- - gem install rake-compiler
11
- - gem install minitest -v '~> 5.0'
12
- script: rake test
13
- matrix:
14
- allow_failures:
15
- - rvm: ruby-head
16
- - rvm: rbx-2
7
+ before_install: gem install bundler --no-document
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,7 @@
1
+ #-*- mode: ruby -*-
2
+
3
+ jar 'org.yaml:snakeyaml:${snakeyaml.version}'
4
+
5
+ plugin :dependency, '2.8', :outputFile => 'pkg/classpath'
6
+
7
+ # vim: syntax=Ruby
data/Rakefile CHANGED
@@ -1,123 +1,31 @@
1
- # -*- ruby -*-
2
-
3
- require 'psych'
4
- require 'rubygems'
5
- require 'hoe'
6
-
7
- def java?
8
- RUBY_PLATFORM =~ /java/
9
- end
10
-
11
- class Hoe
12
- remove_const :RUBY_FLAGS
13
- flags = "-I#{%w(lib ext bin test).join(File::PATH_SEPARATOR)}"
14
- flags = "--1.9 " + flags if java?
15
- RUBY_FLAGS = flags
16
- end
17
-
18
- gem 'rake-compiler', '>= 0.4.1'
19
- gem 'minitest', '~> 5.0'
20
- require "rake/extensiontask"
21
-
22
- Hoe.plugin :doofus, :git, :gemspec
23
-
24
- $hoe = Hoe.spec 'psych' do
25
- license 'MIT'
26
- developer 'Aaron Patterson', 'aaron@tenderlovemaking.com'
27
-
28
- self.extra_rdoc_files = Dir['*.rdoc']
29
- self.history_file = 'CHANGELOG.rdoc'
30
- self.readme_file = 'README.rdoc'
31
- self.testlib = :minitest
32
-
33
- extra_dev_deps << ['rake-compiler', '>= 0.4.1']
34
- extra_dev_deps << ['minitest', '~> 5.0']
35
-
36
- self.spec_extras = {
37
- :required_ruby_version => '>= 1.9.2'
38
- }
39
-
40
- if java?
41
- require './lib/psych/versions.rb'
42
- extra_deps << ['jar-dependencies', '>= 0.1.7']
43
-
44
- # the jar declaration for jar-dependencies
45
- self.spec_extras[ 'requirements' ] = "jar org.yaml:snakeyaml, #{Psych::DEFAULT_SNAKEYAML_VERSION}"
46
- self.spec_extras[ 'platform' ] = 'java'
47
- # TODO: clean this section up.
48
- require "rake/javaextensiontask"
49
- Rake::JavaExtensionTask.new("psych", spec) do |ext|
50
- require 'maven/ruby/maven'
51
- # uses Mavenfile to write classpath into pkg/classpath
52
- # and tell maven via system properties the snakeyaml version
53
- # this is basically the same as running from the commandline:
54
- # rmvn dependency:build-classpath -Dsnakeyaml.version='use version from Psych::DEFAULT_SNAKEYAML_VERSION here'
55
- Maven::Ruby::Maven.new.exec( 'dependency:build-classpath', "-Dsnakeyaml.version=#{Psych::DEFAULT_SNAKEYAML_VERSION}", '-Dverbose=true')#, '--quiet' )
56
- ext.source_version = '1.7'
57
- ext.target_version = '1.7'
58
- ext.classpath = File.read('pkg/classpath')
59
- ext.ext_dir = 'ext/java'
60
- end
61
- else
62
- self.spec_extras[:extensions] = ["ext/psych/extconf.rb"]
63
- Rake::ExtensionTask.new "psych", spec do |ext|
64
- ext.lib_dir = File.join(*['lib', ENV['FAT_DIR']].compact)
65
- end
66
- end
67
- end
68
-
69
- def gem_build_path
70
- File.join 'pkg', $hoe.spec.full_name
71
- end
72
-
73
- def add_file_to_gem relative_path
74
- target_path = File.join gem_build_path, relative_path
75
- target_dir = File.dirname(target_path)
76
- mkdir_p target_dir unless File.directory?(target_dir)
77
- rm_f target_path
78
- safe_ln relative_path, target_path
79
- $hoe.spec.files.concat [relative_path]
80
- end
81
-
82
- if java?
83
- task gem_build_path => [:compile] do
84
- add_file_to_gem 'lib/psych.jar'
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/test_*.rb']
8
+ end
9
+
10
+ if RUBY_PLATFORM =~ /java/
11
+ require 'rake/javaextensiontask'
12
+ Rake::JavaExtensionTask.new("psych") do |ext|
13
+ require 'maven/ruby/maven'
14
+ # uses Mavenfile to write classpath into pkg/classpath
15
+ # and tell maven via system properties the snakeyaml version
16
+ # this is basically the same as running from the commandline:
17
+ # rmvn dependency:build-classpath -Dsnakeyaml.version='use version from Psych::DEFAULT_SNAKEYAML_VERSION here'
18
+ Maven::Ruby::Maven.new.exec( 'dependency:build-classpath', "-Dsnakeyaml.version=#{Psych::DEFAULT_SNAKEYAML_VERSION}", '-Dverbose=true')#, '--quiet' )
19
+ ext.source_version = '1.7'
20
+ ext.target_version = '1.7'
21
+ ext.classpath = File.read('pkg/classpath')
22
+ ext.ext_dir = 'ext/java'
85
23
  end
86
- end
87
-
88
- Hoe.add_include_dirs('.:lib/psych')
89
-
90
- task :test => :compile
91
-
92
- task :hack_spec do
93
- $hoe.spec.extra_rdoc_files.clear
94
- end
95
- task 'core:spec' => [:hack_spec, 'gem:spec']
96
-
97
- desc "merge psych in to ruby trunk"
98
- namespace :merge do
99
- basedir = File.expand_path File.dirname __FILE__
100
- rubydir = File.join ENV['HOME'], 'git', 'ruby'
101
- mergedirs = {
102
- # From # To
103
- [basedir, 'ext', 'psych/'] => [rubydir, 'ext', 'psych/'],
104
- [basedir, 'lib/'] => [rubydir, 'ext', 'psych', 'lib/'],
105
- [basedir, 'test', 'psych/'] => [rubydir, 'test', 'psych/'],
106
- }
107
-
108
- rsync = 'rsync -av --exclude lib --exclude ".*" --exclude "*.o" --exclude Makefile --exclude mkmf.log --delete'
109
-
110
- task :to_ruby do
111
- mergedirs.each do |from, to|
112
- sh "#{rsync} #{File.join(*from)} #{File.join(*to)}"
113
- end
114
- end
115
-
116
- task :from_ruby do
117
- mergedirs.each do |from, to|
118
- sh "#{rsync} #{File.join(*to)} #{File.join(*from)}"
119
- end
24
+ else
25
+ require 'rake/extensiontask'
26
+ Rake::ExtensionTask.new("psych") do |ext|
27
+ ext.lib_dir = File.join(*['lib', ENV['FAT_DIR']].compact)
120
28
  end
121
29
  end
122
30
 
123
- # vim: syntax=ruby
31
+ task :default => [:compile, :test]
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "psych"
5
+
6
+ require "irb"
7
+ IRB.start
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,345 @@
1
+ /***** BEGIN LICENSE BLOCK *****
2
+ * Version: EPL 1.0/GPL 2.0/LGPL 2.1
3
+ *
4
+ * The contents of this file are subject to the Eclipse Public
5
+ * License Version 1.0 (the "License"); you may not use this file
6
+ * except in compliance with the License. You may obtain a copy of
7
+ * the License at http://www.eclipse.org/legal/epl-v10.html
8
+ *
9
+ * Software distributed under the License is distributed on an "AS
10
+ * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11
+ * implied. See the License for the specific language governing
12
+ * rights and limitations under the License.
13
+ *
14
+ * Copyright (C) 2010 Charles O Nutter <headius@headius.com>
15
+ *
16
+ * Alternatively, the contents of this file may be used under the terms of
17
+ * either of the GNU General Public License Version 2 or later (the "GPL"),
18
+ * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19
+ * in which case the provisions of the GPL or the LGPL are applicable instead
20
+ * of those above. If you wish to allow use of your version of this file only
21
+ * under the terms of either the GPL or the LGPL, and not to allow others to
22
+ * use your version of this file under the terms of the EPL, indicate your
23
+ * decision by deleting the provisions above and replace them with the notice
24
+ * and other provisions required by the GPL or the LGPL. If you do not delete
25
+ * the provisions above, a recipient may use your version of this file under
26
+ * the terms of any one of the EPL, the GPL or the LGPL.
27
+ ***** END LICENSE BLOCK *****/
28
+ package org.jruby.ext.psych;
29
+
30
+ import java.io.IOException;
31
+ import java.io.OutputStreamWriter;
32
+ import java.nio.charset.Charset;
33
+ import java.util.HashMap;
34
+ import java.util.Map;
35
+
36
+ import org.jcodings.Encoding;
37
+ import org.jruby.Ruby;
38
+ import org.jruby.RubyArray;
39
+ import org.jruby.RubyClass;
40
+ import org.jruby.RubyFixnum;
41
+ import org.jruby.RubyModule;
42
+ import org.jruby.RubyObject;
43
+ import org.jruby.RubyString;
44
+ import org.jruby.anno.JRubyMethod;
45
+ import org.jruby.runtime.ObjectAllocator;
46
+ import org.jruby.runtime.ThreadContext;
47
+ import org.jruby.runtime.builtin.IRubyObject;
48
+ import org.jruby.util.IOOutputStream;
49
+ import org.yaml.snakeyaml.DumperOptions;
50
+ import org.yaml.snakeyaml.emitter.Emitter;
51
+ import org.yaml.snakeyaml.emitter.EmitterException;
52
+ import org.yaml.snakeyaml.error.Mark;
53
+ import org.yaml.snakeyaml.events.AliasEvent;
54
+ import org.yaml.snakeyaml.events.DocumentEndEvent;
55
+ import org.yaml.snakeyaml.events.DocumentStartEvent;
56
+ import org.yaml.snakeyaml.events.Event;
57
+ import org.yaml.snakeyaml.events.ImplicitTuple;
58
+ import org.yaml.snakeyaml.events.MappingEndEvent;
59
+ import org.yaml.snakeyaml.events.MappingStartEvent;
60
+ import org.yaml.snakeyaml.events.ScalarEvent;
61
+ import org.yaml.snakeyaml.events.SequenceEndEvent;
62
+ import org.yaml.snakeyaml.events.SequenceStartEvent;
63
+ import org.yaml.snakeyaml.events.StreamEndEvent;
64
+ import org.yaml.snakeyaml.events.StreamStartEvent;
65
+
66
+ import static org.jruby.runtime.Visibility.*;
67
+
68
+ public class PsychEmitter extends RubyObject {
69
+ public static void initPsychEmitter(Ruby runtime, RubyModule psych) {
70
+ RubyClass psychHandler = runtime.defineClassUnder("Handler", runtime.getObject(), runtime.getObject().getAllocator(), psych);
71
+ RubyClass psychEmitter = runtime.defineClassUnder("Emitter", psychHandler, new ObjectAllocator() {
72
+ public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
73
+ return new PsychEmitter(runtime, klazz);
74
+ }
75
+ }, psych);
76
+
77
+ psychEmitter.defineAnnotatedMethods(PsychEmitter.class);
78
+ }
79
+
80
+ public PsychEmitter(Ruby runtime, RubyClass klass) {
81
+ super(runtime, klass);
82
+ }
83
+
84
+ @JRubyMethod(visibility = PRIVATE)
85
+ public IRubyObject initialize(ThreadContext context, IRubyObject io) {
86
+ options = new DumperOptions();
87
+ options.setIndent(2);
88
+
89
+ this.io = io;
90
+
91
+ return context.nil;
92
+ }
93
+
94
+ @JRubyMethod(visibility = PRIVATE)
95
+ public IRubyObject initialize(ThreadContext context, IRubyObject io, IRubyObject rbOptions) {
96
+ IRubyObject width = rbOptions.callMethod(context, "line_width");
97
+ IRubyObject canonical = rbOptions.callMethod(context, "canonical");
98
+ IRubyObject level = rbOptions.callMethod(context, "indentation");
99
+
100
+ options = new DumperOptions();
101
+
102
+ options.setCanonical(canonical.isTrue());
103
+ options.setIndent((int)level.convertToInteger().getLongValue());
104
+ options.setWidth((int)width.convertToInteger().getLongValue());
105
+
106
+ this.io = io;
107
+
108
+ return context.nil;
109
+ }
110
+
111
+ @JRubyMethod
112
+ public IRubyObject start_stream(ThreadContext context, IRubyObject encoding) {
113
+ if (!(encoding instanceof RubyFixnum)) {
114
+ throw context.runtime.newTypeError(encoding, context.runtime.getFixnum());
115
+ }
116
+
117
+ initEmitter(context, encoding);
118
+
119
+ StreamStartEvent event = new StreamStartEvent(NULL_MARK, NULL_MARK);
120
+
121
+ emit(context, event);
122
+
123
+ return this;
124
+ }
125
+
126
+ @JRubyMethod
127
+ public IRubyObject end_stream(ThreadContext context) {
128
+ StreamEndEvent event = new StreamEndEvent(NULL_MARK, NULL_MARK);
129
+ emit(context, event);
130
+ return this;
131
+ }
132
+
133
+ @JRubyMethod
134
+ public IRubyObject start_document(ThreadContext context, IRubyObject _version, IRubyObject tags, IRubyObject implicit) {
135
+ DumperOptions.Version version = null;
136
+ boolean implicitBool = implicit.isTrue();
137
+ Map<String, String> tagsMap = null;
138
+
139
+ RubyArray versionAry = _version.convertToArray();
140
+ if (versionAry.size() == 2) {
141
+ int versionInt0 = (int)versionAry.eltInternal(0).convertToInteger().getLongValue();
142
+ int versionInt1 = (int)versionAry.eltInternal(1).convertToInteger().getLongValue();
143
+
144
+ if (versionInt0 == 1) {
145
+ if (versionInt1 == 0) {
146
+ version = DumperOptions.Version.V1_0;
147
+ } else if (versionInt1 == 1) {
148
+ version = DumperOptions.Version.V1_1;
149
+ }
150
+ }
151
+ if (version == null) {
152
+ throw context.runtime.newArgumentError("invalid YAML version: " + versionAry);
153
+ }
154
+ }
155
+
156
+ RubyArray tagsAry = tags.convertToArray();
157
+ if (tagsAry.size() > 0) {
158
+ tagsMap = new HashMap<String, String>(tagsAry.size());
159
+ for (int i = 0; i < tagsAry.size(); i++) {
160
+ RubyArray tagsTuple = tagsAry.eltInternal(i).convertToArray();
161
+ if (tagsTuple.size() != 2) {
162
+ throw context.runtime.newRuntimeError("tags tuple must be of length 2");
163
+ }
164
+ IRubyObject key = tagsTuple.eltInternal(0);
165
+ IRubyObject value = tagsTuple.eltInternal(1);
166
+ tagsMap.put(
167
+ key.asJavaString(),
168
+ value.asJavaString());
169
+ }
170
+ }
171
+
172
+ DocumentStartEvent event = new DocumentStartEvent(NULL_MARK, NULL_MARK, !implicitBool, version, tagsMap);
173
+ emit(context, event);
174
+ return this;
175
+ }
176
+
177
+ @JRubyMethod
178
+ public IRubyObject end_document(ThreadContext context, IRubyObject implicit) {
179
+ DocumentEndEvent event = new DocumentEndEvent(NULL_MARK, NULL_MARK, !implicit.isTrue());
180
+ emit(context, event);
181
+ return this;
182
+ }
183
+
184
+ @JRubyMethod(required = 6)
185
+ public IRubyObject scalar(ThreadContext context, IRubyObject[] args) {
186
+ IRubyObject value = args[0];
187
+ IRubyObject anchor = args[1];
188
+ IRubyObject tag = args[2];
189
+ IRubyObject plain = args[3];
190
+ IRubyObject quoted = args[4];
191
+ IRubyObject style = args[5];
192
+
193
+ if (!(value instanceof RubyString)) {
194
+ throw context.runtime.newTypeError(value, context.runtime.getString());
195
+ }
196
+
197
+ ScalarEvent event = new ScalarEvent(
198
+ anchor.isNil() ? null : anchor.asJavaString(),
199
+ tag.isNil() ? null : tag.asJavaString(),
200
+ new ImplicitTuple(plain.isTrue(),
201
+ quoted.isTrue()),
202
+ value.asJavaString(),
203
+ NULL_MARK,
204
+ NULL_MARK,
205
+ SCALAR_STYLES[(int)style.convertToInteger().getLongValue()]);
206
+ emit(context, event);
207
+ return this;
208
+ }
209
+
210
+ @JRubyMethod(required = 4)
211
+ public IRubyObject start_sequence(ThreadContext context, IRubyObject[] args) {
212
+ IRubyObject anchor = args[0];
213
+ IRubyObject tag = args[1];
214
+ IRubyObject implicit = args[2];
215
+ IRubyObject style = args[3];
216
+
217
+ final int SEQUENCE_BLOCK = 1; // see psych/nodes/sequence.rb
218
+
219
+ SequenceStartEvent event = new SequenceStartEvent(
220
+ anchor.isNil() ? null : anchor.asJavaString(),
221
+ tag.isNil() ? null : tag.asJavaString(),
222
+ implicit.isTrue(),
223
+ NULL_MARK,
224
+ NULL_MARK,
225
+ SEQUENCE_BLOCK != style.convertToInteger().getLongValue());
226
+ emit(context, event);
227
+ return this;
228
+ }
229
+
230
+ @JRubyMethod
231
+ public IRubyObject end_sequence(ThreadContext context) {
232
+ SequenceEndEvent event = new SequenceEndEvent(NULL_MARK, NULL_MARK);
233
+ emit(context, event);
234
+ return this;
235
+ }
236
+
237
+ @JRubyMethod(required = 4)
238
+ public IRubyObject start_mapping(ThreadContext context, IRubyObject[] args) {
239
+ IRubyObject anchor = args[0];
240
+ IRubyObject tag = args[1];
241
+ IRubyObject implicit = args[2];
242
+ IRubyObject style = args[3];
243
+
244
+ final int MAPPING_BLOCK = 1; // see psych/nodes/mapping.rb
245
+
246
+ MappingStartEvent event = new MappingStartEvent(
247
+ anchor.isNil() ? null : anchor.asJavaString(),
248
+ tag.isNil() ? null : tag.asJavaString(),
249
+ implicit.isTrue(),
250
+ NULL_MARK,
251
+ NULL_MARK,
252
+ MAPPING_BLOCK != style.convertToInteger().getLongValue());
253
+ emit(context, event);
254
+ return this;
255
+ }
256
+
257
+ @JRubyMethod
258
+ public IRubyObject end_mapping(ThreadContext context) {
259
+ MappingEndEvent event = new MappingEndEvent(NULL_MARK, NULL_MARK);
260
+ emit(context, event);
261
+ return this;
262
+ }
263
+
264
+ @JRubyMethod
265
+ public IRubyObject alias(ThreadContext context, IRubyObject anchor) {
266
+ AliasEvent event = new AliasEvent(anchor.asJavaString(), NULL_MARK, NULL_MARK);
267
+ emit(context, event);
268
+ return this;
269
+ }
270
+
271
+ @JRubyMethod(name = "canonical=")
272
+ public IRubyObject canonical_set(ThreadContext context, IRubyObject canonical) {
273
+ // TODO: unclear if this affects a running emitter
274
+ options.setCanonical(canonical.isTrue());
275
+ return canonical;
276
+ }
277
+
278
+ @JRubyMethod
279
+ public IRubyObject canonical(ThreadContext context) {
280
+ // TODO: unclear if this affects a running emitter
281
+ return context.runtime.newBoolean(options.isCanonical());
282
+ }
283
+
284
+ @JRubyMethod(name = "indentation=")
285
+ public IRubyObject indentation_set(ThreadContext context, IRubyObject level) {
286
+ // TODO: unclear if this affects a running emitter
287
+ options.setIndent((int)level.convertToInteger().getLongValue());
288
+ return level;
289
+ }
290
+
291
+ @JRubyMethod
292
+ public IRubyObject indentation(ThreadContext context) {
293
+ // TODO: unclear if this affects a running emitter
294
+ return context.runtime.newFixnum(options.getIndent());
295
+ }
296
+
297
+ @JRubyMethod(name = "line_width=")
298
+ public IRubyObject line_width_set(ThreadContext context, IRubyObject width) {
299
+ options.setWidth((int)width.convertToInteger().getLongValue());
300
+ return width;
301
+ }
302
+
303
+ @JRubyMethod
304
+ public IRubyObject line_width(ThreadContext context) {
305
+ return context.runtime.newFixnum(options.getWidth());
306
+ }
307
+
308
+ private void emit(ThreadContext context, Event event) {
309
+ try {
310
+ if (emitter == null) throw context.runtime.newRuntimeError("uninitialized emitter");
311
+
312
+ emitter.emit(event);
313
+ } catch (IOException ioe) {
314
+ throw context.runtime.newIOErrorFromException(ioe);
315
+ } catch (EmitterException ee) {
316
+ throw context.runtime.newRuntimeError(ee.toString());
317
+ }
318
+ }
319
+
320
+ private void initEmitter(ThreadContext context, IRubyObject _encoding) {
321
+ if (emitter != null) throw context.runtime.newRuntimeError("already initialized emitter");
322
+
323
+ Encoding encoding = PsychLibrary.YAMLEncoding.values()[(int)_encoding.convertToInteger().getLongValue()].encoding;
324
+ Charset charset = context.runtime.getEncodingService().charsetForEncoding(encoding);
325
+
326
+ emitter = new Emitter(new OutputStreamWriter(new IOOutputStream(io, encoding), charset), options);
327
+ }
328
+
329
+ Emitter emitter;
330
+ DumperOptions options = new DumperOptions();
331
+ IRubyObject io;
332
+
333
+ private static final Mark NULL_MARK = new Mark(null, 0, 0, 0, null, 0);
334
+
335
+ // Map style constants from Psych values (ANY = 0 ... FOLDED = 5)
336
+ // to SnakeYaml values; see psych/nodes/scalar.rb.
337
+ private static final Character[] SCALAR_STYLES = new Character[] {
338
+ null, // ANY; we'll choose plain
339
+ null, // PLAIN
340
+ '\'', // SINGLE_QUOTED
341
+ '"', // DOUBLE_QUOTED
342
+ '|', // LITERAL
343
+ '>', // FOLDED
344
+ };
345
+ }