Project

General

Profile

Download (5.1 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
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
package eu.etaxonomy.cdm.model.common;
11

    
12
import java.io.Serializable;
13
import java.util.NoSuchElementException;
14
import java.util.StringTokenizer;
15

    
16
import javax.persistence.Embeddable;
17
import javax.persistence.Transient;
18

    
19
import com.ibm.lsid.MalformedLSIDException;
20

    
21
/**
22
 * This class is copied from com.ibm.lsid.LSID, I needed to re-implement this since
23
 * the domain objects are required to be Serializable
24
 *
25
 *
26
 * @author Ben Szekely (<a href="mailto:bhszekel@us.ibm.com">bhszekel@us.ibm.com</a>)
27
 * @author ben.clark
28
 * @see com.ibm.lsid.client.LSID
29
 */
30
@Embeddable
31
public class LSID implements Serializable {
32
	private static final long serialVersionUID = -3568951541851092269L;
33

    
34
	private String lsid;
35

    
36
	private String authority;
37

    
38
	private String namespace;
39

    
40
	private String object;
41

    
42
	private String revision;
43

    
44
	private LSID() { }
45

    
46
	/**
47
	 * Construct a new LSID with the String representation.
48
	 * @param String The lsid String respresentation
49
	 */
50
	public LSID(String lsid) throws MalformedLSIDException {
51
		if (lsid.endsWith(":")) {
52
			lsid = lsid.substring(0, lsid.length() - 1);
53
		}
54
		StringTokenizer st = new StringTokenizer(lsid, ":");
55
		// check for urn and lsid
56
		try {
57
			String urn = st.nextToken().toLowerCase();
58
			String l = st.nextToken().toLowerCase();
59
			if (!urn.equals("urn") || !l.equals("lsid")) {
60
				throw new MalformedLSIDException("urn:lsid: not found: [" + lsid + "]");
61
			}
62
		}
63
		catch (NoSuchElementException e) {
64
			throw new MalformedLSIDException(e, "urn:lsid: not found: [" + lsid + "]");
65
		}
66

    
67
		try {
68
			authority = st.nextToken().toLowerCase();
69
		}
70
		catch (NoSuchElementException e) {
71
			throw new MalformedLSIDException(e, "authority not found: [" + lsid + "]");
72
		}
73

    
74
		try {
75
			namespace = st.nextToken();
76
		}
77
		catch (NoSuchElementException e) {
78
			throw new MalformedLSIDException(e, "namespace not found: [" + lsid + "]");
79
		}
80

    
81
		try {
82
			object = st.nextToken();
83
		}
84
		catch (NoSuchElementException e) {
85
			throw new MalformedLSIDException(e, "object not found: [" + lsid + "]");
86
		}
87
		if (st.hasMoreTokens()) {
88
			revision = st.nextToken();
89
		}
90

    
91
		this.lsid = "urn:lsid:" + this.authority + ":" + this.namespace + ":" + this.object + (this.revision != null ? ":" + this.revision : "");
92
	}
93

    
94
	/**
95
	 * Construct a new LSID with the given components
96
	 * @param String the authority
97
	 * @param String the namespace
98
	 * @param String the object
99
	 * @param String the revision, can be null
100
	 */
101
	public LSID(String authority, String namespace, String object, String revision) throws MalformedLSIDException {
102
		this.authority = authority.toLowerCase();
103
		this.namespace = namespace;//.toLowerCase();
104
		this.object = object;//.toLowerCase();
105
		if (revision != null)
106
         {
107
            this.revision = revision;//.toLowerCase();
108
        }
109
		lsid = "urn:lsid:" + this.authority + ":" + this.namespace + ":" + this.object + (this.revision != null ? ":" + this.revision : "");
110
	}
111

    
112
	/**
113
	 * Returns the lsid
114
	 * @return String The lsid String representation
115
	 */
116
	public String getLsid() {
117
		return lsid;
118
	}
119

    
120

    
121

    
122
	/**
123
	 * Returns the authority component of the LSID
124
	 * @return LSIDAuthority the authority
125
	 */
126
	public String getAuthority() {
127
		return authority;
128
	}
129

    
130
	/**
131
	 * Returns the namespace component of the LSID
132
	 * @return String
133
	 */
134
	public String getNamespace() {
135
		return namespace;
136
	}
137

    
138
	/**
139
	 * Returns the object component of the LSID
140
	 * @return String
141
	 */
142
	public String getObject() {
143
		return object;
144
	}
145

    
146
	/**
147
	 * Returns the revision component of the LSID if it exists
148
	 * @return String
149
	 */
150
	public String getRevision() {
151
		return revision;
152
	}
153

    
154
	public static boolean isLsid(String strLsid){
155
		try {
156
			//TODO use algorithm rather than exceptions
157
			new LSID(strLsid);
158
			return true;
159
		} catch (MalformedLSIDException e) {
160
			return false;
161
		}
162
	}
163

    
164
    /**
165
     * <code>true</code>, if all of the LSID parts are
166
     * empty or <code>null</code>.
167
     */
168
    @Transient
169
    public boolean isEmpty(){
170
        if (isEmpty(authority) && isEmpty(lsid) &&
171
                isEmpty(namespace) && isEmpty(object) &&
172
                isEmpty(revision)){
173
            return true;
174
        }else{
175
            return false;
176
        }
177
    }
178
    private boolean isEmpty(String str) {
179
        return str == null || str.isEmpty();
180
    }
181

    
182
	/**
183
	 * return the string representation
184
	 * @return String
185
	 */
186
	@Override
187
    public String toString() {
188
		return lsid;
189
	}
190

    
191
	/**
192
	 * Two LSIDs are equal their string representations are equal disregarding case.
193
	 */
194
	@Override
195
    public boolean equals(Object lsid) {
196
		if(this == lsid) {
197
			return true;
198
		} else if (lsid != null && lsid instanceof LSID) {
199
			LSID theLSID = (LSID)lsid;
200
			return theLSID.toString().equals(toString());
201
		} else {
202
			return false;
203
		}
204
	}
205
}
(35-35/56)