Search in sources :

Example 1 with Node

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

the class XFormParser method loadInstanceData.

// TODO: hook here for turning sub-trees into complex IAnswerData objects (like for immunizations)
// FIXME: the 'ref' and FormDef parameters (along with the helper function above that initializes them) are only needed so that we
// can fetch QuestionDefs bound to the given node, as the QuestionDef reference is needed to properly represent answers
// to select questions. obviously, we want to fix this.
private static void loadInstanceData(Element node, TreeElement cur, FormDef f) {
    int numChildren = node.getChildCount();
    boolean hasElements = false;
    for (int i = 0; i < numChildren; i++) {
        if (node.getType(i) == Node.ELEMENT) {
            hasElements = true;
            break;
        }
    }
    if (hasElements) {
        // stores max multiplicity seen for a given node name thus far
        HashMap<String, Integer> multiplicities = new HashMap<>();
        for (int i = 0; i < numChildren; i++) {
            if (node.getType(i) == Node.ELEMENT) {
                Element child = node.getElement(i);
                String name = child.getName();
                int index;
                boolean isTemplate = isTemplate(child);
                if (isTemplate) {
                    index = TreeReference.INDEX_TEMPLATE;
                } else {
                    // update multiplicity counter
                    Integer mult = multiplicities.get(name);
                    index = (mult == null ? 0 : mult + 1);
                    multiplicities.put(name, index);
                }
                loadInstanceData(child, cur.getChild(name, index), f);
            }
        }
    } else {
        String text = getXMLText(node, true);
        if (text != null && text.trim().length() > 0) {
            // ignore text that is only whitespace
            // TODO: custom data types? modelPrototypes?
            cur.setValue(XFormAnswerDataParser.getAnswerData(text, cur.getDataType(), ghettoGetQuestionDef(cur.getDataType(), f, cur.getRef())));
        }
    }
}
Also used : HashMap(java.util.HashMap) 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 2 with Node

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

the class XFormParser method parseModel.

// for ease of parsing, we assume a model comes before the controls, which isn't necessarily mandated by the xforms spec
private void parseModel(Element e) {
    // no attributes parsed in title.
    List<String> usedAtts = new ArrayList<>();
    List<Element> delayedParseElements = new ArrayList<>();
    if (modelFound) {
        reporter.warning(XFormParserReporter.TYPE_INVALID_STRUCTURE, "Multiple models not supported. Ignoring subsequent models.", getVagueLocation(e));
        return;
    }
    modelFound = true;
    if (XFormUtils.showUnusedAttributeWarning(e, usedAtts)) {
        reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(e, usedAtts), getVagueLocation(e));
    }
    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 ("itext".equals(childName)) {
            parseIText(child);
        } else if ("instance".equals(childName)) {
            // we save parsing the instance node until the end, giving us the information we need about
            // binds and data types and such
            saveInstanceNode(child);
        } else if (BIND_ATTR.equals(childName)) {
            // <instance> must come before <bind>s
            parseBind(child);
        } else if ("submission".equals(childName)) {
            delayedParseElements.add(child);
        } else if (namedActions.contains(childName) || (childName != null && structuredActions.containsKey(childName))) {
            delayedParseElements.add(child);
        } else {
            // invalid model content
            if (type == Node.ELEMENT) {
                throw new XFormParseException("Unrecognized top-level tag [" + childName + "] found within <model>", child);
            } else if (type == Node.TEXT && getXMLText(e, i, true).length() != 0) {
                throw new XFormParseException("Unrecognized text content found within <model>: \"" + getXMLText(e, i, true) + "\"", child == null ? e : child);
            }
        }
        if (child == null || BIND_ATTR.equals(childName) || "itext".equals(childName)) {
            // 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
            e.removeChild(i);
            --i;
        // #endif
        }
    }
    // Now parse out the submission/action blocks (we needed the binds to all be set before we could)
    for (Element child : delayedParseElements) {
        String name = child.getName();
        if (name.equals("submission")) {
            parseSubmission(child);
        } else {
            // For now, anything that isn't a submission is an action
            if (namedActions.contains(name)) {
                parseNamedAction(child);
            } else {
                structuredActions.get(name).handle(this, child, _f);
            }
        }
    }
}
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 3 with Node

use of org.kxml2.kdom.Node 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)

Aggregations

IFormElement (org.javarosa.core.model.IFormElement)3 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)3 TreeElement (org.javarosa.core.model.instance.TreeElement)3 Element (org.kxml2.kdom.Element)3 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Node (org.kxml2.kdom.Node)1