Search in sources :

Example 51 with Element

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

the class XFormParser method recurseForOutput.

private void recurseForOutput(Element e) {
    if (e.getChildCount() == 0)
        return;
    for (int i = 0; i < e.getChildCount(); i++) {
        int kidType = e.getType(i);
        if (kidType == Node.TEXT) {
            continue;
        }
        if (e.getChild(i) instanceof String) {
            continue;
        }
        Element kid = (Element) e.getChild(i);
        // is just text
        if (kidType == Node.ELEMENT && XFormUtils.isOutput(kid)) {
            String s = "${" + parseOutput(kid) + "}";
            e.removeChild(i);
            e.addChild(i, Node.TEXT, s);
        // has kids? Recurse through them and swap output tag for parsed version
        } else if (kid.getChildCount() != 0) {
            recurseForOutput(kid);
        // is something else
        }
    }
}
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 52 with Element

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

the class XFormParser method parseGroup.

private void parseGroup(IFormElement parent, Element e, int groupType) {
    GroupDef group = new GroupDef();
    // until we come up with a better scheme
    group.setID(serialQuestionID++);
    IDataReference dataRef = null;
    boolean refFromBind = false;
    List<String> usedAtts = new ArrayList<>();
    usedAtts.add(REF_ATTR);
    usedAtts.add(NODESET_ATTR);
    usedAtts.add(BIND_ATTR);
    usedAtts.add(APPEARANCE_ATTR);
    usedAtts.add("count");
    usedAtts.add("noAddRemove");
    if (groupType == CONTAINER_REPEAT) {
        group.setRepeat(true);
    }
    String ref = e.getAttributeValue(null, REF_ATTR);
    String nodeset = e.getAttributeValue(null, NODESET_ATTR);
    String bind = e.getAttributeValue(null, BIND_ATTR);
    group.setAppearanceAttr(e.getAttributeValue(null, APPEARANCE_ATTR));
    if (bind != null) {
        DataBinding binding = bindingsByID.get(bind);
        if (binding == null) {
            throw new XFormParseException("XForm Parse: invalid binding ID [" + bind + "]", e);
        }
        dataRef = binding.getReference();
        refFromBind = true;
    } else {
        if (group.getRepeat()) {
            if (nodeset != null) {
                dataRef = new XPathReference(nodeset);
            } else {
                throw new XFormParseException("XForm Parse: <repeat> with no binding ('bind' or 'nodeset')", e);
            }
        } else {
            if (ref != null) {
                dataRef = new XPathReference(ref);
            } else if (nodeset != null) {
                dataRef = new XPathReference(nodeset);
            }
        // <group> not required to have a binding so no exception thrown
        }
    }
    if (!refFromBind) {
        dataRef = getAbsRef(dataRef, parent);
    }
    group.setBind(dataRef);
    if (group.getRepeat()) {
        repeats.add((TreeReference) dataRef.getReference());
        String countRef = e.getAttributeValue(NAMESPACE_JAVAROSA, "count");
        if (countRef != null) {
            group.count = getAbsRef(new XPathReference(countRef), parent);
            group.noAddRemove = true;
        } else {
            group.noAddRemove = (e.getAttributeValue(NAMESPACE_JAVAROSA, "noAddRemove") != null);
        }
    }
    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);
        String childNamespace = (child != null ? child.getNamespace() : null);
        if (group.getRepeat() && NAMESPACE_JAVAROSA.equals(childNamespace)) {
            if ("chooseCaption".equals(childName)) {
                group.chooseCaption = getLabel(child);
            } else if ("addCaption".equals(childName)) {
                group.addCaption = getLabel(child);
            } else if ("delCaption".equals(childName)) {
                group.delCaption = getLabel(child);
            } else if ("doneCaption".equals(childName)) {
                group.doneCaption = getLabel(child);
            } else if ("addEmptyCaption".equals(childName)) {
                group.addEmptyCaption = getLabel(child);
            } else if ("doneEmptyCaption".equals(childName)) {
                group.doneEmptyCaption = getLabel(child);
            } else if ("entryHeader".equals(childName)) {
                group.entryHeader = getLabel(child);
            } else if ("delHeader".equals(childName)) {
                group.delHeader = getLabel(child);
            } else if ("mainHeader".equals(childName)) {
                group.mainHeader = getLabel(child);
            }
        }
    }
    for (int i = 0; i < e.getChildCount(); i++) {
        if (e.getType(i) == Element.ELEMENT) {
            parseElement(e.getElement(i), group, groupLevelHandlers);
        }
    }
    // save all the unused attributes verbatim...
    for (int i = 0; i < e.getAttributeCount(); i++) {
        String name = e.getAttributeName(i);
        if (usedAtts.contains(name))
            continue;
        group.setAdditionalAttribute(e.getAttributeNamespace(i), name, e.getAttributeValue(i));
    }
    // 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));
    }
    parent.addChild(group);
}
Also used : IDataReference(org.javarosa.core.model.IDataReference) DataBinding(org.javarosa.core.model.DataBinding) 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) XPathReference(org.javarosa.model.xform.XPathReference) GroupDef(org.javarosa.core.model.GroupDef)

Example 53 with Element

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

the class XFormParser method restoreDataModel.

public static FormInstance restoreDataModel(Document doc, Class restorableType) {
    Restorable r = (restorableType != null ? (Restorable) PrototypeFactory.getInstance(restorableType) : null);
    Element e = doc.getRootElement();
    TreeElement te = buildInstanceStructure(e, null, buildNamespacesMap(e), null);
    FormInstance dm = new FormInstance(te);
    loadNamespaces(e, dm);
    if (r != null) {
        RestoreUtils.templateData(r, dm, null);
    }
    loadInstanceData(e, te, null);
    return dm;
}
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) FormInstance(org.javarosa.core.model.instance.FormInstance) Restorable(org.javarosa.core.model.util.restorable.Restorable) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

Example 54 with Element

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

the class XFormParser method getLabel.

private String getLabel(Element e) {
    if (e.getChildCount() == 0)
        return null;
    recurseForOutput(e);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < e.getChildCount(); i++) {
        if (e.getType(i) != Node.TEXT && !(e.getChild(i) instanceof String)) {
            Object b = e.getChild(i);
            Element child = (Element) b;
            // If the child is in the HTML namespace, retain it.
            if (NAMESPACE_HTML.equals(child.getNamespace())) {
                sb.append(XFormSerializer.elementToString(child));
            } else {
                // Otherwise, ignore it.
                Std.out.println("Unrecognized tag inside of text: <" + child.getName() + ">. " + "Did you intend to use HTML markup? If so, ensure that the element is defined in " + "the HTML namespace.");
            }
        } else {
            sb.append(e.getText(i));
        }
    }
    return sb.toString().trim();
}
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 55 with Element

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

the class XFormParser method childOptimizationsOk.

/**
 * If all children of {@code parent} are {@link Element}s ({@link Element#getElement} returns non-null),
 * and the names of the children are all the same, and none of the children contain the template
 * attribute, more efficient methods may be used to build the collection of children. This method makes
 * that determination.
 *
 * @param parent the parent whose children are to be examined
 * @return the determination described above
 */
static boolean childOptimizationsOk(Element parent) {
    if (parent.getChildCount() == 0) {
        return false;
    }
    final Element firstChild = parent.getElement(0);
    if (firstChild == null || isTemplate(firstChild)) {
        return false;
    }
    final String firstName = firstChild.getName();
    for (int i = 1; i < parent.getChildCount(); i++) {
        Element child = parent.getElement(i);
        if (child == null || isTemplate(child) || !child.getName().equals(firstName)) {
            return false;
        }
    }
    return true;
}
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)

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