org.neo4j.server.rest.transactional.StatementDeserializer Maven / Gradle / Ivy
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.neo4j.server.rest.transactional;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.JsonMappingException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.neo4j.helpers.collection.PrefetchingIterator;
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.server.rest.transactional.error.Neo4jError;
import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableMap;
import static org.codehaus.jackson.JsonToken.END_ARRAY;
import static org.codehaus.jackson.JsonToken.END_OBJECT;
import static org.codehaus.jackson.JsonToken.FIELD_NAME;
import static org.codehaus.jackson.JsonToken.START_ARRAY;
import static org.codehaus.jackson.JsonToken.START_OBJECT;
import static org.neo4j.helpers.collection.Iterators.emptyIterator;
import static org.neo4j.helpers.collection.MapUtil.map;
public class StatementDeserializer extends PrefetchingIterator
{
private static final JsonFactory JSON_FACTORY = new JsonFactory().setCodec( new Neo4jJsonCodec() ).disable( JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM );
private static final Map NO_PARAMETERS = unmodifiableMap( map() );
private static final Iterator NO_ERRORS = emptyIterator();
private final JsonParser input;
private State state;
private List errors = null;
private enum State
{
BEFORE_OUTER_ARRAY,
IN_BODY,
FINISHED
}
public StatementDeserializer( InputStream input )
{
try
{
this.input = JSON_FACTORY.createJsonParser( input );
this.state = State.BEFORE_OUTER_ARRAY;
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
}
public Iterator errors()
{
return errors == null ? NO_ERRORS : errors.iterator();
}
@Override
protected Statement fetchNextOrNull()
{
try
{
if ( errors != null )
{
return null;
}
switch ( state )
{
case BEFORE_OUTER_ARRAY:
if ( !beginsWithCorrectTokens() )
{
return null;
}
state = State.IN_BODY;
case IN_BODY:
String statement = null;
Map parameters = null;
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy