@Override public boolean matches(Node n) { if (n instanceof Element) { Namespace ns = ((Element) n).getNamespace(); // TODO this doesn't work with empty prefixes! if (!((Element) n).getName().equals(localName)) { return false; } else { String uri = getNamespaceURI(n); return ns != null;// && namespaceUri.equals(uri); // (!) } } return false; } };
@Override public boolean matches(Node n) { if (n instanceof Element) { Namespace ns = ((Element) n).getNamespace(); // TODO this doesn't work with empty prefixes! if (!((Element) n).getName().equals(localName)) { return false; } else { String uri = getNamespaceURI(n); return ns != null;// && namespaceUri.equals(uri); // (!) } } return false; } };
@Override public boolean matches (Node n) { if (!isElement (n)) return false; Element e = (Element)n; boolean match = true; if (match && name != null && !e.getName ().equals (name)) match = false; if (match && ns != null && !ns.equals (getNamespace ())) match = false; return match; } };
public static String getNamespaceURI(Node node) { if (node instanceof Element) { Namespace ns = ((Element) node).getNamespace(); if (ns != null) { String uri = ns.getURI(); if (uri == null || uri.length() == 0) { String uriAttr = ns.getPrefix().equals("") ? ((Element) node).getAttributeValue("xmlns") : null; if (uriAttr != null) { return uriAttr; } else { return getNamespaceURI(((Element) node).getParent()); } } else { return uri; } } } return null; }
public static String getNamespaceURI(Node node) { if (node instanceof Element) { Namespace ns = ((Element) node).getNamespace(); if (ns != null) { String uri = ns.getURI(); if (uri == null || uri.length() == 0) { String uriAttr = ns.getPrefix().equals("") ? ((Element) node).getAttributeValue("xmlns") : null; if (uriAttr != null) { return uriAttr; } else { return getNamespaceURI(((Element) node).getParent()); } } else { return uri; } } } return null; }
&& (ns == null || ns.equals (getNamespace ())) && (ns2 == null || ns2.equals (e.getNamespace ()))) if (ns != null && !ns.equals (current.getNamespace ())) return null;
protected static Element findOrCreateDescrptionElement(Element element, Parent root) { for (Node node : element.getNodes()) { if (node instanceof Element) { if ("description".equals(((Element) node).getName())) { return (Element) node; } } } Element parent = element.getParentElement(); if (element == root || parent == null || parent == root) { return null; } else if (elementsWithDescription.contains(element.getName())) { // lets check for a namespace prefix String ebn = element.getBeginName(); int idx = ebn.indexOf(":"); String name = idx > 0 ? ebn.substring(0, idx + 1) + "description" : "description"; Element description = new Element(name, element.getNamespace()); element.addNode(0, description); return description; } else { return findOrCreateDescrptionElement(parent, root); } }
&& (ns == null || ns.equals (getRootElement ().getNamespace ())) || (ns2 != null && !ns2.equals (getRootElement ().getNamespace ()))
protected static Element findOrCreateDescrptionElement(Element element, Parent root) { for (Node node : element.getNodes()) { if (node instanceof Element) { if ("description".equals(((Element) node).getName())) { return (Element) node; } } } Element parent = element.getParentElement(); if (element == root || parent == null || parent == root) { return null; } else if (elementsWithDescription.contains(element.getName())) { // lets check for a namespace prefix String ebn = element.getBeginName(); int idx = ebn.indexOf(":"); String name = idx > 0 ? ebn.substring(0, idx + 1) + "description" : "description"; Element description = new Element(name, element.getNamespace()); element.addNode(0, description); return description; } else { return findOrCreateDescrptionElement(parent, root); } }
protected Node replaceNamespace(Node node, String oldNS, String newNS) { if (node instanceof Element) { String ns = getNamespaceURI(node); if (ns != null && ns.equals(oldNS)) { Namespace namespace = ((Element) node).getNamespace(); if (namespace != null) { if (namespace.getURI() != null && namespace.getURI().equals(oldNS)) { ((Element) node).setNamespace(new Namespace(namespace.getPrefix(), newNS)); } } for (Attribute attr : ((Element) node).getAttributes()) { if (attr.getName().startsWith("xmlns")) { String value = attr.getValue(); if (value != null && value.equals(oldNS)) { attr.setValue(newNS); } } } } } if (node instanceof NodeWithChildren) { for (Node n : ((NodeWithChildren) node).getNodes()) { replaceNamespace(n, oldNS, newNS); } } return node; }
protected Node replaceNamespace(Node node, String oldNS, String newNS) { if (node instanceof Element) { String ns = getNamespaceURI(node); if (ns != null && ns.equals(oldNS)) { Namespace namespace = ((Element) node).getNamespace(); if (namespace != null) { if (namespace.getURI() != null && namespace.getURI().equals(oldNS)) { ((Element) node).setNamespace(new Namespace(namespace.getPrefix(), newNS)); } } for (Attribute attr : ((Element) node).getAttributes()) { if (attr.getName().startsWith("xmlns")) { String value = attr.getValue(); if (value != null && value.equals(oldNS)) { attr.setValue(newNS); } } } } } if (node instanceof NodeWithChildren) { for (Node n : ((NodeWithChildren) node).getNodes()) { replaceNamespace(n, oldNS, newNS); } } return node; }
if (parent.getNamespace ().getPrefix ().length () != 0) elementName = parent.getNamespace ().getPrefix () + ":" + elementName;