Example usage for com.fasterxml.jackson.core JsonParser getCurrentLocation

List of usage examples for com.fasterxml.jackson.core JsonParser getCurrentLocation

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonParser getCurrentLocation.

Prototype

public abstract JsonLocation getCurrentLocation();

Source Link

Document

Method that returns location of the last processed character; usually for error reporting purposes.

Usage

From source file:org.eclipse.rdf4j.rio.rdfjson.RDFJSONParser.java

@Override
public void parse(final InputStream inputStream, final String baseUri)
        throws IOException, RDFParseException, RDFHandlerException {
    JsonParser jp = null;

    clear();//from   w w  w.ja  v  a2 s  .c  o  m

    try {
        if (this.rdfHandler != null) {
            this.rdfHandler.startRDF();
        }

        jp = RDFJSONUtility.JSON_FACTORY.createParser(new BOMInputStream(inputStream, false));
        rdfJsonToHandlerInternal(this.rdfHandler, this.valueFactory, jp);
    } catch (final IOException e) {
        if (jp != null) {
            reportFatalError("Found IOException during parsing", e, jp.getCurrentLocation());
        } else {
            reportFatalError(e);
        }
    } finally {
        clear();
        if (jp != null) {
            try {
                jp.close();
            } catch (final IOException e) {
                reportFatalError("Found exception while closing JSON parser", e, jp.getCurrentLocation());
            }
        }
    }
    if (this.rdfHandler != null) {
        this.rdfHandler.endRDF();
    }
}

From source file:org.eclipse.rdf4j.rio.rdfjson.RDFJSONParser.java

@Override
public void parse(final Reader reader, final String baseUri)
        throws IOException, RDFParseException, RDFHandlerException {
    JsonParser jp = null;

    clear();//from w w  w. j ava  2s.c o m

    try {
        if (this.rdfHandler != null) {
            this.rdfHandler.startRDF();
        }

        jp = RDFJSONUtility.JSON_FACTORY.createParser(reader);
        rdfJsonToHandlerInternal(rdfHandler, valueFactory, jp);
    } catch (final IOException e) {
        if (jp != null) {
            reportFatalError("Found IOException during parsing", e, jp.getCurrentLocation());
        } else {
            reportFatalError(e);
        }
    } finally {
        clear();
        if (jp != null) {
            try {
                jp.close();
            } catch (final IOException e) {
                reportFatalError("Found exception while closing JSON parser", e, jp.getCurrentLocation());
            }
        }
    }
    if (rdfHandler != null) {
        rdfHandler.endRDF();
    }
}

From source file:org.eclipse.rdf4j.rio.rdfjson.RDFJSONParser.java

private void rdfJsonToHandlerInternal(final RDFHandler handler, final ValueFactory vf, final JsonParser jp)
        throws IOException, JsonParseException, RDFParseException, RDFHandlerException {
    if (jp.nextToken() != JsonToken.START_OBJECT) {
        reportFatalError("Expected RDF/JSON document to start with an Object", jp.getCurrentLocation());
    }//  www  .  j  a v a2s  .c om

    while (jp.nextToken() != JsonToken.END_OBJECT) {
        final String subjStr = jp.getCurrentName();
        Resource subject = null;

        subject = subjStr.startsWith("_:") ? createNode(subjStr.substring(2)) : vf.createIRI(subjStr);
        if (jp.nextToken() != JsonToken.START_OBJECT) {
            reportFatalError("Expected subject value to start with an Object", jp.getCurrentLocation());
        }

        boolean foundPredicate = false;
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            final String predStr = jp.getCurrentName();

            final IRI predicate = vf.createIRI(predStr);
            foundPredicate = true;

            if (jp.nextToken() != JsonToken.START_ARRAY) {
                reportFatalError("Expected predicate value to start with an array", jp.getCurrentLocation());
            }

            boolean foundObject = false;

            while (jp.nextToken() != JsonToken.END_ARRAY) {
                if (jp.getCurrentToken() != JsonToken.START_OBJECT) {
                    reportFatalError("Expected object value to start with an Object: subject=<" + subjStr
                            + "> predicate=<" + predStr + ">", jp.getCurrentLocation());
                }

                String nextValue = null;
                String nextType = null;
                String nextDatatype = null;
                String nextLanguage = null;
                final Set<String> nextContexts = new HashSet<String>(2);

                while (jp.nextToken() != JsonToken.END_OBJECT) {
                    final String fieldName = jp.getCurrentName();
                    if (RDFJSONUtility.VALUE.equals(fieldName)) {
                        if (nextValue != null) {
                            reportError(
                                    "Multiple values found for a single object: subject=" + subjStr
                                            + " predicate=" + predStr,
                                    jp.getCurrentLocation(),
                                    RDFJSONParserSettings.FAIL_ON_MULTIPLE_OBJECT_VALUES);
                        }

                        jp.nextToken();

                        nextValue = jp.getText();
                    } else if (RDFJSONUtility.TYPE.equals(fieldName)) {
                        if (nextType != null) {
                            reportError(
                                    "Multiple types found for a single object: subject=" + subjStr
                                            + " predicate=" + predStr,
                                    jp.getCurrentLocation(),
                                    RDFJSONParserSettings.FAIL_ON_MULTIPLE_OBJECT_TYPES);
                        }

                        jp.nextToken();

                        nextType = jp.getText();
                    } else if (RDFJSONUtility.LANG.equals(fieldName)) {
                        if (nextLanguage != null) {
                            reportError(
                                    "Multiple languages found for a single object: subject=" + subjStr
                                            + " predicate=" + predStr,
                                    jp.getCurrentLocation(),
                                    RDFJSONParserSettings.FAIL_ON_MULTIPLE_OBJECT_LANGUAGES);
                        }

                        jp.nextToken();

                        nextLanguage = jp.getText();
                    } else if (RDFJSONUtility.DATATYPE.equals(fieldName)) {
                        if (nextDatatype != null) {
                            reportError(
                                    "Multiple datatypes found for a single object: subject=" + subjStr
                                            + " predicate=" + predStr,
                                    jp.getCurrentLocation(),
                                    RDFJSONParserSettings.FAIL_ON_MULTIPLE_OBJECT_DATATYPES);
                        }

                        jp.nextToken();

                        nextDatatype = jp.getText();
                    } else if (RDFJSONUtility.GRAPHS.equals(fieldName)) {
                        if (jp.nextToken() != JsonToken.START_ARRAY) {
                            reportError("Expected graphs to start with an array", jp.getCurrentLocation(),
                                    RDFJSONParserSettings.SUPPORT_GRAPHS_EXTENSION);
                        }

                        while (jp.nextToken() != JsonToken.END_ARRAY) {
                            final String nextGraph = jp.getText();
                            nextContexts.add(nextGraph);
                        }
                    } else {
                        reportError(
                                "Unrecognised JSON field name for object: subject=" + subjStr + " predicate="
                                        + predStr + " fieldname=" + fieldName,
                                jp.getCurrentLocation(), RDFJSONParserSettings.FAIL_ON_UNKNOWN_PROPERTY);
                    }
                }

                Value object = null;

                if (nextType == null) {
                    reportFatalError("No type for object: subject=" + subjStr + " predicate=" + predStr,
                            jp.getCurrentLocation());
                }

                if (nextValue == null) {
                    reportFatalError("No value for object: subject=" + subjStr + " predicate=" + predStr,
                            jp.getCurrentLocation());
                }

                if (RDFJSONUtility.LITERAL.equals(nextType)) {
                    if (nextLanguage != null) {
                        object = this.createLiteral(nextValue, nextLanguage, null, jp.getCurrentLocation());
                    } else if (nextDatatype != null) {
                        object = this.createLiteral(nextValue, null, this.createURI(nextDatatype),
                                jp.getCurrentLocation());
                    } else {
                        object = this.createLiteral(nextValue, null, null, jp.getCurrentLocation());
                    }
                } else if (RDFJSONUtility.BNODE.equals(nextType)) {
                    if (nextLanguage != null) {
                        reportFatalError("Language was attached to a blank node object: subject=" + subjStr
                                + " predicate=" + predStr, jp.getCurrentLocation());
                    }
                    if (nextDatatype != null) {
                        reportFatalError("Datatype was attached to a blank node object: subject=" + subjStr
                                + " predicate=" + predStr, jp.getCurrentLocation());
                    }
                    object = createNode(nextValue.substring(2));
                } else if (RDFJSONUtility.URI.equals(nextType)) {
                    if (nextLanguage != null) {
                        reportFatalError("Language was attached to a uri object: subject=" + subjStr
                                + " predicate=" + predStr, jp.getCurrentLocation());
                    }
                    if (nextDatatype != null) {
                        reportFatalError("Datatype was attached to a uri object: subject=" + subjStr
                                + " predicate=" + predStr, jp.getCurrentLocation());
                    }
                    object = vf.createIRI(nextValue);
                }
                foundObject = true;

                if (!nextContexts.isEmpty()) {
                    for (final String nextContext : nextContexts) {
                        final Resource context;

                        if (nextContext.equals(RDFJSONUtility.NULL)) {
                            context = null;
                        } else if (nextContext.startsWith("_:")) {
                            context = createNode(nextContext.substring(2));
                        } else {
                            context = vf.createIRI(nextContext);
                        }
                        Statement st = vf.createStatement(subject, predicate, object, context);
                        if (handler != null) {
                            handler.handleStatement(st);
                        }
                    }
                } else {
                    Statement st = vf.createStatement(subject, predicate, object);
                    if (handler != null) {
                        handler.handleStatement(st);
                    }
                }

            }
            if (!foundObject) {
                reportFatalError("No object for predicate: subject=" + subjStr + " predicate=" + predStr,
                        jp.getCurrentLocation());
            }
        }

        if (!foundPredicate) {
            reportFatalError("No predicate for object: subject=" + subjStr, jp.getCurrentLocation());
        }
    }
}

From source file:org.openrdf.rio.rdfjson.RDFJSONParser.java

@Override
public void parse(final InputStream inputStream, final String baseUri)
        throws IOException, RDFParseException, RDFHandlerException {
    if (this.rdfHandler != null) {
        this.rdfHandler.startRDF();
    }//from  w w  w  .jav  a 2s.  c o m

    JsonParser jp = null;

    try {
        jp = RDFJSONUtility.JSON_FACTORY.createParser(new BOMInputStream(inputStream, false));
        rdfJsonToHandlerInternal(this.rdfHandler, this.valueFactory, jp);
    } catch (final IOException e) {
        if (jp != null) {
            reportFatalError("Found IOException during parsing", e, jp.getCurrentLocation());
        } else {
            reportFatalError(e);
        }
    } finally {
        clear();
        if (jp != null) {
            try {
                jp.close();
            } catch (final IOException e) {
                reportFatalError("Found exception while closing JSON parser", e, jp.getCurrentLocation());
            }
        }
    }
    if (this.rdfHandler != null) {
        this.rdfHandler.endRDF();
    }
}

From source file:org.openrdf.rio.rdfjson.RDFJSONParser.java

@Override
public void parse(final Reader reader, final String baseUri)
        throws IOException, RDFParseException, RDFHandlerException {
    if (rdfHandler == null) {
        rdfHandler.startRDF();/*from  w  ww .ja v a  2 s  .  c om*/
    }
    JsonParser jp = null;

    try {
        jp = RDFJSONUtility.JSON_FACTORY.createParser(reader);
        rdfJsonToHandlerInternal(rdfHandler, valueFactory, jp);
    } catch (final IOException e) {
        if (jp != null) {
            reportFatalError("Found IOException during parsing", e, jp.getCurrentLocation());
        } else {
            reportFatalError(e);
        }
    } finally {
        if (jp != null) {
            try {
                jp.close();
            } catch (final IOException e) {
                reportFatalError("Found exception while closing JSON parser", e, jp.getCurrentLocation());
            }
        }
    }
    if (rdfHandler != null) {
        rdfHandler.endRDF();
    }
}

From source file:org.openrdf.rio.rdfjson.RDFJSONParser.java

private void rdfJsonToHandlerInternal(final RDFHandler handler, final ValueFactory vf, final JsonParser jp)
        throws IOException, JsonParseException, RDFParseException, RDFHandlerException {
    if (jp.nextToken() != JsonToken.START_OBJECT) {
        reportFatalError("Expected RDF/JSON document to start with an Object", jp.getCurrentLocation());
    }/*from   w  ww  .ja v  a2  s.  com*/

    while (jp.nextToken() != JsonToken.END_OBJECT) {
        final String subjStr = jp.getCurrentName();
        Resource subject = null;

        subject = subjStr.startsWith("_:") ? vf.createBNode(subjStr.substring(2)) : vf.createIRI(subjStr);
        if (jp.nextToken() != JsonToken.START_OBJECT) {
            reportFatalError("Expected subject value to start with an Object", jp.getCurrentLocation());
        }

        boolean foundPredicate = false;
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            final String predStr = jp.getCurrentName();

            final IRI predicate = vf.createIRI(predStr);
            foundPredicate = true;

            if (jp.nextToken() != JsonToken.START_ARRAY) {
                reportFatalError("Expected predicate value to start with an array", jp.getCurrentLocation());
            }

            boolean foundObject = false;

            while (jp.nextToken() != JsonToken.END_ARRAY) {
                if (jp.getCurrentToken() != JsonToken.START_OBJECT) {
                    reportFatalError("Expected object value to start with an Object: subject=<" + subjStr
                            + "> predicate=<" + predStr + ">", jp.getCurrentLocation());
                }

                String nextValue = null;
                String nextType = null;
                String nextDatatype = null;
                String nextLanguage = null;
                final Set<String> nextContexts = new HashSet<String>(2);

                while (jp.nextToken() != JsonToken.END_OBJECT) {
                    final String fieldName = jp.getCurrentName();
                    if (RDFJSONUtility.VALUE.equals(fieldName)) {
                        if (nextValue != null) {
                            reportError(
                                    "Multiple values found for a single object: subject=" + subjStr
                                            + " predicate=" + predStr,
                                    jp.getCurrentLocation(),
                                    RDFJSONParserSettings.FAIL_ON_MULTIPLE_OBJECT_VALUES);
                        }

                        jp.nextToken();

                        nextValue = jp.getText();
                    } else if (RDFJSONUtility.TYPE.equals(fieldName)) {
                        if (nextType != null) {
                            reportError(
                                    "Multiple types found for a single object: subject=" + subjStr
                                            + " predicate=" + predStr,
                                    jp.getCurrentLocation(),
                                    RDFJSONParserSettings.FAIL_ON_MULTIPLE_OBJECT_TYPES);
                        }

                        jp.nextToken();

                        nextType = jp.getText();
                    } else if (RDFJSONUtility.LANG.equals(fieldName)) {
                        if (nextLanguage != null) {
                            reportError(
                                    "Multiple languages found for a single object: subject=" + subjStr
                                            + " predicate=" + predStr,
                                    jp.getCurrentLocation(),
                                    RDFJSONParserSettings.FAIL_ON_MULTIPLE_OBJECT_LANGUAGES);
                        }

                        jp.nextToken();

                        nextLanguage = jp.getText();
                    } else if (RDFJSONUtility.DATATYPE.equals(fieldName)) {
                        if (nextDatatype != null) {
                            reportError(
                                    "Multiple datatypes found for a single object: subject=" + subjStr
                                            + " predicate=" + predStr,
                                    jp.getCurrentLocation(),
                                    RDFJSONParserSettings.FAIL_ON_MULTIPLE_OBJECT_DATATYPES);
                        }

                        jp.nextToken();

                        nextDatatype = jp.getText();
                    } else if (RDFJSONUtility.GRAPHS.equals(fieldName)) {
                        if (jp.nextToken() != JsonToken.START_ARRAY) {
                            reportError("Expected graphs to start with an array", jp.getCurrentLocation(),
                                    RDFJSONParserSettings.SUPPORT_GRAPHS_EXTENSION);
                        }

                        while (jp.nextToken() != JsonToken.END_ARRAY) {
                            final String nextGraph = jp.getText();
                            nextContexts.add(nextGraph);
                        }
                    } else {
                        reportError(
                                "Unrecognised JSON field name for object: subject=" + subjStr + " predicate="
                                        + predStr + " fieldname=" + fieldName,
                                jp.getCurrentLocation(), RDFJSONParserSettings.FAIL_ON_UNKNOWN_PROPERTY);
                    }
                }

                Value object = null;

                if (nextType == null) {
                    reportFatalError("No type for object: subject=" + subjStr + " predicate=" + predStr,
                            jp.getCurrentLocation());
                }

                if (nextValue == null) {
                    reportFatalError("No value for object: subject=" + subjStr + " predicate=" + predStr,
                            jp.getCurrentLocation());
                }

                if (RDFJSONUtility.LITERAL.equals(nextType)) {
                    if (nextLanguage != null) {
                        object = this.createLiteral(nextValue, nextLanguage, null, jp.getCurrentLocation());
                    } else if (nextDatatype != null) {
                        object = this.createLiteral(nextValue, null, this.createURI(nextDatatype),
                                jp.getCurrentLocation());
                    } else {
                        object = this.createLiteral(nextValue, null, null, jp.getCurrentLocation());
                    }
                } else if (RDFJSONUtility.BNODE.equals(nextType)) {
                    if (nextLanguage != null) {
                        reportFatalError("Language was attached to a blank node object: subject=" + subjStr
                                + " predicate=" + predStr, jp.getCurrentLocation());
                    }
                    if (nextDatatype != null) {
                        reportFatalError("Datatype was attached to a blank node object: subject=" + subjStr
                                + " predicate=" + predStr, jp.getCurrentLocation());
                    }
                    object = vf.createBNode(nextValue.substring(2));
                } else if (RDFJSONUtility.URI.equals(nextType)) {
                    if (nextLanguage != null) {
                        reportFatalError("Language was attached to a uri object: subject=" + subjStr
                                + " predicate=" + predStr, jp.getCurrentLocation());
                    }
                    if (nextDatatype != null) {
                        reportFatalError("Datatype was attached to a uri object: subject=" + subjStr
                                + " predicate=" + predStr, jp.getCurrentLocation());
                    }
                    object = vf.createIRI(nextValue);
                }
                foundObject = true;

                if (!nextContexts.isEmpty()) {
                    for (final String nextContext : nextContexts) {
                        final Resource context = nextContext.equals(RDFJSONUtility.NULL) ? null
                                : vf.createIRI(nextContext);
                        Statement st = vf.createStatement(subject, predicate, object, context);
                        if (handler != null) {
                            handler.handleStatement(st);
                        }
                    }
                } else {
                    Statement st = vf.createStatement(subject, predicate, object);
                    if (handler != null) {
                        handler.handleStatement(st);
                    }
                }

            }
            if (!foundObject) {
                reportFatalError("No object for predicate: subject=" + subjStr + " predicate=" + predStr,
                        jp.getCurrentLocation());
            }
        }

        if (!foundPredicate) {
            reportFatalError("No predicate for object: subject=" + subjStr, jp.getCurrentLocation());
        }
    }
}

From source file:org.sead.repositories.reference.RefRepository.java

private static void report(JsonParser jp, JsonToken token) {
    boolean struct = token.isStructStart() || token.isStructEnd();
    try {/*w  w w  . j  av a 2  s.co m*/
        String tag = struct ? token.asString() : jp.getText();
        log.trace("Tag: " + tag);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    long currentOffset = jp.getCurrentLocation().getByteOffset();
    long tokenOffset = jp.getTokenLocation().getByteOffset();
    log.trace("Cur: " + currentOffset + " tok: " + tokenOffset);
}