Example usage for org.w3c.dom Element setAttributeNS

List of usage examples for org.w3c.dom Element setAttributeNS

Introduction

In this page you can find the example usage for org.w3c.dom Element setAttributeNS.

Prototype

public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException;

Source Link

Document

Adds a new attribute.

Usage

From source file:org.alfresco.web.forms.xforms.Schema2XForms.java

private Element createFormControl(final Document xformsDocument, final XSModel schema, final String caption,
        final XSTypeDefinition controlType, final XSObject owner, final String bindId,
        final Element bindElement, final SchemaUtil.Occurrence o, final ResourceBundle resourceBundle) {
    Element formControl = null;
    if (controlType.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE
            && ((XSSimpleTypeDefinition) controlType).getItemType() != null) {
        formControl = this.createControlForListType(xformsDocument, (XSSimpleTypeDefinition) controlType, owner,
                caption, bindElement, resourceBundle);
    } else if (controlType.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE
            && ((XSSimpleTypeDefinition) controlType)
                    .isDefinedFacet(XSSimpleTypeDefinition.FACET_ENUMERATION)) {
        formControl = this.createControlForEnumerationType(xformsDocument, (XSSimpleTypeDefinition) controlType,
                owner, caption, bindElement, resourceBundle);
    } else if (controlType.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE
            && "anyType".equals(controlType.getName())) {
        formControl = this.createControlForAnyType(xformsDocument, caption, controlType);
    } else {/*from w  w  w .j av a2 s. c om*/
        formControl = this.createControlForAtomicType(xformsDocument, (XSSimpleTypeDefinition) controlType,
                owner, caption, resourceBundle);
    }

    formControl.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":bind",
            bindId);

    // TODO: Enhance alert statement based on facet restrictions.
    // TODO: Enhance to support minOccurs > 1 and maxOccurs > 1.
    // TODO: Add i18n/l10n suppport to this - use java MessageFormatter...
    //
    //       e.g. Please provide a valid value for 'Address'. 'Address' is a mandatory decimal field.
    //
    final Element alertElement = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":alert");
    formControl.appendChild(alertElement);
    this.setXFormsId(alertElement);

    String alert = Schema2XForms.extractPropertyFromAnnotation(NamespaceService.ALFRESCO_URI, "alert",
            this.getAnnotation(owner), resourceBundle);
    if (alert == null) {
        alert = ("Please provide a valid value for '" + caption + "'." + " '" + caption + "' is "
                + (o.minimum == 0 ? "an optional" : "a required") + " '"
                + this.createCaption(this.getXFormsTypeName(xformsDocument, schema, controlType)) + "' value.");
    }
    alertElement.appendChild(xformsDocument.createTextNode(alert));

    final String hint = Schema2XForms.extractPropertyFromAnnotation(NamespaceService.ALFRESCO_URI, "hint",
            this.getAnnotation(owner), resourceBundle);
    if (hint != null) {
        final Element hintElement = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
                NamespaceConstants.XFORMS_PREFIX + ":hint");
        formControl.appendChild(hintElement);
        this.setXFormsId(hintElement);
        hintElement.appendChild(xformsDocument.createTextNode(hint));
    }
    return formControl;
}

From source file:org.alfresco.web.forms.xforms.Schema2XForms.java

private Document createFormTemplate(final String formId) {
    final Document xformsDocument = XMLUtil.newDocument();

    final Element envelopeElement = xformsDocument.createElementNS(NamespaceConstants.XHTML_NS,
            NamespaceConstants.XHTML_PREFIX + ":html");
    xformsDocument.appendChild(envelopeElement);

    //set namespace attribute
    addNamespace(envelopeElement, NamespaceConstants.XHTML_PREFIX, NamespaceConstants.XHTML_NS);
    addNamespace(envelopeElement, NamespaceConstants.XFORMS_PREFIX, NamespaceConstants.XFORMS_NS);
    addNamespace(envelopeElement, NamespaceConstants.XMLEVENTS_PREFIX, NamespaceConstants.XMLEVENTS_NS);
    addNamespace(envelopeElement, NamespaceConstants.XMLSCHEMA_INSTANCE_PREFIX,
            NamespaceConstants.XMLSCHEMA_INSTANCE_NS);
    addNamespace(envelopeElement, NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI);

    //base/* w ww .ja va 2s.c o  m*/
    if (this.base != null && this.base.length() != 0) {
        envelopeElement.setAttributeNS(NamespaceConstants.XML_NS, NamespaceConstants.XML_PREFIX + ":base",
                this.base);
    }

    //model element
    final Element modelElement = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":model");
    modelElement.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":functions",
            NamespaceConstants.CHIBA_PREFIX + ":match");
    this.setXFormsId(modelElement);
    final Element modelWrapper = xformsDocument.createElementNS(NamespaceConstants.XHTML_NS,
            NamespaceConstants.XHTML_PREFIX + ":head");
    modelWrapper.appendChild(modelElement);
    envelopeElement.appendChild(modelWrapper);

    //form control wrapper -> created by wrapper
    //Element formWrapper = xformsDocument.createElement("body");
    //envelopeElement.appendChild(formWrapper);
    final Element formWrapper = xformsDocument.createElementNS(NamespaceConstants.XHTML_NS,
            NamespaceConstants.XHTML_PREFIX + ":body");
    envelopeElement.appendChild(formWrapper);
    return xformsDocument;
}

From source file:org.alfresco.web.forms.xforms.Schema2XForms.java

private Element createGroup(final Document xformsDocument, final Element modelSection,
        final Element formSection, final XSElementDeclaration owner, final ResourceBundle resourceBundle) {
    // add a group node and recurse
    final Element result = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":group");
    this.setXFormsId(result);
    final String appearance = extractPropertyFromAnnotation(NamespaceService.ALFRESCO_URI, "appearance",
            this.getAnnotation(owner), resourceBundle);
    result.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":appearance",
            appearance == null || appearance.length() == 0 ? "full" : appearance);

    formSection.appendChild(result);//from  ww  w.j  av a  2s  . c  o  m
    result.appendChild(this.createLabel(xformsDocument, this.createCaption(owner, resourceBundle)));
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("[createGroup] group =\n" + XMLUtil.toString(result));
    }
    return result;
}

From source file:org.alfresco.web.forms.xforms.Schema2XForms.java

/**
 * Creates a form control for an XML Schema any type.
 * <br/>/*from   w w  w.  j  a  v a 2s .  c  om*/
 * This method is called when the form builder determines a form control is required for
 * an any type.
 * The implementation of this method is responsible for creating an XML element of the
 * appropriate type to receive a value for <b>controlType</b>. The caller is responsible
 * for adding the returned element to the form and setting caption, bind, and other
 * standard elements and attributes.
 *
 * @param xformsDocument       The XForm document.
 * @param controlType The XML Schema type for which the form control is to be created.
 * @return The element for the form control.
 */
public Element createControlForAnyType(final Document xformsDocument, final String caption,
        final XSTypeDefinition controlType) {
    final Element control = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":textarea");
    this.setXFormsId(control);
    control.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":appearance",
            "compact");
    control.appendChild(this.createLabel(xformsDocument, caption));
    return control;
}

From source file:org.alfresco.web.forms.xforms.Schema2XForms.java

/**
 * Creates a form control for an XML Schema simple atomic type.
 * <p/>/*w  w  w . j  a v a 2 s  . co  m*/
 * This method is called when the form builder determines a form control is required for
 * an atomic type.
 * The implementation of this method is responsible for creating an XML element of the
 * appropriate type to receive a value for <b>controlType</b>. The caller is responsible
 * for adding the returned element to the form and setting caption, bind, and other
 * standard elements and attributes.
 *
 * @param xformsDocument       The XForm document.
 * @param controlType The XML Schema type for which the form control is to be created.
 * @return The element for the form control.
 */
public Element createControlForAtomicType(final Document xformsDocument,
        final XSSimpleTypeDefinition controlType, final XSObject owner, final String caption,
        final ResourceBundle resourceBundle) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("[createControlForAtomicType] {name: " + controlType.getName() + ", numeric: "
                + controlType.getNumeric() + ", bounded: " + controlType.getBounded() + ", finite: "
                + controlType.getFinite() + ", ordered: " + controlType.getOrdered() + ", final: "
                + controlType.getFinal() + ", minInc: "
                + controlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_MININCLUSIVE) + ", maxInc: "
                + controlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_MAXINCLUSIVE) + ", minExc: "
                + controlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_MINEXCLUSIVE) + ", maxExc: "
                + controlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_MAXEXCLUSIVE)
                + ", totalDigits: " + controlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_TOTALDIGITS)
                + ", length: " + controlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_LENGTH)
                + ", minLength: " + controlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_MINLENGTH)
                + ", maxLength: " + controlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_MAXLENGTH)
                + ", fractionDigits: "
                + controlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_FRACTIONDIGITS)
                + ", builtInTypeName: " + SchemaUtil.getBuiltInTypeName(controlType) + ", builtInType: "
                + SchemaUtil.getBuiltInType(controlType) + "}");
    }
    String appearance = extractPropertyFromAnnotation(NamespaceService.ALFRESCO_URI, "appearance",
            this.getAnnotation(owner), resourceBundle);
    Element result = null;
    if (controlType.getNumeric()) {
        if (controlType.getBounded()
                && controlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_MAXINCLUSIVE) != null
                && controlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_MININCLUSIVE) != null) {
            result = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
                    NamespaceConstants.XFORMS_PREFIX + ":range");
            result.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":start",
                    controlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_MININCLUSIVE));
            result.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":end",
                    controlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_MAXINCLUSIVE));
        } else {
            result = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
                    NamespaceConstants.XFORMS_PREFIX + ":input");
        }
        if (controlType.isDefinedFacet(XSSimpleTypeDefinition.FACET_FRACTIONDIGITS)) {
            String fractionDigits = controlType
                    .getLexicalFacetValue(XSSimpleTypeDefinition.FACET_FRACTIONDIGITS);
            if (fractionDigits == null || fractionDigits.length() == 0) {
                final short builtInType = SchemaUtil.getBuiltInType(controlType);
                fractionDigits = (builtInType >= XSConstants.INTEGER_DT
                        && builtInType <= XSConstants.POSITIVEINTEGER_DT ? "0" : null);
            }
            if (fractionDigits != null) {
                result.setAttributeNS(NamespaceService.ALFRESCO_URI,
                        NamespaceService.ALFRESCO_PREFIX + ":fractionDigits", fractionDigits);
            }
        }
        if (controlType.isDefinedFacet(XSSimpleTypeDefinition.FACET_TOTALDIGITS)) {
            result.setAttributeNS(NamespaceService.ALFRESCO_URI,
                    NamespaceService.ALFRESCO_PREFIX + ":totalDigits",
                    controlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_TOTALDIGITS));

        }
    } else {
        switch (SchemaUtil.getBuiltInType(controlType)) {
        case XSConstants.BOOLEAN_DT: {
            result = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
                    NamespaceConstants.XFORMS_PREFIX + ":select1");
            final String[] values = { "true", "false" };
            for (String v : values) {
                final Element item = this.createXFormsItem(xformsDocument, v, v);
                result.appendChild(item);
            }
            break;
        }
        case XSConstants.STRING_DT: {
            result = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
                    NamespaceConstants.XFORMS_PREFIX + ":textarea");
            if (appearance == null || appearance.length() == 0) {
                appearance = "compact";
            }
            break;
        }
        case XSConstants.ANYURI_DT: {
            result = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
                    NamespaceConstants.XFORMS_PREFIX + ":upload");
            final Element e = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
                    NamespaceConstants.XFORMS_PREFIX + ":filename");
            this.setXFormsId(e);
            result.appendChild(e);
            e.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":ref", ".");
            break;
        }
        default: {
            result = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
                    NamespaceConstants.XFORMS_PREFIX + ":input");
            if ((appearance == null || appearance.length() == 0)
                    && SchemaUtil.getBuiltInType(controlType) == XSConstants.NORMALIZEDSTRING_DT) {
                appearance = "full";
            }
            if (controlType.isDefinedFacet(XSSimpleTypeDefinition.FACET_LENGTH)) {
                result.setAttributeNS(NamespaceService.ALFRESCO_URI,
                        NamespaceService.ALFRESCO_PREFIX + ":length",
                        controlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_LENGTH));
            } else if (controlType.isDefinedFacet(XSSimpleTypeDefinition.FACET_MINLENGTH)
                    || controlType.isDefinedFacet(XSSimpleTypeDefinition.FACET_MAXLENGTH)) {
                if (controlType.isDefinedFacet(XSSimpleTypeDefinition.FACET_MINLENGTH)) {
                    result.setAttributeNS(NamespaceService.ALFRESCO_URI,
                            NamespaceService.ALFRESCO_PREFIX + ":minLength",
                            controlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_MINLENGTH));
                }
                if (controlType.isDefinedFacet(XSSimpleTypeDefinition.FACET_MAXLENGTH)) {
                    result.setAttributeNS(NamespaceService.ALFRESCO_URI,
                            NamespaceService.ALFRESCO_PREFIX + ":maxLength",
                            controlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_MAXLENGTH));
                }
            }
            if (SchemaUtil.getBuiltInType(controlType) == XSConstants.DATE_DT) {
                String minInclusive = null;
                String maxInclusive = null;
                final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                final Calendar calendar = Calendar.getInstance();
                if (controlType.isDefinedFacet(XSSimpleTypeDefinition.FACET_MAXEXCLUSIVE)) {
                    minInclusive = controlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_MINEXCLUSIVE);
                    try {
                        final Date d = sdf.parse(minInclusive);
                        calendar.setTime(d);
                    } catch (ParseException pe) {
                        LOGGER.error(pe);
                    }
                    calendar.roll(Calendar.DATE, true);
                    minInclusive = sdf.format(calendar.getTime());
                } else if (controlType.isDefinedFacet(XSSimpleTypeDefinition.FACET_MININCLUSIVE)) {
                    minInclusive = controlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_MININCLUSIVE);
                }
                if (controlType.isDefinedFacet(XSSimpleTypeDefinition.FACET_MAXEXCLUSIVE)) {
                    maxInclusive = controlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_MAXEXCLUSIVE);
                    try {
                        final Date d = sdf.parse(maxInclusive);
                        calendar.setTime(d);
                    } catch (ParseException pe) {
                        LOGGER.error(pe);
                    }
                    calendar.roll(Calendar.DATE, false);
                    maxInclusive = sdf.format(calendar.getTime());
                } else if (controlType.isDefinedFacet(XSSimpleTypeDefinition.FACET_MAXINCLUSIVE)) {
                    maxInclusive = controlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_MAXINCLUSIVE);
                }
                if (minInclusive != null) {
                    result.setAttributeNS(NamespaceService.ALFRESCO_URI,
                            NamespaceService.ALFRESCO_PREFIX + ":minInclusive", minInclusive);
                }
                if (maxInclusive != null) {
                    result.setAttributeNS(NamespaceService.ALFRESCO_URI,
                            NamespaceService.ALFRESCO_PREFIX + ":maxInclusive", maxInclusive);
                }
            }
        }
        }
    }
    this.setXFormsId(result);
    result.appendChild(this.createLabel(xformsDocument, caption));

    if (appearance != null && appearance.length() != 0) {
        result.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":appearance",
                appearance);
    }
    return result;
}

From source file:org.alfresco.web.forms.xforms.Schema2XForms.java

/**
 * Creates a form control for an XML Schema simple type restricted by an enumeration.
 * This method is called when the form builder determines a form control is required for
 * an enumerated type./*w  w w . jav  a2 s  .  c o m*/
 * The implementation of this method is responsible for creating an XML element of the
 * appropriate type to receive a value for <b>controlType</b>. The caller is responsible
 * for adding the returned element to the form and setting caption, bind, and other
 * standard elements and attributes.
 *
 * @param xformsDocument       The XForm document.
 * @param controlType The XML Schema type for which the form control is to be created.
 * @param caption     The caption for the form control. The caller The purpose of providing the caption
 *                    is to permit the implementation to add a <b>[Select1 .... ]</b> message that involves the caption.
 * @param bindElement The bind element for this control. The purpose of providing the bind element
 *                    is to permit the implementation to add a isValid attribute to the bind element that prevents
 *                    the <b>[Select1 .... ]</b> item from being selected.
 * @return The element for the form control.
 */
public Element createControlForEnumerationType(final Document xformsDocument,
        final XSSimpleTypeDefinition controlType, final XSObject owner, final String caption,
        final Element bindElement, final ResourceBundle resourceBundle) {
    // TODO: Figure out an intelligent or user determined way to decide between
    // selectUI style (listbox, menu, combobox, radio) (radio and listbox best apply)
    // Possibly look for special appInfo section in the schema and if not present default to comboBox...
    //
    // For now, use radio if enumValues < DEFAULT_LONG_LIST_MAX_SIZE otherwise use combobox
    //
    final StringList enumFacets = controlType.getLexicalEnumeration();
    if (enumFacets.getLength() <= 0) {
        return null;
    }

    final Element control = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":select1");
    this.setXFormsId(control);

    //label
    control.appendChild(this.createLabel(xformsDocument, caption));

    final Element choices = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":choices");
    this.setXFormsId(choices);

    final XSObjectList mvFacets = controlType.getMultiValueFacets();
    if (mvFacets.getLength() != 1) {
        throw new RuntimeException("expected exactly one MultiValueFacet for " + controlType);
    }

    final XSObjectList annotations = ((XSMultiValueFacet) mvFacets.item(0)).getAnnotations();

    final Map<String, XSAnnotation> enumValues = new LinkedHashMap<String, XSAnnotation>(
            enumFacets.getLength());

    String appearance = extractPropertyFromAnnotation(NamespaceService.ALFRESCO_URI, "appearance",
            this.getAnnotation(owner), resourceBundle);
    if (appearance == null || appearance.length() == 0) {
        appearance = enumFacets.getLength() < Schema2XForms.LONG_LIST_SIZE ? "full" : "compact";
    }

    // if appearance is "full" a radio button control is used, in this case we don't want the
    // please select option available
    if (!"full".equals(appearance)) {
        final String nullValue = Application.getMessage(FacesContext.getCurrentInstance(), "please_select");
        enumValues.put(nullValue, null);
    }

    for (int i = 0; i < enumFacets.getLength(); i++) {
        enumValues.put(enumFacets.item(i),
                (annotations.getLength() == enumFacets.getLength() ? (XSAnnotation) annotations.item(i)
                        : null));
    }

    control.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":appearance",
            appearance);

    control.appendChild(choices);
    this.addChoicesForSelectControl(xformsDocument, choices, enumValues, resourceBundle);
    return control;
}

From source file:org.alfresco.web.forms.xforms.Schema2XForms.java

/**
 * Creates a form control for an XML Schema simple list type.
 * <p/>//from w w w.  j  a  v a2s  .c o  m
 * This method is called when the form builder determines a form control is required for
 * a list type.
 * The implementation of this method is responsible for creating an XML element of the
 * appropriate type to receive a value for <b>controlType</b>. The caller is responsible
 * for adding the returned element to the form and setting caption, bind, and other
 * standard elements and attributes.
 *
 * @param xformsDocument       The XForm document.
 * @param listType    The XML Schema list type for which the form control is to be created.
 * @param owner
 * @param caption     The caption for the form control. The caller The purpose of providing the caption
 *                    is to permit the implementation to add a <b>[Select1 .... ]</b> message that involves the caption.
 * @param bindElement The bind element for this control. The purpose of providing the bind element
 *                    is to permit the implementation to add a isValid attribute to the bind element that prevents
 *                    the <b>[Select1 .... ]</b> item from being selected.
 * @param resourceBundle
 * @return The element for the form control.
 */
public Element createControlForListType(final Document xformsDocument, final XSSimpleTypeDefinition listType,
        final XSObject owner, final String caption, final Element bindElement,
        final ResourceBundle resourceBundle) {
    XSSimpleTypeDefinition controlType = listType.getItemType();

    final StringList enumFacets = controlType.getLexicalEnumeration();
    if (enumFacets.getLength() <= 0) {
        return null;
    }
    Element control = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":select");
    this.setXFormsId(control);
    control.appendChild(this.createLabel(xformsDocument, caption));

    final XSObjectList mvFacets = controlType.getMultiValueFacets();
    if (mvFacets.getLength() != 1) {
        throw new RuntimeException("expected exactly one MultiValueFacet for " + controlType);
    }

    final XSObjectList annotations = ((XSMultiValueFacet) mvFacets.item(0)).getAnnotations();

    final Map<String, XSAnnotation> enumValues = new LinkedHashMap<String, XSAnnotation>(
            enumFacets.getLength());
    for (int i = 0; i < enumFacets.getLength(); i++) {
        enumValues.put(enumFacets.item(i),
                (annotations.getLength() == enumFacets.getLength() ? (XSAnnotation) annotations.item(i)
                        : null));
    }

    // TODO: Figure out an intelligent or user determined way to decide between
    // selectUI style (listbox, menu, combobox, radio) (radio and listbox best apply)
    // Possibly look for special appInfo section in the schema and if not present default to checkBox...
    //
    // For now, use checkbox if there are < DEFAULT_LONG_LIST_MAX_SIZE items, otherwise use long control
    String appearance = extractPropertyFromAnnotation(NamespaceService.ALFRESCO_URI, "appearance",
            this.getAnnotation(owner), resourceBundle);
    if (appearance == null || appearance.length() == 0) {
        appearance = enumValues.size() < Schema2XForms.LONG_LIST_SIZE ? "full" : "compact";
    }
    control.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":appearance",
            appearance);
    final Element choices = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":choices");
    this.setXFormsId(choices);
    control.appendChild(choices);
    this.addChoicesForSelectControl(xformsDocument, choices, enumValues, resourceBundle);
    return control;
}

From source file:org.alfresco.web.forms.xforms.Schema2XForms.java

/**
 * This method is invoked after an xforms:bind element is created for the specified SimpleType.
 * The implementation is responsible for setting setting any/all bind attributes
 * except for <b>id</b> and <b>ref</b> - these have been automatically set
 * by the caller (and should not be touched by implementation of startBindElement)
 * prior to invoking startBindElement.//from   w  w  w. j  av a2  s.  co m
 * The caller automatically adds the returned element to the model section of
 * the form.
 *
 * @param bindElement The bindElement being processed.
 * @param schema XML Schema type of the element/attribute this bind is for.
 * @param controlType
 * @param owner
 * @param o
 * @return The bind Element to use in the XForm - bindElement or a replacement.
 */
public Element startBindElement(final Element bindElement, final XSModel schema,
        final XSTypeDefinition controlType, final XSObject owner, final SchemaUtil.Occurrence o) {
    // START WORKAROUND
    // Due to a Chiba bug, anyType is not a recognized type name.
    // so, if this is an anyType, then we'll just skip the type
    // setting.
    //
    // type.getName() may be 'null' for anonymous types, so compare against
    // static string (see bug #1172541 on sf.net)
    final List<String> constraints = new LinkedList<String>();
    if (controlType instanceof XSSimpleTypeDefinition
            && ((XSSimpleTypeDefinition) controlType).getBuiltInKind() != XSConstants.ANYSIMPLETYPE_DT) {
        String typeName = this.getXFormsTypeName(bindElement.getOwnerDocument(), schema, controlType);
        if (typeName != null && typeName.length() != 0) {
            bindElement.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":type",
                    typeName);
        }

        typeName = SchemaUtil.getBuiltInTypeName(controlType);
        if (typeName != null && typeName.length() != 0) {
            bindElement.setAttributeNS(NamespaceService.ALFRESCO_URI,
                    NamespaceService.ALFRESCO_PREFIX + ":builtInType", typeName);
        }
        final StringList lexicalPatterns = ((XSSimpleTypeDefinition) controlType).getLexicalPattern();

        // NOTE: from glen.johnson@alfresco.com
        // Workaround to fix issue WCM-952
        //
        // I added expression '&& !typeName.equals(SchemaSymbols.ATTVAL_INTEGER')
        // onto the end of loop condition expression below.
        //
        // This is to stop the pattern matching constraint (using deprecated chiba:match() function)
        // from being generated into binding elements that are linked to "xs:integer"
        // elements (in the xform instance)
        //
        // If this pattern match constraint is indeed added to the binding element linked to
        // a "xs:integer" element in the xform instance, then a value is always required
        // for that element - even if the corresponding schema has minOccurs="0" for
        // that element i.e. it causes a value to be required for "optional" xs:integer
        // elements.
        //
        // Note that the chiba:match() function is unsupported and will be removed from Chiba
        // in the future, so a solution enabling its complete removal will need to be found.
        // I do not see why it has been included here. The Schema inside the xform 
        // model should take care of most validation needs.
        // In the past, when I have completely removed this constraint (see CHK-2333), restrictions
        // using <xs:pattern> in the Schema fail to get enforced -
        // Causing the failure of org.alfresco.web.forms.xforms.Schema2XFormsTest.testConstraint()
        //
        for (int i = 0; lexicalPatterns != null && i < lexicalPatterns.getLength()
                && !SchemaSymbols.ATTVAL_INTEGER.equals(typeName); i++) {
            String pattern = lexicalPatterns.item(i);
            if (o.isOptional()) {
                pattern = "(" + pattern + ")?";
            }
            constraints.add("chiba:match(., '" + pattern + "',null)");
        }

        XSSimpleTypeDefinition simpleControlType = ((XSSimpleTypeDefinition) controlType);

        if (simpleControlType.isDefinedFacet(XSSimpleTypeDefinition.FACET_MAXLENGTH)) {
            constraints.add("string-length(.) <= "
                    + simpleControlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_MAXLENGTH));
        }

        if (simpleControlType.isDefinedFacet(XSSimpleTypeDefinition.FACET_MINLENGTH)) {
            constraints.add("string-length(.) >= "
                    + simpleControlType.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_MINLENGTH));
        }

    }

    final short constraintType = (owner != null && owner instanceof XSElementDeclaration
            ? ((XSElementDeclaration) owner).getConstraintType()
            : (owner != null && owner instanceof XSAttributeDeclaration
                    ? ((XSAttributeDeclaration) owner).getConstraintType()
                    : (owner != null && owner instanceof XSAttributeUse
                            ? ((XSAttributeUse) owner).getConstraintType()
                            : XSConstants.VC_NONE)));

    bindElement.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":readonly",
            (constraintType == XSConstants.VC_FIXED) + "()");

    if (controlType instanceof XSSimpleTypeDefinition) {
        bindElement.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":required",
                (o.minimum != 0) + "()");
    } else if (controlType instanceof XSComplexTypeDefinition) {
        // make all complex types not required since it helps with validation - otherwise
        // chiba seems to expect a nodevalue for the container element
        bindElement.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":required",
                "false()");

    }

    //no more minOccurs & maxOccurs element: add a constraint if maxOccurs>1:
    //count(.) <= maxOccurs && count(.) >= minOccurs
    final String nodeset = bindElement.getAttributeNS(NamespaceConstants.XFORMS_NS, "nodeset");
    if (o.minimum > 1) {
        //if 0 or 1 -> no constraint (managed by "required")
        constraints.add("count(../" + nodeset + ") >= " + o.minimum);
    }
    bindElement.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":minOccurs",
            String.valueOf(o.minimum));
    if (o.maximum > 1) {
        //if 1 or unbounded -> no constraint
        constraints.add("count(../" + nodeset + ") <= " + o.maximum);
    }

    bindElement.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":maxOccurs",
            o.isUnbounded() ? "unbounded" : String.valueOf(o.maximum));

    if (constraints.size() != 0) {
        bindElement.setAttributeNS(NamespaceConstants.XFORMS_NS,
                NamespaceConstants.XFORMS_PREFIX + ":constraint",
                StringUtils.join((String[]) constraints.toArray(new String[constraints.size()]), " and "));
    }
    return bindElement;
}

From source file:org.alfresco.web.forms.xforms.Schema2XForms.java

private static String addNamespace(final Element e, String nsPrefix, final String ns) {
    String prefix;/*from www .  j  a v a2  s.  com*/
    if ((prefix = NamespaceResolver.getPrefix(e, ns)) != null) {
        return prefix;
    }

    if (nsPrefix == null || e.hasAttributeNS(NamespaceConstants.XMLNS_NS, nsPrefix)) {
        // Generate a unique prefix
        int suffix = 1;
        while (e.hasAttributeNS(NamespaceConstants.XMLNS_NS, nsPrefix = "ns" + suffix)) {
            suffix++;
        }
    }

    if (LOGGER.isDebugEnabled())
        LOGGER.debug("[addNamespace] adding namespace " + ns + " with prefix " + nsPrefix + " to "
                + e.getNodeName());

    e.setAttributeNS(NamespaceConstants.XMLNS_NS, NamespaceConstants.XMLNS_PREFIX + ':' + nsPrefix, ns);

    return nsPrefix;
}

From source file:org.alfresco.web.forms.xforms.Schema2XForms.java

private Element createTrigger(final Document xformsDocument, final String id, final String bindId,
        final String label, final Element... actions) {
    final Element trigger = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":trigger");
    this.setXFormsId(trigger, id != null ? id : null);

    //copy the bind attribute
    if (bindId != null) {
        trigger.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":bind",
                bindId);//from www .  j  a v  a2s  . co  m
    }
    trigger.appendChild(this.createLabel(xformsDocument, label));

    //insert action
    final Element actionWrapper = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":action");
    trigger.appendChild(actionWrapper);

    for (final Element action : actions) {
        actionWrapper.appendChild(action);
        this.setXFormsId(action);
    }
    return trigger;
}