/**
 * mpg2dcm by Tom Doel
 *
 * http://github.com/tomdoel/mpg2dcm
 *
 * Distributed under the MIT License
 */

package com.tomdoel.mpg2dcm;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Class used to parse the XML file accompanying endoscopic video captures
 *
 * <p>Part of <a href="http://github.com/tomdoel/mpg2dcm">mpg2dcm</a>
 *
 * @author Tom Doel
 * @version 1.0
 */
public class EndoscopicXmlParser extends DefaultHandler {
    enum Mode {NONE, IMAGES, VIDEOS, SOUNDS}

    private Map<String, String> map = null;
    private String currentKey = null;
    private StringBuilder currentValue = null;
    private List<String> videoFiles = null;
    private List<String> videoThumbnailFiles = null;
    private List<String> pictureFiles = null;
    private List<String> soundFiles = null;
    private Mode mode = Mode.NONE;

    public void startDocument() throws SAXException {
        map = new HashMap<>();
        videoFiles = new ArrayList<>();
        videoThumbnailFiles = new ArrayList<>();
        pictureFiles = new ArrayList<>();
        soundFiles = new ArrayList<>();
    }

    public void endDocument() throws SAXException {
    }

    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
        currentKey = qName;
        currentValue = new StringBuilder();
        if (qName.equals("Images")) {
            mode = Mode.IMAGES;
        } else if (qName.equals("Videos")) {
            mode = Mode.VIDEOS;
        } else if (qName.equals("Sounds")) {
            mode = Mode.SOUNDS;
        }
    }

    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (mode == Mode.VIDEOS) {
            if (currentKey.equals("File")) {
                videoFiles.add(currentValue.toString());
            } else if (currentKey.equals("Thumb")) {
                videoThumbnailFiles.add(currentValue.toString());
            }
        } else {
            map.put(currentKey, currentValue.toString());
        }
        if (qName.equals("Images")) {
            mode = Mode.NONE;
        } else if (qName.equals("Videos")) {
            mode = Mode.NONE;
        } else if (qName.equals("Sounds")) {
            mode = Mode.NONE;
        }
    }

    public void characters(char ch[], int start, int length) throws SAXException {
        if (currentValue != null) {
            currentValue.append(ch, start, length);
        }
    }

    public Map<String, String> getTagMap() {
        return map;
    }

    public List<String> getVideoFilenames() {
        return videoFiles;
    }

    public List<String> getPictureFilenames() {
        return pictureFiles;
    }

    public List<String> getSoundFilenames() {
        return soundFiles;
    }
}
@@ -906,32 +906,132 @@
 reFiles = null;%0A
+    private List%3CString%3E pictureFileHints = null;%0A    private List%3CString%3E pictureFileAudio = null;%0A
     private List
@@ -1174,16 +1174,30 @@
 HashMap%3C
+String, String
 %3E();%0A   
@@ -1220,32 +1220,38 @@
 = new ArrayList%3C
+String
 %3E();%0A        vid
@@ -1275,32 +1275,38 @@
 = new ArrayList%3C
+String
 %3E();%0A        pic
@@ -1323,32 +1323,142 @@
 = new ArrayList%3C
+String%3E();%0A        pictureFileHints = new ArrayList%3CString%3E();%0A        pictureFileAudio = new ArrayList%3CString
 %3E();%0A        sou
@@ -1481,16 +1481,22 @@
 rayList%3C
+String
 %3E();%0A   
@@ -2347,32 +2347,451 @@
 ;%0A            %7D%0A
+        %7D else if (mode == Mode.IMAGES) %7B%0A                if (currentKey.equals(%22File%22)) %7B%0A                    pictureFiles.add(currentValue.toString());%0A                %7D else if (currentKey.equals(%22Hint%22)) %7B%0A                    pictureFileHints.add(currentValue.toString());%0A                %7D else if (currentKey.equals(%22AUDIO%22)) %7B%0A                    pictureFileAudio.add(currentValue.toString());%0A                %7D%0A
         %7D else %7B

Store image files parsed from XML