Search in sources :

Example 46 with Element

use of org.kxml2.kdom.Element in project javarosa by opendatakit.

the class XFormParser method saveInstanceNode.

private void saveInstanceNode(Element instance) {
    Element instanceNode = null;
    String instanceId = instance.getAttributeValue("", "id");
    for (int i = 0; i < instance.getChildCount(); i++) {
        if (instance.getType(i) == Node.ELEMENT) {
            if (instanceNode != null) {
                throw new XFormParseException("XForm Parse: <instance> has more than one child element", instance);
            } else {
                instanceNode = instance.getElement(i);
            }
        }
    }
    if (instanceNode == null) {
        // no kids
        instanceNode = instance;
    }
    if (mainInstanceNode == null) {
        mainInstanceNode = instanceNode;
    }
    instanceNodes.add(instanceNode);
    instanceNodeIdStrs.add(instanceId);
}
Also used : TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) Element(org.kxml2.kdom.Element) IFormElement(org.javarosa.core.model.IFormElement)

Example 47 with Element

use of org.kxml2.kdom.Element in project javarosa by opendatakit.

the class XFormParser method parseTextHandle.

private void parseTextHandle(TableLocaleSource l, Element text) {
    String id = text.getAttributeValue("", ID_ATTR);
    // used for parser warnings...
    List<String> usedAtts = new ArrayList<>();
    List<String> childUsedAtts = new ArrayList<>();
    usedAtts.add(ID_ATTR);
    usedAtts.add(FORM_ATTR);
    childUsedAtts.add(FORM_ATTR);
    childUsedAtts.add(ID_ATTR);
    if (id == null || id.length() == 0) {
        throw new XFormParseException("no id defined for <text>", text);
    }
    for (int k = 0; k < text.getChildCount(); k++) {
        Element value = text.getElement(k);
        if (value == null)
            continue;
        if (!value.getName().equals(VALUE)) {
            throw new XFormParseException("Unrecognized element [" + value.getName() + "] in Itext->translation->text");
        }
        String form = value.getAttributeValue("", FORM_ATTR);
        if (form != null && form.length() == 0) {
            form = null;
        }
        String data = getLabel(value);
        if (data == null) {
            data = "";
        }
        // kind of a hack
        String textID = (form == null ? id : id + ";" + form);
        if (l.hasMapping(textID)) {
            throw new XFormParseException("duplicate definition for text ID \"" + id + "\" and form \"" + form + "\"" + ". Can only have one definition for each text form.", text);
        }
        l.setLocaleMapping(textID, data);
        // print unused attribute warning message for child element
        if (XFormUtils.showUnusedAttributeWarning(value, childUsedAtts)) {
            reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(value, childUsedAtts), getVagueLocation(value));
        }
    }
    // print unused attribute warning message for parent element
    if (XFormUtils.showUnusedAttributeWarning(text, usedAtts)) {
        reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(text, usedAtts), getVagueLocation(text));
    }
}
Also used : TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) Element(org.kxml2.kdom.Element) IFormElement(org.javarosa.core.model.IFormElement) ArrayList(java.util.ArrayList)

Example 48 with Element

use of org.kxml2.kdom.Element in project javarosa by opendatakit.

the class XFormParser method getVagueLocation.

public static String getVagueLocation(Element e) {
    String path = e.getName();
    Element walker = e;
    while (walker != null) {
        Node n = walker.getParent();
        if (n instanceof Element) {
            walker = (Element) n;
            String step = walker.getName();
            for (int i = 0; i < walker.getAttributeCount(); ++i) {
                step += "[@" + walker.getAttributeName(i) + "=";
                step += walker.getAttributeValue(i) + "]";
            }
            path = step + "/" + path;
        } else {
            walker = null;
            path = "/" + path;
        }
    }
    String elementString = getVagueElementPrintout(e, 2);
    String fullmsg = "\n    Problem found at nodeset: " + path;
    fullmsg += "\n    With element " + elementString + "\n";
    return fullmsg;
}
Also used : TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) Element(org.kxml2.kdom.Element) IFormElement(org.javarosa.core.model.IFormElement) Node(org.kxml2.kdom.Node)

Example 49 with Element

use of org.kxml2.kdom.Element in project javarosa by opendatakit.

the class XFormParser method parseTranslation.

private void parseTranslation(Localizer l, Element trans) {
    // ///for warning message
    List<String> usedAtts = new ArrayList<>();
    usedAtts.add("lang");
    usedAtts.add("default");
    // ///////////////////////
    String lang = trans.getAttributeValue("", "lang");
    if (lang == null || lang.length() == 0) {
        throw new XFormParseException("no language specified for <translation>", trans);
    }
    String isDefault = trans.getAttributeValue("", "default");
    if (!l.addAvailableLocale(lang)) {
        throw new XFormParseException("duplicate <translation> for language '" + lang + "'", trans);
    }
    if (isDefault != null) {
        if (l.getDefaultLocale() != null)
            throw new XFormParseException("more than one <translation> set as default", trans);
        l.setDefaultLocale(lang);
    }
    TableLocaleSource source = new TableLocaleSource();
    Collection<Integer> removeIndexes = new HashSet<>();
    for (int j = 0; j < trans.getChildCount(); j++) {
        Element text = trans.getElement(j);
        if (text == null || !text.getName().equals("text")) {
            continue;
        }
        parseTextHandle(source, text);
        // Clayton Sims - Jun 17, 2009 - This code is used when the stinginess flag
        // is set for the build. It dynamically wipes out old model nodes once they're
        // used. This is sketchy if anything else plans on touching the nodes.
        // This code can be removed once we're pull-parsing
        // #if org.javarosa.xform.stingy
        removeIndexes.add(j);
    }
    ElementChildDeleter.delete(trans, removeIndexes);
    // print unused attribute warning message for parent element
    if (XFormUtils.showUnusedAttributeWarning(trans, usedAtts)) {
        reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(trans, usedAtts), getVagueLocation(trans));
    }
    l.registerLocaleResource(lang, source);
}
Also used : TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) Element(org.kxml2.kdom.Element) IFormElement(org.javarosa.core.model.IFormElement) ArrayList(java.util.ArrayList) TableLocaleSource(org.javarosa.core.services.locale.TableLocaleSource) HashSet(java.util.HashSet)

Example 50 with Element

use of org.kxml2.kdom.Element in project javarosa by opendatakit.

the class XFormParser method parseItemset.

private void parseItemset(QuestionDef q, Element e, IFormElement qparent) {
    ItemsetBinding itemset = new ItemsetBinding();
    // //////////////USED FOR PARSER WARNING OUTPUT ONLY
    // catalogue of used attributes in this method/element
    List<String> usedAtts = new ArrayList<>();
    // for child with name 'label'
    List<String> labelUA = new ArrayList<>();
    // for child with name 'value'
    List<String> valueUA = new ArrayList<>();
    // for child with name 'copy'
    List<String> copyUA = new ArrayList<>();
    usedAtts.add(NODESET_ATTR);
    labelUA.add(REF_ATTR);
    valueUA.add(REF_ATTR);
    valueUA.add(FORM_ATTR);
    copyUA.add(REF_ATTR);
    // //////////////////////////////////////////////////
    /*
         * At this point in time, we cannot construct a valid nodesetRef
         *
         * Leave all ...Ref entries as null and test the ...Expr entries for null / non-null values.
         *
         * We will patch this all up in the verifyItemsetBindings() method.
         */
    String nodesetStr = e.getAttributeValue("", NODESET_ATTR);
    if (nodesetStr == null)
        throw new RuntimeException("No nodeset attribute in element: [" + e.getName() + "]. This is required. (Element Printout:" + XFormSerializer.elementToString(e) + ")");
    XPathPathExpr path = XPathReference.getPathExpr(nodesetStr);
    itemset.nodesetExpr = new XPathConditional(path);
    itemset.contextRef = getFormElementRef(qparent);
    // this is not valid yet...
    itemset.nodesetRef = null;
    // itemset.nodesetRef = FormInstance.unpackReference(getAbsRef(new XPathReference(path.getReference(true)), itemset.contextRef));
    itemset.copyMode = false;
    for (int i = 0; i < e.getChildCount(); i++) {
        int type = e.getType(i);
        Element child = (type == Node.ELEMENT ? e.getElement(i) : null);
        String childName = (child != null ? child.getName() : null);
        if (LABEL_ELEMENT.equals(childName)) {
            String labelXpath = child.getAttributeValue("", REF_ATTR);
            boolean labelItext = false;
            // print unused attribute warning message for child element
            if (XFormUtils.showUnusedAttributeWarning(child, labelUA)) {
                reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(child, labelUA), getVagueLocation(child));
            }
            if (labelXpath != null) {
                if (labelXpath.startsWith(DYNAMIC_ITEXT_OPEN) && labelXpath.endsWith(DYNAMIC_ITEXT_CLOSE)) {
                    labelXpath = labelXpath.substring(DYNAMIC_ITEXT_OPEN.length(), labelXpath.lastIndexOf(DYNAMIC_ITEXT_CLOSE));
                    labelItext = true;
                }
            } else {
                throw new XFormParseException("<label> in <itemset> requires 'ref'");
            }
            XPathPathExpr labelPath = XPathReference.getPathExpr(labelXpath);
            itemset.labelRef = null;
            // itemset.labelRef = FormInstance.unpackReference(getAbsRef(new XPathReference(labelPath), itemset.nodesetRef));
            itemset.labelExpr = new XPathConditional(labelPath);
            itemset.labelIsItext = labelItext;
        } else if ("copy".equals(childName)) {
            String copyXpath = child.getAttributeValue("", REF_ATTR);
            // print unused attribute warning message for child element
            if (XFormUtils.showUnusedAttributeWarning(child, copyUA)) {
                reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(child, copyUA), getVagueLocation(child));
            }
            if (copyXpath == null) {
                throw new XFormParseException("<copy> in <itemset> requires 'ref'");
            }
            XPathPathExpr copyPath = XPathReference.getPathExpr(copyXpath);
            itemset.copyRef = null;
            // itemset.copyRef = FormInstance.unpackReference(getAbsRef(new XPathReference(copyPath), itemset.nodesetRef));
            itemset.copyExpr = new XPathConditional(copyPath);
            itemset.copyMode = true;
        } else if (VALUE.equals(childName)) {
            String valueXpath = child.getAttributeValue("", REF_ATTR);
            // print unused attribute warning message for child element
            if (XFormUtils.showUnusedAttributeWarning(child, valueUA)) {
                reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(child, valueUA), getVagueLocation(child));
            }
            if (valueXpath == null) {
                throw new XFormParseException("<value> in <itemset> requires 'ref'");
            }
            XPathPathExpr valuePath = XPathReference.getPathExpr(valueXpath);
            itemset.valueRef = null;
            // itemset.valueRef = FormInstance.unpackReference(getAbsRef(new XPathReference(valuePath), itemset.nodesetRef));
            itemset.valueExpr = new XPathConditional(valuePath);
        }
    }
    if (itemset.labelExpr == null) {
        throw new XFormParseException("<itemset> requires <label>");
    } else if (itemset.copyExpr == null && itemset.valueExpr == null) {
        throw new XFormParseException("<itemset> requires <copy> or <value>");
    }
    if (itemset.copyExpr != null) {
        if (itemset.valueExpr == null) {
            reporter.warning(XFormParserReporter.TYPE_TECHNICAL, "<itemset>s with <copy> are STRONGLY recommended to have <value> as well; pre-selecting, default answers, and display of answers will not work properly otherwise", getVagueLocation(e));
        }
    }
    itemsets.add(itemset);
    q.setDynamicChoices(itemset);
    // print unused attribute warning message for parent element
    if (XFormUtils.showUnusedAttributeWarning(e, usedAtts)) {
        reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(e, usedAtts), getVagueLocation(e));
    }
}
Also used : XPathPathExpr(org.javarosa.xpath.expr.XPathPathExpr) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) Element(org.kxml2.kdom.Element) IFormElement(org.javarosa.core.model.IFormElement) ArrayList(java.util.ArrayList) ItemsetBinding(org.javarosa.core.model.ItemsetBinding) XPathConditional(org.javarosa.xpath.XPathConditional)

Aggregations

Element (org.kxml2.kdom.Element)50 TreeElement (org.javarosa.core.model.instance.TreeElement)25 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)23 ArrayList (java.util.ArrayList)21 IOException (java.io.IOException)17 IFormElement (org.javarosa.core.model.IFormElement)17 KXmlParser (org.kxml2.io.KXmlParser)12 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)12 File (java.io.File)8 Test (org.junit.Test)7 ParsingException (org.opendatakit.briefcase.model.ParsingException)7 XmlPullParser (org.xmlpull.v1.XmlPullParser)7 Document (org.kxml2.kdom.Document)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 OutputStreamWriter (java.io.OutputStreamWriter)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 HashMap (java.util.HashMap)3 KXmlSerializer (org.kxml2.io.KXmlSerializer)3