Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006-2007 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.springframework.batch.item.xml;
 
 
 
Item reader for reading XML input based on StAX. It extracts fragments from the input XML document which correspond to records for processing. The fragments are wrapped with StartDocument and EndDocument events so that the fragments can be further processed like standalone XML documents. The implementation is *not* thread-safe.

Author(s):
Robert Kasanicky
 
 public class StaxEventItemReader<T> extends AbstractItemCountingItemStreamItemReader<T> implements
 
 	private static final Log logger = LogFactory.getLog(StaxEventItemReader.class);
 
 
 
 
 	private Resource resource;
 
 
 
 	private boolean noInput;
 
 	private boolean strict = true;
 
 
 	public StaxEventItemReader() {
 	}

In strict mode the reader will throw an exception on org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(org.springframework.batch.item.ExecutionContext) if the input resource does not exist.

Parameters:
strict false by default
 
 	public void setStrict(boolean strict) {
 		this. = strict;
 	}
 
 	public void setResource(Resource resource) {
 		this. = resource;
 	}

Parameters:
unmarshaller maps xml fragments corresponding to records to objects
 
	public void setUnmarshaller(Unmarshaller unmarshaller) {
		this. = unmarshaller;
	}

Parameters:
fragmentRootElementName name of the root element of the fragment
	public void setFragmentRootElementName(String fragmentRootElementName) {
		this. = fragmentRootElementName;
	}

Ensure that all required dependencies for the ItemReader to run are provided after all properties have been set.

Throws:
java.lang.IllegalArgumentException if the Resource, FragmentDeserializer or FragmentRootElementName is null, or if the root element is empty.
java.lang.IllegalStateException if the Resource does not exist.
See also:
org.springframework.beans.factory.InitializingBean.afterPropertiesSet()
	public void afterPropertiesSet() throws Exception {
		Assert.notNull("The Unmarshaller must not be null.");
		Assert.hasLength("The FragmentRootElementName must not be null");
		}
	}

Responsible for moving the cursor before the StartElement of the fragment root. 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.

Returns:
true if next fragment was found, false otherwise.
Throws:
org.springframework.batch.item.NonTransientResourceException if the cursor could not be moved. This will be treated as fatal and subsequent calls to read will return null.
		try {
			while (true) {
				while (reader.peek() != null && !reader.peek().isStartElement()) {
					reader.nextEvent();
				}
				if (reader.peek() == null) {
					return false;
				}
				QName startElementName = ((StartElementreader.peek()).getName();
				if (startElementName.getLocalPart().equals()) {
						return true;
					}
				}
				reader.nextEvent();
			}
		}
		catch (XMLStreamException e) {
			throw new NonTransientResourceException("Error while reading from event reader"e);
		}
	}
	protected void doClose() throws Exception {
		try {
			if ( != null) {
			}
			if ( != null) {
			}
		}
		finally {
			 = null;
		}
	}
	protected void doOpen() throws Exception {
		Assert.notNull("The Resource must not be null.");
		 = true;
		if (!.exists()) {
			if () {
				throw new IllegalStateException("Input resource must exist (reader is in 'strict' mode)");
			}
			.warn("Input resource does not exist " + .getDescription());
			return;
		}
		if (!.isReadable()) {
			if () {
				throw new IllegalStateException("Input resource must be readable (reader is in 'strict' mode)");
			}
			.warn("Input resource is not readable " + .getDescription());
			return;
		}
		 = false;
	}

Move to next fragment and map it to item.
	protected T doRead() throws Exception {
		if () {
			return null;
		}
item = null;
		boolean success = false;
		try {
		}
			// Prevent caller from retrying indefinitely since this is fatal
			 = true;
			throw e;
		}
		if (success) {
			@SuppressWarnings("unchecked")
mappedFragment = (T) .unmarshal(new StaxSource());
			item = mappedFragment;
		}
		return item;
	}
	/*
	 * jumpToItem is overridden because reading in and attempting to bind an
	 * entire fragment is unacceptable in a restart scenario, and may cause
	 * exceptions to be thrown that were already skipped in previous runs.
	 */
	protected void jumpToItem(int itemIndexthrows Exception {
		for (int i = 0; i < itemIndexi++) {
		}
	}
	/*
	 * Read until the first StartElement tag that matches the provided
	 * fragmentRootElementName. Because there may be any number of tags in
	 * between where the reader is now and the fragment start, this is done in a
	 * loop until the element type and name match.
	 */
	private void readToStartFragment() throws XMLStreamException {
		while (true) {
			XMLEvent nextEvent = .nextEvent();
			if (nextEvent.isStartElement()
				return;
			}
		}
	}
	/*
	 * Read until the first EndElement tag that matches the provided
	 * fragmentRootElementName. Because there may be any number of tags in
	 * between where the reader is now and the fragment end tag, this is done in
	 * a loop until the element type and name match
	 */
	private void readToEndFragment() throws XMLStreamException {
		while (true) {
			XMLEvent nextEvent = .nextEvent();
			if (nextEvent.isEndElement()
				return;
			}
		}
	}
New to GrepCode? Check out our FAQ X