Search in sources :

Example 1 with UnexpectedEndOfInputException

use of org.neo4j.internal.batchimport.input.UnexpectedEndOfInputException in project neo4j by neo4j.

the class CsvInputParser method next.

boolean next(InputEntityVisitor visitor) throws IOException {
    lineNumber++;
    int i = 0;
    Header.Entry entry = null;
    Header.Entry[] entries = header.entries();
    try {
        boolean doContinue = true;
        for (i = 0; i < entries.length && doContinue; i++) {
            entry = entries[i];
            if (!seeker.seek(mark, delimiter)) {
                if (i > 0) {
                    throw new UnexpectedEndOfInputException("Near " + mark);
                }
                // We're just at the end
                return false;
            }
            switch(entry.type()) {
                case ID:
                    if (seeker.tryExtract(mark, entry.extractor())) {
                        switch(idType) {
                            case STRING:
                            case INTEGER:
                                Object idValue = entry.extractor().value();
                                doContinue = visitor.id(idValue, entry.group());
                                if (entry.name() != null) {
                                    doContinue = visitor.property(entry.name(), idValue);
                                }
                                break;
                            case ACTUAL:
                                doContinue = visitor.id(((LongExtractor) entry.extractor()).longValue());
                                break;
                            default:
                                throw new IllegalArgumentException(idType.name());
                        }
                    }
                    break;
                case START_ID:
                    if (seeker.tryExtract(mark, entry.extractor())) {
                        switch(idType) {
                            case STRING:
                                doContinue = visitor.startId(entry.extractor().value(), entry.group());
                                break;
                            case INTEGER:
                                doContinue = visitor.startId(entry.extractor().value(), entry.group());
                                break;
                            case ACTUAL:
                                doContinue = visitor.startId(((LongExtractor) entry.extractor()).longValue());
                                break;
                            default:
                                throw new IllegalArgumentException(idType.name());
                        }
                    }
                    break;
                case END_ID:
                    if (seeker.tryExtract(mark, entry.extractor())) {
                        switch(idType) {
                            case STRING:
                                doContinue = visitor.endId(entry.extractor().value(), entry.group());
                                break;
                            case INTEGER:
                                doContinue = visitor.endId(entry.extractor().value(), entry.group());
                                break;
                            case ACTUAL:
                                doContinue = visitor.endId(((LongExtractor) entry.extractor()).longValue());
                                break;
                            default:
                                throw new IllegalArgumentException(idType.name());
                        }
                    }
                    break;
                case TYPE:
                    if (seeker.tryExtract(mark, entry.extractor())) {
                        doContinue = visitor.type((String) entry.extractor().value());
                    }
                    break;
                case PROPERTY:
                    if (seeker.tryExtract(mark, entry.extractor(), entry.optionalParameter())) {
                        // TODO since PropertyStore#encodeValue takes Object there's no point splitting up
                        // into different primitive types
                        Object value = entry.extractor().value();
                        if (!isEmptyArray(value)) {
                            doContinue = visitor.property(entry.name(), value);
                        }
                    }
                    break;
                case LABEL:
                    if (seeker.tryExtract(mark, entry.extractor())) {
                        Object labelsValue = entry.extractor().value();
                        if (labelsValue.getClass().isArray()) {
                            doContinue = visitor.labels((String[]) labelsValue);
                        } else {
                            doContinue = visitor.labels(new String[] { (String) labelsValue });
                        }
                    }
                    break;
                case IGNORE:
                    break;
                default:
                    throw new IllegalArgumentException(entry.type().toString());
            }
            if (mark.isEndOfLine()) {
                // We're at the end of the line, break and return an entity with what we have.
                break;
            }
        }
        while (!mark.isEndOfLine()) {
            seeker.seek(mark, delimiter);
            if (doContinue) {
                seeker.tryExtract(mark, stringExtractor, entry.optionalParameter());
                badCollector.collectExtraColumns(seeker.sourceDescription(), lineNumber, stringExtractor.value());
            }
        }
        visitor.endOfEntity();
        return true;
    } catch (final RuntimeException e) {
        String stringValue = null;
        try {
            Extractors extractors = new Extractors('?');
            if (seeker.tryExtract(mark, extractors.string(), entry.optionalParameter())) {
                stringValue = extractors.string().value();
            }
        } catch (Exception e1) {
        // OK
        }
        String message = format("ERROR in input" + "%n  data source: %s" + "%n  in field: %s" + "%n  for header: %s" + "%n  raw field value: %s" + "%n  original error: %s", seeker, entry + ":" + (i + 1), header, stringValue != null ? stringValue : "??", e.getMessage());
        if (e instanceof InputException) {
            throw Exceptions.withMessage(e, message);
        }
        throw new InputException(message, e);
    }
}
Also used : UnexpectedEndOfInputException(org.neo4j.internal.batchimport.input.UnexpectedEndOfInputException) IOException(java.io.IOException) InputException(org.neo4j.internal.batchimport.input.InputException) UnexpectedEndOfInputException(org.neo4j.internal.batchimport.input.UnexpectedEndOfInputException) Extractors(org.neo4j.csv.reader.Extractors) LongExtractor(org.neo4j.csv.reader.Extractors.LongExtractor) InputException(org.neo4j.internal.batchimport.input.InputException) UnexpectedEndOfInputException(org.neo4j.internal.batchimport.input.UnexpectedEndOfInputException)

Aggregations

IOException (java.io.IOException)1 Extractors (org.neo4j.csv.reader.Extractors)1 LongExtractor (org.neo4j.csv.reader.Extractors.LongExtractor)1 InputException (org.neo4j.internal.batchimport.input.InputException)1 UnexpectedEndOfInputException (org.neo4j.internal.batchimport.input.UnexpectedEndOfInputException)1