From: <bo...@us...> - 2014-12-30 19:02:45
|
Revision: 584 http://sourceforge.net/p/xmlunit/code/584 Author: bodewig Date: 2014-12-30 19:02:43 +0000 (Tue, 30 Dec 2014) Log Message: ----------- XMLUnit's own stripped down QName Needed for https://sourceforge.net/p/xmlunit/feature-requests/25/ Modified Paths: -------------- trunk/src/java/org/custommonkey/xmlunit/XMLConstants.java Added Paths: ----------- trunk/src/java/org/custommonkey/xmlunit/QualifiedName.java trunk/tests/java/org/custommonkey/xmlunit/test_QualifiedName.java Added: trunk/src/java/org/custommonkey/xmlunit/QualifiedName.java =================================================================== --- trunk/src/java/org/custommonkey/xmlunit/QualifiedName.java (rev 0) +++ trunk/src/java/org/custommonkey/xmlunit/QualifiedName.java 2014-12-30 19:02:43 UTC (rev 584) @@ -0,0 +1,140 @@ +/* +***************************************************************** +Copyright (c) 2014 Jeff Martin, Tim Bacon +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of the xmlunit.sourceforge.net nor the names + of its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +****************************************************************** +*/ + +package org.custommonkey.xmlunit; + +/** + * Since javax.xml.namespace.QName is not present prior to Java5, this + * is XMLUnit's own abstraction. + */ +public final class QualifiedName { + private final String namespaceUri; + private final String localName; + + public QualifiedName(String localName) { + this(XMLConstants.NULL_NS_URI, localName); + } + + public QualifiedName(String namespaceUri, String localName) { + if (localName == null) { + throw new IllegalArgumentException("localName must not be null"); + } + this.namespaceUri = + namespaceUri == null ? XMLConstants.NULL_NS_URI : namespaceUri; + this.localName = localName; + } + + public String getNamespaceURI() { + return namespaceUri; + } + + public String getLocalName() { + return localName; + } + + public int hashCode() { + return 7 * namespaceUri.hashCode() + localName.hashCode(); + } + + public boolean equals(Object o) { + if (!(o instanceof QualifiedName)) { + return false; + } + QualifiedName other = (QualifiedName) o; + return namespaceUri.equals(other.namespaceUri) + && localName.equals(other.localName); + } + + /** + * Parses strings of the form "{NS-URI}LOCAL-NAME" or "prefix:localName" as QualifiedNames. + * + * <p>When using the prefix-version the prefix must be defined + * inside the current NamespaceContext.</p> + * + * @see XMLUnit#setXpathNamespaceContext + */ + public static QualifiedName valueOf(String value) { + return valueOf(value, XMLUnit.getXpathNamespaceContext()); + } + + /** + * Parses strings of the form "{NS-URI}LOCAL-NAME" or "prefix:localName" as QualifiedNames. + * + * <p>When using the prefix-version the prefix must be defined + * inside the NamespaceContext given as argument.</p> + */ + public static QualifiedName valueOf(String value, NamespaceContext ctx) { + if (value == null) { + throw new IllegalArgumentException("value must not be null"); + } + int colon = value.indexOf(':'); + int closingBrace = value.indexOf('}'); + boolean qnameToStringStyle = value.startsWith("{") && closingBrace > 0; + if (!qnameToStringStyle && colon < 0) { + return new QualifiedName(value); // null namespace + } + return qnameToStringStyle ? parseQNameToString(value, closingBrace) + : parsePrefixFormat(value, colon, ctx); + } + + private static QualifiedName parseQNameToString(String value, int closingBrace) { + if (closingBrace + 1 == value.length()) { + throw new IllegalArgumentException("localName must not be empty in " + + value); + } + return new QualifiedName(value.substring(1, closingBrace), + value.substring(closingBrace + 1)); + } + + private static QualifiedName parsePrefixFormat(String value, int colon, + NamespaceContext ctx) { + if (colon + 1 == value.length()) { + throw new IllegalArgumentException("localName must not be empty in " + + value); + } + if (ctx == null) { + throw new IllegalArgumentException("Cannot parse " + value + + " without a NamespaceContext"); + } + String prefix = value.substring(0, colon); + String nsUri = ctx.getNamespaceURI(prefix); + if (nsUri == null) { + throw new IllegalArgumentException(prefix + " is unknown to " + + "NamespaceContext"); + } + return new QualifiedName(nsUri, value.substring(colon + 1)); + } +} Property changes on: trunk/src/java/org/custommonkey/xmlunit/QualifiedName.java ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Modified: trunk/src/java/org/custommonkey/xmlunit/XMLConstants.java =================================================================== --- trunk/src/java/org/custommonkey/xmlunit/XMLConstants.java 2014-12-30 18:43:48 UTC (rev 583) +++ trunk/src/java/org/custommonkey/xmlunit/XMLConstants.java 2014-12-30 19:02:43 UTC (rev 584) @@ -168,4 +168,9 @@ */ public static final String W3C_XML_SCHEMA_INSTANCE_TYPE_ATTR = "type"; + + /** + * "" + */ + String NULL_NS_URI = ""; } Added: trunk/tests/java/org/custommonkey/xmlunit/test_QualifiedName.java =================================================================== --- trunk/tests/java/org/custommonkey/xmlunit/test_QualifiedName.java (rev 0) +++ trunk/tests/java/org/custommonkey/xmlunit/test_QualifiedName.java 2014-12-30 19:02:43 UTC (rev 584) @@ -0,0 +1,114 @@ +/* +***************************************************************** +Copyright (c) 2014 Jeff Martin, Tim Bacon +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of the xmlunit.sourceforge.net nor the names + of its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +****************************************************************** +*/ + +package org.custommonkey.xmlunit; + +import java.util.HashMap; +import java.util.Map; +import junit.framework.Assert; +import junit.framework.TestCase; + +public class test_QualifiedName extends TestCase { + + public void testParseBareLocalName() { + Assert.assertEquals(new QualifiedName("foo"), + QualifiedName.valueOf("foo")); + } + + public void testParseQNameToStringStyle() { + Assert.assertEquals(new QualifiedName("foo", "bar"), + QualifiedName.valueOf("{foo}bar")); + } + + public void testParsePrefixStyle() { + Map m = new HashMap(); + m.put("pre", "foo"); + NamespaceContext ctx = new SimpleNamespaceContext(m); + Assert.assertEquals(new QualifiedName("foo", "bar"), + QualifiedName.valueOf("pre:bar", ctx)); + } + + public void testParsePrefixStyleImplicitContext() { + Map m = new HashMap(); + m.put("pre", "foo"); + XMLUnit.setXpathNamespaceContext(new SimpleNamespaceContext(m)); + try { + Assert.assertEquals(new QualifiedName("foo", "bar"), + QualifiedName.valueOf("pre:bar")); + } finally { + XMLUnit.setXpathNamespaceContext(null); + } + } + + public void testValueMustNotBeNull() { + shouldThrowWithMessage(null, "null"); + } + + public void testLocalPartMustNotBeEmptyQNameStyle() { + shouldThrowWithMessage("{foo}", "must not be empty"); + } + + public void testLocalPartMustNotBeEmptyPrefixStyle() { + shouldThrowWithMessage("foo:", "must not be empty"); + } + + public void testPrefixStyleRequiresNamespaceContext() { + shouldThrowWithMessage("foo:bar", "without a NamespaceContext"); + } + + public void testPrefixMustBeKnownToContext() { + Map m = new HashMap(); + XMLUnit.setXpathNamespaceContext(new SimpleNamespaceContext(m)); + try { + shouldThrowWithMessage("foo:bar", "foo is unknown"); + } finally { + XMLUnit.setXpathNamespaceContext(null); + } + } + + private void shouldThrowWithMessage(String valueToParse, + String messagePart) { + try { + QualifiedName.valueOf(valueToParse); + Assert.fail("expected IllegalArgumentException"); + } catch (IllegalArgumentException ex) { + String msg = ex.getMessage(); + Assert.assertTrue("exception message should contain '" + + messagePart + "' but was " + msg, + msg.indexOf(messagePart) >= 0); + } + } +} Property changes on: trunk/tests/java/org/custommonkey/xmlunit/test_QualifiedName.java ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |