List of usage examples for com.google.gwt.xml.client Element removeAttribute
void removeAttribute(String name);
From source file:com.colinalworth.xmlview.client.ElementCell.java
License:Apache License
/** * @param context/*ww w . j a va2s . c om*/ * @param value */ private void finishEdit(Node value, com.google.gwt.dom.client.Element target) { ElementCell.ViewState state = updateViewState(lastKey, value, target); String newValue = target.<InputElement>cast().getValue(); boolean valid = true; Element parent = (Element) value.getParentNode(); switch (state.section) { case AttributeName: Attr attr = (Attr) value; //TODO this might lose namespace data parent.removeAttribute(attr.getName()); parent.setAttribute(newValue, attr.getValue()); valid = validator.isAttributeNameValid(parent.getAttributeNode(attr.getName()), parent); break; case AttributeValue: value.setNodeValue(newValue); valid = validator.isAttributeValueValid((Attr) value, parent); break; case Content: ((CharacterData) value).setData(newValue); valid = validator.isContentsValid((CharacterData) value); break; case TagName: Element elt = (Element) value; Element replacement = elt.getOwnerDocument().createElement(newValue); while (elt.getChildNodes().getLength() != 0) { replacement.appendChild(elt.getChildNodes().item(0)); } //TODO this might lose namespace data for (int i = 0; i < elt.getAttributes().getLength(); i++) { Attr a = (Attr) elt.getAttributes().item(i); replacement.setAttribute(a.getName(), a.getValue()); } parent.replaceChild(replacement, elt); valid = validator.isElementNameValid(replacement); } if (!valid) { Window.alert("Seems to be invalid: " + newValue + " in " + parent.getNodeName()); //TODO mark invalid } this.lastKey = null; target.blur(); }
From source file:org.openxdata.designer.client.widget.DesignWidgetWrapper.java
public Element buildLayoutXml(Element parent, com.google.gwt.xml.client.Document doc) { Element node = doc.createElement("Item"); parent.appendChild(node);/* w ww. j a v a2s .co m*/ node.setAttribute(WidgetEx.WIDGET_PROPERTY_WIDGETTYPE, getWidgetName()); layoutNode = node; String value = getText(); if (value != null && value.trim().length() > 0) node.setAttribute(WidgetEx.WIDGET_PROPERTY_TEXT, value); else node.removeAttribute(WidgetEx.WIDGET_PROPERTY_TEXT); value = getTitle(); if (value != null && value.trim().length() > 0) node.setAttribute(WidgetEx.WIDGET_PROPERTY_HELPTEXT, value); else node.removeAttribute(WidgetEx.WIDGET_PROPERTY_HELPTEXT); value = getBinding(); if (value == null || value.trim().length() == 0) { //Widgets should have unique bindings to get unique xpath expressions for locale translation setBinding("LEFT" + getLeft() + "TOP" + getTop()); value = getBinding(); } node.setAttribute(WidgetEx.WIDGET_PROPERTY_BINDING, value); value = getParentBinding(); if (value != null && value.trim().length() > 0) node.setAttribute(WidgetEx.WIDGET_PROPERTY_PARENTBINDING, value); else node.removeAttribute(WidgetEx.WIDGET_PROPERTY_PARENTBINDING); node.setAttribute(WidgetEx.WIDGET_PROPERTY_LEFT, getLeft()); node.setAttribute(WidgetEx.WIDGET_PROPERTY_TOP, getTop()); value = getWidth(); if (value != null && value.trim().length() > 0) node.setAttribute(WidgetEx.WIDGET_PROPERTY_WIDTH, value); else node.removeAttribute(WidgetEx.WIDGET_PROPERTY_WIDTH); value = getHeight(); if (value != null && value.trim().length() > 0) node.setAttribute(WidgetEx.WIDGET_PROPERTY_HEIGHT, value); else node.removeAttribute(WidgetEx.WIDGET_PROPERTY_HEIGHT); node.setAttribute(WidgetEx.WIDGET_PROPERTY_TABINDEX, String.valueOf(getTabIndex())); buildLabelProperties(node); if (widget instanceof DesignGroupWidget) { ((DesignGroupWidget) widget).buildLayoutXml(node, doc); if (!isRepeated()) { setBinding("LEFT" + getLeft() + "TOP" + getTop()); node.setAttribute(WidgetEx.WIDGET_PROPERTY_BINDING, binding); DesignWidgetWrapper headerLabel = ((DesignGroupWidget) widget).getHeaderLabel(); if (headerLabel != null) headerLabel.setBinding(binding); } } if (isRepeated()) node.setAttribute(WidgetEx.WIDGET_PROPERTY_REPEATED, WidgetEx.REPEATED_TRUE_VALUE); else node.removeAttribute(WidgetEx.WIDGET_PROPERTY_REPEATED); value = getExternalSource(); if (value != null && value.trim().length() > 0) node.setAttribute(WidgetEx.WIDGET_PROPERTY_EXTERNALSOURCE, value); else node.removeAttribute(WidgetEx.WIDGET_PROPERTY_EXTERNALSOURCE); value = getDisplayField(); if (value != null && value.trim().length() > 0) node.setAttribute(WidgetEx.WIDGET_PROPERTY_DISPLAYFIELD, value); else node.removeAttribute(WidgetEx.WIDGET_PROPERTY_DISPLAYFIELD); value = getValueField(); if (value != null && value.trim().length() > 0) node.setAttribute(WidgetEx.WIDGET_PROPERTY_VALUEFIELD, value); else node.removeAttribute(WidgetEx.WIDGET_PROPERTY_VALUEFIELD); value = getFilterField(); if (value != null && value.trim().length() > 0) node.setAttribute(WidgetEx.WIDGET_PROPERTY_FILTERFIELD, value); else node.removeAttribute(WidgetEx.WIDGET_PROPERTY_FILTERFIELD); value = getId(); if (value != null && value.trim().length() > 0) node.setAttribute(WidgetEx.WIDGET_PROPERTY_ID, value); else node.removeAttribute(WidgetEx.WIDGET_PROPERTY_ID); return node; }
From source file:org.openxdata.designer.client.widget.DesignWidgetWrapper.java
public void buildLabelProperties(Element node) { String value = getForeColor(); if (value != null && value.trim().length() > 0) node.setAttribute("color", value); else// w w w .ja va2 s . c o m node.removeAttribute("color"); value = getFontWeight(); if (value != null && value.trim().length() > 0) node.setAttribute("fontWeight", value); else node.removeAttribute("fontWeight"); value = getFontStyle(); if (value != null && value.trim().length() > 0) node.setAttribute("fontStyle", value); else node.removeAttribute("fontStyle"); value = getFontSize(); if (value != null && value.trim().length() > 0) node.setAttribute("fontSize", value); else node.removeAttribute("fontSize"); value = getFontFamily(); if (value != null && value.trim().length() > 0) node.setAttribute("fontFamily", value); else node.removeAttribute("fontFamily"); value = getTextDecoration(); if (value != null && value.trim().length() > 0) node.setAttribute("textDecoration", value); else node.removeAttribute("textDecoration"); value = getBackgroundColor(); if (value != null && value.trim().length() > 0) node.setAttribute("backgroundColor", value); else node.removeAttribute("backgroundColor"); value = getBorderStyle(); if (value != null && value.trim().length() > 0) node.setAttribute("borderStyle", value); else node.removeAttribute("borderStyle"); value = getBorderWidth(); if (value != null && value.trim().length() > 0) node.setAttribute("borderWidth", value); else node.removeAttribute("borderWidth"); value = getBorderColor(); if (value != null && value.trim().length() > 0) node.setAttribute("borderColor", value); else node.removeAttribute("borderColor"); value = getTextAlign(); if (value != null && value.trim().length() > 0) node.setAttribute("textAlign", value); else node.removeAttribute("textAlign"); }
From source file:org.openxdata.sharedlib.client.model.FormDef.java
public void removeQtnFromCalculations(QuestionDef questionDef) { for (int index = 0; index < getCalculationCount(); index++) { Calculation calculation = getCalculationAt(index); if (calculation.getQuestionId() == questionDef.getId()) { calculations.remove(index);//from w w w . j av a 2 s.c o m Element node = questionDef.getBindNode() != null ? questionDef.getBindNode() : questionDef.getControlNode(); if (questionDef.getBindNode() != null) node.removeAttribute(XformConstants.ATTRIBUTE_NAME_CALCULATE); return; } } }
From source file:org.openxdata.sharedlib.client.model.FormDef.java
/** * Removes a skip rule from the form./*w w w .j a v a 2 s . c om*/ * * @param skipRule the skip rule to remove. * @return true if the skip rule has been found and removed, else false. */ public boolean removeSkipRule(SkipRule skipRule) { if (skipRules == null) return false; boolean ret = skipRules.remove(skipRule); if (dataNode != null) { for (int index = 0; index < skipRule.getActionTargetCount(); index++) { QuestionDef questionDef = getQuestion(skipRule.getActionTargetAt(index)); if (questionDef != null) { Element node = questionDef.getBindNode() != null ? questionDef.getBindNode() : questionDef.getControlNode(); if (node != null) { node.removeAttribute(XformConstants.ATTRIBUTE_NAME_RELEVANT); node.removeAttribute(XformConstants.ATTRIBUTE_NAME_ACTION); } } } } return ret; }
From source file:org.openxdata.sharedlib.client.model.FormDef.java
/** * Removes a validation rule from the form. * //from www . j a va 2 s . c o m * @param validationRule the validation rule to remove. * @return true if the validation rule has been found and removed. */ public boolean removeValidationRule(ValidationRule validationRule) { if (validationRules == null) return false; boolean ret = validationRules.remove(validationRule); if (dataNode != null) { QuestionDef questionDef = getQuestion(validationRule.getQuestionId()); if (questionDef != null) { Element node = questionDef.getBindNode() != null ? questionDef.getBindNode() : questionDef.getControlNode(); if (node != null) { node.removeAttribute(XformConstants.ATTRIBUTE_NAME_CONSTRAINT); node.removeAttribute(XformConstants.ATTRIBUTE_NAME_CONSTRAINT_MESSAGE); } } } return ret; }
From source file:org.openxdata.sharedlib.client.model.QuestionDef.java
public boolean updateDoc(Document doc, Element xformsNode, FormDef formDef, Element formNode, Element modelNode, Element groupNode, boolean appendParentBinding, boolean withData, String orgFormVarName) { boolean isNew = controlNode == null; if (controlNode == null) //Must be new question. UiElementBuilder.fromQuestionDef2Xform(this, doc, xformsNode, formDef, formNode, modelNode, groupNode); else//from ww w . j a va2 s .com updateControlNodeName(); if (labelNode != null) //How can this happen XmlUtil.setTextNodeValue(labelNode, text); Element node = bindNode; if (node == null) { //We are using a ref instead of bind node = controlNode; appendParentBinding = false; } if (node != null) { String binding = variableName; if (!binding.startsWith("/" + formDef.getBinding() + "/") && appendParentBinding) { if (!binding.startsWith(formDef.getBinding() + "/")) binding = "/" + formDef.getBinding() + "/" + binding; else { variableName = "/" + variableName; //correct user binding syntax error binding = variableName; } } if (dataType != QuestionType.REPEAT) { QuestionType questionType = dataType; node.setAttribute(XformConstants.ATTRIBUTE_NAME_TYPE, XformBuilderUtil.getXmlType(questionType, node)); } if (node.getAttribute(XformConstants.ATTRIBUTE_NAME_NODESET) != null) node.setAttribute(XformConstants.ATTRIBUTE_NAME_NODESET, binding); if (node.getAttribute(XformConstants.ATTRIBUTE_NAME_REF) != null) node.setAttribute(XformConstants.ATTRIBUTE_NAME_REF, binding); if (required) node.setAttribute(XformConstants.ATTRIBUTE_NAME_REQUIRED, XformConstants.XPATH_VALUE_TRUE); else node.removeAttribute(XformConstants.ATTRIBUTE_NAME_REQUIRED); if (!enabled) node.setAttribute(XformConstants.ATTRIBUTE_NAME_READONLY, XformConstants.XPATH_VALUE_TRUE); else node.removeAttribute(XformConstants.ATTRIBUTE_NAME_READONLY); if (locked) node.setAttribute(XformConstants.ATTRIBUTE_NAME_LOCKED, XformConstants.XPATH_VALUE_TRUE); else node.removeAttribute(XformConstants.ATTRIBUTE_NAME_LOCKED); if (!visible) node.setAttribute(XformConstants.ATTRIBUTE_NAME_VISIBLE, XformConstants.XPATH_VALUE_FALSE); else node.removeAttribute(XformConstants.ATTRIBUTE_NAME_VISIBLE); if (!(dataType == QuestionType.IMAGE || dataType == QuestionType.AUDIO || dataType == QuestionType.VIDEO || dataType == QuestionType.GPS)) node.removeAttribute(XformConstants.ATTRIBUTE_NAME_FORMAT); if (!(dataType == QuestionType.IMAGE || dataType == QuestionType.AUDIO || dataType == QuestionType.VIDEO)) controlNode.removeAttribute(XformConstants.ATTRIBUTE_NAME_MEDIATYPE); else UiElementBuilder.setMediaType(controlNode, dataType); if (dataNode != null) updateDataNode(doc, formDef, orgFormVarName); } if ((getDataType() == QuestionType.LIST_EXCLUSIVE || getDataType() == QuestionType.LIST_MULTIPLE) && options != null) { boolean allOptionsNew = areAllOptionsNew(); List<OptionDef> newOptns = new ArrayList<OptionDef>(); List<?> optns = (List<?>) options; for (int i = 0; i < optns.size(); i++) { OptionDef optionDef = (OptionDef) optns.get(i); if (!allOptionsNew && optionDef.getControlNode() == null) newOptns.add(optionDef); optionDef.updateDoc(doc, controlNode); if (i == 0) firstOptionNode = optionDef.getControlNode(); } for (int k = 0; k < newOptns.size(); k++) { OptionDef optionDef = (OptionDef) newOptns.get(k); int proposedIndex = optns.size() - (newOptns.size() - k); int currentIndex = optns.indexOf(optionDef); if (currentIndex == proposedIndex) continue; moveOptionNodesUp(optionDef, getRefOption(optns, newOptns, currentIndex)); } } else if (getDataType() == QuestionType.REPEAT) { getRepeatQtnsDef().updateDoc(doc, xformsNode, formDef, formNode, modelNode, groupNode, withData, orgFormVarName); if (controlNode != null) ((Element) controlNode.getParentNode()).setAttribute(XformConstants.ATTRIBUTE_NAME_ID, variableName); if (!withData && dataNode != null) { //Remove all repeating data kids Element parent = (Element) dataNode.getParentNode(); NodeList nodes = parent.getElementsByTagName(dataNode.getNodeName()); for (int index = 1; index < nodes.getLength(); index++) { Node child = nodes.item(index); child.getParentNode().removeChild(child); } } } //Put after options because it depends on the firstOptionNode if (hintNode != null) { if (helpText.trim().length() > 0) XmlUtil.setTextNodeValue(hintNode, helpText); else { controlNode.removeChild(hintNode); hintNode = null; } } else if (hintNode == null && helpText.trim().length() > 0) UiElementBuilder.addHelpTextNode(this, doc, controlNode, firstOptionNode); if (withData) updateNodeValue(doc, formNode, (answer != null) ? answer : defaultValue, withData); else updateNodeValue(doc, formNode, defaultValue, withData); return isNew; }
From source file:org.openxdata.sharedlib.client.xforms.ConstraintBuilder.java
/** * Converts a validation rule to its xforms representation. * The question definition object referenced by the validation rule * is expected to already have reference to the xforms document nodes * that we need to manipulate for the validation rule's xforms contents. * /*from w w w . j a v a2s . c om*/ * @param rule the validation rule. * @param formDef the form definition object. */ public static void fromValidationRule2Xform(ValidationRule rule, FormDef formDef) { QuestionDef questionDef = formDef.getQuestion(rule.getQuestionId()); if (questionDef == null) { formDef.removeValidationRule(rule); return; //possibly question deleted. } Element node = questionDef.getBindNode(); if (node == null) node = questionDef.getControlNode(); Vector<Condition> conditions = rule.getConditions(); if (conditions == null || conditions.size() == 0) { node.removeAttribute(XformConstants.ATTRIBUTE_NAME_CONSTRAINT); node.removeAttribute(XformConstants.ATTRIBUTE_NAME_CONSTRAINT_MESSAGE); return; } String constratint = ""; for (int i = 0; i < conditions.size(); i++) { Condition condition = (Condition) conditions.elementAt(i); if (condition.getValue() == null && conditions.size() == 1) { node.removeAttribute(XformConstants.ATTRIBUTE_NAME_CONSTRAINT); node.removeAttribute(XformConstants.ATTRIBUTE_NAME_CONSTRAINT_MESSAGE); formDef.removeValidationRule(rule); return; //This could happen if say data type changed from text to single select. } if (constratint.length() > 0) constratint += XformBuilderUtil.getConditionsOperatorText(rule.getConditionsOperator()); constratint += fromValidationRuleCondition2Xform(condition, formDef, ModelConstants.ACTION_ENABLE, questionDef); } node.setAttribute(XformConstants.ATTRIBUTE_NAME_CONSTRAINT, constratint); node.setAttribute(XformConstants.ATTRIBUTE_NAME_CONSTRAINT_MESSAGE, rule.getErrorMessage()); }
From source file:org.openxdata.sharedlib.client.xforms.RelevantBuilder.java
/** * Converts a skip rule definition object to xforms. * //from w ww. j ava 2 s . com * @param rule the skip rule definition object * @param formDef the form definition. */ public static void fromSkipRule2Xform(SkipRule rule, FormDef formDef) { String relevant = ""; Vector<Condition> conditions = rule.getConditions(); for (int i = 0; i < conditions.size(); i++) { if (relevant.length() > 0) relevant += XformBuilderUtil.getConditionsOperatorText(rule.getConditionsOperator()); relevant += fromSkipCondition2Xform((Condition) conditions.elementAt(i), formDef, rule.getAction()); } Vector<Integer> actionTargets = rule.getActionTargets(); for (int i = 0; i < actionTargets.size(); i++) { int id = actionTargets.elementAt(i).intValue(); QuestionDef questionDef = formDef.getQuestion(id); if (questionDef == null) continue; Element node = questionDef.getBindNode(); if (node == null) node = questionDef.getControlNode(); if (relevant.trim().length() == 0) { node.removeAttribute(XformConstants.ATTRIBUTE_NAME_RELEVANT); node.removeAttribute(XformConstants.ATTRIBUTE_NAME_ACTION); node.removeAttribute(XformConstants.ATTRIBUTE_NAME_REQUIRED); } else { node.setAttribute(XformConstants.ATTRIBUTE_NAME_RELEVANT, relevant); String value = XformConstants.ATTRIBUTE_VALUE_ENABLE; if ((rule.getAction() & ModelConstants.ACTION_ENABLE) != 0) value = XformConstants.ATTRIBUTE_VALUE_ENABLE; else if ((rule.getAction() & ModelConstants.ACTION_DISABLE) != 0) value = XformConstants.ATTRIBUTE_VALUE_DISABLE; else if ((rule.getAction() & ModelConstants.ACTION_SHOW) != 0) value = XformConstants.ATTRIBUTE_VALUE_SHOW; else if ((rule.getAction() & ModelConstants.ACTION_HIDE) != 0) value = XformConstants.ATTRIBUTE_VALUE_HIDE; node.setAttribute(XformConstants.ATTRIBUTE_NAME_ACTION, value); if ((rule.getAction() & ModelConstants.ACTION_MAKE_MANDATORY) != 0) value = XformConstants.XPATH_VALUE_TRUE; else if ((rule.getAction() & ModelConstants.ACTION_MAKE_OPTIONAL) != 0) value = XformConstants.XPATH_VALUE_FALSE; node.setAttribute(XformConstants.ATTRIBUTE_NAME_REQUIRED, value); } } }
From source file:org.openxdata.sharedlib.client.xforms.XformUtil.java
/** * Creates a node from an xml fragment./*from ww w.java2 s. c o m*/ * * @param xml the xml fragment. * @return the node. */ public static Element getNode(String xml) { xml = "<xf:xforms xmlns:xf=\"http://www.w3.org/2002/xforms\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:h=\"http://www.w3.org/1999/xhtml\">" + xml; xml = xml + "</xf:xforms>"; Document doc = XMLParser.parse(xml); Element node = (Element) doc.getDocumentElement().getChildNodes().item(0); if (node.getAttribute(XformConstants.ATTRIBUTE_NAME_XMLNS) != null) node.removeAttribute(XformConstants.ATTRIBUTE_NAME_XMLNS); return node; }