public void convert(XMLStreamReader in, ParseState ps, Writer out) throws XMLStreamException, IOException {

		CSVChunkWriter cw = new CSVChunkWriter(out);

		while (in.hasNext()) {
			int type = in.next();
			switch(type) {
				case XMLStreamConstants.START_ELEMENT:
					ps.enter(in.getLocalName());
					for (int i=0; i<in.getAttributeCount(); i++) {
						ps.enterAttribute(in.getAttributeLocalName(i), in.getAttributeValue(i));
					}
					break;
				case XMLStreamConstants.END_ELEMENT:
					ps.exit(); 
					if (ps.onChunkEnd()) {
						// HACK
						List<String> data = applyTransforms(ps.getHeaders(), ps.getData());
						
						cw.writeChunk(ps.getHeaders(), data);
					}
					break;
				case XMLStreamConstants.CHARACTERS:
					if (ps.inField()) {
						ps.putFieldValue(in.getText());
					}
					break;
			}
			// HACK -- Some streams (BuggyStringXMLReaderWrapper.java) have inaccurate hasNext() information on end element
			if (ps.getLevel() == 0) break;

		}
		cw.flush();
		return;
	}