List of usage examples for org.w3c.dom Document createElementNS
public Element createElementNS(String namespaceURI, String qualifiedName) throws DOMException;
From source file:org.alfresco.web.forms.xforms.Schema2XForms.java
/** * Generate the XForm based on a user supplied XML Schema. * * @param instanceDocument The document source for the XML Schema. * @param schemaDocument Schema document * @param rootElementName Name of the root element * @param resourceBundle Strings to use/* w w w . j av a 2 s.c om*/ * @return The Document containing the XForm. * @throws org.chiba.tools.schemabuilder.FormBuilderException * If an error occurs building the XForm. */ public Pair<Document, XSModel> buildXForm(final Document instanceDocument, final Document schemaDocument, String rootElementName, final ResourceBundle resourceBundle) throws FormBuilderException { final XSModel schema = SchemaUtil.parseSchema(schemaDocument, true); this.typeTree = SchemaUtil.buildTypeTree(schema); //refCounter = 0; this.counter.clear(); final Document xformsDocument = this.createFormTemplate(rootElementName); //find form element: last element created final Element formSection = (Element) xformsDocument.getDocumentElement().getLastChild(); final Element modelSection = (Element) xformsDocument.getDocumentElement() .getElementsByTagNameNS(NamespaceConstants.XFORMS_NS, "model").item(0); //add XMLSchema if we use schema types final Element importedSchemaDocumentElement = (Element) xformsDocument .importNode(schemaDocument.getDocumentElement(), true); importedSchemaDocumentElement.setAttributeNS(null, "id", "schema-1"); NodeList nl = importedSchemaDocumentElement.getChildNodes(); boolean hasExternalSchema = false; for (int i = 0; i < nl.getLength(); i++) { Node current = nl.item(i); if (current.getNamespaceURI() != null && current.getNamespaceURI().equals(NamespaceConstants.XMLSCHEMA_NS)) { String localName = current.getLocalName(); if (localName.equals("include") || localName.equals("import")) { hasExternalSchema = true; break; } } } // ALF-8105 / ETWOTWO-1384: Only embed the schema if it does not reference externals if (!hasExternalSchema) { modelSection.appendChild(importedSchemaDocumentElement); } //check if target namespace final StringList schemaNamespaces = schema.getNamespaces(); final HashMap<String, String> schemaNamespacesMap = new HashMap<String, String>(); if (schemaNamespaces.getLength() != 0) { // will return null if no target namespace was specified this.targetNamespace = schemaNamespaces.item(0); if (LOGGER.isDebugEnabled()) LOGGER.debug("[buildXForm] using targetNamespace " + this.targetNamespace); for (int i = 0; i < schemaNamespaces.getLength(); i++) { if (schemaNamespaces.item(i) == null) { continue; } final String prefix = addNamespace(xformsDocument.getDocumentElement(), schemaDocument.lookupPrefix(schemaNamespaces.item(i)), schemaNamespaces.item(i)); if (LOGGER.isDebugEnabled()) { LOGGER.debug("[buildXForm] adding namespace " + schemaNamespaces.item(i) + " with prefix " + prefix + " to xform and default instance element"); } schemaNamespacesMap.put(prefix, schemaNamespaces.item(i)); } } //if target namespace & we use the schema types: add it to form ns declarations // if (this.targetNamespace != null && this.targetNamespace.length() != 0) // envelopeElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, // "xmlns:schema", // this.targetNamespace); final XSElementDeclaration rootElementDecl = schema.getElementDeclaration(rootElementName, this.targetNamespace); if (rootElementDecl == null) { throw new FormBuilderException("Invalid root element tag name [" + rootElementName + ", targetNamespace = " + this.targetNamespace + "]"); } rootElementName = this.getElementName(rootElementDecl, xformsDocument); final Element instanceElement = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":instance"); modelSection.appendChild(instanceElement); this.setXFormsId(instanceElement); final Element defaultInstanceDocumentElement = xformsDocument.createElement(rootElementName); addNamespace(defaultInstanceDocumentElement, NamespaceConstants.XMLSCHEMA_INSTANCE_PREFIX, NamespaceConstants.XMLSCHEMA_INSTANCE_NS); if (this.targetNamespace != null) { final String targetNamespacePrefix = schemaDocument.lookupPrefix(this.targetNamespace); if (LOGGER.isDebugEnabled()) { LOGGER.debug("[buildXForm] adding target namespace " + this.targetNamespace + " with prefix " + targetNamespacePrefix + " to xform and default instance element"); } addNamespace(defaultInstanceDocumentElement, targetNamespacePrefix, this.targetNamespace); addNamespace(xformsDocument.getDocumentElement(), targetNamespacePrefix, this.targetNamespace); } Element prototypeInstanceElement = null; if (instanceDocument == null || instanceDocument.getDocumentElement() == null) { instanceElement.appendChild(defaultInstanceDocumentElement); } else { Element instanceDocumentElement = instanceDocument.getDocumentElement(); if (!instanceDocumentElement.getNodeName().equals(rootElementName)) { throw new IllegalArgumentException("instance document root tag name invalid. " + "expected " + rootElementName + ", got " + instanceDocumentElement.getNodeName()); } if (LOGGER.isDebugEnabled()) LOGGER.debug("[buildXForm] importing rootElement from other document"); prototypeInstanceElement = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":instance"); modelSection.appendChild(prototypeInstanceElement); this.setXFormsId(prototypeInstanceElement, "instance_prototype"); prototypeInstanceElement.appendChild(defaultInstanceDocumentElement); } final Element rootGroup = this.addElement(xformsDocument, modelSection, defaultInstanceDocumentElement, formSection, schema, rootElementDecl, "/" + this.getElementName(rootElementDecl, xformsDocument), new SchemaUtil.Occurrence(1, 1), resourceBundle); if (rootGroup.getNodeName() != NamespaceConstants.XFORMS_PREFIX + ":group") { throw new FormBuilderException("Expected root form element to be a " + NamespaceConstants.XFORMS_PREFIX + ":group, not a " + rootGroup.getNodeName() + ". Ensure that " + this.getElementName(rootElementDecl, xformsDocument) + " is a concrete type that has no extensions. " + "Types with extensions are not supported for " + "the root element of a form."); } this.setXFormsId(rootGroup, "alfresco-xforms-root-group"); if (prototypeInstanceElement != null) { Schema2XForms.rebuildInstance(prototypeInstanceElement, instanceDocument, instanceElement, schemaNamespacesMap); } this.createSubmitElements(xformsDocument, modelSection, rootGroup); this.createTriggersForRepeats(xformsDocument, rootGroup); final Comment comment = xformsDocument.createComment( "This XForm was generated by " + this.getClass().getName() + " on " + (new Date()) + " from the '" + rootElementName + "' element of the '" + this.targetNamespace + "' XML Schema."); xformsDocument.getDocumentElement().insertBefore(comment, xformsDocument.getDocumentElement().getFirstChild()); xformsDocument.normalizeDocument(); if (LOGGER.isDebugEnabled()) LOGGER.debug("[buildXForm] Returning XForm =\n" + XMLUtil.toString(xformsDocument)); return new Pair<Document, XSModel>(xformsDocument, schema); }
From source file:org.alfresco.web.forms.xforms.Schema2XForms.java
@SuppressWarnings("unchecked") public static void rebuildInstance(final Node prototypeNode, final Node oldInstanceNode, final Node newInstanceNode, final HashMap<String, String> schemaNamespaces) { final JXPathContext prototypeContext = JXPathContext.newContext(prototypeNode); prototypeContext.registerNamespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI); final JXPathContext instanceContext = JXPathContext.newContext(oldInstanceNode); instanceContext.registerNamespace(NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI); for (final String prefix : schemaNamespaces.keySet()) { prototypeContext.registerNamespace(prefix, schemaNamespaces.get(prefix)); instanceContext.registerNamespace(prefix, schemaNamespaces.get(prefix)); }/* w w w. ja va 2s.c om*/ // Evaluate non-recursive XPaths for all prototype elements at this level final Iterator<Pointer> it = prototypeContext.iteratePointers("*"); while (it.hasNext()) { final Pointer p = it.next(); Element proto = (Element) p.getNode(); String path = p.asPath(); // check if this is a prototype element with the attribute set boolean isPrototype = proto.hasAttributeNS(NamespaceService.ALFRESCO_URI, "prototype") && proto.getAttributeNS(NamespaceService.ALFRESCO_URI, "prototype").equals("true"); // We shouldn't locate a repeatable child with a fixed path if (isPrototype) { path = path.replaceAll("\\[(\\d+)\\]", "[position() >= $1]"); if (LOGGER.isDebugEnabled()) { LOGGER.debug("[rebuildInstance] evaluating prototyped nodes " + path); } } else { if (LOGGER.isDebugEnabled()) { LOGGER.debug("[rebuildInstance] evaluating child node with positional path " + path); } } Document newInstanceDocument = newInstanceNode.getOwnerDocument(); // Locate the corresponding nodes in the instance document List<Node> l = (List<Node>) instanceContext.selectNodes(path); // If the prototype node isn't a prototype element, copy it in as a missing node, complete with all its children. We won't need to recurse on this node if (l.isEmpty()) { if (!isPrototype) { LOGGER.debug("[rebuildInstance] copying in missing node " + proto.getNodeName() + " to " + XMLUtil.buildXPath(newInstanceNode, newInstanceDocument.getDocumentElement())); // Clone the prototype node and all its children Element clone = (Element) proto.cloneNode(true); newInstanceNode.appendChild(clone); if (oldInstanceNode instanceof Document) { // add XMLSchema instance NS addNamespace(clone, NamespaceConstants.XMLSCHEMA_INSTANCE_PREFIX, NamespaceConstants.XMLSCHEMA_INSTANCE_NS); } } } else { // Otherwise, append the matches from the old instance document in order for (Node old : l) { Element oldEl = (Element) old; // Copy the old instance element rather than cloning it, so we don't copy over attributes Element clone = null; String nSUri = oldEl.getNamespaceURI(); if (nSUri == null) { clone = newInstanceDocument.createElement(oldEl.getTagName()); } else { clone = newInstanceDocument.createElementNS(nSUri, oldEl.getTagName()); } newInstanceNode.appendChild(clone); if (oldInstanceNode instanceof Document) { // add XMLSchema instance NS addNamespace(clone, NamespaceConstants.XMLSCHEMA_INSTANCE_PREFIX, NamespaceConstants.XMLSCHEMA_INSTANCE_NS); } // Copy over child text if this is not a complex type boolean isEmpty = true; for (Node n = old.getFirstChild(); n != null; n = n.getNextSibling()) { if (n instanceof Text) { clone.appendChild(newInstanceDocument.importNode(n, false)); isEmpty = false; } else if (n instanceof Element) { break; } } // Populate the nil attribute. It may be true or false if (proto.hasAttributeNS(NamespaceConstants.XMLSCHEMA_INSTANCE_NS, "nil")) { clone.setAttributeNS(NamespaceConstants.XMLSCHEMA_INSTANCE_NS, NamespaceConstants.XMLSCHEMA_INSTANCE_PREFIX + ":nil", String.valueOf(isEmpty)); } // Copy over attributes present in the prototype NamedNodeMap attributes = proto.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Attr attribute = (Attr) attributes.item(i); String localName = attribute.getLocalName(); if (localName == null) { String name = attribute.getName(); if (oldEl.hasAttribute(name)) { clone.setAttributeNode( (Attr) newInstanceDocument.importNode(oldEl.getAttributeNode(name), false)); } else { LOGGER.debug("[rebuildInstance] copying in missing attribute " + attribute.getNodeName() + " to " + XMLUtil.buildXPath(clone, newInstanceDocument.getDocumentElement())); clone.setAttributeNode((Attr) attribute.cloneNode(false)); } } else { String namespace = attribute.getNamespaceURI(); if (!((!isEmpty && (namespace.equals(NamespaceConstants.XMLSCHEMA_INSTANCE_NS) && localName.equals("nil")) || (namespace.equals(NamespaceService.ALFRESCO_URI) && localName.equals("prototype"))))) { if (oldEl.hasAttributeNS(namespace, localName)) { clone.setAttributeNodeNS((Attr) newInstanceDocument .importNode(oldEl.getAttributeNodeNS(namespace, localName), false)); } else { LOGGER.debug("[rebuildInstance] copying in missing attribute " + attribute.getNodeName() + " to " + XMLUtil.buildXPath(clone, newInstanceDocument.getDocumentElement())); clone.setAttributeNodeNS((Attr) attribute.cloneNode(false)); } } } } // recurse on children rebuildInstance(proto, oldEl, clone, schemaNamespaces); } } // Now add in a new copy of the prototype if (isPrototype) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("[rebuildInstance] appending " + proto.getNodeName() + " to " + XMLUtil.buildXPath(newInstanceNode, newInstanceDocument.getDocumentElement())); } newInstanceNode.appendChild(proto.cloneNode(true)); } } }
From source file:org.alfresco.web.forms.xforms.Schema2XForms.java
/** *//* w w w . j a v a 2s .co m*/ protected Map<String, Element> addChoicesForSelectSwitchControl(final Document xformsDocument, final Element formSection, final List<XSTypeDefinition> choiceValues, final String typeBindId) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("[addChoicesForSelectSwitchControl] values = "); for (XSTypeDefinition type : choiceValues) { LOGGER.debug(" - " + type.getName()); } } final Map<String, Element> result = new HashMap<String, Element>(); for (XSTypeDefinition type : choiceValues) { final String textValue = type.getName(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("[addChoicesForSelectSwitchControl] processing " + textValue); } //build the case element final Element caseElement = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":case"); caseElement.appendChild(this.createLabel(xformsDocument, textValue)); final String caseId = this.setXFormsId(caseElement); result.put(textValue, caseElement); final Element toggle = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":toggle"); this.setXFormsId(toggle); toggle.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":case", caseId); final Element setValue = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":setvalue"); setValue.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":bind", typeBindId); setValue.appendChild(xformsDocument.createTextNode(textValue)); formSection .appendChild(this.createTrigger(xformsDocument, null, typeBindId, textValue, toggle, setValue)); } return result; }
From source file:org.alfresco.web.forms.xforms.Schema2XForms.java
private Element addElementWithMultipleCompatibleTypes(final Document xformsDocument, Element modelSection, final Element defaultInstanceElement, final Element formSection, final XSModel schema, final XSElementDeclaration elementDecl, final TreeSet<XSTypeDefinition> compatibleTypes, final String pathToRoot, final ResourceBundle resourceBundle, final SchemaUtil.Occurrence occurs) throws FormBuilderException { if (LOGGER.isDebugEnabled()) LOGGER.debug("[addElementWithMultipleCompatibleTypes] adding element " + elementDecl + " at path " + pathToRoot);/*from w w w. j a v a2 s.co m*/ // look for compatible types final XSTypeDefinition controlType = elementDecl.getTypeDefinition(); //get possible values final List<XSTypeDefinition> enumValues = new LinkedList<XSTypeDefinition>(); //add the type (if not abstract) if (!((XSComplexTypeDefinition) controlType).getAbstract()) { enumValues.add(controlType); } //add compatible types enumValues.addAll(compatibleTypes); // multiple compatible types for this element exist // in the schema - allow the user to choose from // between compatible non-abstract types boolean isRepeated = isRepeated(occurs, controlType); Element bindElement = this.createBind(xformsDocument, pathToRoot + "/@xsi:type"); String bindId = bindElement.getAttributeNS(null, "id"); modelSection.appendChild(bindElement); this.startBindElement(bindElement, schema, controlType, null, occurs); //add the "element" bind, in addition final Element bindElement2 = this.createBind(xformsDocument, pathToRoot + (isRepeated ? "[position() != last()]" : "")); modelSection.appendChild(bindElement2); this.startBindElement(bindElement2, schema, controlType, null, occurs); // add content to select1 final Map<String, Element> caseTypes = this.addChoicesForSelectSwitchControl(xformsDocument, formSection, enumValues, bindId); //add switch final Element switchElement = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":switch"); switchElement.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":bind", bindId); switchElement.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":appearance", "full"); formSection.appendChild(switchElement); if (!((XSComplexTypeDefinition) controlType).getAbstract()) { final Element firstCaseElement = caseTypes.get(controlType.getName()); switchElement.appendChild(firstCaseElement); final Element firstGroupElement = this.addComplexType(xformsDocument, modelSection, defaultInstanceElement, firstCaseElement, schema, (XSComplexTypeDefinition) controlType, elementDecl, pathToRoot, SchemaUtil.getOccurrence(elementDecl), true, false, resourceBundle); firstGroupElement.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":appearance", ""); } defaultInstanceElement.setAttributeNS(NamespaceConstants.XMLSCHEMA_INSTANCE_NS, NamespaceConstants.XMLSCHEMA_INSTANCE_PREFIX + ":type", (((XSComplexTypeDefinition) controlType).getAbstract() ? compatibleTypes.first().getName() : controlType.getName())); defaultInstanceElement.setAttributeNS(NamespaceConstants.XMLSCHEMA_INSTANCE_NS, NamespaceConstants.XMLSCHEMA_INSTANCE_PREFIX + ":nil", "true"); /////////////// add sub types ////////////// // add each compatible type within // a case statement for (final XSTypeDefinition type : compatibleTypes) { final String compatibleTypeName = type.getName(); if (LOGGER.isDebugEnabled()) { LOGGER.debug(type == null ? ("[addElementWithMultipleCompatibleTypes] compatible type is null!! type = " + compatibleTypeName + ", targetNamespace = " + this.targetNamespace) : ("[addElementWithMultipleCompatibleTypes] adding compatible type " + type.getName())); } if (type == null || type.getTypeCategory() != XSTypeDefinition.COMPLEX_TYPE) { continue; } final Element caseElement = caseTypes.get(type.getName()); switchElement.appendChild(caseElement); // ALF-9524 fix, add an extra element to the instance for each type that extends the abstract parent Element newDefaultInstanceElement = xformsDocument.createElement(getElementName(type, xformsDocument)); Attr nodesetAttr = modelSection.getAttributeNodeNS(NamespaceConstants.XFORMS_NS, "nodeset"); // construct the nodeset that is used in bind for abstract type String desiredBindNodeset = getElementName(elementDecl, xformsDocument) + (isRepeated ? "[position() != last()]" : ""); // check the current bind if (nodesetAttr == null || !nodesetAttr.getValue().equals(desiredBindNodeset)) { // look for desired bind in children Element newModelSection = DOMUtil.getElementByAttributeValueNS(modelSection, NamespaceConstants.XFORMS_NS, "bind", NamespaceConstants.XFORMS_NS, "nodeset", desiredBindNodeset); if (newModelSection == null) { // look for absolute path desiredBindNodeset = "/" + desiredBindNodeset; newModelSection = DOMUtil.getElementByAttributeValueNS(modelSection, NamespaceConstants.XFORMS_NS, "bind", NamespaceConstants.XFORMS_NS, "nodeset", desiredBindNodeset); } modelSection = newModelSection; } // create the extra bind for each child of abstract type Element bindElement3 = this.createBind(xformsDocument, getElementName(type, xformsDocument)); modelSection.appendChild(bindElement3); bindElement3 = this.startBindElement(bindElement3, schema, controlType, elementDecl, occurs); // add the relevant attribute that checks the value of parent' @xsi:type bindElement3.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":relevant", "../@xsi:type='" + type.getName() + "'"); final Element groupElement = this.addComplexType(xformsDocument, modelSection, newDefaultInstanceElement, caseElement, schema, (XSComplexTypeDefinition) type, elementDecl, pathToRoot, SchemaUtil.getOccurrence(elementDecl), true, true, resourceBundle); groupElement.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":appearance", ""); defaultInstanceElement.appendChild(newDefaultInstanceElement.cloneNode(true)); // modify bind to add a "relevant" attribute that checks the value of @xsi:type if (LOGGER.isDebugEnabled()) { LOGGER.debug("[addElementWithMultipleCompatibleTypes] Model section =\n" + XMLUtil.toString(bindElement3)); } final NodeList binds = bindElement3.getElementsByTagNameNS(NamespaceConstants.XFORMS_NS, "bind"); for (int i = 0; i < binds.getLength(); i++) { final Element subBind = (Element) binds.item(i); String name = subBind.getAttributeNS(NamespaceConstants.XFORMS_NS, "nodeset"); // ETHREEOH-3308 fix name = repeatableNamePattern.matcher(name).replaceAll(""); if (!subBind.getParentNode().getAttributes().getNamedItem("id").getNodeValue() .equals(bindElement3.getAttribute("id"))) { continue; } if (LOGGER.isDebugEnabled()) { LOGGER.debug("[addElementWithMultipleCompatibleTypes] Testing sub-bind with nodeset " + name); } Pair<String, String> parsed = parseName(name, xformsDocument); if (!SchemaUtil.isElementDeclaredIn(parsed.getFirst(), parsed.getSecond(), (XSComplexTypeDefinition) type, false) && !SchemaUtil.isAttributeDeclaredIn(parsed.getFirst(), parsed.getSecond(), (XSComplexTypeDefinition) type, false)) { continue; } if (LOGGER.isDebugEnabled()) { LOGGER.debug("[addElementWithMultipleCompatibleTypes] Element/Attribute " + name + " declared in type " + type.getName() + ": adding relevant attribute"); } //test sub types of this type //TreeSet subCompatibleTypes = (TreeSet) typeTree.get(type); String newRelevant = "../../@xsi:type='" + type.getName() + "'"; if (this.typeTree.containsKey(type.getName())) { for (XSTypeDefinition otherType : this.typeTree.get(type.getName())) { newRelevant = newRelevant + " or ../../@xsi:type='" + otherType.getName() + "'"; } } //change relevant attribute final String relevant = subBind.getAttributeNS(NamespaceConstants.XFORMS_NS, "relevant"); if (relevant != null && relevant.length() != 0) { newRelevant = ("(" + relevant + ") and " + newRelevant); } subBind.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":relevant", newRelevant); } } return switchElement; }
From source file:org.alfresco.web.forms.xforms.Schema2XForms.java
/** * Add a repeat section if maxOccurs > 1. *///ww w. ja v a 2 s. c om private Element addRepeatIfNecessary(final Document xformsDocument, final Element modelSection, final Element formSection, final XSTypeDefinition controlType, final String pathToRoot, final SchemaUtil.Occurrence o) { // add xforms:repeat section if this element re-occurs if ((o.isOptional() && (controlType instanceof XSSimpleTypeDefinition || "anyType".equals(controlType.getName()))) || (o.maximum == 1 && o.minimum == 1) || (controlType instanceof XSComplexTypeDefinition && pathToRoot.equals(""))) { return formSection; } if (LOGGER.isDebugEnabled()) { LOGGER.debug("[addRepeatIfNecessary] for multiple element for type " + controlType.getName() + ", maxOccurs = " + o.maximum); } final Element repeatSection = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":repeat"); //bind instead of repeat //repeatSection.setAttributeNS(NamespaceConstants.XFORMS_NS,NamespaceConstants.XFORMS_PREFIX + ":nodeset",pathToRoot); // bind -> last element in the modelSection Element bind = DOMUtil.getLastChildElement(modelSection); // ALF-9524 fix, previously we've added extra bind element, so last child is not correct for repeatable switch String attribute = bind.getAttribute(NamespaceConstants.XFORMS_PREFIX + ":relevant"); if (controlType instanceof XSComplexTypeDefinition && ((XSComplexTypeDefinition) controlType).getDerivationMethod() == XSConstants.DERIVATION_EXTENSION && attribute != null && !attribute.isEmpty()) { bind = modelSection; } String bindId = null; if (bind != null && bind.getLocalName() != null && "bind".equals(bind.getLocalName())) { bindId = bind.getAttributeNS(null, "id"); } else { LOGGER.warn("[addRepeatIfNecessary] bind not found: " + bind + " (model selection name = " + modelSection.getNodeName() + ")"); //if no bind is found -> modelSection is already a bind, get its parent last child bind = DOMUtil.getLastChildElement(modelSection.getParentNode()); if (bind != null && bind.getLocalName() != null && "bind".equals(bind.getLocalName())) { bindId = bind.getAttributeNS(null, "id"); } else { LOGGER.warn("[addRepeatIfNecessary] bind really not found"); } } repeatSection.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":bind", bindId); this.setXFormsId(repeatSection); //appearance=full is more user friendly repeatSection.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":appearance", "full"); formSection.appendChild(repeatSection); //add a group inside the repeat? final Element group = xformsDocument.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; }
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;//w w w . j av a 2 s. co m 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 { 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/*www . j a v a 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);// w w w.j ava2s. c om 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/>/* w w w . ja v a 2 s. c o m*/ * 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/>//from w w w . j a va 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; }