All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cucumber.table.xstream.ListOfObjectReader Maven / Gradle / Ivy

There is a newer version: 1.2.6
Show newest version
package cucumber.table.xstream;

import com.thoughtworks.xstream.converters.ErrorWriter;
import com.thoughtworks.xstream.io.AbstractReader;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * Generates XStream XML data from table rows that will create a List of objects. Example:
 * 
 * 
 *     
 *         Sid Vicious
 *         1957-05-10 00:00:00.0 UTC
 *         1000
 *     
 *     
 *         Frank Zappa
 *         1940-12-21 00:00:00.0 UTC
 *         3000
 *     
 * 
 * 
*/ public class ListOfObjectReader extends AbstractReader { private final Type elementType; private final List attributeNames; private final Iterator> itemIterator; private int depth = 0; private Iterator attributeNameIterator; private String attributeName; private Iterator attributeValueIterator; private String attributeValue; public ListOfObjectReader(Type elementType, List attributeNames, List> items) { this.elementType = elementType; this.attributeNames = attributeNames; this.itemIterator = items.iterator(); } @Override public boolean hasMoreChildren() { switch (depth) { case 0: return itemIterator.hasNext(); case 1: return attributeNameIterator.hasNext(); case 2: return false; default: throw new IllegalStateException("Depth is " + depth); } } @Override public void moveDown() { depth++; switch (depth) { case 1: attributeNameIterator = attributeNames.iterator(); attributeValueIterator = itemIterator.next().iterator(); break; case 2: attributeName = attributeNameIterator.next(); attributeValue = attributeValueIterator.next(); break; default: throw new IllegalStateException("Depth is " + depth); } } @Override public void moveUp() { depth--; } private Class getElementClass() { return ((elementType instanceof Class) ? (Class) elementType : (Class) ((ParameterizedType) elementType).getRawType()); } @Override public String getNodeName() { switch (depth) { case 0: return "list"; case 1: return getElementClass().getName(); case 2: return attributeName; default: throw new IllegalStateException("Depth is " + depth); } } @Override public String getValue() { return attributeValue; } @Override public String getAttribute(String name) { return null; } @Override public String getAttribute(int index) { throw new UnsupportedOperationException(); } @Override public int getAttributeCount() { throw new UnsupportedOperationException(); } @Override public String getAttributeName(int index) { throw new UnsupportedOperationException(); } @Override public Iterator getAttributeNames() { return Collections.emptyList().iterator(); } @Override public void appendErrors(ErrorWriter errorWriter) { } @Override public void close() { throw new UnsupportedOperationException(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy