<commit_before>/**
 *
 */
package org.commcare.xml;

import org.commcare.suite.model.StackFrameStep;
import org.commcare.suite.model.StackOperation;
import org.javarosa.xml.ElementParser;
import org.javarosa.xml.util.InvalidStructureException;
import org.javarosa.xpath.parser.XPathSyntaxException;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.Vector;

/**
 * @author ctsims
 */
public class StackOpParser extends ElementParser<StackOperation> {

    public StackOpParser(KXmlParser parser) {
        super(parser);
    }

    /* (non-Javadoc)
     * @see org.javarosa.xml.ElementParser#parse()
     */
    public StackOperation parse() throws InvalidStructureException, IOException, XmlPullParserException {
        String operation = parser.getName();

        String ifConditional = parser.getAttributeValue(null, "if");
        //XPath check

        try {
            if ("create".equals(operation)) {
                String id = parser.getAttributeValue(null, "id");
                Vector<StackFrameStep> children = getChildren(operation);
                return StackOperation.CreateFrame(id, ifConditional, children);
            } else if ("push".equals(operation)) {
                Vector<StackFrameStep> children = getChildren(operation);
                return StackOperation.PushData(ifConditional, children);
            } else if ("clear".equals(operation)) {
                String id = parser.getAttributeValue(null, "frame");
                if (this.nextTagInBlock("clear")) {
                    //This means there are children of the clear, no good!
                    throw new InvalidStructureException("The <clear> operation does not support children", this.parser);
                }
                return StackOperation.ClearFrame(id, ifConditional);
            } else {
                throw new InvalidStructureException("<" + operation + "> is not a valid stack operation!", this.parser);
            }
        } catch (XPathSyntaxException e) {
            throw new InvalidStructureException("Invalid condition expression for " + operation + " operation: " + ifConditional + ".\n" + e.getMessage(), parser);
        }
    }

    private Vector<StackFrameStep> getChildren(String operation) throws InvalidStructureException, IOException, XmlPullParserException {
        Vector<StackFrameStep> elements = new Vector<>();
        StackFrameStepParser sfep = new StackFrameStepParser(parser);
        while (nextTagInBlock(operation)) {
            elements.addElement(sfep.parse());
        }<commit_msg>Refactor to use switch<commit_after>package org.commcare.xml;

import org.commcare.suite.model.StackFrameStep;
import org.commcare.suite.model.StackOperation;
import org.javarosa.xml.ElementParser;
import org.javarosa.xml.util.InvalidStructureException;
import org.javarosa.xpath.parser.XPathSyntaxException;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.Vector;

/**
 * @author ctsims
 */
class StackOpParser extends ElementParser<StackOperation> {

    StackOpParser(KXmlParser parser) {
        super(parser);
    }

    @Override
    public StackOperation parse() throws InvalidStructureException, IOException, XmlPullParserException {
        String operation = parser.getName();

        String ifConditional = parser.getAttributeValue(null, "if");

        try {
            switch (operation) {
                case "create":
                    String id = parser.getAttributeValue(null, "id");
                    return StackOperation.CreateFrame(id, ifConditional, getChildren(operation));
                case "push":
                    return StackOperation.PushData(ifConditional, getChildren(operation));
                case "clear":
                    String clearId = parser.getAttributeValue(null, "frame");
                    if (nextTagInBlock("clear")) {
                        //This means there are children of the clear, no good!
                        throw new InvalidStructureException("The <clear> operation does not support children", this.parser);
                    }
                    return StackOperation.ClearFrame(clearId, ifConditional);
                default:
                    throw new InvalidStructureException("<" + operation + "> is not a valid stack operation!", this.parser);
            }
        } catch (XPathSyntaxException e) {
            throw new InvalidStructureException("Invalid condition expression for " + operation + " operation: " + ifConditional + ".\n" + e.getMessage(), parser);
        }
    }

    private Vector<StackFrameStep> getChildren(String operation) throws InvalidStructureException, IOException, XmlPullParserException {
        Vector<StackFrameStep> elements = new Vector<>();
        StackFrameStepParser sfep = new StackFrameStepParser(parser);
        while (nextTagInBlock(operation)) {
            elements.addElement(sfep.parse());
        }
{'fraction_non_alphanumeric': 0.09854957281939201, 'fraction_numerical': 0.0007947546195112259, 'mean_word_length': 2.751117734724292, 'pattern_counts': {'":': 3, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 11}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '970438', 'n_tokens_mistral': 1272, 'n_tokens_neox': 1233, 'n_words': 294}