/
GlobalId.java
191 lines (149 loc) · 4.79 KB
/
GlobalId.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package edu.harvard.iq.dataverse;
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.net.URL;
import javax.ejb.EJB;
/**
*
* @author skraffmiller
*/
public class GlobalId implements java.io.Serializable {
public static final String DOI_PROTOCOL = "doi";
public static final String HDL_PROTOCOL = "hdl";
public static final String HDL_RESOLVER_URL = "https://hdl.handle.net/";
public static final String DOI_RESOLVER_URL = "https://doi.org/";
@EJB
SettingsServiceBean settingsService;
public GlobalId(String identifier) {
// set the protocol, authority, and identifier via parsePersistentId
if (!this.parsePersistentId(identifier)){
throw new IllegalArgumentException("Failed to parse identifier: " + identifier);
}
}
public GlobalId(String protocol, String authority, String identifier) {
this.protocol = protocol;
this.authority = authority;
this.identifier = identifier;
}
public GlobalId(Dataset dataset){
this.authority = dataset.getAuthority();
this.protocol = dataset.getProtocol();
this.identifier = dataset.getIdentifier();
}
private String protocol;
private String authority;
private String identifier;
public String getProtocol() {
return protocol;
}
public void setProtocol(String protocol) {
this.protocol = protocol;
}
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
public String getIdentifier() {
return identifier;
}
public void setIdentifier(String identifier) {
this.identifier = identifier;
}
public String toString() {
return protocol + ":" + authority + "/" + identifier;
}
public URL toURL() {
URL url = null;
try {
if (protocol.equals(DOI_PROTOCOL)){
url = new URL(DOI_RESOLVER_URL + authority + "/" + identifier);
} else if (protocol.equals(HDL_PROTOCOL)){
url = new URL(HDL_RESOLVER_URL + authority + "/" + identifier);
}
} catch (MalformedURLException ex) {
Logger.getLogger(GlobalId.class.getName()).log(Level.SEVERE, null, ex);
}
return url;
}
/**
* Parse a Persistent Id and set the protocol, authority, and identifier
*
* Example 1: doi:10.5072/FK2/BYM3IW
* protocol: doi
* authority: 10.5072/FK2
* identifier: BYM3IW
*
* Example 2: hdl:1902.1/111012
* protocol: hdl
* authority: 1902.1
* identifier: 111012
*
* @param identifierString
*
*/
private boolean parsePersistentId(String identifierString){
if (identifierString == null){
return false;
}
int index1 = identifierString.indexOf(':');
int index2 = identifierString.lastIndexOf('/');
if (index1==-1) {
return false;
}
String protocol = identifierString.substring(0, index1);
if (!"doi".equals(protocol) && !"hdl".equals(protocol)) {
return false;
}
if (index2 == -1) {
return false;
}
this.protocol = protocol;
this.authority = formatIdentifierString(identifierString.substring(index1+1, index2));
this.identifier = formatIdentifierString(identifierString.substring(index2+1));
if (this.protocol.equals(DOI_PROTOCOL)) {
if (!this.checkDOIAuthority(this.authority)) {
return false;
}
}
return true;
}
private String formatIdentifierString(String str){
if (str == null){
return null;
}
// remove whitespace, single quotes, and semicolons
return str.replaceAll("\\s+|'|;","");
/*
< (%3C)
> (%3E)
{ (%7B)
} (%7D)
^ (%5E)
[ (%5B)
] (%5D)
` (%60)
| (%7C)
\ (%5C)
+
*/
// http://www.doi.org/doi_handbook/2_Numbering.html
}
private boolean checkDOIAuthority(String doiAuthority){
if (doiAuthority==null){
return false;
}
if (!(doiAuthority.startsWith("10."))){
return false;
}
return true;
}
}