List of usage examples for org.w3c.dom Element getParentNode
public Node getParentNode();
From source file:net.wastl.webmail.xml.XMLCommon.java
public static void setTagValue(Element e, String tagname, String text, boolean unique, String errormsg, boolean cdata) throws Exception { if (text == null || tagname == null) throw new NullPointerException("Text or Tagname may not be null!"); Document root = e.getOwnerDocument(); if (unique) { // Check for double entries! NodeList nl = ((Element) e.getParentNode()).getElementsByTagName(tagname); for (int i = 0; i < nl.getLength(); i++) { if (getElementTextValue((Element) nl.item(0)).equals(text)) { throw new Exception(errormsg); }//from w w w . j a v a 2 s. c om } } NodeList namel = e.getElementsByTagName(tagname); Element elem; if (namel.getLength() <= 0) { log.debug("Creating Element " + tagname + "; will set to " + text); elem = root.createElement(tagname); e.appendChild(elem); } else { elem = (Element) namel.item(0); } //debugXML(root); setElementTextValue(elem, text, cdata); }
From source file:net.wastl.webmail.xml.XMLSystemData.java
protected String getConfigRaw(String key) { NodeList nl = sysdata.getElementsByTagName("KEY"); for (int i = 0; i < nl.getLength(); i++) { Element e = (Element) nl.item(i); if (XMLCommon.getElementTextValue(e).equals(key)) { Element p = (Element) e.getParentNode(); NodeList valuel = p.getElementsByTagName("VALUE"); if (valuel.getLength() >= 0) { return XMLCommon.getElementTextValue((Element) valuel.item(0)); }//w w w . j ava2s.c o m } } return null; }
From source file:net.wastl.webmail.xml.XMLSystemData.java
public void setConfigRaw(String groupname, String key, String value, String type) { String curval = getConfigRaw(key); if (curval == null || !curval.equals(value)) { log.debug("XMLSystemData: " + groupname + "/" + key + " = " + value); /* Find all GROUP elements */ NodeList groupl = sysdata.getElementsByTagName("GROUP"); int i = 0; for (i = 0; i < groupl.getLength(); i++) { Element group = (Element) groupl.item(i); if (group.getAttribute("name").equals(groupname)) { /* If the group name matches, find all keys */ NodeList keyl = group.getElementsByTagName("KEY"); int j = 0; for (j = 0; j < keyl.getLength(); j++) { Element keyelem = (Element) keyl.item(j); if (key.equals(XMLCommon.getElementTextValue(keyelem))) { /* If the key already exists, replace it */ Element conf = (Element) keyelem.getParentNode(); group.replaceChild(createConfigElement(key, value, type), conf); return; }// w w w . j a va 2s.c om } /* If the key was not found, append it */ if (j >= keyl.getLength()) { group.appendChild(createConfigElement(key, value, type)); return; } } } if (i >= groupl.getLength()) { Element group = createConfigGroup(groupname); group.appendChild(createConfigElement(key, value, type)); } } }
From source file:net.wastl.webmail.xml.XMLSystemData.java
protected Element getConfigElementByKey(String key) { NodeList nl = sysdata.getElementsByTagName("KEY"); Element config = null;/*from w w w . j a v a 2 s . c o m*/ for (int i = 0; i < nl.getLength(); i++) { Element keyelem = (Element) nl.item(i); Element parent = (Element) keyelem.getParentNode(); if (XMLCommon.getElementTextValue(keyelem).equals(key) && parent.getTagName().equals("CONFIG")) { config = parent; break; } } return config; }
From source file:net.wastl.webmail.xml.XMLUserData.java
public void removeEmail(String s) throws WebMailException { NodeList nl = getNodeListXPath("/USERDATA/EMAIL/ADDY"); if (nl.getLength() == 1) { throw new WebMailException("Can not delete last email address!"); }//from w ww . j a v a2 s. c om for (int i = 0; i < nl.getLength(); i++) { Element addy = (Element) nl.item(i); if (XMLCommon.getElementTextValue(addy).equals(s)) { Element email = (Element) addy.getParentNode(); if (addy.getAttribute("default").equals("yes")) { // Need to set a new default email.removeChild(addy); addy = (Element) nl.item(0); addy.setAttribute("default", "yes"); } else { // Just remove it email.removeChild(addy); } break; } } invalidateCache(); }
From source file:nl.strohalm.cyclos.utils.conversion.HtmlConverter.java
private static void removeBadNodes(final Document document) { final NodeList elements = document.getElementsByTagName("*"); for (int i = 0; i < elements.getLength(); i++) { final Element element = (Element) elements.item(i); if (ArrayUtils.contains(BAD_TAGS, element.getTagName())) { element.getParentNode().removeChild(element); }/*from w w w.j ava 2 s . c o m*/ final NamedNodeMap attributes = element.getAttributes(); for (int j = 0; j < attributes.getLength(); j++) { final Attr attr = (Attr) attributes.item(j); if (attr.getNodeName().startsWith("on")) { // This is an event handler: remove it element.removeAttributeNode(attr); } } } }
From source file:no.sesat.search.query.analyser.AnalysisRuleFactory.java
private Predicate createPredicate(final Element element, final Map predicateMap, final Map inheritedPredicates) { Predicate result = null;/*from w w w .ja v a 2 s .co m*/ // The operator to use from PredicateUtils. // The replaceAll's are so we end up with a method with one Predicate[] argument. final String methodName = element.getTagName().replaceAll("and", "all").replaceAll("or", "any") .replaceAll("either", "one").replaceAll("neither", "none") + "Predicate"; // because we can't use the above operator methods with only one child predicate // the not operator must be a special case. final boolean notPredicate = "not".equals(element.getTagName()); try { // Find PredicateUtils static method through reflection final Method method = notPredicate ? null : PredicateUtils.class.getMethod(methodName, new Class[] { Collection.class }); // load all the predicates it will apply to final List childPredicates = new LinkedList(); final NodeList predicates = element.getChildNodes(); for (int i = 0; i < predicates.getLength(); ++i) { final Node node = predicates.item(i); if (node instanceof Element) { final Element e = (Element) node; if ("predicate".equals(e.getTagName())) { childPredicates.add(readPredicate(e, predicateMap, inheritedPredicates)); } } } if (notPredicate) { // there should only be one in the list if (childPredicates.size() > 1) { throw new IllegalStateException(ERR_TOO_MANY_PREDICATES_IN_NOT + element.getParentNode()); } result = PredicateUtils.notPredicate((Predicate) childPredicates.get(0)); } else { // use the operator through reflection result = (Predicate) method.invoke(null, new Object[] { childPredicates }); } } catch (SecurityException ex) { LOG.error(ERR_WHILE_READING_ELEMENT + element); LOG.error(ERR_UNABLE_TO_FIND_PREDICATE_UTILS_METHOD + methodName, ex); } catch (NoSuchMethodException ex) { LOG.error(ERR_WHILE_READING_ELEMENT + element); LOG.error(ERR_UNABLE_TO_FIND_PREDICATE_UTILS_METHOD + methodName, ex); } catch (IllegalAccessException ex) { LOG.error(ERR_WHILE_READING_ELEMENT + element); LOG.error(ERR_UNABLE_TO_USE_PREDICATE_UTILS_METHOD + methodName, ex); } catch (InvocationTargetException ex) { LOG.error(ERR_WHILE_READING_ELEMENT + element); LOG.error(ERR_UNABLE_TO_USE_PREDICATE_UTILS_METHOD + methodName, ex); } catch (IllegalArgumentException ex) { LOG.error(ERR_WHILE_READING_ELEMENT + element); LOG.error(ERR_UNABLE_TO_USE_PREDICATE_UTILS_METHOD + methodName, ex); } return result; }
From source file:org.alfresco.util.XMLUtil.java
/** * FOR DIAGNOSTIC PURPOSES ONLY - incomplete<br/> * Builds a path to the node relative to the to node provided. * @param from the node from which to build the xpath * @param to an ancestor of <tt>from</tt> which will be the root of the path * @return an xpath to <tt>to</tt> rooted at <tt>from</tt>. *//* ww w .ja v a 2s.c o m*/ public static String buildXPath(final Node from, final Element to) { String result = ""; Node tmp = from; do { if (tmp instanceof Attr) { assert result.length() == 0; result = "@" + tmp.getNodeName(); } else if (tmp instanceof Element) { Node tmp2 = tmp; int position = 1; while (tmp2.getPreviousSibling() != null) { if (tmp2.getNodeName().equals(tmp.getNodeName())) { position++; } tmp2 = tmp2.getPreviousSibling(); } String part = tmp.getNodeName() + "[" + position + "]"; result = "/" + part + result; } else if (tmp instanceof Text) { assert result.length() == 0; result = "/text()"; } else { if (LOGGER.isDebugEnabled()) { throw new IllegalArgumentException("unsupported node type " + tmp); } } tmp = tmp.getParentNode(); } while (tmp != to.getParentNode() && tmp != null); return result; }
From source file:org.alfresco.web.forms.xforms.Schema2XForms.java
public static void removePrototypeNodes(final Node instanceDocumentElement) { final Map<String, LinkedList<Element>> prototypes = new HashMap<String, LinkedList<Element>>(); final NodeList children = instanceDocumentElement.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { if (!(children.item(i) instanceof Element)) { continue; }//from www .ja v a 2s .c om final String nodeName = children.item(i).getNodeName(); if (!prototypes.containsKey(nodeName)) { prototypes.put(nodeName, new LinkedList<Element>()); } prototypes.get(nodeName).add((Element) children.item(i)); } for (LinkedList<Element> l : prototypes.values()) { for (Element e : l) { if (e.hasAttributeNS(NamespaceService.ALFRESCO_URI, "prototype")) { assert "true".equals(e.getAttributeNS(NamespaceService.ALFRESCO_URI, "prototype")); e.removeAttributeNS(NamespaceService.ALFRESCO_URI, "prototype"); if (l.getLast().equals(e)) { e.getParentNode().removeChild(e); } } if (e.getParentNode() != null) { Schema2XForms.removePrototypeNodes(e); } } } }
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 ww w. j a v a 2s . 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; }