From 98fe7f854fda355dfb0371b84a0b472e9ec37ff8 Mon Sep 17 00:00:00 2001 From: Ariel Backenroth Date: Sat, 13 Jan 2007 21:16:45 +0000 Subject: [PATCH] - adding some tests for types with restrictions. - using xsi:nil to deal with invalid values (based on restrictions) for optional elements. no equivalent solution for attributes which is unfortunate. for attributes, the types will need to be able to accept empty string as a value for all types where no default is specified. this fixes part of the bug with dealing with comboboxes with no default. - more predictable inclusion of group headers using xf:appearance="repeated" attribute. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@4821 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../forms/xforms/FormBuilderException.java | 11 ++ .../web/forms/xforms/SchemaFormBuilder.java | 120 ++++++++----- .../unit-tests/simple-test/integer-test.xsd | 169 ++++++++++++++++++ .../unit-tests/simple-test/select1-test.xsd | 82 +++++++-- source/web/scripts/ajax/xforms.js | 65 ++----- 5 files changed, 342 insertions(+), 105 deletions(-) create mode 100644 source/test-resources/xforms/unit-tests/simple-test/integer-test.xsd diff --git a/source/java/org/alfresco/web/forms/xforms/FormBuilderException.java b/source/java/org/alfresco/web/forms/xforms/FormBuilderException.java index 3eff8a4d30..98988bd1b6 100644 --- a/source/java/org/alfresco/web/forms/xforms/FormBuilderException.java +++ b/source/java/org/alfresco/web/forms/xforms/FormBuilderException.java @@ -51,4 +51,15 @@ public class FormBuilderException { super(x); } + + /** + * Constructs an instance of FormBuilderException with the specified root exception. + * + * @param msg the detail message. + * @param x The root exception. + */ + public FormBuilderException(String msg, Exception x) + { + super(msg, x); + } } diff --git a/source/java/org/alfresco/web/forms/xforms/SchemaFormBuilder.java b/source/java/org/alfresco/web/forms/xforms/SchemaFormBuilder.java index 5dd3479aab..f3d06a4024 100644 --- a/source/java/org/alfresco/web/forms/xforms/SchemaFormBuilder.java +++ b/source/java/org/alfresco/web/forms/xforms/SchemaFormBuilder.java @@ -723,11 +723,14 @@ public class SchemaFormBuilder final String pathToRoot, final boolean checkIfExtension, final ResourceBundle resourceBundle) + throws FormBuilderException { XSObjectList attrUses = controlType.getAttributeUses(); if (attrUses == null) + { return; + } for (int i = 0; i < attrUses.getLength(); i++) { final XSAttributeUse currentAttributeUse = (XSAttributeUse)attrUses.item(i); @@ -771,41 +774,56 @@ public class SchemaFormBuilder { if (LOGGER.isDebugEnabled()) LOGGER.debug("bindId found: " + bindId); - + JXPathContext context = JXPathContext.newContext(formSection.getOwnerDocument()); final Pointer pointer = context.getPointer("//*[@" + NamespaceConstants.XFORMS_PREFIX + ":bind='" + bindId + "']"); if (pointer != null) + { control = (Element)pointer.getNode(); + } } - + //copy it if (control == null) + { LOGGER.warn("Corresponding control not found"); + } else { Element newControl = (Element) control.cloneNode(true); //set new Ids to XForm elements this.resetXFormIds(newControl); - + formSection.appendChild(newControl); } } else { - defaultInstanceElement.setAttributeNS(this.targetNamespace, - // XXXarielb - i probably need the prefix here i.e. "alf:" + attributeName - attributeName, - (currentAttributeUse.getConstraintType() == XSConstants.VC_NONE - ? null - : currentAttributeUse.getConstraintValue())); final String newPathToRoot = (pathToRoot == null || pathToRoot.length() == 0 ? "@" + currentAttribute.getName() : (pathToRoot.endsWith("/") ? pathToRoot + "@" + currentAttribute.getName() : pathToRoot + "/@" + currentAttribute.getName())); - + + LOGGER.debug("adding attribute " + attributeName + + " at " + newPathToRoot); + try + { + final String defaultValue = (currentAttributeUse.getConstraintType() == XSConstants.VC_NONE + ? null + : currentAttributeUse.getConstraintValue()); + defaultInstanceElement.setAttributeNS(this.targetNamespace, + // XXXarielb - i probably need the prefix here i.e. "alf:" + attributeName + attributeName, + defaultValue); + } + catch (Exception e) + { + throw new FormBuilderException("error retrieving default value for attribute " + + attributeName + " at " + newPathToRoot, e); + } this.addSimpleType(xForm, modelSection, formSection, @@ -829,6 +847,7 @@ public class SchemaFormBuilder boolean relative, final boolean checkIfExtension, final ResourceBundle resourceBundle) + throws FormBuilderException { if (controlType == null) { @@ -846,37 +865,32 @@ public class SchemaFormBuilder // add a group node and recurse // - Element groupElement = this.createGroup(xForm, - modelSection, - formSection, - owner, - resourceBundle); - Element groupWrapper = groupElement; - - if (groupElement != modelSection) - { - groupWrapper = groupElement; - } + final Element groupElement = this.createGroup(xForm, + modelSection, + formSection, + owner, + resourceBundle); final SchemaUtil.Occurance o = SchemaUtil.getOccurance(owner); final Element repeatSection = this.addRepeatIfNecessary(xForm, modelSection, - groupWrapper, + groupElement, controlType, o, pathToRoot); - Element repeatContentWrapper = repeatSection; - - if (repeatSection != groupWrapper) + if (repeatSection != groupElement) { + groupElement.setAttributeNS(NamespaceConstants.XFORMS_NS, + NamespaceConstants.XFORMS_PREFIX + ":appearance", + "repeated"); + // we have a repeat - repeatContentWrapper = repeatSection; relative = true; } this.addComplexTypeChildren(xForm, modelSection, defaultInstanceElement, - repeatContentWrapper, + repeatSection, schema, controlType, owner, @@ -897,6 +911,7 @@ public class SchemaFormBuilder final boolean relative, final boolean checkIfExtension, final ResourceBundle resourceBundle) + throws FormBuilderException { if (controlType == null) @@ -1015,6 +1030,7 @@ public class SchemaFormBuilder final XSElementDeclaration elementDecl, final String pathToRoot, final ResourceBundle resourceBundle) + throws FormBuilderException { XSTypeDefinition controlType = elementDecl.getTypeDefinition(); if (controlType == null) @@ -1483,17 +1499,19 @@ public class SchemaFormBuilder final SchemaUtil.Occurance o, final boolean checkIfExtension, final ResourceBundle resourceBundle) + throws FormBuilderException { if (group == null) + { return; + } - final Element repeatSection = - this.addRepeatIfNecessary(xForm, - modelSection, - formSection, - owner.getTypeDefinition(), - o, - pathToRoot); + final Element repeatSection = this.addRepeatIfNecessary(xForm, + modelSection, + formSection, + owner.getTypeDefinition(), + o, + pathToRoot); Element repeatContentWrapper = repeatSection; if (repeatSection != formSection) @@ -1629,12 +1647,11 @@ public class SchemaFormBuilder " default instance element for " + elementName + " at path " + path); // update the default instance - if (elementOccurs.maximum == 1) - { - defaultInstanceElement.appendChild(newDefaultInstanceElement.cloneNode(true)); - } - else + if (elementOccurs.isRepeated()) { + LOGGER.debug("adding " + (elementOccurs.minimum + 1) + + " default instance elements for " + elementName + + " at path " + path); for (int i = 0; i < elementOccurs.minimum + 1; i++) { final Element e = (Element)newDefaultInstanceElement.cloneNode(true); @@ -1647,6 +1664,18 @@ public class SchemaFormBuilder defaultInstanceElement.appendChild(e); } } + else + { + LOGGER.debug("adding one default instance element for " + elementName + + " at path " + path); + if (elementOccurs.minimum == 0) + { + newDefaultInstanceElement.setAttributeNS(NamespaceConstants.XMLSCHEMA_INSTANCE_NS, + NamespaceConstants.XMLSCHEMA_INSTANCE_PREFIX + ":nil", + "true"); + } + defaultInstanceElement.appendChild(newDefaultInstanceElement); + } } } else @@ -1671,13 +1700,16 @@ public class SchemaFormBuilder { // add xforms:repeat section if this element re-occurs - // if (o.maximum == 1) + { return formSection; + } if (LOGGER.isDebugEnabled()) + { LOGGER.debug("AddRepeatIfNecessary for multiple element for type " + controlType.getName() + ", maxOccurs=" + o.maximum); + } final Element repeatSection = xForm.createElementNS(NamespaceConstants.XFORMS_NS, @@ -1724,6 +1756,10 @@ public class SchemaFormBuilder //add a group inside the repeat? final Element group = xForm.createElementNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":group"); + + group.setAttributeNS(NamespaceConstants.XFORMS_NS, + NamespaceConstants.XFORMS_PREFIX + ":appearance", + "repeated"); this.setXFormsId(group); repeatSection.appendChild(group); return group; @@ -1770,6 +1806,10 @@ public class SchemaFormBuilder formSection, (XSElementDeclaration)owner, resourceBundle); + groupElement.setAttributeNS(NamespaceConstants.XFORMS_NS, + NamespaceConstants.XFORMS_PREFIX + ":appearance", + "repeated"); + //set content formSection = groupElement; } diff --git a/source/test-resources/xforms/unit-tests/simple-test/integer-test.xsd b/source/test-resources/xforms/unit-tests/simple-test/integer-test.xsd new file mode 100644 index 0000000000..4275ea15bc --- /dev/null +++ b/source/test-resources/xforms/unit-tests/simple-test/integer-test.xsd @@ -0,0 +1,169 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Integer Restricted -10 to 10 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/source/test-resources/xforms/unit-tests/simple-test/select1-test.xsd b/source/test-resources/xforms/unit-tests/simple-test/select1-test.xsd index 00d73edeea..8f9bd95b65 100644 --- a/source/test-resources/xforms/unit-tests/simple-test/select1-test.xsd +++ b/source/test-resources/xforms/unit-tests/simple-test/select1-test.xsd @@ -19,24 +19,70 @@ - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/source/web/scripts/ajax/xforms.js b/source/web/scripts/ajax/xforms.js index 3dc9d60924..7007d079c9 100644 --- a/source/web/scripts/ajax/xforms.js +++ b/source/web/scripts/ajax/xforms.js @@ -548,14 +548,12 @@ dojo.declare("alfresco.xforms.AbstractSelectWidget", valid = _evaluateXPath(binding.constraint, value, XPathResult.BOOLEAN_TYPE); } dojo.debug("valid " + dojo.dom.textContent(value) + "? " + valid); - if (valid) - { - result.push({ - id: value.getAttribute("id"), - label: dojo.dom.textContent(label), - value: dojo.dom.textContent(value) - }); - } + result.push({ + id: value.getAttribute("id"), + label: valid ? dojo.dom.textContent(label) : "", + value: valid ? dojo.dom.textContent(value) : "xxx", + valid: valid + }); } return result; } @@ -566,7 +564,6 @@ dojo.declare("alfresco.xforms.Select", { initializer: function(xform, xformsNode) { -// this.inherited("initializer", [ xform, xformsNode ]); }, render: function(attach_point) { @@ -662,7 +659,6 @@ dojo.declare("alfresco.xforms.Select1", { initializer: function(xform, xformsNode) { -// this.inherited("initializer", [ xform, xformsNode ]); }, render: function(attach_point) { @@ -707,6 +703,11 @@ dojo.declare("alfresco.xforms.Select1", attach_point.appendChild(this.widget); for (var i = 0; i < values.length; i++) { + if (initial_value && !values[i].valid) + { + // skip the invalid value if we have a default value + continue; + } var option = document.createElement("option"); this.widget.appendChild(option); option.appendChild(document.createTextNode(values[i].label)); @@ -789,24 +790,9 @@ dojo.declare("alfresco.xforms.Group", { this.children = []; dojo.html.removeClass(this.domNode, "xformsItem"); - this.showHeader = false; - }, - setShowHeader: function(showHeader) - { - if (showHeader == this.showHeader) - { - return; - } - - this.showHeader = showHeader; - if (this.showHeader && this.groupHeaderNode.style.display == "none") - { - this.groupHeaderNode.style.display = "block"; - } - if (!this.showHeader && this.groupHeaderNode.style.display == "block") - { - this.groupHeaderNode.style.display = "none"; - } + this.showHeader = + (this.xformsNode.getAttribute("appearance") != "repeated" && + this.xformsNode.getAttribute(alfresco_xforms_constants.XFORMS_PREFIX + ":appearance") != "repeated"); }, getWidgetsInvalidForSubmit: function() { @@ -947,6 +933,8 @@ dojo.declare("alfresco.xforms.Group", return child; }, + _childAdded: function(child) { }, + _childRemoved: function(child) { }, _destroy: function() { this.inherited("_destroy", []); @@ -977,11 +965,12 @@ dojo.declare("alfresco.xforms.Group", this.domNode.appendChild(idNode); } + this.showHeader = this.showHeader && this.parent != null; this.groupHeaderNode = document.createElement("div"); this.groupHeaderNode.id = this.id + "-groupHeaderNode"; this.domNode.appendChild(this.groupHeaderNode); dojo.html.setClass(this.groupHeaderNode, "xformsGroupHeader"); - this.groupHeaderNode.style.display = "none"; + this.groupHeaderNode.style.display = this.showHeader ? "block" : "none"; this.toggleExpandedImage = document.createElement("img"); this.groupHeaderNode.appendChild(this.toggleExpandedImage); @@ -1042,24 +1031,6 @@ dojo.declare("alfresco.xforms.Group", { this.children[i].hideAlert(); } - }, - _childAdded: function(child) - { - var hasNonGroupChildren = false; - for (var i in this.children) - { - if (!(this.children[i] instanceof alfresco.xforms.Group)) - { - hasNonGroupChildren = true; - break; - } - } - this.setShowHeader(hasNonGroupChildren && - this.children.length != 1 && - this.parent != null); - }, - _childRemoved: function(child) - { } });