Skip to content

Commit e252be9

Browse files
committed
Fixes #176 - Namespaces starting with XML in other cases, etc.
1 parent 7c5f9aa commit e252be9

File tree

4 files changed

+132
-5
lines changed

4 files changed

+132
-5
lines changed

core/src/java/org/jdom2/Namespace.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ public final class Namespace implements Serializable {
9898
public static final Namespace XML_NAMESPACE = new Namespace(NS_PREFIX_XML,
9999
NS_URI_XML);
100100

101+
private static final Namespace XMLNS_NAMESPACE = new Namespace(NS_PREFIX_XMLNS,
102+
NS_URI_XMLNS);
101103

102104
static {
103105
// pre-populate the map with the constant namespaces that would
@@ -111,6 +113,11 @@ public final class Namespace implements Serializable {
111113
new ConcurrentHashMap<String, Namespace>();
112114
xmap.put(XML_NAMESPACE.getPrefix(), XML_NAMESPACE);
113115
namespacemap.put(XML_NAMESPACE.getURI(), xmap);
116+
117+
final ConcurrentMap<String,Namespace> xnsmap =
118+
new ConcurrentHashMap<String, Namespace>();
119+
xnsmap.put(XMLNS_NAMESPACE.getPrefix(), XMLNS_NAMESPACE);
120+
namespacemap.put(XMLNS_NAMESPACE.getURI(), xnsmap);
114121
}
115122

116123
/**
@@ -188,6 +195,7 @@ public static Namespace getNamespace(final String prefix, final String uri) {
188195
"Namespace URIs must be non-null and non-empty Strings");
189196
}
190197

198+
// http://www.w3.org/TR/REC-xml-names/#xmlReserved
191199
// The erratum to Namespaces in XML 1.0 that suggests this
192200
// next check is controversial. Not everyone accepts it.
193201
if (NS_URI_XML.equals(uri)) {
@@ -196,10 +204,36 @@ public static Namespace getNamespace(final String prefix, final String uri) {
196204
"only the '" + NS_PREFIX_XML + "' prefix.");
197205
}
198206

207+
// http://www.w3.org/TR/REC-xml-names/#xmlReserved
208+
if (NS_URI_XMLNS.equals(uri)) {
209+
throw new IllegalNameException(uri, "Namespace URI",
210+
"The " + NS_URI_XMLNS + " must be bound to " +
211+
"only the '" + NS_PREFIX_XMLNS + "' prefix.");
212+
}
213+
199214
// no namespace found, we validate the prefix
200215
final String pfx = prefix == null ? NS_PREFIX_DEFAULT : prefix;
201216

202217
String reason;
218+
219+
// http://www.w3.org/TR/REC-xml-names/#xmlReserved
220+
// checkNamespacePrefix no longer checks for xml prefix
221+
if (NS_PREFIX_XML.equals(pfx)) {
222+
// The xml namespace prefix was in the map. attempts to rebind it are illegal
223+
throw new IllegalNameException(uri, "Namespace prefix",
224+
"The prefix " + NS_PREFIX_XML + " (any case) can only be bound to " +
225+
"only the '" + NS_URI_XML + "' uri.");
226+
}
227+
228+
// http://www.w3.org/TR/REC-xml-names/#xmlReserved
229+
// checkNamespacePrefix no longer checks for xmlns prefix
230+
if (NS_PREFIX_XMLNS.equals(pfx)) {
231+
// The xml namespace prefix was in the map. attempts to rebind it are illegal
232+
throw new IllegalNameException(uri, "Namespace prefix",
233+
"The prefix " + NS_PREFIX_XMLNS + " (any case) can only be bound to " +
234+
"only the '" + NS_URI_XMLNS + "' uri.");
235+
}
236+
203237
if ((reason = Verifier.checkNamespacePrefix(pfx)) != null) {
204238
throw new IllegalNameException(pfx, "Namespace prefix", reason);
205239
}

core/src/java/org/jdom2/Verifier.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ public static String checkNamespacePrefix(final String prefix) {
539539
}
540540

541541
// Cannot start with "xml" in any character case
542+
/* See Issue 126 - https://github.com/hunterhacker/jdom/issues/126
542543
if (prefix.length() >= 3) {
543544
if (prefix.charAt(0) == 'x' || prefix.charAt(0) == 'X') {
544545
if (prefix.charAt(1) == 'm' || prefix.charAt(1) == 'M') {
@@ -549,6 +550,7 @@ public static String checkNamespacePrefix(final String prefix) {
549550
}
550551
}
551552
}
553+
*/
552554

553555
// If we got here, everything is OK
554556
return null;

test/src/java/org/jdom2/test/cases/TestNamespace.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ public void test_TCM__String_toString() {
263263

264264
}
265265

266+
@Test
267+
public void testXMLNamespaceGood() {
268+
Namespace ns = Namespace.getNamespace("xml", "http://www.w3.org/XML/1998/namespace");
269+
assertTrue(ns == Namespace.XML_NAMESPACE);
270+
}
271+
266272
@Test
267273
public void testXMLNamespacePrefix() {
268274
try {
@@ -276,4 +282,77 @@ public void testXMLNamespacePrefix() {
276282
}
277283
}
278284

285+
@Test
286+
public void testXMLNamespaceOnlyPrefix() {
287+
try {
288+
Namespace.getNamespace("other", JDOMConstants.NS_URI_XML);
289+
fail("Should not be able to have XML Namespace URI mapped to other prefix.");
290+
} catch (IllegalNameException ine) {
291+
// good
292+
} catch (Exception e) {
293+
e.printStackTrace();
294+
fail("We expect IllegalNameException not " + e.getClass());
295+
}
296+
}
297+
298+
@Test
299+
public void testXMLNamespaceDefault() {
300+
try {
301+
Namespace.getNamespace(JDOMConstants.NS_URI_XML);
302+
fail("Should not be able to have XML Namespace URI mapped the default namespace.");
303+
} catch (IllegalNameException ine) {
304+
// good
305+
} catch (Exception e) {
306+
e.printStackTrace();
307+
fail("We expect IllegalNameException not " + e.getClass());
308+
}
309+
}
310+
311+
312+
@Test
313+
public void testXMLNSNamespaceGood() {
314+
Namespace ns = Namespace.getNamespace("xmlns", "http://www.w3.org/2000/xmlns/");
315+
assertTrue("xmlns".equals(ns.getPrefix()));
316+
}
317+
318+
@Test
319+
public void testXMLNSNamespacePrefix() {
320+
try {
321+
Namespace.getNamespace("xmlns", "not right");
322+
fail("Should not be able to redefine 'xmlns' prefix.");
323+
} catch (IllegalNameException ine) {
324+
// good
325+
} catch (Exception e) {
326+
e.printStackTrace();
327+
fail("We expect IllegalNameException not " + e.getClass());
328+
}
329+
}
330+
331+
@Test
332+
public void testXMLNSNamespaceOnlyPrefix() {
333+
try {
334+
Namespace.getNamespace("other", JDOMConstants.NS_URI_XMLNS);
335+
fail("Should not be able to have XMLNS Namespace URI mapped to other prefix.");
336+
} catch (IllegalNameException ine) {
337+
// good
338+
} catch (Exception e) {
339+
e.printStackTrace();
340+
fail("We expect IllegalNameException not " + e.getClass());
341+
}
342+
}
343+
344+
@Test
345+
public void testXMLNSNamespaceDefault() {
346+
try {
347+
Namespace.getNamespace(JDOMConstants.NS_URI_XMLNS);
348+
fail("Should not be able to have XMLNS Namespace URI mapped the default namespace.");
349+
} catch (IllegalNameException ine) {
350+
// good
351+
} catch (Exception e) {
352+
e.printStackTrace();
353+
fail("We expect IllegalNameException not " + e.getClass());
354+
}
355+
}
356+
357+
279358
}

test/src/java/org/jdom2/test/cases/TestVerifier.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,17 +256,18 @@ public void testCheckNamespacePrefix() {
256256

257257
//invalid start characters
258258
assertNotNull("validated invalid name with startin -", Verifier.checkNamespacePrefix('-' + "test"));
259-
assertNotNull("validated invalid name with xmlns", Verifier.checkNamespacePrefix("xmlns"));
260259
assertNotNull("validated invalid name with startin :", Verifier.checkNamespacePrefix(':' + "test"));
261260
assertNotNull("validated invalid name with starting digit", Verifier.checkNamespacePrefix("9"));
262261
assertNotNull("validated invalid name with starting $", Verifier.checkNamespacePrefix("$"));
263262
assertNotNull("validated invalid name with starting .", Verifier.checkNamespacePrefix("."));
264263

265264
// cannot start with xml (case insensitive).
266-
assertNotNull("validated invalid name beginning with xml", Verifier.checkNamespacePrefix("xmlabc"));
267-
assertNotNull("validated invalid name beginning with xml", Verifier.checkNamespacePrefix("xmLabc"));
268-
assertNotNull("validated invalid name beginning with xml", Verifier.checkNamespacePrefix("xMlabc"));
269-
assertNotNull("validated invalid name beginning with xml", Verifier.checkNamespacePrefix("Xmlabc"));
265+
// See issue 126: https://github.com/hunterhacker/jdom/issues/126
266+
// assertNotNull("validated invalid name with xmlns", Verifier.checkNamespacePrefix("xmlns"));
267+
// assertNotNull("validated invalid name beginning with xml", Verifier.checkNamespacePrefix("xmlabc"));
268+
// assertNotNull("validated invalid name beginning with xml", Verifier.checkNamespacePrefix("xmLabc"));
269+
// assertNotNull("validated invalid name beginning with xml", Verifier.checkNamespacePrefix("xMlabc"));
270+
// assertNotNull("validated invalid name beginning with xml", Verifier.checkNamespacePrefix("Xmlabc"));
270271

271272

272273
//valid tests
@@ -282,6 +283,17 @@ public void testCheckNamespacePrefix() {
282283
assertNull("invalidated valid name with xml embedded", Verifier.checkNamespacePrefix("txml"));
283284
assertNull("invalidated valid name with xml embedded", Verifier.checkNamespacePrefix("xmml"));
284285

286+
// These tests all used to be NotNull tests, but the Verifier as been changed to pass them
287+
// See issue 126: https://github.com/hunterhacker/jdom/issues/126
288+
assertNull("invalidated invalid name with xmlns", Verifier.checkNamespacePrefix("xmlns"));
289+
assertNull("invalidated invalid name beginning with xml", Verifier.checkNamespacePrefix("xmlabc"));
290+
assertNull("invalidated invalid name beginning with xml", Verifier.checkNamespacePrefix("xmLabc"));
291+
assertNull("invalidated invalid name beginning with xml", Verifier.checkNamespacePrefix("xMlabc"));
292+
assertNull("invalidated invalid name beginning with xml", Verifier.checkNamespacePrefix("Xmlabc"));
293+
294+
assertNull("invalidated invalid name beginning with xml", Verifier.checkNamespacePrefix("XML"));
295+
assertNull("invalidated invalid name beginning with xml", Verifier.checkNamespacePrefix("XMLNS"));
296+
assertNull("invalidated invalid name beginning with xml", Verifier.checkNamespacePrefix("XmL"));
285297
}
286298

287299
/**

0 commit comments

Comments
 (0)