Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.core.util;
  
  import java.util.*;
  
Helper class that can be used to sequence multiple physical com.fasterxml.jackson.core.JsonParsers to create a single logical sequence of tokens, as a single com.fasterxml.jackson.core.JsonParser.

Fairly simple use of JsonParserDelegate: only need to override nextToken() to handle transition

 
 public class JsonParserSequence extends JsonParserDelegate
 {
    
Parsers other than the first one (which is initially assigned as delegate)
 
     protected final JsonParser[] _parsers;
    
    
Index of the next parser in _parsers.
 
     protected int _nextParser;
     
     /*
      *******************************************************
      * Construction
      *******************************************************
      */
 
     protected JsonParserSequence(JsonParser[] parsers)
     {
         super(parsers[0]);
          = parsers;
          = 1;
     }

    
Method that will construct a parser (possibly a sequence) that contains all given sub-parsers. All parsers given are checked to see if they are sequences: and if so, they will be "flattened", that is, contained parsers are directly added in a new sequence instead of adding sequences within sequences. This is done to minimize delegation depth, ideally only having just a single level of delegation.
 
     public static JsonParserSequence createFlattened(JsonParser firstJsonParser second)
     {
         if (!(first instanceof JsonParserSequence || second instanceof JsonParserSequence)) {
             // simple:
             return new JsonParserSequence(new JsonParser[] { firstsecond });
         }
         ArrayList<JsonParserp = new ArrayList<JsonParser>();
         if (first instanceof JsonParserSequence) {
             ((JsonParserSequencefirst).addFlattenedActiveParsers(p);
         } else {
             p.add(first);
         }
         if (second instanceof JsonParserSequence) {
             ((JsonParserSequencesecond).addFlattenedActiveParsers(p);
         } else {
             p.add(second);
         }
         return new JsonParserSequence(p.toArray(new JsonParser[p.size()]));
     }
 
     protected void addFlattenedActiveParsers(List<JsonParserresult)
     {
         for (int i = -1, len = .i < len; ++i) {
             JsonParser p = [i];
             if (p instanceof JsonParserSequence) {
                 ((JsonParserSequencep).addFlattenedActiveParsers(result);
             } else {
                 result.add(p);
             }
         }
     }
     
     /*
      *******************************************************
      * Overridden methods, needed: cases where default
      * delegation does not work
      *******************************************************
      */
     
     @Override
     public void close() throws IOException
     {
         do {
             .close();
         } while (switchToNext());
     }
 
     @Override
     public JsonToken nextToken() throws IOExceptionJsonParseException
    {
        JsonToken t = .nextToken();
        if (t != nullreturn t;
        while (switchToNext()) {
            t = .nextToken();
            if (t != nullreturn t;
        }
        return null;
    }
    /*
    /*******************************************************
    /* Additional extended API
    /*******************************************************
     */

    
Method that is most useful for debugging or testing; returns actual number of underlying parsers sequence was constructed with (nor just ones remaining active)
    public int containedParsersCount() {
        return .;
    }
    
    /*
    /*******************************************************
    /* Helper methods
    /*******************************************************
     */

    
Method that will switch active parser from the current one to next parser in sequence, if there is another parser left, making this the new delegate. Old delegate is returned if switch succeeds.

Returns:
True if switch succeeded; false otherwise
    protected boolean switchToNext()
    {
        if ( >= .) {
            return false;
        }
         = [++];
        return true;
    }
New to GrepCode? Check out our FAQ X