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);
}
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));
}
}
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;
}
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);
}
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));
}
}
Aggregations