From 2c27e8423150d547f841af11568961165e01592c Mon Sep 17 00:00:00 2001 From: codistmonk Date: Mon, 5 Jul 2010 14:04:33 +0000 Subject: [PATCH] [Aprog] Added XML events helpers to XMLTools. git-svn-id: https://aprog.svn.sourceforge.net/svnroot/aprog/trunk@126 7cbf5e2b-b55d-4b93-acdd-c0d7b961df51 --- Aprog/src/net/sourceforge/aprog/xml/XMLTools.java | 108 ++++++++++++++++++++- .../net/sourceforge/aprog/xml/XMLToolsTest.java | 41 +++++++- 2 files changed, 145 insertions(+), 4 deletions(-) diff --git a/Aprog/src/net/sourceforge/aprog/xml/XMLTools.java b/Aprog/src/net/sourceforge/aprog/xml/XMLTools.java index 30af97c..68c374a 100644 --- a/Aprog/src/net/sourceforge/aprog/xml/XMLTools.java +++ b/Aprog/src/net/sourceforge/aprog/xml/XMLTools.java @@ -24,6 +24,9 @@ package net.sourceforge.aprog.xml; +import static java.util.Arrays.asList; +import static java.util.Collections.unmodifiableList; + import static net.sourceforge.aprog.tools.Tools.*; import java.io.ByteArrayInputStream; @@ -65,6 +68,8 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; +import org.w3c.dom.events.EventListener; +import org.w3c.dom.events.EventTarget; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; @@ -86,16 +91,115 @@ public final class XMLTools { /** * {@value}. */ - public static final String XML_1_UTF8_NOT_STANDALONE = + public static final String XML_1_0_UTF8 = + ""; + + /** + * {@value}. + */ + public static final String XML_1_0_UTF8_STANDALONE_NO = ""; /** * {@value}. */ - public static final String XML_1_UTF8_STANDALONE = + public static final String XML_1_0_UTF8_STANDALONE_YES = ""; /** + * {@value}. + */ + public static final String DOM_EVENT_SUBTREE_MODIFIED = "DOMSubtreeModified"; + + /** + * {@value}. + */ + public static final String DOM_EVENT_NODE_INSERTED = "DOMNodeInserted"; + + /** + * {@value}. + */ + public static final String DOM_EVENT_NODE_REMOVED = "DOMNodeRemoved"; + + /** + * {@value}. + */ + public static final String DOM_EVENT_NODE_REMOVED_FROM_DOCUMENT = "DOMNodeRemovedFromDocument"; + + /** + * {@value}. + */ + public static final String DOM_EVENT_NODE_INSERTED_INTO_DOCUMENT = "DOMNodeInsertedIntoDocument"; + + /** + * {@value}. + */ + public static final String DOM_EVENT_ATTRIBUTE_MODIFIED = "DOMAttrModified"; + + /** + * {@value}. + */ + public static final String DOM_EVENT_CHARACTER_DATA_MODIFIED = "DOMCharacterDataModified"; + + public static final List DOM_EVENT_TYPES = unmodifiableList(asList( + DOM_EVENT_ATTRIBUTE_MODIFIED, + DOM_EVENT_CHARACTER_DATA_MODIFIED, + DOM_EVENT_NODE_INSERTED, + DOM_EVENT_NODE_INSERTED_INTO_DOCUMENT, + DOM_EVENT_NODE_REMOVED, + DOM_EVENT_NODE_REMOVED_FROM_DOCUMENT, + DOM_EVENT_SUBTREE_MODIFIED + )); + + /** + * + * @param node + *
Not null + *
Input-output + * @param listener + *
Not null + *
Shared + * @throws IllegalArgumentException If {@code node} doesn't have the "events 2.0" feature + */ + public static final void addDOMEventListener(final Node node, final EventListener listener) { + checkHasEventsFeature(node); + + for (final String eventType : DOM_EVENT_TYPES) { + ((EventTarget) node).addEventListener(eventType, listener, false); + } + } + + /** + * + * @param node + *
Not null + *
Input-output + * @param listener + *
Not null + *
Shared + * @throws IllegalArgumentException If {@code node} doesn't have the "events 2.0" feature + */ + public static final void removeDOMEventListener(final Node node, final EventListener listener) { + checkHasEventsFeature(node); + + for (final String eventType : DOM_EVENT_TYPES) { + ((EventTarget) node).removeEventListener(eventType, listener, false); + } + } + + /** + * + * @param node + *
Not null + * @throws IllegalArgumentException If {@code node} doesn't have the "events 2.0" feature + */ + public static final void checkHasEventsFeature(final Node node) { + if (!XMLTools.getOwnerDocument(node).getImplementation().hasFeature("events", "2.0")) { + throw new IllegalArgumentException("Events 2.0 feature unavailable for node " + node); + } + } + + /** * * @param node *
Not null diff --git a/Aprog/test/net/sourceforge/aprog/xml/XMLToolsTest.java b/Aprog/test/net/sourceforge/aprog/xml/XMLToolsTest.java index 0d1a26a..040829e 100644 --- a/Aprog/test/net/sourceforge/aprog/xml/XMLToolsTest.java +++ b/Aprog/test/net/sourceforge/aprog/xml/XMLToolsTest.java @@ -24,6 +24,7 @@ package net.sourceforge.aprog.xml; +import static net.sourceforge.aprog.events.EventsTestingTools.*; import static net.sourceforge.aprog.tools.Tools.*; import static net.sourceforge.aprog.xml.XMLTools.*; @@ -34,10 +35,14 @@ import java.util.logging.Level; import javax.xml.transform.Result; import javax.xml.transform.stream.StreamResult; +import net.sourceforge.aprog.events.EventsTestingTools.EventRecorder; import org.junit.Test; import org.w3c.dom.Document; import org.w3c.dom.Node; +import org.w3c.dom.events.Event; +import org.w3c.dom.events.EventListener; +import org.w3c.dom.events.MutationEvent; /** * Automated tests using JUnit 4 for {@link XMLTools}. @@ -47,6 +52,38 @@ import org.w3c.dom.Node; public final class XMLToolsTest { @Test + public final & EventListener> void testEvents() { + final Document document = parse(""); + @SuppressWarnings("unchecked") + final R recorder = (R) newEventRecorder(EventListener.class); + + addDOMEventListener(document, recorder); + getNode(document, "a/b/@c").setNodeValue("e"); + + { + final MutationEvent event = recorder.getEvent(0); + + assertEquals(DOM_EVENT_ATTRIBUTE_MODIFIED, event.getType()); + assertSame(getNode(document, "a/b"), event.getTarget()); + assertEquals(MutationEvent.MODIFICATION, event.getAttrChange()); + assertEquals("d", event.getPrevValue()); + assertEquals("e", event.getNewValue()); + } + + { + final MutationEvent event = recorder.getEvent(1); + + assertEquals(DOM_EVENT_SUBTREE_MODIFIED, event.getType()); + assertSame(getNode(document, "a/b"), event.getTarget()); + assertEquals(0, event.getAttrChange()); + assertNull(event.getPrevValue()); + assertNull(event.getNewValue()); + } + + assertEquals(2, recorder.getEvents().size()); + } + + @Test public final void testGetQualifiedName() { final Document document = XMLTools.parse(""); @@ -107,14 +144,14 @@ public final class XMLToolsTest { XMLTools.write(document, buffer, 0); - assertEquals(XML_1_UTF8_NOT_STANDALONE + xmlInput, buffer.toString()); + assertEquals(XML_1_0_UTF8_STANDALONE_NO + xmlInput, buffer.toString()); } { final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); XMLTools.write(standalone(document), buffer, 0); - assertEquals(XML_1_UTF8_STANDALONE + xmlInput, buffer.toString()); + assertEquals(XML_1_0_UTF8_STANDALONE_YES + xmlInput, buffer.toString()); } { final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); -- 2.11.4.GIT