Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.dataformat.yaml;
  
  import java.io.*;
  import java.net.URL;
  
  import  org.yaml.snakeyaml.DumperOptions;
  import  org.yaml.snakeyaml.DumperOptions.FlowStyle;
  
 
 public class YAMLFactory extends JsonFactory
 {
    
Name used to identify YAML format. (and returned by getFormatName()
 
     public final static String FORMAT_NAME_YAML = "YAML";
    
    
Bitfield (set of flags) of all parser features that are enabled by default.
 
     final static int DEFAULT_YAML_PARSER_FEATURE_FLAGS = YAMLParser.Feature.collectDefaults();

    
Bitfield (set of flags) of all generator features that are enabled by default.
     
     final static int DEFAULT_YAML_GENERATOR_FEATURE_FLAGS = YAMLGenerator.Feature.collectDefaults();
 
     final static byte UTF8_BOM_1 = (byte) 0xEF;
     final static byte UTF8_BOM_2 = (byte) 0xBB;
     final static byte UTF8_BOM_3 = (byte) 0xBF;
     
     /*
     /**********************************************************************
     /* Configuration
     /**********************************************************************
      */
 
 
     
     /*
     /**********************************************************************
     /* Factory construction, configuration
     /**********************************************************************
      */
 
     protected DumperOptions _outputOptions;
 
     protected Integer[] _version;
    
    
Default constructor used to create factory instances. Creation of a factory instance is a light-weight operation, but it is still a good idea to reuse limited number of factory instances (and quite often just a single instance): factories are used as context for storing some reused processing objects (such as symbol tables parsers use) and this reuse only works within context of a single factory instance.
 
     public YAMLFactory() { this(null); }
 
     public YAMLFactory(ObjectCodec oc)
     {
         super(oc);
          = _defaultOptions();
         DumperOptions.Version version = .getVersion();
          = (version == null) ? null : version.getArray();
     }
 
     private static DumperOptions _defaultOptions()
     {
         DumperOptions opt = new DumperOptions();
         // would we want canonical?
         opt.setCanonical(false);
         // if not, MUST specify flow styles
         opt.setDefaultFlowStyle(FlowStyle.BLOCK);
         return opt;
         
     }
     
     /*                                                                                       
     /**********************************************************                              
     /* Versioned                                                                             
     /**********************************************************                              
      */
 
     @Override
     public Version version() {
         return ..version();
     }
    
    /*
    /**********************************************************
    /* Format detection functionality (since 1.8)
    /**********************************************************
     */
    
    @Override
    public String getFormatName() {
        return ;
    }
    
    
Sub-classes need to override this method (as of 1.8)
    @Override
    public MatchStrength hasFormat(InputAccessor accthrows IOException
    {
        /* Actually quite possible to do, thanks to (optional) "---"
         * indicator we may be getting...
         */
        if (!acc.hasMoreBytes()) {
            return .;
        }
        byte b = acc.nextByte();
        // Very first thing, a UTF-8 BOM?
        if (b == ) { // yes, looks like UTF-8 BOM
            if (!acc.hasMoreBytes()) {
                return .;
            }
            if (acc.nextByte() != ) {
                return .;
            }
            if (!acc.hasMoreBytes()) {
                return .;
            }
            if (acc.nextByte() != ) {
                return .;
            }
            if (!acc.hasMoreBytes()) {
                return .;
            }
            b = acc.nextByte();
        }
        // as far as I know, leading space is NOT allowed before "---" marker?
        if (b == '-' && (acc.hasMoreBytes() && acc.nextByte() == '-')
                && (acc.hasMoreBytes() && acc.nextByte() == '-')) {
            return .;
        }
        return .;
    }
    
    /*
    /**********************************************************
    /* Configuration, parser settings
    /**********************************************************
     */

    
Method for enabling or disabling specified parser feature (check YAMLParser.Feature for list of features)
    public final YAMLFactory configure(YAMLParser.Feature fboolean state)
    {
        if (state) {
            enable(f);
        } else {
            disable(f);
        }
        return this;
    }

    
Method for enabling specified parser feature (check YAMLParser.Feature for list of features)
    public YAMLFactory enable(YAMLParser.Feature f) {
         |= f.getMask();
        return this;
    }

    
Method for disabling specified parser features (check YAMLParser.Feature for list of features)
    public YAMLFactory disable(YAMLParser.Feature f) {
         &= ~f.getMask();
        return this;
    }

    
Checked whether specified parser feature is enabled.
    public final boolean isEnabled(YAMLParser.Feature f) {
        return ( & f.getMask()) != 0;
    }
    /*
    /**********************************************************
    /* Configuration, generator settings
    /**********************************************************
     */

    
Method for enabling or disabling specified generator feature (check YAMLGenerator.Feature for list of features)
    public final YAMLFactory configure(YAMLGenerator.Feature fboolean state) {
        if (state) {
            enable(f);
        } else {
            disable(f);
        }
        return this;
    }


    
Method for enabling specified generator features (check YAMLGenerator.Feature for list of features)
    public YAMLFactory enable(YAMLGenerator.Feature f) {
         |= f.getMask();
        return this;
    }

    
Method for disabling specified generator feature (check YAMLGenerator.Feature for list of features)
         &= ~f.getMask();
        return this;
    }

    
Check whether specified generator feature is enabled.
    public final boolean isEnabled(YAMLGenerator.Feature f) {
        return ( & f.getMask()) != 0;
    }
    /*
    /**********************************************************
    /* Overridden parser factory methods (for 2.1)
    /**********************************************************
     */
    @Override
    public YAMLParser createParser(String content)
        throws IOExceptionJsonParseException
    {
        Reader r = new StringReader(content);
        IOContext ctxt = _createContext(rtrue); // true->own, can close
        // [JACKSON-512]: allow wrapping with InputDecorator
        if ( != null) {
            r = .decorate(ctxtr);
        }
        return _createParser(rctxt);
    }
    @Override
    public YAMLParser createParser(File f)
        throws IOExceptionJsonParseException
    {
        IOContext ctxt = _createContext(ftrue);
        InputStream in = new FileInputStream(f);
        // [JACKSON-512]: allow wrapping with InputDecorator
        if ( != null) {
            in = .decorate(ctxtin);
        }
        return _createParser(inctxt);
    }
    
    @Override
    public YAMLParser createParser(URL url)
        throws IOExceptionJsonParseException
    {
        IOContext ctxt = _createContext(urltrue);
        InputStream in = _optimizedStreamFromURL(url);
        // [JACKSON-512]: allow wrapping with InputDecorator
        if ( != null) {
            in = .decorate(ctxtin);
        }
        return _createParser(inctxt);
    }
    @Override
    public YAMLParser createParser(InputStream in)
        throws IOExceptionJsonParseException
    {
        IOContext ctxt = _createContext(infalse);
        // [JACKSON-512]: allow wrapping with InputDecorator
        if ( != null) {
            in = .decorate(ctxtin);
        }
        return _createParser(inctxt);
    }
    @Override
    public JsonParser createParser(Reader r)
        throws IOExceptionJsonParseException
    {
        IOContext ctxt = _createContext(rfalse);
        if ( != null) {
            r = .decorate(ctxtr);
        }
        return _createParser(rctxt);
    }
    @Override
    public YAMLParser createParser(byte[] data)
        throws IOExceptionJsonParseException
    {
        IOContext ctxt = _createContext(datatrue);
        // [JACKSON-512]: allow wrapping with InputDecorator
        if ( != null) {
            InputStream in = .decorate(ctxtdata, 0, data.length);
            if (in != null) {
                return _createParser(inctxt);
            }
        }
        return _createParser(data, 0, data.lengthctxt);
    }
    
    @Override
    public YAMLParser createParser(byte[] dataint offsetint len)
        throws IOExceptionJsonParseException
    {
        IOContext ctxt = _createContext(datatrue);
        // [JACKSON-512]: allow wrapping with InputDecorator
        if ( != null) {
            InputStream in = .decorate(ctxtdataoffsetlen);
            if (in != null) {
                return _createParser(inctxt);
            }
        }
        return _createParser(dataoffsetlenctxt);
    }
    
    /*
    /**********************************************************
    /* Overridden parser factory methods (2.0 and prior)
    /**********************************************************
     */
    @Override
    public YAMLParser createJsonParser(String content)
        throws IOExceptionJsonParseException
    {
        Reader r = new StringReader(content);
        IOContext ctxt = _createContext(rtrue); // true->own, can close
        // [JACKSON-512]: allow wrapping with InputDecorator
        if ( != null) {
            r = .decorate(ctxtr);
        }
        return _createParser(rctxt);
    }
    @Override
    public YAMLParser createJsonParser(File f)
        throws IOExceptionJsonParseException
    {
        IOContext ctxt = _createContext(ftrue);
        InputStream in = new FileInputStream(f);
        // [JACKSON-512]: allow wrapping with InputDecorator
        if ( != null) {
            in = .decorate(ctxtin);
        }
        return _createParser(inctxt);
    }
    
    @Override
    public YAMLParser createJsonParser(URL url)
        throws IOExceptionJsonParseException
    {
        IOContext ctxt = _createContext(urltrue);
        InputStream in = _optimizedStreamFromURL(url);
        // [JACKSON-512]: allow wrapping with InputDecorator
        if ( != null) {
            in = .decorate(ctxtin);
        }
        return _createParser(inctxt);
    }
    @Override
        throws IOExceptionJsonParseException
    {
        IOContext ctxt = _createContext(infalse);
        // [JACKSON-512]: allow wrapping with InputDecorator
        if ( != null) {
            in = .decorate(ctxtin);
        }
        return _createParser(inctxt);
    }
    @Override
        throws IOExceptionJsonParseException
    {
        IOContext ctxt = _createContext(rfalse);
        if ( != null) {
            r = .decorate(ctxtr);
        }
        return _createParser(rctxt);
    }
    @Override
    public YAMLParser createJsonParser(byte[] data)
        throws IOExceptionJsonParseException
    {
        IOContext ctxt = _createContext(datatrue);
        // [JACKSON-512]: allow wrapping with InputDecorator
        if ( != null) {
            InputStream in = .decorate(ctxtdata, 0, data.length);
            if (in != null) {
                return _createParser(inctxt);
            }
        }
        return _createParser(data, 0, data.lengthctxt);
    }
    
    @Override
    public YAMLParser createJsonParser(byte[] dataint offsetint len)
        throws IOExceptionJsonParseException
    {
        IOContext ctxt = _createContext(datatrue);
        // [JACKSON-512]: allow wrapping with InputDecorator
        if ( != null) {
            InputStream in = .decorate(ctxtdataoffsetlen);
            if (in != null) {
                return _createParser(inctxt);
            }
        }
        return _createParser(dataoffsetlenctxt);
    }
    /*
    /**********************************************************
    /* Overridden generator factory methods (2.1)
    /**********************************************************
     */
    @Override
    {
        // false -> we won't manage the stream unless explicitly directed to
        IOContext ctxt = _createContext(outfalse);
        // [JACKSON-512]: allow wrapping with _outputDecorator
        if ( != null) {
            out = .decorate(ctxtout);
        }
        return _createGenerator(_createWriter(out.ctxt), ctxt);
    }
    @Override
    {
        // false -> we won't manage the stream unless explicitly directed to
        IOContext ctxt = _createContext(outfalse);
        // [JACKSON-512]: allow wrapping with _outputDecorator
        if ( != null) {
            out = .decorate(ctxtout);
        }
        return _createGenerator(_createWriter(out.ctxt), ctxt);
    }
    
    @Override
    public YAMLGenerator createGenerator(Writer outthrows IOException
    {
        IOContext ctxt = _createContext(outfalse);
        // [JACKSON-512]: allow wrapping with _outputDecorator
        if ( != null) {
            out = .decorate(ctxtout);
        }
        return _createGenerator(outctxt);
    }
    
    /*
    /**********************************************************
    /* Overridden generator factory methods (2.0 and before)
    /**********************************************************
     */
    @Override
        return createGenerator(outenc);
    }
    @Override
        return createGenerator(out);
    }
    
    @Override
    public YAMLGenerator createJsonGenerator(Writer outthrows IOException {
        return createGenerator(out);
    }
    
    /*
    /******************************************************
    /* Overridden internal factory methods
    /******************************************************
     */
    //protected IOContext _createContext(Object srcRef, boolean resourceManaged)
    @Override
    protected YAMLParser _createParser(InputStream inIOContext ctxt)
        throws IOExceptionJsonParseException
    {
        Reader r = _createReader(innullctxt);
        return new YAMLParser(ctxt_getBufferRecycler(), ,
                r);
    }
    @Override
    protected YAMLParser _createParser(Reader rIOContext ctxt)
        throws IOExceptionJsonParseException
    {
        return new YAMLParser(ctxt_getBufferRecycler(), ,
                r);
    }
    @Override
    protected YAMLParser _createParser(byte[] dataint offsetint lenIOContext ctxt)
        throws IOExceptionJsonParseException
    {
        Reader r = _createReader(dataoffsetlennullctxt);
        return new YAMLParser(ctxt_getBufferRecycler(), ,
                r);
    }
    @Override
    protected YAMLParser _createJsonParser(InputStream inIOContext ctxt)
        throws IOExceptionJsonParseException
    {
        return _createParser(inctxt);
    }
    @Override
    protected JsonParser _createJsonParser(Reader rIOContext ctxt)
        throws IOExceptionJsonParseException
    {
        return _createParser(rctxt);
    }
    @Override
    protected YAMLParser _createJsonParser(byte[] dataint offsetint lenIOContext ctxt)
        throws IOExceptionJsonParseException
    {
        return _createParser(dataoffsetlenctxt);
    }
    @Override
    protected YAMLGenerator _createGenerator(Writer outIOContext ctxt)
        throws IOException
    {
        int feats = ;
        YAMLGenerator gen = new YAMLGenerator(ctxtfeats,
                out);
        // any other initializations? No?
        return gen;
    }
    @Override
    protected YAMLGenerator _createJsonGenerator(Writer outIOContext ctxt)
        throws IOException
    {
        return _createGenerator(outctxt);
    }
    @Override
    protected YAMLGenerator _createUTF8Generator(OutputStream outIOContext ctxtthrows IOException {
        throw new IllegalStateException("Method should never get called");
    }
    @Override
    @Deprecated
    protected YAMLGenerator _createUTF8JsonGenerator(OutputStream outIOContext ctxtthrows IOException {
        return _createUTF8Generator(outctxt);
    }
    
    @Override
    protected Writer _createWriter(OutputStream outJsonEncoding encIOContext ctxtthrows IOException
    {
        if (enc == .) {
            return new UTF8Writer(out);
        }
        return new OutputStreamWriter(outenc.getJavaName());
    }
    
    /*
    /**********************************************************
    /* Internal methods
    /**********************************************************
     */
    
    protected final Charset UTF8 = Charset.forName("UTF-8");
    protected Reader _createReader(InputStream inJsonEncoding encIOContext ctxtthrows IOException
    {
        if (enc == null) {
            enc = .;
        }
        // default to UTF-8 if encoding missing
        if (enc == .) {
            boolean autoClose = ctxt.isResourceManaged() || this.isEnabled(..);
            return new UTF8Reader(inautoClose);
//          return new InputStreamReader(in, UTF8);
        }
        return new InputStreamReader(inenc.getJavaName());
    }
    protected Reader _createReader(byte[] dataint offsetint len,
            JsonEncoding encIOContext ctxtthrows IOException
    {
        if (enc == null) {
            enc = .;
        }
        // default to UTF-8 if encoding missing
        if (enc == null || enc == .) {
            return new UTF8Reader(dataoffsetlentrue);
        }
        ByteArrayInputStream in = new ByteArrayInputStream(dataoffsetlen);
        return new InputStreamReader(inenc.getJavaName());
    }
New to GrepCode? Check out our FAQ X