/** * Creates a Predicate that accepts Attributes with a given name. * * @param name The name. * @return A new Predicate that accepts Attributes that are named {@code name}. */ public static Predicate<Attribute> named(String name) { return att -> att.getName().equals(name); }
/** * Creates a Predicate that accepts Attributes with a name belonging to a set. * * @param names The names. * @return A new Predicate that accepts Attributes whose name belongs to {@code names}. */ public static Predicate<Attribute> named(Set<String> names) { return att -> names.contains(att.getName()); }
/** * Returns the attribute that has a particular name, or {@code null}. * * @param name The name. * @return The attribute named {@code name} or {@code null}. */ public Attribute getAttribute(String name) { if (attributes != null) { for (final Attribute attribute : attributes) { if (attribute.getName().equals(name)) { return attribute; } } } return null; }
/** * Returns the index of the attribute that has a particular name or -1. * * @param name The name. * @return The index of attribute named {@code name} or -1. */ public int getAttributeIndex(String name) { if (attributes != null) { for (int index = 0; index < attributes.size(); index++) { if (attributes.get(index).getName().equals(name)) { return index; } } } return -1; }
static String getNameAndAttributes(Element element) { final StringBuilder builder = new StringBuilder(); builder.append(element.getName()); for (final Attribute att : element.getSortedAttributes()) { builder.append(':'); builder.append(att.getName()); builder.append('='); builder.append(att.getValue()); } return builder.toString(); }
@Override public String toString() { final StringBuilder builder = new StringBuilder(); builder.append("[" + getType() + " " + getName()); for (final Attribute att : getAttributes()) { builder.append(" " + att.getName() + "='" + att.getValue() + "'"); } builder.append("]"); return builder.toString(); }
/** * Adds an attribute. * * @param attribute The attribute. * @throws IllegalArgumentException When {@code attribute} is invalid * or another attribute with same name already exists. */ public void addAttribute(Attribute attribute) { Checks.isNotNull(attribute, "attribute"); if (hasAttribute(attribute.getName())) { throw new IllegalArgumentException("Duplicate eattribute"); } else { if (attributes == null) { attributes = new ArrayList<>(); } attributes.add(attribute); } }
/** * Removes matching attributes. * <p> * Uses a Depth First traversal for recursion.<br> * Removal is applied on parent before its children. * * @param parent The initial node. <em>MUST NOT</em> be null. * @param predicate The predicate of matching attributes. <em>MUST NOT</em> be null. * @param evaluator The evaluator. <em>MUST NOT</em> be null. */ public static void removeMatchingAttributes(Parent parent, AttributePredicate predicate, Evaluator<? super Parent> evaluator) { if (parent instanceof Element) { final Element element = (Element) parent; final Iterator<Attribute> atts = element.getAttributes().iterator(); while (atts.hasNext()) { final Attribute att = atts.next(); if (predicate.accepts(element, att.getName(), att.getValue())) { atts.remove(); } } } for (final Parent child : parent.getChildren(Parent.class)) { if (evaluator.evaluate(child) == Evaluation.CONTINUE) { removeMatchingAttributes(child, predicate, evaluator); } } }
if (predicate.test(element) && element.getAttributesCount() > 1 && !element.getAttributeAt(0).getName().equals(name)) { final Attribute attribute = element.removeAttribute(name); if (attribute != null) {
private void analyze(File file, Element element) { for (final SPath path : margs.paths) { if (path.matchesElement(element)) { final String s = element.getText(null); checkClassName(s, file, element.getQName()); } for (final Attribute att : element.getAttributes()) { if (path.matchesAttribute(element, att.getName())) { final String s = att.getValue(); checkClassName(s, file, element.getQName() + "/@" + att.getName()); } } } for (final Element child : element.getElements()) { analyze(file, child); } }
/** * Sets value of matching attributes. * <p> * Uses a Depth First traversal for recursion.<br> * Change is applied on parent before its children. * * @param parent The initial node. <em>MUST NOT</em> be null. * @param predicate The predicate of matching attributes. <em>MUST NOT</em> be null. * @param converter The attribute value converter. <em>MUST NOT</em> be null. * @param evaluator The evaluator. <em>MUST NOT</em> be null. */ public static void setValueOfMatchingAttributes(Parent parent, AttributePredicate predicate, AttributeValueConverter converter, Evaluator<? super Parent> evaluator) { if (parent instanceof Element) { final Element element = (Element) parent; for (final Attribute att : element.getAttributes()) { if (predicate.accepts(element, att.getName(), att.getValue())) { final String newValue = converter.convertAttributeValue(element, att.getName(), att.getValue()); att.setValue(newValue); } } } for (final Parent child : parent.getChildren(Parent.class)) { if (evaluator.evaluate(child) == Evaluation.CONTINUE) { setValueOfMatchingAttributes(child, predicate, converter, evaluator); } } }
@Test public void testAttributeNameConversion() throws IOException { final XmlDataReader reader = new XmlDataReader(); reader.setEnabled(XmlDataReader.Feature.LOAD_SPACES, false); reader.setAttributeNameConverter((element, name) -> name.toUpperCase()); final URL url = getClass().getClassLoader().getResource("test-reader.xml"); final Document doc = reader.read(Files.toFile(url).getPath()); assertTrue(DataUtil.hasAllDescendantsMatching(doc, node -> { if (node instanceof Element) { final Element e = (Element) node; for (final Attribute a : e.getAttributes()) { if (!a.getName().toUpperCase().equals(a.getName())) { return false; } } return true; } else { return true; } }, true)); LOGGER.debug("Upper case attribute names"); XmlDataWriter.print(doc, OUT, " ", false); }
@Test public void testMisc() { final Attribute att = new Attribute(NAME); assertEquals(att, att); assertEquals(NAME, att.getName()); final Attribute att2 = new Attribute(att); assertEquals(att, att2); att2.setValue("XXX"); assertNotEquals(att, att2); assertNotEquals(att, new Attribute("NAME")); }
writer.beginElement(element.getName()); for (final Attribute attribute : element.getAttributes()) { writer.addAttribute(attribute.getName(), attribute.getValue());