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(); }
@Test public void testShort() { final Attribute att = new Attribute(NAME, (short) 10); assertEquals((short) 10, att.getValueAsShort()); assertEquals("10", att.getValue()); }
@Test public void testLong() { final Attribute att = new Attribute(NAME, 10L); assertEquals(10L, att.getValueAsLong()); assertEquals("10", att.getValue()); }
@Test public void testBoolean() { final Attribute att = new Attribute(NAME, true); assertTrue(att.getValueAsBoolean()); assertEquals("true", att.getValue()); }
@Test public void testFloat() { final Attribute att = new Attribute(NAME, 10.0f); assertEquals(10.0f, att.getValueAsFloat(), 1.0e-10); assertEquals("10.0", att.getValue()); }
@Test public void testInteger() { final Attribute att = new Attribute(NAME, 10); assertEquals(10, att.getValueAsInt()); assertEquals("10", att.getValue()); }
@Test public void testDouble() { final Attribute att = new Attribute(NAME, 10.0); assertEquals(10.0, att.getValueAsDouble(), 1.0e-10); assertEquals("10.0", att.getValue()); }
@Test public void testByte() { final Attribute att = new Attribute(NAME, (byte) 10); assertEquals((byte) 10, att.getValueAsByte()); assertEquals("10", att.getValue()); }
@Test public void testEnum() { final Attribute att = new Attribute(NAME, Enum.A); assertEquals(Enum.A, att.getValueAsEnum(Enum.class)); assertEquals(Enum.A.name(), att.getValue()); } }
@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(); }
/** * Changes the value of attributes that have a given name. * * @param name The attribute name. * @param modifier A function that take value and returns new value. */ public void changeAttributeValue(String name, Function<String, String> modifier) { final Attribute att = getAttribute(name); if (att != null) { final String modified = modifier.apply(att.getValue()); att.setValue(modified); } }
/** * Returns the value associated to an attribute. * * @param name The attribute name. * @param def The value to return if no attribute named {@code name} exists. * @param missingReaction The reaction to adopt when attribute is not found. * @return The value of attribute named {@code name} or {@code def}. * @throws NotFoundException when attribute is not found and {@code missingReaction} is {@link FailureReaction#FAIL}. */ public String getAttributeValue(String name, String def, FailureReaction missingReaction) { final Attribute att = getAttribute(name); if (att == null) { if (missingReaction == FailureReaction.DEFAULT) { return def; } else if (missingReaction == FailureReaction.WARN) { LOGGER.warn("No attribute named '" + name + "'"); return def; } else { throw new NotFoundException("No attribute named '" + name + "'"); } } else { return att.getValue(); } }
/** * 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); } } }
/** * 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 testString() { final Attribute att = new Attribute(NAME, "SSS"); assertEquals("SSS", att.getValue()); }
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); } }
writer.beginElement(element.getName()); for (final Attribute attribute : element.getAttributes()) { writer.addAttribute(attribute.getName(), attribute.getValue());