Example usage for org.springframework.batch.item ParseException ParseException

List of usage examples for org.springframework.batch.item ParseException ParseException

Introduction

In this page you can find the example usage for org.springframework.batch.item ParseException ParseException.

Prototype

public ParseException(String message, Throwable cause) 

Source Link

Document

Create a new ParseException based on a message and another exception.

Usage

From source file:biz.c24.io.spring.batch.reader.C24ItemReader.java

@SuppressWarnings("unchecked")
@Override/*w  w w  .ja v a2  s  .  co m*/
public Result read() throws UnexpectedInputException, ParseException, NonTransientResourceException {

    ComplexDataObject result = null;
    Object context = null;
    Parser parser = null;

    // Keep trying to parse an entity until either we get one (result != null) or we run out of data to read (parser == null)
    // BufferedReaderSources such as the ZipFileSource can return multiple BufferedReaders; when our current one is exhausted it
    // will return another one
    while (result == null && (parser = getParser()) != null) {

        if (elementStartPattern != null) {

            // We're possibly sharing a BufferedReader with other threads. Get our data out of it as quickly as we can to reduce
            // the amount of time we spend blocking others
            SplittingReader reader = parser.getSplitter();
            if (reader == null) {
                // There's nothing left to read
                break;
            }

            // Get the textual source for an element from the reader
            ElementContext elementContext = readElement(reader);
            String element = elementContext.element;
            context = elementContext.context;

            // If we got something then parse it
            if (element != null && element.trim().length() > 0) {

                StringReader stringReader = new StringReader(element);

                parser.setReader(stringReader);

                try {
                    result = parser.read();
                } catch (IOException ioEx) {
                    throw new ParseException(
                            "Failed to parse CDO from " + source.getName() + ". Message: " + element, ioEx);
                }
            } else {
                // This parser has been exhausted
                discardParser(parser);
            }

        } else {
            // We'll parse CDOs from the parser in serial
            try {
                result = parser.read();
            } catch (IOException ioEx) {
                throw new ParseException("Failed to parse CDO from " + source.getName(), ioEx);
            } finally {
                if (result != null && result.getTotalAttrCount() == 0 && result.getTotalElementCount() == 0) {
                    // We didn't manage to read anything
                    result = null;
                }
                if (result == null) {
                    // We've exhausted this reader
                    // In the event of an exception being thrown there might still be data left in the reader 
                    // but as we have no way to skip to the next message, we have to abandon it
                    discardParser(parser);
                }
            }
        }
    }

    if (validator != null && result != null) {
        try {
            ValidationManager mgr = validator.get();
            if (mgr == null) {
                mgr = new ValidationManager();
                validator.set(mgr);
            }
            if (failfast) {
                mgr.validateByException(result);
            } else {
                // Capture all failures
                final Collection<ValidationEvent> events = new LinkedList<ValidationEvent>();

                ValidationListener listener = new ValidationListener() {
                    public void validationPassed(ValidationEvent ve) {
                    }

                    public void validationFailed(ValidationEvent ve) {
                        events.add(ve);
                    }
                };

                mgr.addValidationListener(listener);

                try {
                    if (!mgr.validateByEvents(result)) {
                        if (events.size() == 1) {
                            // Treat it as though we were validating by exception
                            mgr.setEventBased(false);
                            mgr.fireValidationEvent(events.iterator().next());
                        } else {
                            throw new C24CompoundValidationException(result, events);
                        }
                    }
                } finally {
                    mgr.removeValidationListener(listener);
                }
            }
        } catch (ValidationException vEx) {
            throw new C24ValidationException(
                    "Failed to validate message: " + vEx.getLocalizedMessage() + " [" + source.getName() + "]",
                    result, vEx);
        }
    }

    // If we have a ParseListener registered, allow it to intercept the return value
    return parseListener == null || result == null ? (Result) result : parseListener.process(result, context);

}