Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: CPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Common Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.eclipse.org/legal/cpl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2010 Charles O Nutter <headius@headius.com> Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the CPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the CPL, the GPL or the LGPL. END LICENSE BLOCK ***
 
 package org.jruby.ext.psych;
 
 import java.util.Map;
 
 import org.jruby.Ruby;
 
 import static org.jruby.runtime.Visibility.*;
 
 public class PsychEmitter extends RubyObject {
     public static void initPsychEmitter(Ruby runtimeRubyModule psych) {
         RubyClass psychHandler = runtime.defineClassUnder("Handler"runtime.getObject(), runtime.getObject().getAllocator(), psych);
         RubyClass psychEmitter = runtime.defineClassUnder("Emitter"psychHandlernew ObjectAllocator() {
             public IRubyObject allocate(Ruby runtimeRubyClass klazz) {
                 return new PsychEmitter(runtimeklazz);
             }
         }, psych);
 
         psychEmitter.defineAnnotatedMethods(PsychEmitter.class);
     }
 
     public PsychEmitter(Ruby runtimeRubyClass klass) {
         super(runtimeklass);
     }
 
     @JRubyMethod(visibility = )
     public IRubyObject initialize(ThreadContext contextIRubyObject io) {
          = new DumperOptions();
         .setIndent(2);
 
         this. = io;
 
         return context.nil;
     }
 
     @JRubyMethod(visibility = )
     public IRubyObject initialize(ThreadContext contextIRubyObject ioIRubyObject rbOptions) {
         IRubyObject width     = rbOptions.callMethod(context"line_width");
         IRubyObject canonical = rbOptions.callMethod(context"canonical");
         IRubyObject level     = rbOptions.callMethod(context"indentation");
 
         = new DumperOptions();
        .setCanonical(canonical.isTrue());
        .setIndent((int)level.convertToInteger().getLongValue());
        .setWidth((int)width.convertToInteger().getLongValue());
        this. = io;
        return context.nil;
    }
    public IRubyObject start_stream(ThreadContext contextIRubyObject encoding) {
        if (!(encoding instanceof RubyFixnum)) {
            throw context.runtime.newTypeError(encodingcontext.runtime.getFixnum());
        }
        initEmitter(contextencoding);
        StreamStartEvent event = new StreamStartEvent();
        emit(contextevent);
        return this;
    }
    public IRubyObject end_stream(ThreadContext context) {
        StreamEndEvent event = new StreamEndEvent();
        emit(contextevent);
        return this;
    }
    public IRubyObject start_document(ThreadContext contextIRubyObject _versionIRubyObject tagsIRubyObject implicit) {
        DumperOptions.Version version = null;
        boolean implicitBool = implicit.isTrue();
        Map<StringStringtagsMap = null;
        RubyArray versionAry = _version.convertToArray();
        if (versionAry.size() == 2) {
            int versionInt0 = (int)versionAry.eltInternal(0).convertToInteger().getLongValue();
            int versionInt1 = (int)versionAry.eltInternal(1).convertToInteger().getLongValue();
            if (versionInt0 == 1) {
                if (versionInt1 == 0) {
                    version = ..;
                } else if (versionInt1 == 1) {
                    version = ..;
                }
            }
            if (version == null) {
                throw context.runtime.newArgumentError("invalid YAML version: " + versionAry);
            }
        }
        RubyArray tagsAry = tags.convertToArray();
        if (tagsAry.size() > 0) {
            tagsMap = new HashMap<StringString>(tagsAry.size());
            for (int i = 0; i < tagsAry.size(); i++) {
                RubyArray tagsTuple = tagsAry.eltInternal(i).convertToArray();
                if (tagsTuple.size() != 2) {
                    throw context.runtime.newRuntimeError("tags tuple must be of length 2");
                }
                IRubyObject key = tagsTuple.eltInternal(0);
                IRubyObject value = tagsTuple.eltInternal(1);
                tagsMap.put(
                        key.asJavaString(),
                        value.asJavaString());
            }
        }
        DocumentStartEvent event = new DocumentStartEvent(, !implicitBoolversiontagsMap);
        emit(contextevent);
        return this;
    }
    public IRubyObject end_document(ThreadContext contextIRubyObject implicit) {
        DocumentEndEvent event = new DocumentEndEvent(, !implicit.isTrue());
        emit(contextevent);
        return this;
    }
    @JRubyMethod(required = 6)
    public IRubyObject scalar(ThreadContext contextIRubyObject[] args) {
        IRubyObject value = args[0];
        IRubyObject anchor = args[1];
        IRubyObject tag = args[2];
        IRubyObject plain = args[3];
        IRubyObject quoted = args[4];
        IRubyObject style = args[5];
        
        if (!(value instanceof RubyString)) {
            throw context.runtime.newTypeError(valuecontext.runtime.getString());
        }
        ScalarEvent event = new ScalarEvent(
                anchor.isNil() ? null : anchor.asJavaString(),
                tag.isNil() ? null : tag.asJavaString(),
                new ImplicitTuple(plain.isTrue(),
                quoted.isTrue()),
                value.asJavaString(),
                ,
                ,
                [(int)style.convertToInteger().getLongValue()]);
        emit(contextevent);
        return this;
    }
    @JRubyMethod(required = 4)
    public IRubyObject start_sequence(ThreadContext contextIRubyObject[] args) {
        IRubyObject anchor = args[0];
        IRubyObject tag = args[1];
        IRubyObject implicit = args[2];
        IRubyObject style = args[3];
        final int SEQUENCE_BLOCK = 1; // see psych/nodes/sequence.rb
        SequenceStartEvent event = new SequenceStartEvent(
                anchor.isNil() ? null : anchor.asJavaString(),
                tag.isNil() ? null : tag.asJavaString(),
                implicit.isTrue(),
                ,
                ,
                SEQUENCE_BLOCK != style.convertToInteger().getLongValue());
        emit(contextevent);
        return this;
    }
    public IRubyObject end_sequence(ThreadContext context) {
        SequenceEndEvent event = new SequenceEndEvent();
        emit(contextevent);
        return this;
    }
    @JRubyMethod(required = 4)
    public IRubyObject start_mapping(ThreadContext contextIRubyObject[] args) {
        IRubyObject anchor = args[0];
        IRubyObject tag = args[1];
        IRubyObject implicit = args[2];
        IRubyObject style = args[3];
        final int MAPPING_BLOCK = 1; // see psych/nodes/mapping.rb
        MappingStartEvent event = new MappingStartEvent(
                anchor.isNil() ? null : anchor.asJavaString(),
                tag.isNil() ? null : tag.asJavaString(),
                implicit.isTrue(),
                ,
                ,
                MAPPING_BLOCK != style.convertToInteger().getLongValue());
        emit(contextevent);
        return this;
    }
    public IRubyObject end_mapping(ThreadContext context) {
        MappingEndEvent event = new MappingEndEvent();
        emit(contextevent);
        return this;
    }
    
    public IRubyObject alias(ThreadContext contextIRubyObject anchor) {
        AliasEvent event = new AliasEvent(anchor.asJavaString(), );
        emit(contextevent);
        return this;
    }
    @JRubyMethod(name = "canonical=")
    public IRubyObject canonical_set(ThreadContext contextIRubyObject canonical) {
        // TODO: unclear if this affects a running emitter
        .setCanonical(canonical.isTrue());
        return canonical;
    }
    public IRubyObject canonical(ThreadContext context) {
        // TODO: unclear if this affects a running emitter
        return context.runtime.newBoolean(.isCanonical());
    }
    @JRubyMethod(name = "indentation=")
    public IRubyObject indentation_set(ThreadContext contextIRubyObject level) {
        // TODO: unclear if this affects a running emitter
        .setIndent((int)level.convertToInteger().getLongValue());
        return level;
    }
    public IRubyObject indentation(ThreadContext context) {
        // TODO: unclear if this affects a running emitter
        return context.runtime.newFixnum(.getIndent());
    }
    @JRubyMethod(name = "line_width=")
    public IRubyObject line_width_set(ThreadContext contextIRubyObject width) {
        .setWidth((int)width.convertToInteger().getLongValue());
        return width;
    }
    public IRubyObject line_width(ThreadContext context) {
        return context.runtime.newFixnum(.getWidth());
    }
    private void emit(ThreadContext contextEvent event) {
        try {
            if ( == nullthrow context.runtime.newRuntimeError("uninitialized emitter");
            .emit(event);
        } catch (IOException ioe) {
            throw context.runtime.newIOErrorFromException(ioe);
        } catch (EmitterException ee) {
            throw context.runtime.newRuntimeError(ee.toString());
        }
    }
    private void initEmitter(ThreadContext contextIRubyObject _encoding) {
        if ( != nullthrow context.runtime.newRuntimeError("already initialized emitter");
        Encoding encoding = PsychLibrary.YAMLEncoding.values()[(int)_encoding.convertToInteger().getLongValue()].;
        Charset charset = context.runtime.getEncodingService().charsetForEncoding(encoding);
         = new Emitter(new OutputStreamWriter(new IOOutputStream(), charset), );
    }
    IRubyObject io;
    private static final Mark NULL_MARK = new Mark(null, 0, 0, 0, null, 0);
    // Map style constants from Psych values (ANY = 0 ... FOLDED = 5)
    // to SnakeYaml values; see psych/nodes/scalar.rb.
    private static final Character[] SCALAR_STYLES = new Character[] {
        null// ANY; we'll choose plain
        null// PLAIN
        '\''// SINGLE_QUOTED
        '"',  // DOUBLE_QUOTED
        '|',  // LITERAL
        '>',  // FOLDED
    };
New to GrepCode? Check out our FAQ X