Example usage for org.w3c.dom Document createElementNS

List of usage examples for org.w3c.dom Document createElementNS

Introduction

In this page you can find the example usage for org.w3c.dom Document createElementNS.

Prototype

public Element createElementNS(String namespaceURI, String qualifiedName) throws DOMException;

Source Link

Document

Creates an element of the given qualified name and namespace URI.

Usage

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 ww . j  av a 2 s .c om
 * 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 av 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

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);// www. jav a 2  s.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;
}

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

/**
 * add triggers to use the repeat elements (allow to add an element, ...)
 *//*from   w  ww. j ava 2  s  .c o  m*/
private void createTriggersForRepeat(final Document xformsDocument, final Element rootGroup,
        final String repeatId, final String nodeset, final String bindId) {
    //xforms:at = xforms:index from the "id" attribute on the repeat element
    //trigger insert
    Element action = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":insert");
    action.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":nodeset", nodeset);
    action.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":position",
            "before");
    action.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":at", "1");

    final Element trigger_insert_before = this.createTrigger(xformsDocument, repeatId + "-insert_before",
            bindId, "insert at beginning", action);

    action = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":insert");
    action.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":nodeset", nodeset);
    action.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":position",
            "after");
    action.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":at",
            NamespaceConstants.XFORMS_PREFIX + ":index('" + repeatId + "')");

    final Element trigger_insert_after = this.createTrigger(xformsDocument, repeatId + "-insert_after", bindId,
            "insert after selected", action);

    //trigger delete
    action = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":delete");
    action.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":nodeset", nodeset);
    action.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":at",
            NamespaceConstants.XFORMS_PREFIX + ":index('" + repeatId + "')");

    final Element trigger_delete = this.createTrigger(xformsDocument,
            repeatId != null ? repeatId + "-delete" : null, bindId, "delete selected", action);

    //add the triggers
    rootGroup.appendChild(trigger_insert_before);
    rootGroup.appendChild(trigger_insert_after);
    rootGroup.appendChild(trigger_delete);
}

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

private Element createSubmissionElement(final Document xformDocument, final String id, final boolean validate) {
    final Element result = xformDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":submission");

    this.setXFormsId(result, id);

    result.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":validate",
            validate ? "true" : "false");

    result.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":action",
            this.action == null ? "" : this.base + this.action);

    result.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":method",
            (this.submitMethod != null ? this.submitMethod : Schema2XForms.SubmitMethod.POST).toString());
    result.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":encoding",
            "UTF-8");
    return result;
}

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

private Element createSubmitControl(final Document xformsDocument, final Element submission, final String id,
        final String label) {
    final Element result = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":submit");
    result.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":submission",
            submission.getAttributeNS(null, "id"));
    this.setXFormsId(result, id);

    result.appendChild(this.createLabel(xformsDocument, label));

    return result;
}

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

private Element createXFormsItem(final Document xformsDocument, final String label, final String value) {
    final Element item = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":item");
    this.setXFormsId(item);
    item.appendChild(this.createLabel(xformsDocument, label));

    final Element e = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":value");
    this.setXFormsId(e);
    e.appendChild(xformsDocument.createTextNode(value));
    item.appendChild(e);/*from   w  w w. ja v  a2 s.  co  m*/
    return item;
}

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

private Element createLabel(final Document xformsDocument, final String label) {
    final Element e = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":label");
    this.setXFormsId(e);
    e.appendChild(xformsDocument.createTextNode(label));
    return e;//from   w w  w  .j ava 2  s.  c  om
}

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

private Element createBind(final Document xformsDocument, final String nodeset) {
    final Element result = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":bind");
    final String id = this.setXFormsId(result);
    result.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":nodeset", nodeset);

    if (LOGGER.isDebugEnabled())
        LOGGER.debug("[createBind] created bind " + id + " for nodeset " + nodeset);

    return result;
}

From source file:org.alfresco.web.forms.XSLTRenderingEngine.java

/**
 * Adds a script element to the xsl which makes static methods on this
 * object available to the xsl tempalte.
 *
 * @param xslTemplate the xsl template/*from w w  w . ja va2  s. c  o  m*/
 */
protected List<String> addScripts(final Map<QName, Object> model, final Document xslTemplate) {
    final Map<QName, List<Map.Entry<QName, Object>>> methods = new HashMap<QName, List<Map.Entry<QName, Object>>>();
    for (final Map.Entry<QName, Object> entry : model.entrySet()) {
        if (entry.getValue() instanceof TemplateProcessorMethod) {
            final String prefix = QName.splitPrefixedQName(entry.getKey().toPrefixString())[0];
            final QName qn = QName.createQName(entry.getKey().getNamespaceURI(), prefix);
            if (!methods.containsKey(qn)) {
                methods.put(qn, new LinkedList());
            }
            methods.get(qn).add(entry);
        }
    }

    final Element docEl = xslTemplate.getDocumentElement();
    final String XALAN_NS = Constants.S_BUILTIN_EXTENSIONS_URL;
    final String XALAN_NS_PREFIX = "xalan";
    docEl.setAttribute("xmlns:" + XALAN_NS_PREFIX, XALAN_NS);

    final Set<String> excludePrefixes = new HashSet<String>();
    if (docEl.hasAttribute("exclude-result-prefixes")) {
        excludePrefixes.addAll(Arrays.asList(docEl.getAttribute("exclude-result-prefixes").split(" ")));
    }
    excludePrefixes.add(XALAN_NS_PREFIX);

    final List<String> result = new LinkedList<String>();
    for (QName ns : methods.keySet()) {
        final String prefix = ns.getLocalName();
        docEl.setAttribute("xmlns:" + prefix, ns.getNamespaceURI());
        excludePrefixes.add(prefix);

        final Element compEl = xslTemplate.createElementNS(XALAN_NS, XALAN_NS_PREFIX + ":component");
        compEl.setAttribute("prefix", prefix);
        docEl.appendChild(compEl);
        String functionNames = null;
        final Element scriptEl = xslTemplate.createElementNS(XALAN_NS, XALAN_NS_PREFIX + ":script");
        scriptEl.setAttribute("lang", "javascript");
        final StringBuilder js = new StringBuilder("var _xsltp_invoke = java.lang.Class.forName('"
                + ProcessorMethodInvoker.class.getName() + "').newInstance();\n"
                + "function _xsltp_to_java_array(js_array) {\n"
                + "var java_array = java.lang.reflect.Array.newInstance(java.lang.Object, js_array.length);\n"
                + "for (var i = 0; i < js_array.length; i++) { java_array[i] = js_array[i]; }\n"
                + "return java_array; }\n");
        for (final Map.Entry<QName, Object> entry : methods.get(ns)) {
            if (functionNames == null) {
                functionNames = entry.getKey().getLocalName();
            } else {
                functionNames += " " + entry.getKey().getLocalName();
            }
            final String id = entry.getKey().getLocalName() + entry.getValue().hashCode();
            js.append("function " + entry.getKey().getLocalName() + "() { return _xsltp_invoke.invokeMethod('"
                    + id + "', _xsltp_to_java_array(arguments)); }\n");
            ProcessorMethodInvoker.PROCESSOR_METHODS.put(id, (TemplateProcessorMethod) entry.getValue());
            result.add(id);
        }
        LOGGER.debug("generated JavaScript bindings:\n" + js);
        scriptEl.appendChild(xslTemplate.createTextNode(js.toString()));
        compEl.setAttribute("functions", functionNames);
        compEl.appendChild(scriptEl);
    }
    docEl.setAttribute("exclude-result-prefixes",
            StringUtils.join(excludePrefixes.toArray(new String[excludePrefixes.size()]), " "));
    return result;
}