/** * Adds all attributes of a collection to this element. * * @param attributes The attributes to add. * @throws IllegalArgumentException When one of {@code attributes} is invalid * or another attribute with same name already exists. */ public void addAttributes(Collection<Attribute> attributes) { for (final Attribute attribute : attributes) { addAttribute(attribute); } }
/** * Adds an array of attributes to this element. * * @param attributes The attributes to add. * @throws IllegalArgumentException When one of {@code attributes} is invalid * or another attribute with same name already exists. */ public void addAttributes(Attribute... attributes) { for (final Attribute attribute : attributes) { addAttribute(attribute); } }
/** * Adds a string attribute. * * @param name The attribute name. * @param value The attribute value. * @throws IllegalArgumentException When {@code name} is invalid * or another attribute with same name already exists. */ public void addAttribute(String name, String value) { addAttribute(new Attribute(name, value)); }
/** * Adds a short attribute. * * @param name The attribute name. * @param value The attribute value. * @throws IllegalArgumentException When {@code name} is invalid * or another attribute with same name already exists. */ public void addAttribute(String name, short value) { addAttribute(new Attribute(name, value)); }
/** * Adds a byte attribute. * * @param name The attribute name. * @param value The attribute value. * @throws IllegalArgumentException When {@code name} is invalid * or another attribute with same name already exists. */ public void addAttribute(String name, byte value) { addAttribute(new Attribute(name, value)); }
/** * Adds a double attribute. * * @param name The attribute name. * @param value The attribute value. * @throws IllegalArgumentException When {@code name} is invalid * or another attribute with same name already exists. */ public void addAttribute(String name, double value) { addAttribute(new Attribute(name, value)); }
/** * Adds an int attribute. * * @param name The attribute name. * @param value The attribute value. * @throws IllegalArgumentException When {@code name} is invalid * or another attribute with same name already exists. */ public void addAttribute(String name, int value) { addAttribute(new Attribute(name, value)); }
/** * Adds a float attribute. * * @param name The attribute name. * @param value The attribute value. * @throws IllegalArgumentException When {@code name} is invalid * or another attribute with same name already exists. */ public void addAttribute(String name, float value) { addAttribute(new Attribute(name, value)); }
/** * Adds a boolean attribute. * * @param name The attribute name. * @param value The attribute value. * @throws IllegalArgumentException When {@code name} is invalid * or another attribute with same name already exists. */ public void addAttribute(String name, boolean value) { addAttribute(new Attribute(name, value)); }
/** * Adds a long attribute. * * @param name The attribute name. * @param value The attribute value. * @throws IllegalArgumentException When {@code name} is invalid * or another attribute with same name already exists. */ public void addAttribute(String name, long value) { addAttribute(new Attribute(name, value)); }
/** * Adds an object attribute. * <p> * The string value is constructed using value.toString(). * * @param name The attribute name. * @param value The attribute value. * @throws IllegalArgumentException When {@code name} is invalid * or another attribute with same name already exists. */ public void addAttribute(String name, Object value) { addAttribute(new Attribute(name, value)); }
@Test public void testDeepEquals() { final Element e1 = new Element(NAME); e1.addAttribute(NAME1, VALUE); e1.addAttribute(NAME2, VALUE); final Element e2 = new Element(NAME); e2.addAttribute(NAME2, VALUE); e2.addAttribute(NAME1, VALUE); assertTrue(e1.deepEquals(e2)); }
/** * Creates an element by cloning another one. * <p> * Cloning is shallow.<br> * For deep cloning, use {@link #clone(boolean)}. * * @param other The element to clone. */ public Element(Element other) { this(null, other.getName()); for (int index = 0; index < other.getAttributesCount(); index++) { addAttribute(new Attribute(other.getAttributeAt(index))); } }
@Test public void testClone() { final Element e1 = new Element(NAME); e1.addAttribute(NAME1, VALUE); e1.addAttribute(NAME2, VALUE); final Element e2 = e1.clone(true); assertTrue(e1.deepEquals(e2)); }
final String value = atts.getValue(index); if (acceptsAttribute(element, name, value)) { element.addAttribute(convertAttributeName(element, name), convertAttributeValue(element, name, value));
public static void main(String[] args) throws IOException { LOGGER.info("Start " + XmlDataWriterDemo.class.getSimpleName()); Debug.printClassPath(); for (final URL url : Resources.getResources("log4j2.xml")) { LOGGER.info(" " + url); } final Document doc = new Document(); final Element root = new Element("root"); doc.addComment("Hello"); doc.addChild(root); doc.addComment("Hello"); root.addAttribute("att1", "value1"); final Element child = new Element("child"); child.addAttribute("att1", "value1"); root.addChild(child); doc.removeChildren(); doc.addComment("Hello"); doc.addChild(root); doc.addComment("Hello"); try (final XmlDataWriter writer = new XmlDataWriter(NonCloseableOutputStream.NON_CLOSABLE_SYSTEM_OUT)) { writer.getXmlWriter().setEnabled(XmlWriter.Feature.PRETTY_PRINT, true); writer.write(doc); writer.flush(); } } }
@Test public void testSortAttribute() { final Element root = new Element("root"); for (int i = 9; i >= 0; i--) { final Element child = root.addElement("child"); child.addAttribute("id", i); } assertEquals(root.getElementNamedWithAttribute("child", "id", "0"), root.getChildAt(9)); root.sortChildren(ComparatorsUtil.adapt(Element.class, Element.compareAttribute("id"))); assertEquals(root.getElementNamedWithAttribute("child", "id", "0"), root.getChildAt(0)); } }
@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)); }
@Test public void testDeepEquals() { final Document doc1 = new Document(); final Document doc2 = new Document(); assertTrue(doc1.deepEquals(doc2)); doc1.addChild(new Comment()); assertFalse(doc1.deepEquals(doc2)); doc2.addChild(new Comment()); assertTrue(doc1.deepEquals(doc2)); doc1.addChild(new Element(NAME)); assertFalse(doc1.deepEquals(doc2)); doc2.addChild(new Element(NAME)); assertTrue(doc1.deepEquals(doc2)); ((Element) doc1.getLastChild()).addAttribute(NAME, VALUE); assertFalse(doc1.deepEquals(doc2)); ((Element) doc2.getLastChild()).addAttribute(NAME, VALUE); assertTrue(doc1.deepEquals(doc2)); final Document doc3 = doc1.clone(true); assertTrue(doc1.deepEquals(doc3)); doc3.removeChildren(); assertEquals(0, doc3.getChildrenCount()); }
@Test public void testGetChildren() { final Element root = new Element(NAME); assertEquals(null, root.getLastChild()); final Element child1 = root.addElement(NAME1); final Element child2 = root.addElement(NAME2); final Element child3 = root.addElement(NAME1); child3.addAttribute(NAME, VALUE); final Comment child4 = root.addComment("Hello"); assertEquals(4, root.getChildrenCount()); assertTrue(root.hasChildren(Element.class)); assertTrue(root.hasChildren(Comment.class)); assertFalse(root.hasChildren(Text.class)); assertEquals(child4, root.getLastChild()); assertEquals(child1, root.getChild(Element.class)); assertEquals(child1, root.getElementNamed(NAME1)); assertEquals(3, root.getChildrenCount(Element.class)); assertEquals(2, root.getChildrenCount(Element.class, Element.named(NAME1))); assertEquals(child2, root.getChild(Element.class, Element.named(NAME2))); assertEquals(child2, root.getChildAt(Element.class, Element.named(NAME2), 0)); assertEquals(null, root.getChildAt(Element.class, Element.named(NAME2), 1)); assertEquals(child3, root.getChildAt(2)); assertEquals(child3, root.getChildAt(Element.class, 2)); assertEquals(child4, root.getChildAt(Comment.class, 0)); assertEquals(3, Iterables.size(root.getElements())); assertEquals(1, Iterables.size(root.getComments())); assertEquals(0, Iterables.size(root.getTexts())); assertEquals(child3, root.getElementNamedWithAttribute(NAME1, NAME, VALUE)); }