1 |
1412a4c4
|
Katja Luther
|
/**
|
2 |
|
|
* Copyright (C) 2009 EDIT
|
3 |
|
|
* European Distributed Institute of Taxonomy
|
4 |
|
|
* http://www.e-taxonomy.eu
|
5 |
|
|
*
|
6 |
|
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7 |
|
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8 |
|
|
*/
|
9 |
|
|
|
10 |
ee91bcd9
|
ben.clark
|
package eu.etaxonomy.cdm.model.common;
|
11 |
|
|
|
12 |
4208745a
|
ben.clark
|
import java.io.Serializable;
|
13 |
ee91bcd9
|
ben.clark
|
import java.util.NoSuchElementException;
|
14 |
|
|
import java.util.StringTokenizer;
|
15 |
|
|
|
16 |
|
|
import javax.persistence.Embeddable;
|
17 |
|
|
|
18 |
|
|
import com.ibm.lsid.MalformedLSIDException;
|
19 |
|
|
|
20 |
|
|
/**
|
21 |
|
|
* This class is copied from com.ibm.lsid.LSID, I needed to re-implement this since
|
22 |
|
|
* the domain objects are required to be Serializable
|
23 |
|
|
*
|
24 |
|
|
*
|
25 |
|
|
* @author Ben Szekely (<a href="mailto:bhszekel@us.ibm.com">bhszekel@us.ibm.com</a>)
|
26 |
|
|
* @author ben.clark
|
27 |
|
|
* @see com.ibm.lsid.client.LSID
|
28 |
|
|
*/
|
29 |
|
|
@Embeddable
|
30 |
4208745a
|
ben.clark
|
public class LSID implements Serializable {
|
31 |
8accd608
|
Andreas Müller
|
private static final long serialVersionUID = -3568951541851092269L;
|
32 |
ee91bcd9
|
ben.clark
|
|
33 |
|
|
private String lsid;
|
34 |
|
|
|
35 |
|
|
private String authority;
|
36 |
|
|
|
37 |
|
|
private String namespace;
|
38 |
|
|
|
39 |
|
|
private String object;
|
40 |
|
|
|
41 |
|
|
private String revision;
|
42 |
|
|
|
43 |
|
|
private LSID() { }
|
44 |
|
|
|
45 |
|
|
/**
|
46 |
|
|
* Construct a new LSID with the String representation.
|
47 |
|
|
* @param String The lsid String respresentation
|
48 |
|
|
*/
|
49 |
|
|
public LSID(String lsid) throws MalformedLSIDException {
|
50 |
|
|
if (lsid.endsWith(":")) {
|
51 |
|
|
lsid = lsid.substring(0, lsid.length() - 1);
|
52 |
|
|
}
|
53 |
|
|
StringTokenizer st = new StringTokenizer(lsid, ":");
|
54 |
|
|
// check for urn and lsid
|
55 |
|
|
try {
|
56 |
|
|
String urn = st.nextToken().toLowerCase();
|
57 |
|
|
String l = st.nextToken().toLowerCase();
|
58 |
|
|
if (!urn.equals("urn") || !l.equals("lsid")) {
|
59 |
|
|
throw new MalformedLSIDException("urn:lsid: not found: [" + lsid + "]");
|
60 |
|
|
}
|
61 |
|
|
}
|
62 |
|
|
catch (NoSuchElementException e) {
|
63 |
|
|
throw new MalformedLSIDException(e, "urn:lsid: not found: [" + lsid + "]");
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
try {
|
67 |
|
|
authority = st.nextToken().toLowerCase();
|
68 |
|
|
}
|
69 |
|
|
catch (NoSuchElementException e) {
|
70 |
|
|
throw new MalformedLSIDException(e, "authority not found: [" + lsid + "]");
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
try {
|
74 |
|
|
namespace = st.nextToken();
|
75 |
|
|
}
|
76 |
|
|
catch (NoSuchElementException e) {
|
77 |
|
|
throw new MalformedLSIDException(e, "namespace not found: [" + lsid + "]");
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
try {
|
81 |
|
|
object = st.nextToken();
|
82 |
|
|
}
|
83 |
|
|
catch (NoSuchElementException e) {
|
84 |
|
|
throw new MalformedLSIDException(e, "object not found: [" + lsid + "]");
|
85 |
|
|
}
|
86 |
|
|
if (st.hasMoreTokens()) {
|
87 |
|
|
revision = st.nextToken();
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
this.lsid = "urn:lsid:" + this.authority + ":" + this.namespace + ":" + this.object + (this.revision != null ? ":" + this.revision : "");
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
/**
|
94 |
|
|
* Construct a new LSID with the given components
|
95 |
|
|
* @param String the authority
|
96 |
|
|
* @param String the namespace
|
97 |
|
|
* @param String the object
|
98 |
|
|
* @param String the revision, can be null
|
99 |
|
|
*/
|
100 |
|
|
public LSID(String authority, String namespace, String object, String revision) throws MalformedLSIDException {
|
101 |
|
|
this.authority = authority.toLowerCase();
|
102 |
|
|
this.namespace = namespace;//.toLowerCase();
|
103 |
|
|
this.object = object;//.toLowerCase();
|
104 |
|
|
if (revision != null)
|
105 |
|
|
this.revision = revision;//.toLowerCase();
|
106 |
|
|
lsid = "urn:lsid:" + this.authority + ":" + this.namespace + ":" + this.object + (this.revision != null ? ":" + this.revision : "");
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
/**
|
110 |
|
|
* Returns the lsid
|
111 |
|
|
* @return String The lsid String representation
|
112 |
|
|
*/
|
113 |
|
|
public String getLsid() {
|
114 |
|
|
return lsid;
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
|
118 |
|
|
|
119 |
|
|
/**
|
120 |
|
|
* Returns the authority component of the LSID
|
121 |
|
|
* @return LSIDAuthority the authority
|
122 |
|
|
*/
|
123 |
|
|
public String getAuthority() {
|
124 |
|
|
return authority;
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
/**
|
128 |
|
|
* Returns the namespace component of the LSID
|
129 |
|
|
* @return String
|
130 |
|
|
*/
|
131 |
|
|
public String getNamespace() {
|
132 |
|
|
return namespace;
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
/**
|
136 |
|
|
* Returns the object component of the LSID
|
137 |
|
|
* @return String
|
138 |
|
|
*/
|
139 |
|
|
public String getObject() {
|
140 |
|
|
return object;
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
/**
|
144 |
|
|
* Returns the revision component of the LSID if it exists
|
145 |
|
|
* @return String
|
146 |
|
|
*/
|
147 |
|
|
public String getRevision() {
|
148 |
|
|
return revision;
|
149 |
|
|
}
|
150 |
|
|
|
151 |
4d10bc12
|
Andreas Müller
|
public static boolean isLsid(String strLsid){
|
152 |
|
|
try {
|
153 |
|
|
//TODO use algorithm rather than exceptions
|
154 |
|
|
new LSID(strLsid);
|
155 |
|
|
return true;
|
156 |
|
|
} catch (MalformedLSIDException e) {
|
157 |
|
|
return false;
|
158 |
|
|
}
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
|
162 |
ee91bcd9
|
ben.clark
|
/**
|
163 |
|
|
* return the string representation
|
164 |
|
|
* @return String
|
165 |
|
|
*/
|
166 |
|
|
public String toString() {
|
167 |
|
|
return lsid;
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
/**
|
171 |
|
|
* Two LSIDs are equal their string representations are equal disregarding case.
|
172 |
|
|
*/
|
173 |
|
|
public boolean equals(Object lsid) {
|
174 |
|
|
if(this == lsid) {
|
175 |
|
|
return true;
|
176 |
|
|
} else if (lsid != null && lsid instanceof LSID) {
|
177 |
|
|
LSID theLSID = (LSID)lsid;
|
178 |
|
|
return theLSID.toString().equals(toString());
|
179 |
|
|
} else {
|
180 |
|
|
return false;
|
181 |
|
|
}
|
182 |
|
|
}
|
183 |
|
|
}
|