Android Open Source - colibri Cursor






From Project

Back to project page colibri.

License

The source code is released under:

Apache License

If you think the Android project colibri listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package labrom.colibri.xml;
//from  w ww.  jav  a2 s  .  c om
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Iterator;
import java.util.NoSuchElementException;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

/**
 * A cursor on an XML stream. Before using a Cursor one must define a {@link CursorDef} that describes the mapping
 * from XML elements and attributes to the Java class variables.
 * 
 * @author Romain Laboisse labrom@gmail.com
 * 
 * @param <T> The type of objects that Pulloid will create. This class must be written by developers.
 */
public class Cursor<T> implements Iterator<T>, Iterable<T> {
  

  private ElementContext<T> ctx;
  private XmlPullParser parser;
  private Reflector<T> reflector;
  
  private T next;
  
  
  protected Cursor(Reflector<T> reflector, ElementContext<T> ctx, XmlPullParser parser, InputStream input) throws XmlPullParserException {
    this.ctx = ctx;
    this.parser = parser;
    parser.setInput(input, null);
    this.reflector = reflector;
  }

  protected Cursor(Reflector<T> reflector, ElementContext<T> ctx, XmlPullParser parser, Reader input) throws XmlPullParserException {
    this.ctx = ctx;
    this.parser = parser;
    parser.setInput(input);
    this.reflector = reflector;
  }

  @Override
  public boolean hasNext() {
    try {
      if(ParserUtil.isEndDocument(parser))
        return false;
      if(ctx.position(parser) != ctx)
        return false;

      next = reflector.createObject();
      ctx.hydrate(parser, reflector);
      return true;
    } catch (XmlPullParserException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return false;
  }
  
  public T getNext() {
    if(next != null || hasNext()) {
      T result = next;
      next = null;
      return result;
    }
    return null; // FIXME Should throw an Exception instead?
  }
  
  @Override
  public T next() {
    T next = getNext();
    if(next == null)
      throw new NoSuchElementException();
    return next;
  }
  
  @Override
  public void remove() {
    throw new UnsupportedOperationException();
  }
  
  @Override
  public Iterator<T> iterator() {
    return this;
  }
  

}




Java Source Code List

labrom.colibri.Util.java
labrom.colibri.cache.CacheEntry.java
labrom.colibri.cache.Cache.java
labrom.colibri.cache.Entries.java
labrom.colibri.data.ActiveContentProvider.java
labrom.colibri.data.ActiveRecordList.java
labrom.colibri.data.ActiveRecord.java
labrom.colibri.data.Database.java
labrom.colibri.data.SelectionArgs.java
labrom.colibri.xml.AttributeContext.java
labrom.colibri.xml.CursorDef.java
labrom.colibri.xml.CursorException.java
labrom.colibri.xml.Cursor.java
labrom.colibri.xml.CustomMap.java
labrom.colibri.xml.ElementContext.java
labrom.colibri.xml.FieldSetter.java
labrom.colibri.xml.Item.java
labrom.colibri.xml.NodeContext.java
labrom.colibri.xml.ObjectFactory.java
labrom.colibri.xml.ParserFactory.java
labrom.colibri.xml.ParserUtil.java
labrom.colibri.xml.PullTest.java
labrom.colibri.xml.ReflectorTest.java
labrom.colibri.xml.Reflector.java
labrom.colibri.xml.RootContext.java
labrom.colibri.xml.SetterTest.java
labrom.colibri.xml.StringToDateTransformer.java
labrom.colibri.xml.StringTransformer.java
labrom.colibri.xml.maps.Cursor4Maps.java
labrom.colibri.xml.maps.CursorDef4Maps.java
labrom.colibri.xml.maps.ElementContext4Maps.java
labrom.colibri.xml.maps.RootContext4Maps.java
labrom.colibri.xml.maps.package-info.java
labrom.colibri.xml.package-info.java