| 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 | |
|---|
| 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 | public class LSID implements Serializable { |
|---|
| 31 | |
|---|
| 32 | private String lsid; |
|---|
| 33 | |
|---|
| 34 | private String authority; |
|---|
| 35 | |
|---|
| 36 | private String namespace; |
|---|
| 37 | |
|---|
| 38 | private String object; |
|---|
| 39 | |
|---|
| 40 | private String revision; |
|---|
| 41 | |
|---|
| 42 | private LSID() { } |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * Construct a new LSID with the String representation. |
|---|
| 46 | * @param String The lsid String respresentation |
|---|
| 47 | */ |
|---|
| 48 | public LSID(String lsid) throws MalformedLSIDException { |
|---|
| 49 | if (lsid.endsWith(":")) { |
|---|
| 50 | lsid = lsid.substring(0, lsid.length() - 1); |
|---|
| 51 | } |
|---|
| 52 | StringTokenizer st = new StringTokenizer(lsid, ":"); |
|---|
| 53 | // check for urn and lsid |
|---|
| 54 | try { |
|---|
| 55 | String urn = st.nextToken().toLowerCase(); |
|---|
| 56 | String l = st.nextToken().toLowerCase(); |
|---|
| 57 | if (!urn.equals("urn") || !l.equals("lsid")) { |
|---|
| 58 | throw new MalformedLSIDException("urn:lsid: not found: [" + lsid + "]"); |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | catch (NoSuchElementException e) { |
|---|
| 62 | throw new MalformedLSIDException(e, "urn:lsid: not found: [" + lsid + "]"); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | try { |
|---|
| 66 | authority = st.nextToken().toLowerCase(); |
|---|
| 67 | } |
|---|
| 68 | catch (NoSuchElementException e) { |
|---|
| 69 | throw new MalformedLSIDException(e, "authority not found: [" + lsid + "]"); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | try { |
|---|
| 73 | namespace = st.nextToken(); |
|---|
| 74 | } |
|---|
| 75 | catch (NoSuchElementException e) { |
|---|
| 76 | throw new MalformedLSIDException(e, "namespace not found: [" + lsid + "]"); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | try { |
|---|
| 80 | object = st.nextToken(); |
|---|
| 81 | } |
|---|
| 82 | catch (NoSuchElementException e) { |
|---|
| 83 | throw new MalformedLSIDException(e, "object not found: [" + lsid + "]"); |
|---|
| 84 | } |
|---|
| 85 | if (st.hasMoreTokens()) { |
|---|
| 86 | revision = st.nextToken(); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | this.lsid = "urn:lsid:" + this.authority + ":" + this.namespace + ":" + this.object + (this.revision != null ? ":" + this.revision : ""); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | * Construct a new LSID with the given components |
|---|
| 94 | * @param String the authority |
|---|
| 95 | * @param String the namespace |
|---|
| 96 | * @param String the object |
|---|
| 97 | * @param String the revision, can be null |
|---|
| 98 | */ |
|---|
| 99 | public LSID(String authority, String namespace, String object, String revision) throws MalformedLSIDException { |
|---|
| 100 | this.authority = authority.toLowerCase(); |
|---|
| 101 | this.namespace = namespace;//.toLowerCase(); |
|---|
| 102 | this.object = object;//.toLowerCase(); |
|---|
| 103 | if (revision != null) |
|---|
| 104 | this.revision = revision;//.toLowerCase(); |
|---|
| 105 | lsid = "urn:lsid:" + this.authority + ":" + this.namespace + ":" + this.object + (this.revision != null ? ":" + this.revision : ""); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | /** |
|---|
| 109 | * Returns the lsid |
|---|
| 110 | * @return String The lsid String representation |
|---|
| 111 | */ |
|---|
| 112 | public String getLsid() { |
|---|
| 113 | return lsid; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | /** |
|---|
| 119 | * Returns the authority component of the LSID |
|---|
| 120 | * @return LSIDAuthority the authority |
|---|
| 121 | */ |
|---|
| 122 | public String getAuthority() { |
|---|
| 123 | return authority; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | /** |
|---|
| 127 | * Returns the namespace component of the LSID |
|---|
| 128 | * @return String |
|---|
| 129 | */ |
|---|
| 130 | public String getNamespace() { |
|---|
| 131 | return namespace; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /** |
|---|
| 135 | * Returns the object component of the LSID |
|---|
| 136 | * @return String |
|---|
| 137 | */ |
|---|
| 138 | public String getObject() { |
|---|
| 139 | return object; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | /** |
|---|
| 143 | * Returns the revision component of the LSID if it exists |
|---|
| 144 | * @return String |
|---|
| 145 | */ |
|---|
| 146 | public String getRevision() { |
|---|
| 147 | return revision; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | public static boolean isLsid(String strLsid){ |
|---|
| 151 | try { |
|---|
| 152 | //TODO use algorithm rather than exceptions |
|---|
| 153 | new LSID(strLsid); |
|---|
| 154 | return true; |
|---|
| 155 | } catch (MalformedLSIDException e) { |
|---|
| 156 | return false; |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | /** |
|---|
| 162 | * return the string representation |
|---|
| 163 | * @return String |
|---|
| 164 | */ |
|---|
| 165 | public String toString() { |
|---|
| 166 | return lsid; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | /** |
|---|
| 170 | * Two LSIDs are equal their string representations are equal disregarding case. |
|---|
| 171 | */ |
|---|
| 172 | public boolean equals(Object lsid) { |
|---|
| 173 | if(this == lsid) { |
|---|
| 174 | return true; |
|---|
| 175 | } else if (lsid != null && lsid instanceof LSID) { |
|---|
| 176 | LSID theLSID = (LSID)lsid; |
|---|
| 177 | return theLSID.toString().equals(toString()); |
|---|
| 178 | } else { |
|---|
| 179 | return false; |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | } |
|---|