From: Aron Roberts Date: Tue, 5 Jun 2012 00:11:23 +0000 (-0700) Subject: CSPACE-5301: refName parsing for authority items now uses tokenization code exclusive... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=72f4a309f6bcdf96054f755befc247f3f38aa07d;p=tmp%2Fjakarta-migration.git CSPACE-5301: refName parsing for authority items now uses tokenization code exclusively, rather then tokenization code in some code path(s) and regex code in other(s). --- diff --git a/services/common-api/src/main/java/org/collectionspace/services/common/api/RefName.java b/services/common-api/src/main/java/org/collectionspace/services/common/api/RefName.java index 3ed0c33b7..5485b6bfa 100644 --- a/services/common-api/src/main/java/org/collectionspace/services/common/api/RefName.java +++ b/services/common-api/src/main/java/org/collectionspace/services/common/api/RefName.java @@ -2,7 +2,7 @@ package org.collectionspace.services.common.api; import java.util.regex.Matcher; import java.util.regex.Pattern; - +import org.collectionspace.services.common.api.RefNameUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,7 +53,7 @@ public class RefName { public static final String URN_NAME_PREFIX = "urn:cspace:name"; public static final String REFNAME = "refName"; public static final String AUTHORITY_REGEX = "urn:cspace:(.*):(.*):name\\((.*)\\)\\'?([^\\']*)\\'?"; - public static final String AUTHORITY_ITEM_REGEX = "urn:cspace:(.*):(.*):name\\((.*)\\):item:name\\((.*)\\)\\'?([^\\']*)\\'?"; + public static final String AUTHORITY_ITEM_REGEX = "urn:cspace:(.*):(.*):name\\((.*)\\):item:name\\((.*)\\)\\'?([^\\']*)\\'"; public static final String AUTHORITY_EXAMPLE = "urn:cspace:collectionspace.org:Loansin:name(shortID)'displayName'"; public static final String AUTHORITY_EXAMPLE2 = "urn:cspace:collectionspace.org:Loansin:name(shortID)"; public static final String AUTHORITY_ITEM_EXAMPLE = "urn:cspace:collectionspace.org:Loansin:name(shortID):item:name(itemShortID)'itemDisplayName'"; @@ -123,26 +123,32 @@ public class RefName { public String displayName = ""; public static AuthorityItem parse(String urn) { - Authority info = new Authority(); - AuthorityItem termInfo = new AuthorityItem(); - termInfo.inAuthority = info; - Pattern p = Pattern.compile(AUTHORITY_ITEM_REGEX); - Matcher m = p.matcher(urn); - if (m.find()) { - if (m.groupCount() < 5) { - if (m.groupCount() == 4 && logger.isDebugEnabled()) { - logger.debug("AuthorityItem.parse only found 4 items; Missing displayName? Urn:"+urn); - } - return null; + Authority authority = new Authority(); + AuthorityItem authorityItem = new AuthorityItem(); + try { + RefNameUtils.AuthorityTermInfo termInfo = RefNameUtils.parseAuthorityTermInfo(urn); + + authority.tenantName = termInfo.inAuthority.domain; + authority.resource = termInfo.inAuthority.resource; + if (termInfo.inAuthority.name != null && + ! termInfo.inAuthority.name.trim().isEmpty()) { + authority.shortIdentifier = termInfo.inAuthority.name; + } else { + authority.shortIdentifier = termInfo.inAuthority.csid; + } + authorityItem.inAuthority = authority; + + if (termInfo.name != null && + ! termInfo.name.trim().isEmpty()) { + authorityItem.shortIdentifier = termInfo.name; + } else { + authorityItem.shortIdentifier = termInfo.csid; } - termInfo.inAuthority.tenantName = m.group(1); - termInfo.inAuthority.resource = m.group(2); - termInfo.inAuthority.shortIdentifier = m.group(3); - termInfo.shortIdentifier = m.group(4); - termInfo.displayName = m.group(5); - return termInfo; + authorityItem.displayName = termInfo.displayName; + } catch (IllegalArgumentException iae) { + return null; } - return null; + return authorityItem; } public String getParentShortIdentifier() { diff --git a/services/common/src/main/java/org/collectionspace/services/common/vocabulary/RefNameUtils.java b/services/common-api/src/main/java/org/collectionspace/services/common/api/RefNameUtils.java 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 920261c45..17f6f36da 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/vocabulary/RefNameUtils.java +++ b/services/common-api/src/main/java/org/collectionspace/services/common/api/RefNameUtils.java @@ -1,256 +1,256 @@ -/** - * 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.vocabulary; - -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