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

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

Introduction

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

Prototype

public NonTransientResourceException(String message, Throwable cause) 

Source Link

Document

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

Usage

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

/**
 * Extracts the textual data for an element from the SplittingReader using the elementStartPattern to split
 * up the data. //from   w  ww  .  ja  va  2s  .  co  m
 * 
 * If a ParseListener is registered, it will receive a callback when a line is read from the reader and when 
 * an element has been extracted.
 * 
 * @param reader The SplittingReader to extract the element from
 */
protected ElementContext readElement(SplittingReader reader) {

    StringBuffer elementCache = new StringBuffer();
    boolean inElement = false;

    synchronized (reader) {
        try {
            while (reader.ready()) {
                String line = readLine(reader);

                if (line != null) {
                    if (parseListener != null) {
                        // Invoke callback
                        line = parseListener.processLine(line);
                    }
                    // We look for the start of a new element if either:
                    // a) We're not in an element or
                    // b) We don't have an elementStopPattern set (if we do and we're in a element, the presence of a line
                    // that matches the element start pattern is deemed to still be part of the same element)
                    if ((!inElement || elementStopPattern == null)
                            && elementStartPattern.matcher(line).matches()) {
                        // We've encountered the start of a new element
                        String message = elementCache.toString();
                        if (message.trim().length() > 0) {
                            // We were already parsing an element; thus we've finished extracting our element
                            // Cache the line
                            reader.pushback(line);
                            // ...and return what we have already extracted
                            ElementContext context = new ElementContext(message,
                                    parseListener == null ? null : parseListener.getContext(message));
                            return context;
                        } else {
                            // This is the start of our element. Add it to our elementCache.
                            inElement = true;
                        }
                    }

                    if (inElement) {
                        // More data for our current element
                        elementCache.append(line);

                        // If we have an elementStopPattern, see if the line matched
                        if (elementStopPattern != null && elementStopPattern.matcher(line).matches()) {
                            // We've encountered the end of the element
                            break;
                        }
                    }
                }
            }
        } catch (IOException ioEx) {
            throw new NonTransientResourceException("Failed to extract entity", ioEx);
        }
    }

    String message = elementCache.toString();
    ElementContext context = new ElementContext(message,
            parseListener == null ? null : parseListener.getContext(message));
    return context;
}

From source file:org.geoserver.backuprestore.reader.CatalogFileReader.java

/**
 * Responsible for moving the cursor before the StartElement of the fragment root.
 * /*from  w  w w. j  av a2s  . c om*/
 * This implementation simply looks for the next corresponding element, it does not care about element nesting. You will need to override this
 * method to correctly handle composite fragments.
 * 
 * @return <code>true</code> if next fragment was found, <code>false</code> otherwise.
 * 
 * @throws NonTransientResourceException if the cursor could not be moved. This will be treated as fatal and subsequent calls to read will return
 *         null.
 */
protected boolean moveCursorToNextFragment(XMLEventReader reader) {
    try {
        while (true) {
            while (reader.peek() != null && !reader.peek().isStartElement()) {
                reader.nextEvent();
            }
            if (reader.peek() == null) {
                return false;
            }
            QName startElementName = ((StartElement) reader.peek()).getName();
            if (isFragmentRootElementName(startElementName)) {
                return true;
            }
            reader.nextEvent();
        }
    } catch (XMLStreamException e) {
        return logValidationExceptions((T) null,
                new NonTransientResourceException("Error while reading from event reader", e));
    }
}

From source file:org.springframework.batch.item.xml.StaxEventItemReader.java

/**
 * Responsible for moving the cursor before the StartElement of the fragment root.
 * /*from w w w .  j a va  2 s.  c o m*/
 * This implementation simply looks for the next corresponding element, it does not care about element nesting. You
 * will need to override this method to correctly handle composite fragments.
 * 
 * @return <code>true</code> if next fragment was found, <code>false</code> otherwise.
 * 
 * @throws NonTransientResourceException if the cursor could not be moved. This will be treated as fatal and
 * subsequent calls to read will return null.
 */
protected boolean moveCursorToNextFragment(XMLEventReader reader) throws NonTransientResourceException {
    try {
        while (true) {
            while (reader.peek() != null && !reader.peek().isStartElement()) {
                reader.nextEvent();
            }
            if (reader.peek() == null) {
                return false;
            }
            QName startElementName = ((StartElement) reader.peek()).getName();
            if (isFragmentRootElementName(startElementName)) {
                return true;
            }
            reader.nextEvent();

        }
    } catch (XMLStreamException e) {
        throw new NonTransientResourceException("Error while reading from event reader", e);
    }
}