package org.pulloid; import java.io.InputStream; import java.io.Reader; import org.xmlpull.v1.XmlPullParserException; /** * A cursor definition represents a mapping from XML structures to Java objects. * An instance of CursorDef can be (re)used to create one or multiple {@linkplain Cursor cursors} on an actual XML stream. * * To obtain a CursorDef, use {@link RootContext} and {@link ElementContext}. * * @author Romain Laboisse labrom@gmail.com * * @see RootContext * * @param The type of objects that Pulloid will create. This class must be written by developers. */ public class CursorDef { Class cursorType; ElementContext ctx; protected CursorDef(ElementContext ctx) { this(null, ctx); } protected CursorDef(Class cursorType, ElementContext ctx) { this.cursorType = cursorType; this.ctx = ctx; } public ElementContext getContext() { return ctx; } /** * Creates a new cursor. Many cursors can be created from a single CursorDef. * @param input The XML input stream. * @return */ public Cursor pull(InputStream input) { try { return new Cursor(new Reflector(cursorType), ctx, ParserFactory.newParser(), input); } catch (XmlPullParserException e) { throw new CursorException(e); } } /** * Creates a new cursor. Many cursors can be created from a single CursorDef. * @param reader The XML reader. * @return */ public Cursor pull(Reader reader) { try { return new Cursor(new Reflector(cursorType), ctx, ParserFactory.newParser(), reader); } catch (XmlPullParserException e) { throw new CursorException(e); } } /** * Creates a new cursor. Many cursors can be created from a single CursorDef. * @param input The XML input stream. * @param objectFactory An object factory that allows the application to provide its own objects instantiation mechanism. * @return */ public Cursor pull(InputStream input, ObjectFactory objectFactory) { try { return new Cursor(new Reflector(objectFactory), ctx, ParserFactory.newParser(), input); } catch (XmlPullParserException e) { throw new CursorException(e); } } /** * Creates a new cursor. Many cursors can be created from a single CursorDef. * @param reader The XML reader. * @param objectFactory An object factory that allows the application to provide its own objects instantiation mechanism. * @return */ public Cursor pull(Reader reader, ObjectFactory objectFactory) { try { return new Cursor(new Reflector(objectFactory), ctx, ParserFactory.newParser(), reader); } catch (XmlPullParserException e) { throw new CursorException(e); } } }