package org.odk.collect.android.jr.extensions;

import org.javarosa.core.model.QuestionDataExtension;
import org.javarosa.xform.parse.QuestionExtensionParser;
import org.javarosa.xform.parse.XFormParser;
import org.kxml2.kdom.Element;

/**
 * An additional parser for the "upload" question type that looks for an extra attribute
 * specifying the maximum allowable dimension for an image
 *
 * @author amstone
 */
public class ImageRestrictionExtensionParser extends QuestionExtensionParser {

    public ImageRestrictionExtensionParser() {
        setElementName("upload");
    }

    @Override
    public QuestionDataExtension parse(Element elt) {
        String s = elt.getAttributeValue(XFormParser.NAMESPACE_JAVAROSA,
                "imageDimensionScaledMax");
        if (s != null) {
            if (s.endsWith("px")) {
                s = s.substring(0, s.length() - 2);
            }
            try {
                int maxDimens = Integer.parseInt(s);
                return new ImageRestrictionExtension(maxDimens);
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return null;
    }
}

@@ -144,32 +144,85 @@
 xtensionParser;%0A
+import org.javarosa.xform.parse.XFormParseException;%0A
 import org.javar
@@ -1136,35 +1136,100 @@
             
-return null
+throw new XFormParseException(%22Invalid input for image max dimension: %22 + s)
 ;%0A          

throw XFormParseException if max dimen format is invalid