EName implements Serializable, Comparable<EName> { public static EName fromString(String strEName, String defaultNameSpace) throws IllegalArgumentException { if (strEName == null) { throw new IllegalArgumentException("Cannot parse 'null' as EName"); } Matcher m = pattern.matcher(strEName); if (m.matches()) { if (StringUtils.isNotBlank(defaultNameSpace) && m.group("namespace") == null) return new EName(defaultNameSpace, m.group("localname")); else return new EName(StringUtils.trimToEmpty(m.group("namespace")), m.group("localname")); } throw new IllegalArgumentException(format("Cannot parse '%s' as EName", strEName)); } EName(String namespaceURI, String localName); static EName mk(String namespaceURI, String localName); static EName mk(String localName); String getNamespaceURI(); String getLocalName(); boolean hasNamespace(); @Override int hashCode(); @Override boolean equals(Object that); @Override String toString(); @Override int compareTo(EName o); static EName fromString(String strEName, String defaultNameSpace); static EName fromString(String strEName);  }
@Test public void testFromString() { final String[] invalids = { "", "{", "}", "{}", "{http: "invalid{name", "invalid}name", "invalid name", "\t invalid\nname", "{invalid namespace}correct-name", "{ invalidnamespace}correct-name", "{invalid{namespace}correct-name", "{invalid name{space}incorrect\t name" }; for (String invalid : invalids) { try { EName.fromString(invalid); } catch (IllegalArgumentException iae) { } } final String[] valids = { "validname", "{}validwithemptyNS", "{http: }; final EName[] validENames = { new EName("", "validname"), new EName("", "validwithemptyNS"), new EName("http: }; for (int i = 0; i < valids.length; i++) { assertEquals(validENames[i], EName.fromString(valids[i])); } }
@Test public void testFromStringDefault() { final String defaultNS = "http: final String[] strings = { "localname", "{}with-empty-namespace", "{http: }; final EName[] eNames = { new EName(defaultNS, "localname"), new EName("", "with-empty-namespace"), new EName("http: }; for (int i = 0; i < strings.length; i++) { assertEquals(eNames[i], EName.fromString(strings[i], defaultNS)); } }