Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.dataformat.yaml;
  
  import java.io.*;
  import java.util.Arrays;
  
  import  org.yaml.snakeyaml.DumperOptions;
 import  org.yaml.snakeyaml.emitter.Emitter;
 import  org.yaml.snakeyaml.events.*;
 
 
 public class YAMLGenerator extends GeneratorBase
 {
    
Enumeration that defines all togglable features for YAML generators
 
     public enum Feature {
         BOGUS(false// placeholder
         ;
 
         protected final boolean _defaultState;
         protected final int _mask;
        
        
Method that calculates bit set (flags) of all features that are enabled by default.
 
         public static int collectDefaults()
         {
             int flags = 0;
             for (Feature f : values()) {
                 if (f.enabledByDefault()) {
                     flags |= f.getMask();
                 }
             }
             return flags;
         }
         
         private Feature(boolean defaultState) {
              = defaultState;
              = (1 << ordinal());
         }
         
         public boolean enabledByDefault() { return ; }
         public int getMask() { return ; }
     };
 
     protected final static long MIN_INT_AS_LONG = (long.;
     protected final static long MAX_INT_AS_LONG = (long.;
     
     /*
     /**********************************************************
     /* Configuration
     /**********************************************************
      */
 
     final protected IOContext _ioContext;

    
Bit flag composed of bits that indicate which org.codehaus.jackson.smile.SmileGenerator.Features are enabled.
 
     protected int _yamlFeatures;
 
     protected Writer _writer;
 
     protected DumperOptions _outputOptions;
 
     // for field names, leave out quotes
     private final static Character STYLE_NAME = null;
     
     // numbers, booleans, should use implicit
     private final static Character STYLE_SCALAR = null;
     // Strings quoted for fun
     private final static Character STYLE_STRING = Character.valueOf('"');
         
     // Which flow style to use for Base64? Maybe basic quoted?
     private final static Character STYLE_BASE64 = Character.valueOf('"');
     
     /*
     /**********************************************************
     /* Output state
     /**********************************************************
      */
 
     protected Emitter _emitter;
     
     /*
     /**********************************************************
     /* Life-cycle
     /**********************************************************
      */
    
    public YAMLGenerator(IOContext ctxtint jsonFeaturesint yamlFeatures,
            ObjectCodec codecWriter out,
            DumperOptions outputOptionsInteger[] version
            ) throws IOException
    {
        super(jsonFeaturescodec);
         = ctxt;
         = yamlFeatures;
         = out;
         = new Emitter(outputOptions);
         = outputOptions;
        // should we start output now, or try to defer?
        .emit(new StreamStartEvent(nullnull));
        .emit(new DocumentStartEvent(nullnull/*explicit start*/ false,
                version/*tags*/ Collections.<String,String>emptyMap()));
    }
    /*                                                                                       
    /**********************************************************                              
    /* Versioned                                                                             
    /**********************************************************                              
     */
    @Override
    public Version version() {
        return ..version();
    }
    /*
    /**********************************************************
    /* Overridden methods, configuration
    /**********************************************************
     */

    
Not sure what to do here; could reset indentation to some value maybe?
    @Override
    {
        return this;
    }

    
Not sure what to do here; will always indent, but uses YAML-specific settings etc.
    @Override
        return this;
    }
    @Override
    public Object getOutputTarget() {
        return ;
    }
    @Override
    public boolean canUseSchema(FormatSchema schema) {
        return false;
    }
    
    //@Override public void setSchema(FormatSchema schema)
    /*
    /**********************************************************************
    /* Overridden methods; writing field names
    /**********************************************************************
     */
    
    /* And then methods overridden to make final, streamline some
     * aspects...
     */
    @Override
    public final void writeFieldName(String namethrows IOExceptionJsonGenerationException
    {
            _reportError("Can not write a field name, expecting a value");
        }
        _writeFieldName(name);
    }
    @Override
    public final void writeFieldName(SerializableString name)
        throws IOExceptionJsonGenerationException
    {
        // Object is a value, need to verify it's allowed
            _reportError("Can not write a field name, expecting a value");
        }
        _writeFieldName(name.getValue());
    }
    @Override
    public final void writeStringField(String fieldNameString value)
        throws IOExceptionJsonGenerationException
    {
        if (.writeFieldName(fieldName) == .) {
            _reportError("Can not write a field name, expecting a value");
        }
        _writeFieldName(fieldName);
        writeString(value);
    }
    private final void _writeFieldName(String name)
        throws IOExceptionJsonGenerationException
    {
        _writeScalar(name"string");
    }
    
    /*
    /**********************************************************
    /* Extended API, configuration
    /**********************************************************
     */
    public YAMLGenerator enable(Feature f) {
         |= f.getMask();
        return this;
    }
    public YAMLGenerator disable(Feature f) {
         &= ~f.getMask();
        return this;
    }
    public final boolean isEnabled(Feature f) {
        return ( & f.getMask()) != 0;
    }
    public YAMLGenerator configure(Feature fboolean state) {
        if (state) {
            enable(f);
        } else {
            disable(f);
        }
        return this;
    }
    /*
    /**********************************************************
    /* Public API: low-level I/O
    /**********************************************************
     */
    @Override
    public final void flush() throws IOException
    {
        .flush();
    }
    
    @Override
    public void close() throws IOException
    {
        .emit(new DocumentEndEvent(nullnullfalse));
        .emit(new StreamEndEvent(nullnull));
        super.close();
        .close();
    }
    /*
    /**********************************************************
    /* Public API: structural output
    /**********************************************************
     */
    
    @Override
    public final void writeStartArray() throws IOExceptionJsonGenerationException
    {
        _verifyValueWrite("start an array");
        Boolean style = .getDefaultFlowStyle().getStyleBoolean();
        // note: can NOT be implicit, to avoid having to specify tag
        .emit(new SequenceStartEvent(/*anchor*/null/*tag*/null,
                /*implicit*/ true,  nullnullstyle));
    }
    
    @Override
    public final void writeEndArray() throws IOExceptionJsonGenerationException
    {
        if (!.inArray()) {
            _reportError("Current context not an ARRAY but "+.getTypeDesc());
        }
        .emit(new SequenceEndEvent(nullnull));
    }
    @Override
    public final void writeStartObject() throws IOExceptionJsonGenerationException
    {
        _verifyValueWrite("start an object");
        Boolean style = .getDefaultFlowStyle().getStyleBoolean();
        // note: can NOT be implicit, to avoid having to specify tag
        .emit(new MappingStartEvent(/* anchor */nullnull//TAG_OBJECT,
                /*implicit*/truenullnullstyle));
    }
    @Override
    public final void writeEndObject() throws IOExceptionJsonGenerationException
    {
        if (!.inObject()) {
            _reportError("Current context not an object but "+.getTypeDesc());
        }
        .emit(new MappingEndEvent(nullnull));
    }
    
    /*
    /**********************************************************
    /* Output method implementations, textual
    /**********************************************************
     */
    @Override
    public void writeString(String textthrows IOException,JsonGenerationException
    {
        if (text == null) {
            writeNull();
            return;
        }
        _verifyValueWrite("write String value");
        _writeScalar(text"string");
    }
    @Override
    public void writeString(char[] textint offsetint lenthrows IOExceptionJsonGenerationException
    {
        writeString(new String(textoffsetlen));
    }
    @Override
    public final void writeString(SerializableString sstr)
        throws IOExceptionJsonGenerationException
    {
        writeString(sstr.toString());
    }
    @Override
    public void writeRawUTF8String(byte[] textint offsetint len)
        throws IOExceptionJsonGenerationException
    {
    }
    @Override
    public final void writeUTF8String(byte[] textint offsetint len)
        throws IOExceptionJsonGenerationException
    {
        writeString(new String(textoffsetlen"UTF-8"));
    }
    /*
    /**********************************************************
    /* Output method implementations, unprocessed ("raw")
    /**********************************************************
     */
    @Override
    public void writeRaw(String textthrows IOExceptionJsonGenerationException {
    }
    @Override
    public void writeRaw(String textint offsetint lenthrows IOExceptionJsonGenerationException {
    }
    @Override
    public void writeRaw(char[] textint offsetint lenthrows IOExceptionJsonGenerationException {
    }
    @Override
    public void writeRaw(char cthrows IOExceptionJsonGenerationException {
    }
    @Override
    public void writeRawValue(String textthrows IOExceptionJsonGenerationException {
    }
    @Override
    public void writeRawValue(String textint offsetint lenthrows IOExceptionJsonGenerationException {
    }
    @Override
    public void writeRawValue(char[] textint offsetint lenthrows IOExceptionJsonGenerationException {
    }
    /*
    /**********************************************************
    /* Output method implementations, base64-encoded binary
    /**********************************************************
     */
    
    @Override
    public void writeBinary(Base64Variant b64variantbyte[] dataint offsetint lenthrows IOExceptionJsonGenerationException
    {
        if (data == null) {
            writeNull();
            return;
        }
        _verifyValueWrite("write Binary value");
        // ok, better just Base64 encode as a String...
        if (offset > 0 || (offset+len) != data.length) {
            data = Arrays.copyOfRange(dataoffsetoffset+len);
        }
        String encoded = b64variant.encode(data);
        _writeScalar(encoded"byte[]");
    }
    /*
    /**********************************************************
    /* Output method implementations, primitive
    /**********************************************************
     */
    @Override
    public void writeBoolean(boolean statethrows IOExceptionJsonGenerationException
    {
        _verifyValueWrite("write boolean value");
        _writeScalar(state ? "true" : "false""bool");
    }
    @Override
    public void writeNull() throws IOExceptionJsonGenerationException
    {
        _verifyValueWrite("write null value");
        // no real type for this, is there?
        _writeScalar("null""object");
    }
    @Override
    public void writeNumber(int ithrows IOExceptionJsonGenerationException
    {
        _verifyValueWrite("write number");
        _writeScalar(String.valueOf(i), "int");
    }
    @Override
    public void writeNumber(long lthrows IOExceptionJsonGenerationException
    {
        // First: maybe 32 bits is enough?
        if (l <=  && l >= ) {
            writeNumber((intl);
            return;
        }
        _verifyValueWrite("write number");
        _writeScalar(String.valueOf(l), "long");
    }
    @Override
    public void writeNumber(BigInteger vthrows IOExceptionJsonGenerationException
    {
        if (v == null) {
            writeNull();
            return;
        }
        _verifyValueWrite("write number");
        _writeScalar(String.valueOf(v.toString()), "java.math.BigInteger");
    }
    
    @Override
    public void writeNumber(double dthrows IOExceptionJsonGenerationException
    {
        _verifyValueWrite("write number");
        _writeScalar(String.valueOf(d), "double");
    }    
    @Override
    public void writeNumber(float fthrows IOExceptionJsonGenerationException
    {
        _verifyValueWrite("write number");
        _writeScalar(String.valueOf(f), "float");
    }
    @Override
    public void writeNumber(BigDecimal decthrows IOExceptionJsonGenerationException
    {
        if (dec == null) {
            writeNull();
            return;
        }
        _verifyValueWrite("write number");
        _writeScalar(dec.toString(), "java.math.BigDecimal");
    }
    @Override
    {
        if (encodedValue == null) {
            writeNull();
            return;
        }
        _verifyValueWrite("write number");
        _writeScalar(encodedValue"number");
    }
    /*
    /**********************************************************
    /* Implementations for methods from base class
    /**********************************************************
     */
    
    @Override
    protected final void _verifyValueWrite(String typeMsg)
        throws IOExceptionJsonGenerationException
    {
        int status = .writeValue();
        if (status == .) {
            _reportError("Can not "+typeMsg+", expecting field name");
        }
    }
    @Override
    protected void _releaseBuffers() {
        // nothing special to do...
    }
    /*
    /**********************************************************
    /* Internal methods
    /**********************************************************
     */
    // Implicit means that (type) tags won't be shown, right?
    private final static ImplicitTuple DEFAULT_IMPLICIT = new ImplicitTuple(truetrue);
    protected void _writeScalar(String valueString typeCharacter stylethrows IOException
    {
        .emit(_scalarEvent(valuetypestyle));
    }
    
    protected ScalarEvent _scalarEvent(String valueString tagCharacter style)
    {
        // 'type' can be used as 'tag'... but should we?
        return new ScalarEvent(nullnullvalue,
                nullnullstyle);
    }
New to GrepCode? Check out our FAQ X