/**
 *
 */
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());
        }
        return elements;
    }
}

@@ -1,15 +1,4 @@
-/**%0A *%0A */%0A
 pack
@@ -425,23 +425,16 @@
 ims%0A */%0A
-public 
 class St
@@ -486,31 +486,24 @@
 ion%3E %7B%0A%0A    
-public 
 StackOpParse
@@ -563,83 +563,17 @@
     
-/* (non-Javadoc)%0A     * @see org.javarosa.xml.ElementParser#parse()%0A     */
+@Override
 %0A   
@@ -790,24 +790,25 @@
  %22if%22);%0A
+%0A
         
 //XPath 
@@ -803,83 +803,82 @@
     
-//XPath check%0A%0A        try %7B%0A            if (%22create%22.equals(operation)) %7B%0A
+try %7B%0A            switch (operation) %7B%0A                case %22create%22:%0A    
     
@@ -952,73 +952,8 @@
     
-       Vector%3CStackFrameStep%3E children = getChildren(operation);%0A
     
@@ -951,37 +951,32 @@
                 
-     
 return StackOper
@@ -1016,55 +1016,19 @@
 al, 
-c
+getC
 hildren
-);%0A            %7D else if (%22push%22.equals
 (ope
@@ -1027,34 +1027,33 @@
 dren(operation))
- %7B
+;
 %0A               
@@ -1057,66 +1057,24 @@
     
-Vector%3CStackFrameStep%3E children = getChildren(operation);%0A
+case %22push%22:%0A   
     
@@ -1077,32 +1077,33 @@
                 
+ 
 return StackOper
@@ -1132,24 +1132,38 @@
 tional, 
-c
+getC
 hildren
+(operation)
 );%0A     
@@ -1173,48 +1173,30 @@
     
-%7D else if (%22clear%22.equals(operation)) %7B%0A
+    case %22clear%22:%0A    
     
@@ -1206,33 +1206,38 @@
          String 
-i
+clearI
 d = parser.getAt
@@ -1285,17 +1285,16 @@
     
+    
 if (
-this.
 next
@@ -1312,24 +1312,28 @@
 %22clear%22)) %7B%0A
+    
             
@@ -1395,16 +1395,20 @@
 o good!%0A
+    
         
@@ -1528,32 +1528,36 @@
                 
+    
 %7D%0A              
@@ -1550,32 +1550,36 @@
                 
+ 
+   
 return StackOper
@@ -1591,25 +1591,30 @@
 .ClearFrame(
-i
+clearI
 d, ifConditi
@@ -1636,17 +1636,25 @@
     
-%7D else %7B%0A
+    default:%0A    
     

Refactor to use switch