/** * @return A list of attributes of this element, sorted by names. */ public List<Attribute> getSortedAttributes() { final List<Attribute> sorted = getAttributes(); if (sorted.size() > 1) { Collections.sort(sorted, Attribute.NAME_COMPARATOR); } return sorted; }
final Attribute attribute = element.removeAttribute(name); if (attribute != null) { element.getAttributes().add(0, attribute);
public void removeAttributes(Predicate<Attribute> predicate) { final Iterator<Attribute> atts = getAttributes().iterator(); while (atts.hasNext()) { final Attribute att = atts.next(); if (predicate.test(att)) { atts.remove(); } } }
@Test public void testAttributes() { final Element e = new Element(NAME); assertEquals(0, e.getAttributesCount()); assertFalse(e.hasAttribute(NAME1)); assertEquals(-1, e.getAttributeIndex(NAME1)); assertTrue(e.getAttributes().isEmpty()); assertEquals(null, e.getAttributeValue(NAME1)); e.addAttribute(NAME1, null); assertTrue(e.hasAttribute(NAME1)); assertEquals(null, e.getAttributeValue(NAME1)); assertEquals(0, e.getAttributeIndex(NAME1)); assertEquals(-1, e.getAttributeIndex(NAME2)); assertFalse(e.hasAttribute(NAME2)); assertEquals(1, e.getAttributes().size()); assertEquals(Boolean.TRUE, e.getAttributeAsOptionalBoolean(NAME1, Boolean.TRUE)); assertEquals(Long.valueOf(0L), e.getAttributeAsOptionalLong(NAME1, 0L)); assertEquals(Integer.valueOf(0), e.getAttributeAsOptionalInt(NAME1, 0)); assertEquals(Short.valueOf((short) 0), e.getAttributeAsOptionalShort(NAME1, (short) 0)); assertEquals(Byte.valueOf((byte) 0), e.getAttributeAsOptionalByte(NAME1, (byte) 0)); assertEquals(Double.valueOf(0.0), e.getAttributeAsOptionalDouble(NAME1, 0.0)); assertEquals(Float.valueOf(0.0F), e.getAttributeAsOptionalFloat(NAME1, 0.0F)); assertEquals(FailureReaction.DEFAULT, e.getAttributeAsOptionalEnum(NAME1, FailureReaction.class, FailureReaction.DEFAULT)); }
@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(); }
/** * Sorts attributes of elements using a user defined sorter. * <p> * Uses a Depth First traversal for recursion.<br> * Sort is applied on parent before its children. * * @param parent The initial node. <em>MUST NOT</em> be null. * @param comparator The attribute name comparator. <em>MUST NOT</em> be null. * @param evaluator The evaluator. <em>MUST NOT</em> be null. */ public static void sortAttributes(Parent parent, Comparator<Attribute> comparator, Evaluator<? super Parent> evaluator) { if (parent instanceof Element) { final List<Attribute> atts = ((Element) parent).getAttributes(); if (atts.size() > 1) { Collections.sort(atts, comparator); } } for (final Parent child : parent.getChildren(Parent.class)) { if (evaluator.evaluate(child) == Evaluation.CONTINUE) { sortAttributes(child, comparator, evaluator); } } }
/** * 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); } } }
@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); }
for (final Attribute att : element.getAttributes()) { if (predicate.accepts(element, att.getName(), att.getValue())) { if (atts == null) {
/** * 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); } } }
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); } }
final Element element = (Element) node; writer.beginElement(element.getName()); for (final Attribute attribute : element.getAttributes()) { writer.addAttribute(attribute.getName(), attribute.getValue());