001package com.hfg.citation; 002 003 004 005import com.hfg.util.StringBuilderPlus; 006import com.hfg.util.StringUtil; 007 008//------------------------------------------------------------------------------ 009/** 010 Author object. 011 <div> 012 @author J. Alex Taylor, hairyfatguy.com 013 </div> 014 */ 015//------------------------------------------------------------------------------ 016// com.hfg XML/HTML Coding Library 017// 018// This library is free software; you can redistribute it and/or 019// modify it under the terms of the GNU Lesser General Public 020// License as published by the Free Software Foundation; either 021// version 2.1 of the License, or (at your option) any later version. 022// 023// This library is distributed in the hope that it will be useful, 024// but WITHOUT ANY WARRANTY; without even the implied warranty of 025// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 026// Lesser General Public License for more details. 027// 028// You should have received a copy of the GNU Lesser General Public 029// License along with this library; if not, write to the Free Software 030// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 031// 032// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 033// jataylor@hairyfatguy.com 034//------------------------------------------------------------------------------ 035 036public class Author 037{ 038 private String mRawName; // Un-parsed name 039 private String mFirstName; 040 private String mMiddleName; 041 private String mLastName; 042 043 //########################################################################### 044 // CONSTRUCTORS 045 //########################################################################### 046 047 //--------------------------------------------------------------------------- 048 public Author() 049 { 050 } 051 052 //--------------------------------------------------------------------------- 053 public Author(String inValue) 054 { 055 mRawName = inValue; 056 057 parse(inValue); 058 } 059 060 061 //########################################################################### 062 // PUBLIC METHODS 063 //########################################################################### 064 065 //--------------------------------------------------------------------------- 066 @Override 067 public String toString() 068 { 069 return (StringUtil.isSet(mRawName) ? mRawName : buildName()); 070 } 071 072 //--------------------------------------------------------------------------- 073 public Author setFirstName(String inValue) 074 { 075 mFirstName = inValue; 076 return this; 077 } 078 079 //--------------------------------------------------------------------------- 080 public String getFirstName() 081 { 082 return mFirstName; 083 } 084 085 //--------------------------------------------------------------------------- 086 public Character getFirstInitial() 087 { 088 return StringUtil.isSet(mFirstName) ? Character.toUpperCase(mFirstName.charAt(0)) : null; 089 } 090 091 092 //--------------------------------------------------------------------------- 093 public Author setMiddleName(String inValue) 094 { 095 mMiddleName = inValue; 096 return this; 097 } 098 099 //--------------------------------------------------------------------------- 100 public String getMiddleName() 101 { 102 return mMiddleName; 103 } 104 105 //--------------------------------------------------------------------------- 106 public Character getMiddleInitial() 107 { 108 return StringUtil.isSet(mMiddleName) ? Character.toUpperCase(mMiddleName.charAt(0)) : null; 109 } 110 111 112 //--------------------------------------------------------------------------- 113 public Author setLastName(String inValue) 114 { 115 mLastName = inValue; 116 return this; 117 } 118 119 //--------------------------------------------------------------------------- 120 public String getLastName() 121 { 122 return mLastName; 123 } 124 125 126 //########################################################################### 127 // PRIVATE METHODS 128 //########################################################################### 129 130 //--------------------------------------------------------------------------- 131 public String buildName() 132 { 133 StringBuilderPlus buffer = new StringBuilderPlus(getLastName()); 134 135 if (StringUtil.isSet(getFirstName())) 136 { 137 buffer.append(", " + getFirstName()); 138 139 if (StringUtil.isSet(getMiddleName())) 140 { 141 buffer.append(" " + getMiddleName()); 142 } 143 } 144 145 return buffer.toString(); 146 } 147 148 //--------------------------------------------------------------------------- 149 private void parse(String inFullName) 150 { 151 if (StringUtil.isSet(inFullName)) 152 { 153 String[] pieces = inFullName.split("\\s+"); 154 155 if (pieces.length > 1) 156 { 157 if (pieces[0].endsWith(",")) 158 { 159 // LastName, firstName <MiddleName> 160 mLastName = pieces[0].substring(0, pieces[0].length() - 1); 161 mFirstName = pieces[1]; 162 163 if (pieces.length > 2) 164 { 165 StringBuilderPlus buffer = new StringBuilderPlus().setDelimiter(" "); 166 for (int i = 2; i < pieces.length; i++) 167 { 168 buffer.delimitedAppend(pieces[i]); 169 } 170 mMiddleName = buffer.toString(); 171 } 172 } 173 else if (pieces[0].length() > 1 174 && (1 == pieces[1].length() 175 || (pieces[1].length() > 1 176 && pieces[1].charAt(1) == '.'))) 177 { 178 // LastName FirstInitial <MiddleInitial> 179 mLastName = pieces[0]; 180 mFirstName = pieces[1]; 181 182 if (pieces.length > 2) 183 { 184 StringBuilderPlus buffer = new StringBuilderPlus().setDelimiter(" "); 185 for (int i = 2; i < pieces.length; i++) 186 { 187 buffer.delimitedAppend(pieces[i]); 188 } 189 mMiddleName = buffer.toString(); 190 } 191 } 192 else 193 { 194 // FirstName <MiddleName> LastName 195 mLastName = pieces[pieces.length - 1]; 196 mFirstName = pieces[0]; 197 198 if (pieces.length > 2) 199 { 200 StringBuilderPlus buffer = new StringBuilderPlus().setDelimiter(" "); 201 for (int i = 1; i < pieces.length - 1; i++) 202 { 203 buffer.delimitedAppend(pieces[i]); 204 } 205 mMiddleName = buffer.toString(); 206 } 207 } 208 } 209 else if (pieces[0].contains(",")) 210 { 211 pieces = pieces[0].split(","); 212 mLastName = pieces[0]; 213 mFirstName = pieces[1]; 214 } 215 216 if (StringUtil.isSet(mFirstName)) 217 { 218 if (mFirstName.length() > 1 219 && mFirstName.charAt(1) == '.') 220 { 221 if (mFirstName.length() > 2) 222 { 223 mMiddleName = mFirstName.substring(2); 224 } 225 226 mFirstName = mFirstName.substring(0, 1); 227 } 228 } 229 230 if (StringUtil.isSet(mMiddleName) 231 && mMiddleName.endsWith(".")) 232 { 233 mMiddleName = mMiddleName.substring(0, mMiddleName.length() - 1); 234 } 235 } 236 } 237}