package be.iminds.ilabt.jfed.rspec.parser;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * StringExtraXml
 */
public class StringExtraXml extends EventListExtraXml {
    private static List<XMLEvent> parseEvents(String xmlString) throws XMLStreamException {
        List<XMLEvent> res = new ArrayList<>();

        XMLInputFactory xif = XMLInputFactory.newFactory();
        StreamSource xml = new StreamSource(new StringReader(xmlString));
        XMLEventReader eventReader = xif.createXMLEventReader(xml);

        //read
        while (eventReader.hasNext()) {
            XMLEvent e = eventReader.nextEvent();
            if (e.isStartDocument() || e.isEndDocument())
                continue;
            res.add(e);
        }

        return res;
    }

    public StringExtraXml(String xml) throws XMLStreamException, RspecParseException {
        super(parseEvents(xml));
    }
}
