Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge remote-tracking branch 'upstream/master'

  • Loading branch information...
commit 7da35ab9dc7eb5031b5016f3e3cca0fe323b7dac 2 parents 8fbe70c + da0f2eb
Patrick Toomey authored
28 src/org/jruby/ext/psych/PsychEmitter.java
@@ -29,8 +29,12 @@
29 29
30 30 import java.io.IOException;
31 31 import java.io.OutputStreamWriter;
  32 +import java.io.UnsupportedEncodingException;
  33 +import java.nio.charset.Charset;
32 34 import java.util.HashMap;
33 35 import java.util.Map;
  36 +
  37 +import org.jcodings.Encoding;
34 38 import org.jruby.Ruby;
35 39 import org.jruby.RubyArray;
36 40 import org.jruby.RubyClass;
@@ -81,7 +85,8 @@ public PsychEmitter(Ruby runtime, RubyClass klass) {
81 85 public IRubyObject initialize(ThreadContext context, IRubyObject io) {
82 86 options = new DumperOptions();
83 87 options.setIndent(2);
84   - emitter = new Emitter(new OutputStreamWriter(new IOOutputStream(io)), options);
  88 +
  89 + this.io = io;
85 90
86 91 return context.nil;
87 92 }
@@ -98,7 +103,7 @@ public IRubyObject initialize(ThreadContext context, IRubyObject io, IRubyObject
98 103 options.setIndent((int)level.convertToInteger().getLongValue());
99 104 options.setWidth((int)width.convertToInteger().getLongValue());
100 105
101   - emitter = new Emitter(new OutputStreamWriter(new IOOutputStream(io)), options);
  106 + this.io = io;
102 107
103 108 return context.nil;
104 109 }
@@ -108,10 +113,13 @@ public IRubyObject start_stream(ThreadContext context, IRubyObject encoding) {
108 113 if (!(encoding instanceof RubyFixnum)) {
109 114 throw context.runtime.newTypeError(encoding, context.runtime.getFixnum());
110 115 }
111   -
112   - // TODO: do something with encoding? perhaps at the stream level?
  116 +
  117 + initEmitter(context, encoding);
  118 +
113 119 StreamStartEvent event = new StreamStartEvent(NULL_MARK, NULL_MARK);
  120 +
114 121 emit(context, event);
  122 +
115 123 return this;
116 124 }
117 125
@@ -299,6 +307,8 @@ public IRubyObject line_width(ThreadContext context) {
299 307
300 308 private void emit(ThreadContext context, Event event) {
301 309 try {
  310 + if (emitter == null) throw context.runtime.newRuntimeError("uninitialized emitter");
  311 +
302 312 emitter.emit(event);
303 313 } catch (IOException ioe) {
304 314 throw context.runtime.newIOErrorFromException(ioe);
@@ -307,8 +317,18 @@ private void emit(ThreadContext context, Event event) {
307 317 }
308 318 }
309 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.YAML_ENCODING.values()[(int)_encoding.convertToInteger().getLongValue()].encoding;
  324 + Charset charset = context.runtime.getEncodingService().charsetForEncoding(encoding);
  325 +
  326 + emitter = new Emitter(new OutputStreamWriter(new IOOutputStream(io), charset), options);
  327 + }
  328 +
310 329 Emitter emitter;
311 330 DumperOptions options = new DumperOptions();
  331 + IRubyObject io;
312 332
313 333 private static final Mark NULL_MARK = new Mark(null, 0, 0, 0, null, 0);
314 334
17 src/org/jruby/ext/psych/PsychLibrary.java
@@ -27,6 +27,10 @@
27 27 ***** END LICENSE BLOCK *****/
28 28 package org.jruby.ext.psych;
29 29
  30 +import org.jcodings.Encoding;
  31 +import org.jcodings.specific.UTF16BEEncoding;
  32 +import org.jcodings.specific.UTF16LEEncoding;
  33 +import org.jcodings.specific.UTF8Encoding;
30 34 import org.jruby.Ruby;
31 35 import org.jruby.RubyArray;
32 36 import org.jruby.RubyModule;
@@ -60,4 +64,17 @@ public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule claz
60 64 PsychToRuby.initPsychToRuby(runtime, psych);
61 65 PsychYamlTree.initPsychYamlTree(runtime, psych);
62 66 }
  67 +
  68 + public enum YAML_ENCODING {
  69 + YAML_ANY_ENCODING(UTF8Encoding.INSTANCE),
  70 + YAML_UTF8_ENCODING(UTF8Encoding.INSTANCE),
  71 + YAML_UTF16LE_ENCODING(UTF16LEEncoding.INSTANCE),
  72 + YAML_UTF16BE_ENCODING(UTF16BEEncoding.INSTANCE);
  73 +
  74 + YAML_ENCODING(Encoding encoding) {
  75 + this.encoding = encoding;
  76 + }
  77 +
  78 + public final Encoding encoding;
  79 + }
63 80 }
16 src/org/jruby/ext/psych/PsychParser.java
@@ -44,6 +44,7 @@
44 44 import org.jruby.RubyObject;
45 45 import org.jruby.RubyString;
46 46 import org.jruby.anno.JRubyMethod;
  47 +import static org.jruby.ext.psych.PsychLibrary.YAML_ENCODING.*;
47 48 import org.jruby.runtime.Block;
48 49 import org.jruby.runtime.ObjectAllocator;
49 50 import org.jruby.runtime.ThreadContext;
@@ -75,11 +76,6 @@
75 76 public class PsychParser extends RubyObject {
76 77
77 78 private static final Logger LOG = LoggerFactory.getLogger("PsychParser");
78   -
79   - public static final int YAML_ANY_ENCODING = 0;
80   - public static final int YAML_UTF8_ENCODING = UTF8Encoding.INSTANCE.getIndex();
81   - public static final int YAML_UTF16LE_ENCODING = UTF16LEEncoding.INSTANCE.getIndex();
82   - public static final int YAML_UTF16BE_ENCODING = UTF16BEEncoding.INSTANCE.getIndex();
83 79
84 80 public static void initPsychParser(Ruby runtime, RubyModule psych) {
85 81 RubyClass psychParser = runtime.defineClassUnder("Parser", runtime.getObject(), new ObjectAllocator() {
@@ -90,10 +86,10 @@ public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
90 86
91 87 RubyKernel.require(runtime.getNil(),
92 88 runtime.newString("psych/syntax_error"), Block.NULL_BLOCK);
93   - psychParser.defineConstant("ANY", runtime.newFixnum(YAML_ANY_ENCODING));
94   - psychParser.defineConstant("UTF8", runtime.newFixnum(YAML_UTF8_ENCODING));
95   - psychParser.defineConstant("UTF16LE", runtime.newFixnum(YAML_UTF16LE_ENCODING));
96   - psychParser.defineConstant("UTF16BE", runtime.newFixnum(YAML_UTF16BE_ENCODING));
  89 + psychParser.defineConstant("ANY", runtime.newFixnum(YAML_ANY_ENCODING.ordinal()));
  90 + psychParser.defineConstant("UTF8", runtime.newFixnum(YAML_UTF8_ENCODING.ordinal()));
  91 + psychParser.defineConstant("UTF16LE", runtime.newFixnum(YAML_UTF16LE_ENCODING.ordinal()));
  92 + psychParser.defineConstant("UTF16BE", runtime.newFixnum(YAML_UTF16BE_ENCODING.ordinal()));
97 93
98 94 psychParser.defineAnnotatedMethods(PsychParser.class);
99 95
@@ -154,7 +150,7 @@ public IRubyObject parse(ThreadContext context, IRubyObject yaml, IRubyObject pa
154 150
155 151 // FIXME: Event should expose a getID, so it can be switched
156 152 if (event.is(ID.StreamStart)) {
157   - invoke(context, handler, "start_stream", runtime.newFixnum(YAML_ANY_ENCODING));
  153 + invoke(context, handler, "start_stream", runtime.newFixnum(YAML_ANY_ENCODING.ordinal()));
158 154 } else if (event.is(ID.DocumentStart)) {
159 155 handleDocumentStart(context, (DocumentStartEvent) event, tainted, handler);
160 156 } else if (event.is(ID.DocumentEnd)) {

0 comments on commit 7da35ab

Please sign in to comment.
Something went wrong with that request. Please try again.