001/*
002 * Copyright © 2012, 2013, 2014 Royal Botanic Gardens, Kew.
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
005 *
006 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
007 *
008 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
009 */
010package org.kew.rmf.transformers.collations;
011
012import org.kew.rmf.transformers.Transformer;
013
014public class CollationStructureTransformer implements Transformer{
015
016        @Override
017        public String transform(String s) {
018                return assessCollationStructure(s);
019        }
020
021        private static char[] DASHES = {'\u2012', '\u2013', '\u2014'};
022        
023        // This converts the extended characters to a normalised equivalent which is more easy to work with: 
024        //      accented e 
025        //      emdashes / emrules (sometimes used in place of hyphens).
026        private static String convertExtChars(String str){
027                for (char c : DASHES)
028                        while (str.indexOf(c)!=-1)
029                                str = str.replace(c, '-');
030                while (str.indexOf('\u00E9')!=-1)
031                        str = str.replace('\u00E9', 'e');               
032                return str;
033        }
034        
035        public static String[] splitCollation(String collation){
036                return convertExtChars(collation).split("[^A-Za-z\u00E90-9\\-]+");
037        }
038        
039        public static String assessCollationStructure(String collation){
040                String c_structure = null;
041                if (collation != null){
042                        c_structure  = convertExtChars(collation.toLowerCase());
043                        c_structure=c_structure.replaceAll("1[7-9][0-9][0-9]","YYYY");
044                        c_structure=c_structure.replaceAll("20[0-1][0-9]","YYYY");
045                        c_structure=c_structure.replaceAll("[0-9]+","D");
046                        c_structure=c_structure.replaceAll("[ivxlc]+","R");
047                        c_structure=c_structure.replaceAll("[a-z\u00E9]+","A");
048                        c_structure=c_structure.replaceAll("RA","A");
049                        c_structure=c_structure.replaceAll("AR","A");
050                        c_structure=c_structure.replaceAll("A+","A");
051                        // Treat things like 21A as a "number":
052                        c_structure=c_structure.replaceAll("DA","D");
053                        // And treat things like 1891A as a "number":
054                        c_structure=c_structure.replaceAll("YYYYA","D");                        
055                        // ...and things like 21C as a "number":
056                        c_structure=c_structure.replaceAll("DR","D");                   
057                        // Treat numeric ranges as a single number:
058                        c_structure=c_structure.replaceAll("D\\-D","D");
059                        c_structure=c_structure.toLowerCase();
060                }
061                return c_structure;
062        }
063}