package org.collectionspace.services.common.api;\r
\r
-import java.util.regex.Matcher;\r
-import java.util.regex.Pattern;\r
-import org.collectionspace.services.common.api.RefNameUtils;\r
+import org.collectionspace.services.common.api.RefNameUtils.AuthorityInfo;\r
+import org.collectionspace.services.common.api.RefNameUtils.AuthorityTermInfo;\r
import org.slf4j.Logger;\r
import org.slf4j.LoggerFactory;\r
\r
/**\r
- * Usage for this class, if you have a URN and would like to get at its fields, is to call one of these methods:\r
+ * Usage for this class, if you have a URN and would like to get at its fields,\r
+ * is to call one of these methods:\r
*\r
- * RefName.AuthorityItem item = RefName.AuthorityItem.parse(RefName.AUTHORITY_ITEM_EXAMPLE);\r
- * or\r
- * RefName.Authority authority = RefName.Authority.parse(RefName.AUTHORITY_EXAMPLE);\r
+ * RefName.AuthorityItem item =\r
+ * RefName.AuthorityItem.parse(RefName.AUTHORITY_ITEM_EXAMPLE); or\r
+ * RefName.Authority authority =\r
+ * RefName.Authority.parse(RefName.AUTHORITY_EXAMPLE);\r
*\r
* From the object returned, you may set/get any of the public fields.\r
*\r
- * If you want to format a string urn, then you need to construct either a RefName.AuthorityItem or RefName.Authority.\r
- * You can parse a URN to do so, as shown above, or you can construct one with a constructor, setting its fields afterwards.\r
- * A better way is to use one of the build*() methods on this class:\r
+ * If you want to format a string urn, then you need to construct either a\r
+ * RefName.AuthorityItem or RefName.Authority. You can parse a URN to do so, as\r
+ * shown above, or you can construct one with a constructor, setting its fields\r
+ * afterwards. A better way is to use one of the build*() methods on this class:\r
*\r
- * RefName.Authority authority2 = RefName.buildAuthority(tenantName, serviceName, authorityShortIdentifier, authorityDisplayName);\r
+ * RefName.Authority authority2 = RefName.buildAuthority(tenantName,\r
+ * serviceName, authorityShortIdentifier, authorityDisplayName);\r
*\r
- * RefName.AuthorityItem item2 = RefName.buildAuthorityItem(authority2,\r
- * RefName.EX_itemShortIdentifier,\r
- * RefName.EX_itemDisplayName);\r
+ * RefName.AuthorityItem item2 = RefName.buildAuthorityItem(authority2,\r
+ * RefName.EX_itemShortIdentifier, RefName.EX_itemDisplayName);\r
*\r
- * Note that authority2 is an object, not a String, and is passed in to RefName.buildAuthorityItem().\r
+ * Note that authority2 is an object, not a String, and is passed in to\r
+ * RefName.buildAuthorityItem().\r
*\r
* Then simply call toString() on the object:\r
*\r
- * String authorityURN = authority2.toString();\r
+ * String authorityURN = authority2.toString();\r
*\r
- * String itemURN = item2.toString();\r
+ * String itemURN = item2.toString();\r
*\r
* These test cases are kept up-to-date in\r
*\r
- * org.collectionspace.services.common.api.test.RefNameTest\r
+ * org.collectionspace.services.common.api.test.RefNameTest\r
*\r
* User: laramie\r
*/\r
public class RefName {\r
- \r
- /** The logger. */\r
- private static final Logger logger = LoggerFactory.getLogger(RefName.class);\r
\r
- public static final String HACK_VOCABULARIES = "Vocabularies"; //TODO: get rid of these.\r
- public static final String HACK_ORGANIZATIONS = "Organizations"; //TODO: get rid of these.\r
- public static final String HACK_ORGAUTHORITIES = "Orgauthorities"; //TODO: get rid of these.\r
- public static final String HACK_PERSONAUTHORITIES = "Personauthorities"; //TODO: get rid of these.\r
- public static final String HACK_LOCATIONAUTHORITIES = "Locationauthorities"; //TODO: get rid of these.\r
+ /**\r
+ * The logger.\r
+ */\r
+ private static final Logger logger = LoggerFactory.getLogger(RefName.class);\r
public static final String URN_PREFIX = "urn:cspace:";\r
public static final String URN_NAME_PREFIX = "urn:cspace:name";\r
public static final String REFNAME = "refName";\r
- public static final String AUTHORITY_REGEX = "urn:cspace:(.*):(.*):name\\((.*)\\)\\'?([^\\']*)\\'?";\r
- public static final String AUTHORITY_ITEM_REGEX = "urn:cspace:(.*):(.*):name\\((.*)\\):item:name\\((.*)\\)\\'?([^\\']*)\\'";\r
+ // public static final String AUTHORITY_REGEX = "urn:cspace:(.*):(.*):name\\((.*)\\)\\'?([^\\']*)\\'?";\r
+ // public static final String AUTHORITY_ITEM_REGEX = "urn:cspace:(.*):(.*):name\\((.*)\\):item:name\\((.*)\\)\\'?([^\\']*)\\'";\r
public static final String AUTHORITY_EXAMPLE = "urn:cspace:collectionspace.org:Loansin:name(shortID)'displayName'";\r
public static final String AUTHORITY_EXAMPLE2 = "urn:cspace:collectionspace.org:Loansin:name(shortID)";\r
public static final String AUTHORITY_ITEM_EXAMPLE = "urn:cspace:collectionspace.org:Loansin:name(shortID):item:name(itemShortID)'itemDisplayName'";\r
public String displayName = "";\r
\r
public static Authority parse(String urn) {\r
- Authority info = new Authority();\r
- Pattern p = Pattern.compile(AUTHORITY_REGEX);\r
- Matcher m = p.matcher(urn);\r
- if (m.find()) {\r
- if (m.groupCount() < 4) {\r
- return null;\r
- }\r
- info.tenantName = m.group(1);\r
- info.resource = m.group(2);\r
- info.shortIdentifier = m.group(3);\r
- info.displayName = m.group(4);\r
- return info;\r
+ Authority authority;\r
+ try {\r
+ RefNameUtils.AuthorityInfo authorityInfo =\r
+ RefNameUtils.parseAuthorityInfo(urn);\r
+ authority = authorityFromAuthorityInfo(authorityInfo, true);\r
+ } catch (IllegalArgumentException iae) {\r
+ return null;\r
}\r
- return null;\r
+ return authority;\r
}\r
- \r
+\r
public String getShortIdentifier() {\r
return this.shortIdentifier;\r
}\r
public String displayName = "";\r
\r
public static AuthorityItem parse(String urn) {\r
- Authority authority = new Authority();\r
- AuthorityItem authorityItem = new AuthorityItem();\r
+ AuthorityItem authorityItem = null;\r
try {\r
- RefNameUtils.AuthorityTermInfo termInfo = RefNameUtils.parseAuthorityTermInfo(urn);\r
-\r
- authority.tenantName = termInfo.inAuthority.domain;\r
- authority.resource = termInfo.inAuthority.resource;\r
- if (termInfo.inAuthority.name != null &&\r
- ! termInfo.inAuthority.name.trim().isEmpty()) {\r
- authority.shortIdentifier = termInfo.inAuthority.name;\r
- } else {\r
- authority.shortIdentifier = termInfo.inAuthority.csid;\r
- }\r
- authorityItem.inAuthority = authority;\r
-\r
- if (termInfo.name != null &&\r
- ! termInfo.name.trim().isEmpty()) {\r
- authorityItem.shortIdentifier = termInfo.name;\r
- } else {\r
- authorityItem.shortIdentifier = termInfo.csid;\r
- }\r
- authorityItem.displayName = termInfo.displayName;\r
+ RefNameUtils.AuthorityTermInfo termInfo =\r
+ RefNameUtils.parseAuthorityTermInfo(urn);\r
+ authorityItem = authorityItemFromTermInfo(termInfo);\r
} catch (IllegalArgumentException iae) {\r
return null;\r
}\r
return authorityItem;\r
}\r
- \r
+\r
public String getParentShortIdentifier() {\r
return this.inAuthority.shortIdentifier;\r
}\r
- \r
+\r
public String getShortIdentifier() {\r
return this.shortIdentifier;\r
}\r
- \r
+\r
public boolean equals(Object other) {\r
if (other == null) {\r
return false;\r
return item;\r
}\r
\r
- /** Use this method to avoid formatting any urn's outside of this unit;\r
+ /**\r
+ * Use this method to avoid formatting any urn's outside of this unit;\r
* Caller passes in a shortId, such as "TestAuthority", and method returns\r
- * the correct urn path element, without any path delimiters such as '/'\r
- * so that calling shortIdToPath("TestAuthority") returns "urn:cspace:name(TestAuthority)", and\r
- * then this value may be put into a path, such as "/personauthorities/urn:cspace:name(TestAuthority)/items".\r
+ * the correct urn path element, without any path delimiters such as '/' so\r
+ * that calling shortIdToPath("TestAuthority") returns\r
+ * "urn:cspace:name(TestAuthority)", and then this value may be put into a\r
+ * path, such as "/personauthorities/urn:cspace:name(TestAuthority)/items".\r
*/\r
public static String shortIdToPath(String shortId) {\r
return URN_NAME_PREFIX + '(' + shortId + ')';\r
}\r
+\r
+ /**\r
+ * Glue to create an AuthorityTermInfo object, used in RefNameUtils, from the\r
+ * highly similar AuthorityItem object, used in this class.\r
+ *\r
+ * @param termInfo an AuthorityTermInfo object\r
+ * @return an AuthorityItem object\r
+ */\r
+ private static AuthorityItem authorityItemFromTermInfo(AuthorityTermInfo termInfo) {\r
+ if (termInfo == null) {\r
+ return null;\r
+ }\r
+ AuthorityItem authorityItem = new AuthorityItem();\r
+ authorityItem.inAuthority =\r
+ authorityFromAuthorityInfo(termInfo.inAuthority, false);\r
+ if (termInfo.name != null\r
+ && !termInfo.name.trim().isEmpty()) {\r
+ authorityItem.shortIdentifier = termInfo.name;\r
+ } else {\r
+ authorityItem.shortIdentifier = termInfo.csid;\r
+ }\r
+ authorityItem.displayName = termInfo.displayName;\r
+ return authorityItem;\r
+ }\r
+\r
+ /**\r
+ * Glue to create an AuthorityInfo object, used in RefNameUtils, from the\r
+ * highly similar Authority object, used in this class.\r
+ *\r
+ * @param authorityInfo an AuthorityInfo object\r
+ * @param includeDisplayName true to include the display name during creation;\r
+ * false to exclude it.\r
+ * @return an Authority object\r
+ */\r
+ private static Authority authorityFromAuthorityInfo(AuthorityInfo authorityInfo,\r
+ boolean includeDisplayName) {\r
+ if (authorityInfo == null) {\r
+ return null;\r
+ }\r
+ Authority authority = new Authority();\r
+ authority.tenantName = authorityInfo.domain;\r
+ authority.resource = authorityInfo.resource;\r
+ if (authorityInfo.name != null\r
+ && !authorityInfo.name.trim().isEmpty()) {\r
+ authority.shortIdentifier = authorityInfo.name;\r
+ } else {\r
+ authority.shortIdentifier = authorityInfo.csid;\r
+ }\r
+ if (includeDisplayName) {\r
+ authority.displayName = authorityInfo.displayName;\r
+ }\r
+ return authority;\r
+ }\r
}\r