@@ -55,11 +55,11 @@ public class PersonName {
5555
5656 public enum Component {
5757 FamilyName , GivenName , MiddleName , NamePrefix , NameSuffix
58- };
58+ }
5959
6060 public enum Group {
6161 Alphabetic , Ideographic , Phonetic
62- };
62+ }
6363
6464 private final String [] fields = new String [15 ];
6565
@@ -74,60 +74,78 @@ public PersonName(String s, boolean lenient) {
7474 parse (s , lenient );
7575 }
7676
77+ /**
78+ * Set all components of a component group from encoded component group value.
79+ *
80+ * @param g component group
81+ * @param s encoded component group value
82+ */
83+ public void set (Group g , String s ) {
84+ set (g , s , false );
85+ }
86+
87+ /**
88+ * Sets all components of a specified component group based on an encoded component group value.
89+ *
90+ * @param g the component group to be set
91+ * @param s the encoded component group value, must not contain '=' and must have at most 5 components when split by '^'
92+ * @param lenient whether the operation should be lenient in handling the input value
93+ * @throws IllegalArgumentException if the input string contains '=' or has more than 5 components when split by '^'
94+ */
95+ public void set (Group g , String s , boolean lenient ) {
96+ if (s .indexOf ('=' ) >= 0 )
97+ throw new IllegalArgumentException (s );
98+
99+ parse (g , s , lenient );
100+ }
101+
77102 private void parse (String s , boolean lenient ) {
78- int gindex = 0 ;
103+ parseInternal (null , s , lenient );
104+ }
105+
106+ private void parse (Group g , String s , boolean lenient ) {
107+ parseInternal (g , s , lenient );
108+ }
109+
110+ private void parseInternal (Group g , String s , boolean lenient ) {
111+ int gindex = (g == null ) ? 0 : g .ordinal ();
79112 int cindex = 0 ;
80- StringTokenizer stk = new StringTokenizer (s , "^=" , true );
113+ StringTokenizer stk = new StringTokenizer (s , ( g == null ) ? "^=" : "^ " , true );
81114 while (stk .hasMoreTokens ()) {
82115 String tk = stk .nextToken ();
83116 switch (tk .charAt (0 )) {
84117 case '=' :
85- if (++gindex > 2 )
118+ if (g != null ) {
119+ throw new IllegalArgumentException ("Invalid '=' token in group parsing: " + s );
120+ }
121+ if (++gindex > 2 ) {
86122 if (lenient ) {
87- LOG .info (
88- "illegal PN: {} - truncate illegal component group(s)" , s );
123+ LOG .info ("illegal PN: {} - truncate illegal component group(s)" , s );
89124 return ;
90- } else
125+ } else {
91126 throw new IllegalArgumentException (s );
127+ }
128+ }
92129 cindex = 0 ;
93130 break ;
94131 case '^' :
95132 ++cindex ;
96133 break ;
97134 default :
98- if (cindex <= 4 )
135+ if (cindex <= 4 ) {
99136 set (gindex , cindex , tk );
100- else if (lenient ) {
137+ } else if (lenient ) {
101138 if ((tk = trim (tk )) != null ) {
102139 LOG .info ("illegal PN: {} - subsumes {}th component in suffix" , s , cindex + 1 );
103140 set (gindex , 4 , StringUtils .maskNull (get (gindex , 4 ), "" ) + ' ' + tk );
104141 }
105- } else
142+ } else {
106143 throw new IllegalArgumentException (s );
144+ }
107145 }
108146 }
109147 }
110148
111- /**
112- * Set all components of a component group from encoded component group value.
113- *
114- * @param g component group
115- * @param s encoded component group value
116- */
117- public void set (Group g , String s ) {
118- int gindex = g .ordinal ();
119- if (s .indexOf ('=' ) >= 0 )
120- throw new IllegalArgumentException (s );
121-
122- String [] ss = StringUtils .split (s , '^' );
123- if (ss .length > 5 )
124- throw new IllegalArgumentException (s );
125-
126- for (int cindex = 0 ; cindex < 5 ; cindex ++) {
127- fields [gindex * 5 + cindex ] = cindex < ss .length ? trim (ss [cindex ]) : null ;
128- }
129- }
130-
131149 public String toString () {
132150 int totLen = 0 ;
133151 Group lastGroup = Group .Alphabetic ;
0 commit comments