URN_Parser extends URI_Parser { public static URN_Parser buildParser(final String urn) throws NoSuchAuthorityCodeException { final String code = urn.trim(); String type = urn; for (int i = 0; i < URN_BASES.length; i++) { final String urnBase = URN_BASES[i]; final int typeStart = urnBase.length(); if (code.regionMatches(true, 0, urnBase, 0, typeStart)) { final int typeEnd = code.indexOf(URN_SEPARATOR, typeStart); if (typeEnd >= 0) { type = code.substring(typeStart, typeEnd).trim(); final URI_Type candidate = URI_Type.get(type); if (candidate != null) { final int nameEnd = code.indexOf(URN_SEPARATOR, typeEnd + 1); if (nameEnd >= 0) { final int lastEnd = code.lastIndexOf(URN_SEPARATOR); Version urnVersion = (lastEnd <= nameEnd) ? null : new Version( code.substring(nameEnd + 1, lastEnd)); String urnAuthority = code.substring(typeEnd + 1, nameEnd).trim(); String urnCode = code.substring(lastEnd + 1).trim(); URI_Type urnType = candidate; return new URN_Parser(urn, urnType, urnAuthority, urnVersion, urnCode); } } } } } throw new NoSuchAuthorityCodeException( Errors.format(ErrorKeys.ILLEGAL_IDENTIFIER_$1, type), "urn:ogc:def", type); } protected  URN_Parser(String urn, URI_Type type, String authority, Version version, String code); static URN_Parser buildParser(final String urn);  }
@Test public void testParse() throws NoSuchAuthorityCodeException { final URN_Parser parser = URN_Parser.buildParser("urn:ogc:def:CRS:EPSG:6.11.2:4326"); assertEquals("crs", parser.type.name); assertEquals("EPSG", parser.authority); assertEquals("6.11.2", parser.version.toString()); assertEquals("4326", parser.code); assertEquals("EPSG:4326", parser.getAuthorityCode()); }
@Test public void testParseWithoutVersion() throws NoSuchAuthorityCodeException { final URN_Parser parser = URN_Parser.buildParser("urn:ogc:def:CRS:EPSG:4326"); assertEquals("crs", parser.type.name); assertEquals("EPSG", parser.authority); assertNull ( parser.version); assertEquals("4326", parser.code); assertEquals("EPSG:4326", parser.getAuthorityCode()); }
@Test public void testInvalidParse() { final String urn = "urn:ogcx:def:CRS:EPSG:6.8:4326"; try { URN_Parser.buildParser(urn); fail(); } catch (NoSuchAuthorityCodeException e) { assertEquals(urn, e.getAuthorityCode()); } }
@Test public void testInvalidType() { final String urn = "urn:ogc:def:dummy:EPSG:6.8:4326"; try { URN_Parser.buildParser(urn); fail(); } catch (NoSuchAuthorityCodeException e) { assertEquals("dummy", e.getAuthorityCode()); } }