Skip to content

Commit 1b53530

Browse files
authored
add shortname funtion with test (#85)
1 parent 9b7ba45 commit 1b53530

File tree

2 files changed

+133
-98
lines changed

2 files changed

+133
-98
lines changed

src/main/java/org/w3id/cwl/cwl1_2/utils/Uris.java

+109-98
Original file line numberDiff line numberDiff line change
@@ -7,112 +7,123 @@
77

88
public class Uris {
99

10-
// Emulate Python's urlsplit.
11-
public static class UriSplit {
12-
String scheme;
13-
String netloc;
14-
String path;
15-
String query;
16-
String fragment;
10+
// Emulate Python's urlsplit.
11+
public static class UriSplit {
12+
String scheme;
13+
String netloc;
14+
String path;
15+
String query;
16+
String fragment;
1717

18-
public UriSplit(String scheme, String netloc, String path, String query, String fragment) {
19-
this.scheme = scheme;
20-
this.netloc = netloc;
21-
this.path = path;
22-
this.query = query;
23-
this.fragment = fragment;
24-
}
18+
public UriSplit(String scheme, String netloc, String path, String query, String fragment) {
19+
this.scheme = scheme;
20+
this.netloc = netloc;
21+
this.path = path;
22+
this.query = query;
23+
this.fragment = fragment;
24+
}
2525

26-
public String toString() {
27-
return String.format(
28-
"UriSplit[%s,%s,%s,%s,%s]",
29-
this.scheme, this.netloc, this.path, this.query, this.fragment);
30-
}
26+
public String toString() {
27+
return String.format("UriSplit[%s,%s,%s,%s,%s]", this.scheme, this.netloc, this.path, this.query,
28+
this.fragment);
29+
}
3130

32-
}
31+
}
3332

34-
public static String fileUri(final String path) {
35-
return fileUri(path, false);
36-
}
33+
public static String fileUri(final String path) {
34+
return fileUri(path, false);
35+
}
3736

38-
public static String fileUri(final String path, final boolean splitFrag) {
39-
if (path.equals("file://")) {
40-
return path;
41-
}
42-
String frag;
43-
String urlPath;
44-
if (splitFrag) {
45-
final String[] pathsp = path.split("#", 2);
46-
// is quoting this?
47-
urlPath = Uris.quote(pathsp[0]);
48-
if (pathsp.length == 2) {
49-
frag = "#" + Uris.quote(pathsp[1]);
50-
} else {
51-
frag = "";
52-
urlPath = Uris.quote(path);
53-
}
54-
} else {
55-
urlPath = Uris.quote(path);
56-
frag = "";
57-
}
58-
if (urlPath.startsWith("//")) {
59-
return "file:" + urlPath + frag;
60-
} else {
61-
return "file://" + urlPath + frag;
62-
}
63-
}
37+
public static String fileUri(final String path, final boolean splitFrag) {
38+
if (path.equals("file://")) {
39+
return path;
40+
}
41+
String frag;
42+
String urlPath;
43+
if (splitFrag) {
44+
final String[] pathsp = path.split("#", 2);
45+
// is quoting this?
46+
urlPath = Uris.quote(pathsp[0]);
47+
if (pathsp.length == 2) {
48+
frag = "#" + Uris.quote(pathsp[1]);
49+
} else {
50+
frag = "";
51+
urlPath = Uris.quote(path);
52+
}
53+
} else {
54+
urlPath = Uris.quote(path);
55+
frag = "";
56+
}
57+
if (urlPath.startsWith("//")) {
58+
return "file:" + urlPath + frag;
59+
} else {
60+
return "file://" + urlPath + frag;
61+
}
62+
}
6463

65-
public static UriSplit split(final String uriString) {
66-
try {
67-
final URI uri = new URI(uriString);
68-
return new Uris.UriSplit(uri.getScheme(), uri.getAuthority(), uri.getPath(), uri.getQuery(), uri.getFragment());
69-
} catch (URISyntaxException e) {
70-
return new Uris.UriSplit(null, null, uriString, null, null);
71-
}
72-
}
64+
public static UriSplit split(final String uriString) {
65+
try {
66+
final URI uri = new URI(uriString);
67+
return new Uris.UriSplit(uri.getScheme(), uri.getAuthority(), uri.getPath(), uri.getQuery(),
68+
uri.getFragment());
69+
} catch (URISyntaxException e) {
70+
return new Uris.UriSplit(null, null, uriString, null, null);
71+
}
72+
}
7373

74-
public static String unsplit(
75-
final String scheme,
76-
final String netloc,
77-
final String path,
78-
final String query,
79-
final String fragment
80-
) {
81-
try {
82-
return new URI(scheme, netloc, path, query, fragment).toString();
83-
} catch (URISyntaxException e) {
84-
if (scheme == null && path.startsWith("_:")) {
85-
String uri = path;
86-
if(fragment != null && fragment.length() > 0) {
87-
uri += "#" + fragment;
88-
}
89-
return fragment;
90-
}
91-
throw new RuntimeException(e);
92-
}
93-
}
74+
public static String unsplit(final String scheme, final String netloc, final String path, final String query,
75+
final String fragment) {
76+
try {
77+
return new URI(scheme, netloc, path, query, fragment).toString();
78+
} catch (URISyntaxException e) {
79+
if (scheme == null && path.startsWith("_:")) {
80+
String uri = path;
81+
if (fragment != null && fragment.length() > 0) {
82+
uri += "#" + fragment;
83+
}
84+
return fragment;
85+
}
86+
throw new RuntimeException(e);
87+
}
88+
}
9489

95-
public static URI toUri(final String url) {
96-
try {
97-
return new URI(url);
98-
} catch (URISyntaxException e) {
99-
throw new RuntimeException(e);
100-
}
101-
}
90+
public static URI toUri(final String url) {
91+
try {
92+
return new URI(url);
93+
} catch (URISyntaxException e) {
94+
throw new RuntimeException(e);
95+
}
96+
}
10297

103-
public static String quote(final String uri) {
104-
try {
105-
return java.net.URLDecoder.decode(uri, StandardCharsets.UTF_8.name());
106-
} catch (UnsupportedEncodingException e) {
107-
throw new RuntimeException(e);
108-
}
109-
}
98+
public static String quote(final String uri) {
99+
try {
100+
return java.net.URLDecoder.decode(uri, StandardCharsets.UTF_8.name());
101+
} catch (UnsupportedEncodingException e) {
102+
throw new RuntimeException(e);
103+
}
104+
}
110105

111-
public static String unquote(final String uri) {
112-
try {
113-
return java.net.URLEncoder.encode(uri, StandardCharsets.UTF_8.name());
114-
} catch (UnsupportedEncodingException e) {
115-
throw new RuntimeException(e);
116-
}
117-
}
106+
public static String unquote(final String uri) {
107+
try {
108+
return java.net.URLEncoder.encode(uri, StandardCharsets.UTF_8.name());
109+
} catch (UnsupportedEncodingException e) {
110+
throw new RuntimeException(e);
111+
}
112+
}
113+
114+
public static String shortname(final String input_id) {
115+
try {
116+
final URI uri = new URI(input_id);
117+
final String fragment = uri.getFragment();
118+
if (fragment != null) {
119+
String[] fragment_elements = fragment.split("/");
120+
return fragment_elements[fragment_elements.length - 1];
121+
} else {
122+
String[] path_elements = uri.getPath().split("/");
123+
return path_elements[path_elements.length - 1];
124+
}
125+
} catch (URISyntaxException e) {
126+
return input_id;
127+
}
128+
}
118129
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.w3id.cwl.cwl1_2.utils;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class ShortnameTest {
7+
@Test
8+
public void testShortname() {
9+
Assert.assertEquals(Uris.shortname(
10+
"file:/Users/jdidion/projects/cwlScala/target/test-classes/CommandLineTools/conformance/#anon_enum_inside_array_inside_schemadef.cwl/first/user_type_2/species/homo_sapiens"),
11+
"homo_sapiens");
12+
Assert.assertEquals(Uris.shortname(
13+
"file:///home/michael/cwljava/src/test/resources/org/w3id/cwl/cwl1_2/utils/valid_anon_enum_inside_array_inside_schemadef.cwl#vcf2maf_params/ncbi_build/GRCh37"),
14+
"GRCh37");
15+
// Below are from https://w3id.org/cwl/v1.2/SchemaSalad.html#Short_names
16+
Assert.assertEquals(Uris.shortname("http://example.com/foo"), "foo");
17+
Assert.assertEquals(Uris.shortname("http://example.com/#bar"), "bar");
18+
Assert.assertEquals(Uris.shortname("http://example.com/foo/bar"), "bar");
19+
Assert.assertEquals(Uris.shortname("http://example.com/foo#bar"), "bar");
20+
Assert.assertEquals(Uris.shortname("http://example.com/#foo/bar"), "bar");
21+
Assert.assertEquals(Uris.shortname("http://example.com/foo#bar/baz"), "baz");
22+
}
23+
24+
}

0 commit comments

Comments
 (0)