]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5301: refName parsing for authority items now uses tokenization code exclusive...
authorAron Roberts <aron@socrates.berkeley.edu>
Tue, 5 Jun 2012 00:11:23 +0000 (17:11 -0700)
committerAron Roberts <aron@socrates.berkeley.edu>
Tue, 5 Jun 2012 00:11:23 +0000 (17:11 -0700)
services/common-api/src/main/java/org/collectionspace/services/common/api/RefName.java
services/common-api/src/main/java/org/collectionspace/services/common/api/RefNameUtils.java [moved from services/common/src/main/java/org/collectionspace/services/common/vocabulary/RefNameUtils.java with 97% similarity]
services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteDocumentModelHandlerImpl.java

index 3ed0c33b78b766587b824703ae15ab6bcd4a5722..5485b6bfa34babb39ef090cd391117d0a801cee1 100644 (file)
@@ -2,7 +2,7 @@ package org.collectionspace.services.common.api;
 \r
 import java.util.regex.Matcher;\r
 import java.util.regex.Pattern;\r
-\r
+import org.collectionspace.services.common.api.RefNameUtils;\r
 import org.slf4j.Logger;\r
 import org.slf4j.LoggerFactory;\r
 \r
@@ -53,7 +53,7 @@ public class RefName {
     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_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
@@ -123,26 +123,32 @@ public class RefName {
         public String displayName = "";\r
 \r
         public static AuthorityItem parse(String urn) {\r
-            Authority info = new Authority();\r
-            AuthorityItem termInfo = new AuthorityItem();\r
-            termInfo.inAuthority = info;\r
-            Pattern p = Pattern.compile(AUTHORITY_ITEM_REGEX);\r
-            Matcher m = p.matcher(urn);\r
-            if (m.find()) {\r
-                if (m.groupCount() < 5) {\r
-                       if (m.groupCount() == 4 && logger.isDebugEnabled()) {\r
-                               logger.debug("AuthorityItem.parse only found 4 items; Missing displayName? Urn:"+urn);\r
-                       }\r
-                    return null;\r
+            Authority authority = new Authority();\r
+            AuthorityItem authorityItem = new AuthorityItem();\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
-                termInfo.inAuthority.tenantName = m.group(1);\r
-                termInfo.inAuthority.resource = m.group(2);\r
-                termInfo.inAuthority.shortIdentifier = m.group(3);\r
-                termInfo.shortIdentifier = m.group(4);\r
-                termInfo.displayName = m.group(5);\r
-                return termInfo;\r
+                authorityItem.displayName = termInfo.displayName;\r
+            } catch (IllegalArgumentException iae) {\r
+                return null;\r
             }\r
-            return null;\r
+            return authorityItem;\r
         }\r
         \r
         public String getParentShortIdentifier() {\r
similarity index 97%
rename from services/common/src/main/java/org/collectionspace/services/common/vocabulary/RefNameUtils.java
rename to services/common-api/src/main/java/org/collectionspace/services/common/api/RefNameUtils.java
index 920261c45f5d10b83f1de052b0ce4834bdd5326a..17f6f36dac376c5a437b6888e18d439bf092cb4d 100644 (file)
-/**\r
- *  This document is a part of the source code and related artifacts\r
- *  for CollectionSpace, an open source collections management system\r
- *  for museums and related institutions:\r
-\r
- *  http://www.collectionspace.org\r
- *  http://wiki.collectionspace.org\r
-\r
- *  Copyright 2009 University of California at Berkeley\r
-\r
- *  Licensed under the Educational Community License (ECL), Version 2.0.\r
- *  You may not use this file except in compliance with this License.\r
-\r
- *  You may obtain a copy of the ECL 2.0 License at\r
-\r
- *  https://source.collectionspace.org/collection-space/LICENSE.txt\r
-\r
- *  Unless required by applicable law or agreed to in writing, software\r
- *  distributed under the License is distributed on an "AS IS" BASIS,\r
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- *  See the License for the specific language governing permissions and\r
- *  limitations under the license.\r
- */\r
-package org.collectionspace.services.common.vocabulary;\r
-\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-/**\r
- * RefNameUtils is a collection of utilities related to refName URN strings\r
- * refNames are URNs that reference a document entity, often an authority or \r
- * authority term. They are strings that take the form (for authorities):\r
- * urn:cspace:org.collectionspace.demo:vocabulary:name(Entry Methods)'Entry Methods'\r
- * or the form (for authority terms):\r
- * urn:cspace:org.collectionspace.demo:vocabulary:name(Entry Methods):item:name(Loan)'Loan'\r
- *\r
- * $LastChangedRevision: $\r
- * $LastChangedDate: $\r
- */\r
-public class RefNameUtils {\r
-\r
-    private final Logger logger = LoggerFactory.getLogger(RefNameUtils.class);\r
-\r
-    public static final String URN_PREFIX = "urn:cspace:";\r
-    public static final int URN_PREFIX_LEN = 11;\r
-    public static final String URN_NAME_PREFIX = "urn:cspace:name(";\r
-    public static final int URN_NAME_PREFIX_LEN = 16;\r
-       // FIXME Should not be hard-coded\r
-    private static final String ITEMS_REGEX = "item|person|organization";\r
-    // In a list of tokens, these are indices for each part\r
-    private static final int DOMAIN_TOKEN = 0;                         // e.g., 'org.collectionspace.demo'\r
-    private static final int RESOURCE_TOKEN = 1;               // vocabulary, personauthority, etc.\r
-    private static final int AUTH_INSTANCE_TOKEN = 2;  // name(Entry Methods)'Entry Methods'\r
-    private static final int ITEMS_TOKEN = 3;                  // 'item', 'person', etc.\r
-    private static final int ITEM_INSTANCE_TOKEN = 4;  // name(Entry Methods)'Entry Methods'\r
-    private static final int AUTH_REFNAME_TOKENS = 3;  // domain, resource, auth\r
-    private static final int AUTH_ITEM_REFNAME_TOKENS = 5;     // domain, resource, auth, "items", item\r
-    // Tokenizing the INSTANCE, these are indices for each item-part\r
-    private static final int INSTANCE_SPEC_TYPE_TOKEN = 0;     // 'name' or 'id' \r
-    private static final int INSTANCE_SPEC_TOKEN = 1;          // name or id value\r
-    private static final int INSTANCE_DISPLAYNAME_TOKEN = 2;// optional displayName suffix\r
-    private static final int INSTANCE_TOKENS_MIN = 2;\r
-    private static final int INSTANCE_TOKENS_MAX = 3;\r
-    public static final String SEPARATOR = ":";\r
-\r
-    public static class AuthorityInfo {\r
-        private final Logger logger = LoggerFactory.getLogger(AuthorityInfo.class);\r
-       private static int MIN_TOKENS = 3;\r
-        public String domain;\r
-        public String resource;\r
-        public String csid;\r
-        public String name;\r
-        public String displayName;\r
-        \r
-        public AuthorityInfo(String refNameTokens[]) throws IllegalArgumentException {\r
-               try {\r
-                       if(refNameTokens.length < MIN_TOKENS) {\r
-                               throw new IllegalArgumentException("Malformed refName for Authority (too few tokens)");\r
-                       }\r
-                       this.domain = refNameTokens[DOMAIN_TOKEN]; \r
-                       this.resource = refNameTokens[RESOURCE_TOKEN];\r
-                       String idTokens[] = refNameTokens[AUTH_INSTANCE_TOKEN].split("[()]", INSTANCE_TOKENS_MAX);\r
-                       if(idTokens.length<INSTANCE_TOKENS_MIN) {\r
-                               throw new IllegalArgumentException("Missing/malformed identifier");\r
-                       }\r
-                       if(idTokens[INSTANCE_SPEC_TYPE_TOKEN].equals("name")) {\r
-                               this.name = idTokens[INSTANCE_SPEC_TOKEN];\r
-                               this.csid = null;\r
-                       } else if(idTokens[INSTANCE_SPEC_TYPE_TOKEN].startsWith("id")) {\r
-                               this.csid = idTokens[INSTANCE_SPEC_TOKEN];\r
-                               this.name = null;\r
-                       } else {\r
-                               throw new IllegalArgumentException("Identifier type must be 'name' or 'id'");\r
-                       }\r
-                       // displayName is always in quotes, so must have at least 3 chars\r
-                       this.displayName = \r
-                               ((idTokens.length<INSTANCE_TOKENS_MAX)||(idTokens[INSTANCE_DISPLAYNAME_TOKEN].length()<3))? null:\r
-                               idTokens[INSTANCE_DISPLAYNAME_TOKEN].substring(1, idTokens[INSTANCE_DISPLAYNAME_TOKEN].length()-1);\r
-               } catch (IllegalArgumentException e) {\r
-                   if (logger.isDebugEnabled()) {\r
-                       logger.debug("Problem Building AuthorityInfo from tokens: " \r
-                                       + RefNameUtils.implodeStringArray(refNameTokens, ", "));\r
-                   }\r
-                   throw e;\r
-               }\r
-        }\r
-        \r
-        public String getRelativeUri() {\r
-               StringBuilder uri = new StringBuilder();\r
-               // FIXME This should not be hard-coded.\r
-               if(resource.equals("vocabulary")) {\r
-                       uri.append("/vocabularies/");\r
-               } else if(resource.equals("personauthority")) {\r
-                       uri.append("/personauthorities/");\r
-               } else if(resource.equals("orgauthority")) {\r
-                       uri.append("/orgauthorities/");\r
-               } else {\r
-                       if(!(resource.equals("orgauthorities")\r
-                               || resource.equals("personauthorities")\r
-                               || resource.equals("locationauthorities")\r
-                               || resource.equals("placeauthorities")\r
-                               || resource.equals("vocabularies"))) {  \r
-                               logger.error("Unrecognized Authority Type: " + resource);\r
-                       }\r
-                       uri.append("/"+resource+"/");\r
-               }\r
-               if(csid!=null) {\r
-                       uri.append(csid);\r
-               } else if(name!=null) {\r
-                       uri.append("urn:cspace:name("+name+")");\r
-               } else {\r
-                       throw new RuntimeException("Missing id/name specifier");\r
-               }\r
-               return uri.toString();\r
-        }\r
-    };\r
-\r
-    public static class AuthorityTermInfo {\r
-        private final Logger logger = LoggerFactory.getLogger(AuthorityTermInfo.class);\r
-       private static int MIN_TOKENS = 5;\r
-       public AuthorityInfo inAuthority;\r
-       public String csid;\r
-       public String name;\r
-       public String displayName;\r
-        \r
-        public AuthorityTermInfo(String refNameTokens[]) throws IllegalArgumentException {\r
-               try {\r
-                       if(refNameTokens.length < MIN_TOKENS) {\r
-                               throw new IllegalArgumentException("Malformed refName for AuthorityTerm (too few tokens)");\r
-                       }\r
-                       this.inAuthority = new AuthorityInfo(refNameTokens); \r
-                       if(!refNameTokens[ITEMS_TOKEN].matches(ITEMS_REGEX)) {\r
-                               throw new IllegalArgumentException("Item spec must be one of: "+ITEMS_REGEX);\r
-                       }\r
-                       String idTokens[] = refNameTokens[ITEM_INSTANCE_TOKEN].split("[\\(\\)]", 3);\r
-                       if(idTokens.length<INSTANCE_TOKENS_MIN) {\r
-                               throw new IllegalArgumentException("Missing/malformed identifier");\r
-                       }\r
-                       if(idTokens[INSTANCE_SPEC_TYPE_TOKEN].equals("name")) {\r
-                               this.name = idTokens[INSTANCE_SPEC_TOKEN];\r
-                               this.csid = null;\r
-                       } else if(idTokens[INSTANCE_SPEC_TYPE_TOKEN].startsWith("id")) {\r
-                               this.csid = idTokens[INSTANCE_SPEC_TOKEN];\r
-                               this.name = null;\r
-                       } else {\r
-                               throw new IllegalArgumentException("Identifier type must be 'name' or 'id'");\r
-                       }\r
-                       // displayName is always in quotes, so must have at least 3 chars\r
-                       this.displayName = \r
-                               ((idTokens.length<INSTANCE_TOKENS_MAX)||(idTokens[INSTANCE_DISPLAYNAME_TOKEN].length()<3))? null:\r
-                               idTokens[INSTANCE_DISPLAYNAME_TOKEN].substring(1, idTokens[INSTANCE_DISPLAYNAME_TOKEN].length()-1);\r
-               } catch (IllegalArgumentException e) {\r
-                   if (logger.isDebugEnabled()) {\r
-                       logger.debug("Problem Building AuthorityTermInfo from tokens: " \r
-                                       + RefNameUtils.implodeStringArray(refNameTokens, ", "));\r
-                   }\r
-                   throw e;\r
-               }\r
-        }\r
-        \r
-        public String getRelativeUri() {\r
-               StringBuilder uri = new StringBuilder(inAuthority.getRelativeUri()+"/items/");\r
-               if(csid!=null) {\r
-                       uri.append(csid);\r
-               } else if(name!=null) {\r
-                       uri.append("urn:cspace:name("+name+")");\r
-               } else {\r
-                       throw new RuntimeException("Missing id/name specifier");\r
-               }\r
-               return uri.toString();\r
-        }\r
-    };\r
-\r
-    public static AuthorityInfo parseAuthorityInfo(String refName)\r
-            throws IllegalArgumentException {\r
-       if(refName==null || !refName.startsWith(URN_PREFIX))\r
-               throw new IllegalArgumentException( "Null or invalid refName syntax");\r
-       String[] refNameTokens = refName.substring(URN_PREFIX_LEN).split(SEPARATOR, AUTH_REFNAME_TOKENS);\r
-       return new AuthorityInfo(refNameTokens);\r
-    }\r
-\r
-    public static AuthorityTermInfo parseAuthorityTermInfo(String refName)\r
-            throws IllegalArgumentException {\r
-       if(refName==null || !refName.startsWith(URN_PREFIX))\r
-               throw new IllegalArgumentException( "Null or invalid refName syntax");\r
-       String[] refNameTokens = refName.substring(URN_PREFIX_LEN).split(SEPARATOR, AUTH_ITEM_REFNAME_TOKENS);\r
-       return new AuthorityTermInfo(refNameTokens);\r
-    }\r
-\r
-    public static String implodeStringArray(String tokens[], String separator) {\r
-       if (tokens.length==0) {\r
-               return "";\r
-       } else {\r
-               StringBuffer sb = new StringBuffer();\r
-               sb.append(tokens[0]);\r
-               for (int i=1;i<tokens.length;i++) {\r
-                       sb.append(separator);\r
-                       sb.append(tokens[i]);\r
-               }\r
-               return sb.toString();\r
-       }\r
-    }\r
-\r
-    /*\r
-     * Returns the name / shortIdentifier value of an authority item in a refName\r
-     */\r
-    public static String getItemShortId(String refName) {\r
-        String name = "";\r
-        try {\r
-               String[] refNameTokens = refName.substring(URN_PREFIX_LEN).split(SEPARATOR, AUTH_ITEM_REFNAME_TOKENS);\r
-            AuthorityTermInfo authTermInfo = new AuthorityTermInfo(refNameTokens);\r
-            name = authTermInfo.name;\r
-        } catch(Exception e) {\r
-            // do nothing\r
-        }\r
-        return name;\r
-    }\r
-\r
-    /**\r
-     * Creates a refName in the name / shortIdentifier form.\r
-     *\r
-     * @param shortId a shortIdentifier for an authority or one of its terms\r
-     * @return a refName for that authority or term, in the name / shortIdentifier form.\r
-     *         If the provided shortIdentifier is null or empty, returns\r
-     *         the empty string.\r
-     */\r
-    public static String createShortIdRefName(String shortId) {\r
-        if (shortId == null || shortId.trim().isEmpty()) {\r
-            return "";\r
-        } else {\r
-            return "urn:cspace:name("+shortId+")";\r
-        }\r
-    }\r
-    \r
-}\r
-\r
+/**
+ *  This document is a part of the source code and related artifacts
+ *  for CollectionSpace, an open source collections management system
+ *  for museums and related institutions:
+
+ *  http://www.collectionspace.org
+ *  http://wiki.collectionspace.org
+
+ *  Copyright 2009 University of California at Berkeley
+
+ *  Licensed under the Educational Community License (ECL), Version 2.0.
+ *  You may not use this file except in compliance with this License.
+
+ *  You may obtain a copy of the ECL 2.0 License at
+
+ *  https://source.collectionspace.org/collection-space/LICENSE.txt
+
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the license.
+ */
+package org.collectionspace.services.common.api;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * RefNameUtils is a collection of utilities related to refName URN strings
+ * refNames are URNs that reference a document entity, often an authority or 
+ * authority term. They are strings that take the form (for authorities):
+ * urn:cspace:org.collectionspace.demo:vocabulary:name(Entry Methods)'Entry Methods'
+ * or the form (for authority terms):
+ * urn:cspace:org.collectionspace.demo:vocabulary:name(Entry Methods):item:name(Loan)'Loan'
+ *
+ * $LastChangedRevision: $
+ * $LastChangedDate: $
+ */
+public class RefNameUtils {
+
+    private final Logger logger = LoggerFactory.getLogger(RefNameUtils.class);
+
+    public static final String URN_PREFIX = "urn:cspace:";
+    public static final int URN_PREFIX_LEN = 11;
+    public static final String URN_NAME_PREFIX = "urn:cspace:name(";
+    public static final int URN_NAME_PREFIX_LEN = 16;
+       // FIXME Should not be hard-coded
+    private static final String ITEMS_REGEX = "item|person|organization";
+    // In a list of tokens, these are indices for each part
+    private static final int DOMAIN_TOKEN = 0;                         // e.g., 'org.collectionspace.demo'
+    private static final int RESOURCE_TOKEN = 1;               // vocabulary, personauthority, etc.
+    private static final int AUTH_INSTANCE_TOKEN = 2;  // name(Entry Methods)'Entry Methods'
+    private static final int ITEMS_TOKEN = 3;                  // 'item', 'person', etc.
+    private static final int ITEM_INSTANCE_TOKEN = 4;  // name(Entry Methods)'Entry Methods'
+    private static final int AUTH_REFNAME_TOKENS = 3;  // domain, resource, auth
+    private static final int AUTH_ITEM_REFNAME_TOKENS = 5;     // domain, resource, auth, "items", item
+    // Tokenizing the INSTANCE, these are indices for each item-part
+    private static final int INSTANCE_SPEC_TYPE_TOKEN = 0;     // 'name' or 'id' 
+    private static final int INSTANCE_SPEC_TOKEN = 1;          // name or id value
+    private static final int INSTANCE_DISPLAYNAME_TOKEN = 2;// optional displayName suffix
+    private static final int INSTANCE_TOKENS_MIN = 2;
+    private static final int INSTANCE_TOKENS_MAX = 3;
+    public static final String SEPARATOR = ":";
+
+    public static class AuthorityInfo {
+        private final Logger logger = LoggerFactory.getLogger(AuthorityInfo.class);
+       private static int MIN_TOKENS = 3;
+        public String domain;
+        public String resource;
+        public String csid;
+        public String name;
+        public String displayName;
+        
+        public AuthorityInfo(String refNameTokens[]) throws IllegalArgumentException {
+               try {
+                       if(refNameTokens.length < MIN_TOKENS) {
+                               throw new IllegalArgumentException("Malformed refName for Authority (too few tokens)");
+                       }
+                       this.domain = refNameTokens[DOMAIN_TOKEN]; 
+                       this.resource = refNameTokens[RESOURCE_TOKEN];
+                       String idTokens[] = refNameTokens[AUTH_INSTANCE_TOKEN].split("[()]", INSTANCE_TOKENS_MAX);
+                       if(idTokens.length<INSTANCE_TOKENS_MIN) {
+                               throw new IllegalArgumentException("Missing/malformed identifier");
+                       }
+                       if(idTokens[INSTANCE_SPEC_TYPE_TOKEN].equals("name")) {
+                               this.name = idTokens[INSTANCE_SPEC_TOKEN];
+                               this.csid = null;
+                       } else if(idTokens[INSTANCE_SPEC_TYPE_TOKEN].startsWith("id")) {
+                               this.csid = idTokens[INSTANCE_SPEC_TOKEN];
+                               this.name = null;
+                       } else {
+                               throw new IllegalArgumentException("Identifier type must be 'name' or 'id'");
+                       }
+                       // displayName is always in quotes, so must have at least 3 chars
+                       this.displayName = 
+                               ((idTokens.length<INSTANCE_TOKENS_MAX)||(idTokens[INSTANCE_DISPLAYNAME_TOKEN].length()<3))? null:
+                               idTokens[INSTANCE_DISPLAYNAME_TOKEN].substring(1, idTokens[INSTANCE_DISPLAYNAME_TOKEN].length()-1);
+               } catch (IllegalArgumentException e) {
+                   if (logger.isDebugEnabled()) {
+                       logger.debug("Problem Building AuthorityInfo from tokens: " 
+                                       + RefNameUtils.implodeStringArray(refNameTokens, ", "));
+                   }
+                   throw e;
+               }
+        }
+        
+        public String getRelativeUri() {
+               StringBuilder uri = new StringBuilder();
+               // FIXME This should not be hard-coded.
+               if(resource.equals("vocabulary")) {
+                       uri.append("/vocabularies/");
+               } else if(resource.equals("personauthority")) {
+                       uri.append("/personauthorities/");
+               } else if(resource.equals("orgauthority")) {
+                       uri.append("/orgauthorities/");
+               } else {
+                       if(!(resource.equals("orgauthorities")
+                               || resource.equals("personauthorities")
+                               || resource.equals("locationauthorities")
+                               || resource.equals("placeauthorities")
+                               || resource.equals("vocabularies"))) {  
+                               logger.error("Unrecognized Authority Type: " + resource);
+                       }
+                       uri.append("/"+resource+"/");
+               }
+               if(csid!=null) {
+                       uri.append(csid);
+               } else if(name!=null) {
+                       uri.append("urn:cspace:name("+name+")");
+               } else {
+                       throw new RuntimeException("Missing id/name specifier");
+               }
+               return uri.toString();
+        }
+    };
+
+    public static class AuthorityTermInfo {
+        private final Logger logger = LoggerFactory.getLogger(AuthorityTermInfo.class);
+       private static int MIN_TOKENS = 5;
+       public AuthorityInfo inAuthority;
+       public String csid;
+       public String name;
+       public String displayName;
+        
+        public AuthorityTermInfo(String refNameTokens[]) throws IllegalArgumentException {
+               try {
+                       if(refNameTokens.length < MIN_TOKENS) {
+                               throw new IllegalArgumentException("Malformed refName for AuthorityTerm (too few tokens)");
+                       }
+                       this.inAuthority = new AuthorityInfo(refNameTokens); 
+                       if(!refNameTokens[ITEMS_TOKEN].matches(ITEMS_REGEX)) {
+                               throw new IllegalArgumentException("Item spec must be one of: "+ITEMS_REGEX);
+                       }
+                       String idTokens[] = refNameTokens[ITEM_INSTANCE_TOKEN].split("[\\(\\)]", 3);
+                       if(idTokens.length<INSTANCE_TOKENS_MIN) {
+                               throw new IllegalArgumentException("Missing/malformed identifier");
+                       }
+                       if(idTokens[INSTANCE_SPEC_TYPE_TOKEN].equals("name")) {
+                               this.name = idTokens[INSTANCE_SPEC_TOKEN];
+                               this.csid = null;
+                       } else if(idTokens[INSTANCE_SPEC_TYPE_TOKEN].startsWith("id")) {
+                               this.csid = idTokens[INSTANCE_SPEC_TOKEN];
+                               this.name = null;
+                       } else {
+                               throw new IllegalArgumentException("Identifier type must be 'name' or 'id'");
+                       }
+                       // displayName is always in quotes, so must have at least 3 chars
+                       this.displayName = 
+                               ((idTokens.length<INSTANCE_TOKENS_MAX)||(idTokens[INSTANCE_DISPLAYNAME_TOKEN].length()<3))? null:
+                               idTokens[INSTANCE_DISPLAYNAME_TOKEN].substring(1, idTokens[INSTANCE_DISPLAYNAME_TOKEN].length()-1);
+               } catch (IllegalArgumentException e) {
+                   if (logger.isDebugEnabled()) {
+                       logger.debug("Problem Building AuthorityTermInfo from tokens: " 
+                                       + RefNameUtils.implodeStringArray(refNameTokens, ", "));
+                   }
+                   throw e;
+               }
+        }
+        
+        public String getRelativeUri() {
+               StringBuilder uri = new StringBuilder(inAuthority.getRelativeUri()+"/items/");
+               if(csid!=null) {
+                       uri.append(csid);
+               } else if(name!=null) {
+                       uri.append("urn:cspace:name("+name+")");
+               } else {
+                       throw new RuntimeException("Missing id/name specifier");
+               }
+               return uri.toString();
+        }
+    };
+
+    public static AuthorityInfo parseAuthorityInfo(String refName)
+            throws IllegalArgumentException {
+       if(refName==null || !refName.startsWith(URN_PREFIX))
+               throw new IllegalArgumentException( "Null or invalid refName syntax");
+       String[] refNameTokens = refName.substring(URN_PREFIX_LEN).split(SEPARATOR, AUTH_REFNAME_TOKENS);
+       return new AuthorityInfo(refNameTokens);
+    }
+
+    public static AuthorityTermInfo parseAuthorityTermInfo(String refName)
+            throws IllegalArgumentException {
+       if(refName==null || !refName.startsWith(URN_PREFIX))
+               throw new IllegalArgumentException( "Null or invalid refName syntax");
+       String[] refNameTokens = refName.substring(URN_PREFIX_LEN).split(SEPARATOR, AUTH_ITEM_REFNAME_TOKENS);
+       return new AuthorityTermInfo(refNameTokens);
+    }
+
+    public static String implodeStringArray(String tokens[], String separator) {
+       if (tokens.length==0) {
+               return "";
+       } else {
+               StringBuffer sb = new StringBuffer();
+               sb.append(tokens[0]);
+               for (int i=1;i<tokens.length;i++) {
+                       sb.append(separator);
+                       sb.append(tokens[i]);
+               }
+               return sb.toString();
+       }
+    }
+
+    /*
+     * Returns the name / shortIdentifier value of an authority item in a refName
+     */
+    public static String getItemShortId(String refName) {
+        String name = "";
+        try {
+               String[] refNameTokens = refName.substring(URN_PREFIX_LEN).split(SEPARATOR, AUTH_ITEM_REFNAME_TOKENS);
+            AuthorityTermInfo authTermInfo = new AuthorityTermInfo(refNameTokens);
+            name = authTermInfo.name;
+        } catch(Exception e) {
+            // do nothing
+        }
+        return name;
+    }
+
+    /**
+     * Creates a refName in the name / shortIdentifier form.
+     *
+     * @param shortId a shortIdentifier for an authority or one of its terms
+     * @return a refName for that authority or term, in the name / shortIdentifier form.
+     *         If the provided shortIdentifier is null or empty, returns
+     *         the empty string.
+     */
+    public static String createShortIdRefName(String shortId) {
+        if (shortId == null || shortId.trim().isEmpty()) {
+            return "";
+        } else {
+            return "urn:cspace:name("+shortId+")";
+        }
+    }
+    
+}
+
index 3e5dc322a009d750943c498ead894ef16d66dc2d..68b3a6e0dde926622ac861cd716526850281ef27 100644 (file)
@@ -55,7 +55,7 @@ import org.collectionspace.services.common.document.DocumentFilter;
 import org.collectionspace.services.common.profile.Profiler;
 import org.collectionspace.services.common.security.SecurityUtils;
 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
-import org.collectionspace.services.common.vocabulary.RefNameUtils;
+import org.collectionspace.services.common.api.RefNameUtils;
 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils;
 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.AuthRefConfigInfo;
 import org.collectionspace.services.config.service.ListResultField;