mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noel Rouvignac
22.24.2013 53ac6966e65a907785505fd1da1ac196730cd442
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2006-2008 Sun Microsystems, Inc.
 */
package org.opends.server.schema;
 
 
 
import static org.opends.server.loggers.debug.DebugLogger.*;
import static org.opends.server.schema.SchemaConstants.*;
 
import java.util.Collection;
import java.util.Collections;
 
import org.opends.server.api.ApproximateMatchingRule;
import org.opends.server.loggers.debug.DebugTracer;
import org.opends.server.types.ByteSequence;
import org.opends.server.types.ByteString;
import org.opends.server.types.DebugLogLevel;
import org.opends.server.types.DirectoryException;
 
 
 
/**
 * This class defines an approximate matching rule based on the Double Metaphone
 * algorithm.  The Metaphone and Double Metaphone algorithms were originally
 * devised by Lawrence Philips (published in the December 1990 issue of
 * <I>Computer Language</I> and the
 * <A HREF="http://www.cuj.com/documents/s=8038/cuj0006philips/">June 2000 issue
 * of <I>C/C++ Users Journal</I></A>, respectively), and this version of the
 * algorithm is based on a version modified by Kevin Atkinson to include
 * bugfixes and additional functionality (source is available
 * <A HREF="http://aspell.net/metaphone/dmetaph.cpp">here</A> and additional
 * Metaphone and Double Metaphone information is available at
 * <A HREF="http://aspell.net/metaphone/">http://aspell.net/metaphone/</A>).
 * This implementation is largely the same as the one provided by Kevin
 * Atkinson, but it has been re-written for better readability, for more
 * efficiency, to get rid of checks for conditions that can't possibly happen,
 * and to get rid of redundant checks that aren't needed.  It has also been
 * updated to always only generate a single value rather than one or possibly
 * two values.
 */
class DoubleMetaphoneApproximateMatchingRule
       extends ApproximateMatchingRule
{
  /**
   * The tracer object for the debug logger.
   */
  private static final DebugTracer TRACER = getTracer();
 
 
 
  /**
   * Creates a new instance of this double metaphone approximate matching rule.
   */
  public DoubleMetaphoneApproximateMatchingRule()
  {
    super();
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override
  public Collection<String> getAllNames()
  {
    return Collections.singleton(getName());
  }
 
 
 
  /**
   * Retrieves the common name for this matching rule.
   *
   * @return  The common name for this matching rule, or <CODE>null</CODE> if
   * it does not have a name.
   */
  @Override
  public String getName()
  {
    return AMR_DOUBLE_METAPHONE_NAME;
  }
 
 
 
  /**
   * Retrieves the OID for this matching rule.
   *
   * @return  The OID for this matching rule.
   */
  @Override
  public String getOID()
  {
    return AMR_DOUBLE_METAPHONE_OID;
  }
 
 
 
  /**
   * Retrieves the description for this matching rule.
   *
   * @return  The description for this matching rule, or <CODE>null</CODE> if
   *          there is none.
   */
  @Override
  public String getDescription()
  {
    // There is no standard description for this matching rule.
    return AMR_DOUBLE_METAPHONE_DESCRIPTION;
  }
 
 
 
  /**
   * Retrieves the OID of the syntax with which this matching rule is
   * associated.
   *
   * @return  The OID of the syntax with which this matching rule is associated.
   */
  @Override
  public String getSyntaxOID()
  {
    // Approximate matching is really only appropriate for DirectoryString
    // values.
    return SYNTAX_DIRECTORY_STRING_OID;
  }
 
 
 
  /**
   * Retrieves the normalized form of the provided value, which is best suited
   * for efficiently performing matching operations on that value.
   *
   * @param  value  The value to be normalized.
   *
   * @return  The normalized version of the provided value.
   *
   * @throws  DirectoryException  If the provided value is invalid according to
   *                              the associated attribute syntax.
   */
  @Override
  public ByteString normalizeValue(ByteSequence value)
         throws DirectoryException
  {
    String valueString = value.toString();
    int length = valueString.length();
    if (length == 0)
    {
      // The value is empty, so it is already normalized.
      return ByteString.empty();
    }
 
    int last = length - 1;
 
 
    // Pad the value to allow for checks to go past the end of the value.
    valueString = valueString.toUpperCase() + "     ";
 
 
    // The metaphone value that is being constructed.
    StringBuilder metaphone = new StringBuilder(4);
 
 
    // Skip over GN, KN, PN, WR, and PS at the beginning of a word.
    int pos = 0;
    String substring = valueString.substring(0, 2);
    if (substring.equals("GN") || substring.equals("KN") ||
        substring.equals("PN") || substring.equals("WR") ||
        substring.equals("PS"))
    {
      pos++;
    }
 
 
    // 'X' at the beginning of a word will sound like Z, but Z will always be
    // mapped to S.
    else if (valueString.charAt(0) == 'X')
    {
      metaphone.append("S");
      pos++;
    }
 
 
    // Loop until we have at least four metaphone characters or have reached the
    // end of the string.
    while ((metaphone.length() < 4) && (pos < length))
    {
      // Check the character at the current position against various targets.
      char posMinusFour;
      char posMinusThree;
      char posMinusTwo;
      char posMinusOne;
      char posPlusOne;
      char posPlusTwo;
      switch (valueString.charAt(pos))
      {
        case 'A':
        case 'E':
        case 'I':
        case 'O':
        case 'U':
        case 'Y':
          // All initial vowels map to 'A'.  All others will be ignored.
          if (pos == 0)
          {
            metaphone.append("A");
          }
 
          pos++;
          break;
 
 
        case 'B':
          // B and BB will be mapped to P, with the exception of "MB" as in
          // "crumb", but that will be handled elsewhere.
          metaphone.append("P");
 
          if (valueString.charAt(++pos) == 'B')
          {
            pos++;
          }
 
          break;
 
 
        case 'C':
          // Check for various Germanic sequences, which will be mapped to 'K'.
          // This basically includes all occurrences of "ACH" where the
          // preceding character is not a vowel and the following character is
          // neither an 'E' nor an 'I' except in "BACHER" and "MACHER".
          if ((pos > 1) &&
              (! isVowel(posMinusTwo = valueString.charAt(pos-2))) &&
              hasSubstring(valueString, pos-1, "ACH") &&
              ((posPlusTwo = valueString.charAt(pos+2)) != 'I') &&
              ((posPlusTwo != 'E') ||
               ((valueString.charAt(pos+3) == 'R') &&
                ((posMinusTwo == 'B') || (posMinusTwo == 'M')))))
          {
            metaphone.append("K");
            pos += 2;
            break;
          }
 
 
          // Check for a special case of "caesar", which will be maped to 'S'.
          if ((pos == 0) && hasSubstring(valueString, pos+1, "AESAR"))
          {
            metaphone.append("S");
            pos += 2;
            break;
          }
 
 
          // CH can be treated in lots of different ways.
          if ((posPlusOne = valueString.charAt(pos+1)) == 'H')
          {
            // Check for "chia" as in "chianti" and map to 'K'.
            if (hasSubstring(valueString, pos+2, "IA"))
            {
              metaphone.append("K");
              pos += 2;
              break;
            }
 
            // Check for "chae" as in "michael" and map to 'K'.
            if (hasSubstring(valueString, pos+2, "AE"))
            {
              metaphone.append("K");
              pos += 2;
              break;
            }
 
            // Check for a Greek root at the beginning of the value like
            // chemistry or chorus and map to 'K'.
            if ((pos == 0) && (! hasSubstring(valueString, 2, "ORE")) &&
                (hasSubstring(valueString, 2, "ARAC") ||
                 hasSubstring(valueString, 2, "ARIS") ||
                 hasSubstring(valueString, 2, "OR") ||
                 hasSubstring(valueString, 2, "YM") ||
                 hasSubstring(valueString, 2, "IA") ||
                 hasSubstring(valueString, 2, "EM")))
            {
              metaphone.append("K");
              pos += 2;
              break;
            }
 
 
            // Check for "CH" values that produce a "KH" sound that will be
            // mapped to 'K'.
            if (isGermanic(valueString) ||
                hasSubstring(valueString, pos-2, "ORCHES") ||
                hasSubstring(valueString, pos-2, "ARCHIT") ||
                hasSubstring(valueString, pos-2, "ORCHID") ||
                ((posPlusTwo = valueString.charAt(pos+2)) == 'T') ||
                (posPlusTwo == 'S') ||
                (((pos == 0) ||
                 (((posMinusOne = valueString.charAt(pos-1)) == 'A') ||
                   (posMinusOne == 'O') || (posMinusOne == 'U') ||
                   (posMinusOne == 'E'))) &&
                 ((posPlusTwo == 'L') || (posPlusTwo == 'R') ||
                  (posPlusTwo == 'N')|| (posPlusTwo == 'M') ||
                  (posPlusTwo == 'B')|| (posPlusTwo == 'H') ||
                  (posPlusTwo == 'F')|| (posPlusTwo == 'V') ||
                  (posPlusTwo == 'W'))))
            {
              metaphone.append("K");
              pos += 2;
              break;
            }
 
 
            // All other "CH" values.
            if (pos > 0)
            {
              if (hasSubstring(valueString, 0, "MC"))
              {
                metaphone.append("K");
              }
              else
              {
                metaphone.append("X");
              }
            }
            else
            {
              metaphone.append("X");
            }
 
            pos += 2;
            break;
          }
 
 
          // Check for "CZ" as in "czerny" but not "wicz" and map to 'S'.
          if ((posPlusOne == 'Z') &&
              (! hasSubstring(valueString, pos-2, "WI")))
          {
            metaphone.append("S");
            pos += 2;
            break;
          }
 
 
          // Check for "CIA" as in "focaccia" and map to 'X'.
          if ((posPlusOne == 'I') && (valueString.charAt(pos+2) == 'A'))
          {
            metaphone.append("X");
            pos += 3;
            break;
          }
 
 
          // Check for a double C but not in values that start with "McC"
          if ((posPlusOne == 'C') &&
              (! ((pos == 1) && valueString.charAt(0) == 'M')))
          {
            if ((((posPlusTwo = valueString.charAt(pos+2)) == 'I') ||
                 (posPlusTwo == 'E') || (posPlusTwo == 'H')) &&
                (! ((posPlusTwo == 'H') && valueString.charAt(pos+3) == 'U')))
            {
              if (((pos == 1) && (valueString.charAt(pos-1) == 'A')) ||
                  hasSubstring(valueString, pos-1, "UCCEE") ||
                  hasSubstring(valueString, pos-1, "UCCES"))
              {
                // Values like "accident", "accede", and "succeed".
                metaphone.append("K");
                pos += 2;
                break;
              }
              else
              {
                // Values like "bacci" or "bertucci".
                metaphone.append("X");
                pos += 3;
                break;
              }
            }
            else
            {
              // This is Pierce's Rule, whatever that means.
              metaphone.append("K");
              pos += 2;
              break;
            }
          }
 
 
          // Check for CK, CG, or CQ and map to 'K'.  Check for CI, CE, and CY
          // and map to "S".
          if (((posPlusOne = valueString.charAt(pos+1)) == 'K') ||
              (posPlusOne == 'G') || (posPlusOne == 'Q'))
          {
            metaphone.append("K");
            pos += 2;
            break;
          }
 
 
          // Check for CI, CE, or CY and map to 'S'.
          if ((posPlusOne == 'I') || (posPlusOne == 'E') || (posPlusOne == 'Y'))
          {
            metaphone.append("S");
            pos += 2;
            break;
          }
 
 
          // All other cases of "C" will be mapped to 'K'.  However, the number
          // of positions that we skip ahead may vary.  If there is a value that
          // consists of two words like "mac caffrey", then skip ahead three.
          // For the character combinations of "CK" and "CQ", then skip ahead
          // two.  For the character combinations of "CC" except "CCE" and
          // "CCI", then skip ahead two.  For all other cases, skip ahead one.
          metaphone.append("K");
          switch (valueString.charAt(pos+1))
          {
            case ' ':
              switch (valueString.charAt(pos+2))
              {
                case 'C':
                case 'Q':
                case 'G':
                  pos += 3;
                  break;
                default:
                  pos++;
                  break;
              }
              break;
 
            case 'K':
            case 'Q':
              pos += 2;
              break;
 
            case 'C':
              switch (valueString.charAt(pos+2))
              {
                case 'E':
                case 'I':
                  pos++;
                  break;
                default:
                  pos += 2;
                  break;
              }
              break;
            default:
              pos++;
          }
          break;
 
 
        case 'D':
          // DG will be mapped to either 'J' (in cases like edge) or 'TK' (in
          // cases like Edgar).
          if ((posPlusOne = valueString.charAt(pos+1)) == 'G')
          {
            if (((posPlusTwo = valueString.charAt(pos+2)) == 'I') ||
                (posPlusTwo == 'E') || (posPlusTwo == 'Y'))
            {
              metaphone.append("J");
              pos += 3;
              break;
            }
            else
            {
              metaphone.append("TK");
              pos += 2;
              break;
            }
          }
 
 
          // DT and DD will be mapped to 'T'.
          if ((posPlusOne == 'T') || (posPlusOne == 'D'))
          {
            metaphone.append("T");
            pos += 2;
            break;
          }
 
 
          // All other cases will be mapped to 'T'.
          metaphone.append("T");
          pos++;
          break;
 
 
        case 'F':
          // F always maps to F.  If there is a double F, then skip the second
          // one.
          metaphone.append("F");
          pos++;
          if (valueString.charAt(pos) == 'F')
          {
            pos++;
          }
          break;
 
 
        case 'G':
          if ((posPlusOne = valueString.charAt(pos+1)) == 'H')
          {
            // A "GH" that is not preceded by a vowel will be mapped to 'K'.
            if ((pos > 0) && (! isVowel(valueString.charAt(pos-1))))
            {
              metaphone.append("K");
              pos += 2;
              break;
            }
 
            if (pos == 0)
            {
              if (valueString.charAt(pos+2) == 'I')
              {
                // Words like ghislane or ghiradelli
                metaphone.append("J");
              }
              else
              {
                metaphone.append("K");
              }
 
              pos += 2;
              break;
            }
 
            // A refined version of Parker's Rule.
            if (((pos > 1) &&
                 (((posMinusTwo = valueString.charAt(pos-2)) == 'B') ||
                  (posMinusTwo == 'H') || (posMinusTwo == 'D'))) ||
                ((pos > 2) &&
                 (((posMinusThree = valueString.charAt(pos-3)) == 'B') ||
                  (posMinusThree == 'H') || (posMinusThree == 'D'))) ||
                ((pos > 3) &&
                 (((posMinusFour = valueString.charAt(pos-4)) == 'B') ||
                  (posMinusFour == 'H'))))
            {
              pos += 2;
              break;
            }
            else
            {
              if ((pos > 2) && (valueString.charAt(pos-1) == 'U') &&
                  (((posMinusThree = valueString.charAt(pos-3)) == 'C') ||
                   (posMinusThree == 'G') || (posMinusThree == 'L') ||
                   (posMinusThree == 'R') || (posMinusThree == 'T')))
              {
                // Words like laugh, McLaughlin, cough, rough are mapped to 'F'.
                metaphone.append("F");
              }
              else if ((pos > 0) && (valueString.charAt(pos-1) != 'I'))
              {
                metaphone.append("K");
              }
 
              pos += 2;
              break;
            }
          }
 
 
          if (posPlusOne == 'N')
          {
            if ((pos == 1) && isVowel(valueString.charAt(0)) &&
                (! isSlavoGermanic(valueString)))
            {
              metaphone.append("KN");
              pos += 2;
              break;
            }
            else
            {
              if ((! hasSubstring(valueString, pos+2, "EY")) &&
                  (! isSlavoGermanic(valueString)))
              {
                metaphone.append("N");
              }
              else
              {
                metaphone.append("KN");
              }
 
              pos += 2;
              break;
            }
          }
 
 
          // GLI as in tagliaro will be mapped to "KL".
          if ((posPlusOne == 'L') && (valueString.charAt(pos+2) == 'I'))
          {
            metaphone.append("KL");
            pos += 2;
            break;
          }
 
 
          // Forms of GY, GE, and GI at the beginning of a word will map to 'K'.
          if ((pos == 0) &&
              ((posPlusOne == 'Y') ||
               (substring = valueString.substring(pos+1,pos+3)).equals("ES") ||
               substring.equals("EP") || substring.equals("EB") ||
               substring.equals("EL") || substring.equals("EY") ||
               substring.equals("IB") || substring.equals("IL") ||
               substring.equals("IN") || substring.equals("IE") ||
               substring.equals("EI") || substring.equals("ER")))
          {
            metaphone.append("K");
            pos += 2;
            break;
          }
 
 
          // Some occurrences of GER and GY in a word will be mapped to 'K'.
          posPlusTwo = valueString.charAt(pos+2);
          if ((((posPlusOne == 'E') && (posPlusTwo == 'R')) ||
              (posPlusOne == 'Y')) &&
              ((posMinusOne = valueString.charAt(pos-1)) != 'E') &&
              (posMinusOne != 'I') &&
              (! hasSubstring(valueString, 0, "DANGER")) &&
              (! hasSubstring(valueString, 0, "RANGER")) &&
              (! hasSubstring(valueString, 0, "MANGER")) &&
              (! hasSubstring(valueString, pos-1, "RGY")) &&
              (! hasSubstring(valueString, pos-1, "OGY")))
          {
            metaphone.append("K");
            pos += 2;
            break;
          }
 
 
          // Check for Italian uses like 'biaggi" and map to 'J'.
          if ((posPlusOne == 'E') || (posPlusOne == 'I') ||
              (posPlusOne == 'Y') ||
              hasSubstring(valueString, pos-1, "AGGI") ||
              hasSubstring(valueString, pos-1, "OGGI"))
          {
            // Germanic uses will be mapped to 'K'.
            if (isGermanic(valueString) ||
                hasSubstring(valueString, pos+1, "ET"))
            {
              metaphone.append("K");
            }
            else
            {
              metaphone.append("J");
            }
 
            pos += 2;
            break;
          }
 
 
          // All other cases will be mapped to 'K'.  If there is a double G,
          // then skip two.  Otherwise, just skip one.
          metaphone.append("K");
          pos++;
 
          if (posPlusOne == 'G')
          {
            pos++;
          }
 
          break;
 
 
        case 'H':
          // The letter 'H' will only be processed if it is immediately followed
          // by a vowel and is either the start of the word or preceded by a
          // vowel.
          if (isVowel(valueString.charAt(pos+1)))
          {
            if ((pos == 0) || isVowel(valueString.charAt(pos-1)))
            {
              metaphone.append("H");
              pos++;
            }
          }
 
          pos++;
          break;
 
 
        case 'J':
          // Take care of obvious Spanish uses that should map to 'H'.
          if (hasSubstring(valueString, 0, "SAN "))
          {
            metaphone.append("H");
            pos++;
            break;
          }
 
          if (hasSubstring(valueString, pos, "JOSE"))
          {
            if ((pos == 0) && (valueString.charAt(pos+4) == ' '))
            {
              metaphone.append("H");
            }
            else
            {
              metaphone.append("J");
            }
 
            pos++;
            break;
          }
 
 
          // All other cases will be mapped to 'J'.
          metaphone.append("J");
 
          if (valueString.charAt(pos+1) == 'J')
          {
            pos++;
          }
 
          pos++;
          break;
 
 
        case 'K':
          // 'K' will always be mapped to 'K'.  KK will be treated like K.
          metaphone.append("K");
 
          if (valueString.charAt(pos+1) == 'K')
          {
            pos++;
          }
 
          pos++;
          break;
 
 
        case 'L':
          // 'L' will always be mapped to 'L'.  LL will be treated like L, even
          // for potential Spanish uses.
          metaphone.append("L");
 
          if (valueString.charAt(pos+1) == 'L')
          {
            pos++;
          }
 
          pos++;
          break;
 
 
        case 'M':
          // 'M' will always be mapped to 'M'.  MM will be treated like M.
          // UMB in cases like "dumb" and "thumb" will be treated like M.
          metaphone.append("M");
 
          if (valueString.charAt(pos+1) == 'M')
          {
            pos++;
          }
          else if (hasSubstring(valueString, pos-1, "UMB"))
          {
            if (((pos+1) == last) ||
                hasSubstring(valueString, pos+2, "ER"))
            {
              pos++;
            }
          }
 
          pos++;
          break;
 
 
        case 'N':
          // 'N' will always be mapped to 'N'.  NN will be treated like N.
          metaphone.append("N");
 
          if (valueString.charAt(pos+1) == 'N')
          {
            pos++;
          }
 
          pos++;
          break;
 
 
        case 'P':
          // PH will be mapped to 'F'.
          if ((posPlusOne = valueString.charAt(pos+1)) == 'H')
          {
            metaphone.append("F");
            pos += 2;
            break;
          }
 
 
          // All other cases will be mapped to 'P', with PP and PB being treated
          // like P.
          metaphone.append("P");
 
          if ((posPlusOne == 'P') || (posPlusOne == 'B'))
          {
            pos++;
          }
 
          pos++;
          break;
 
 
        case 'Q':
          // 'Q' will always be mapped to 'K'.  QQ will be treated like Q.
          metaphone.append("K");
 
          if (valueString.charAt(pos+1) == 'Q')
          {
            pos++;
          }
 
          pos++;
          break;
 
 
        case 'R':
          // Ignore R at the end of French words.
          if ((pos == last) && (! isSlavoGermanic(valueString)) &&
              hasSubstring(valueString, pos-2, "IE") &&
              (! hasSubstring(valueString, pos-4, "ME")) &&
              (! hasSubstring(valueString, pos-4, "MA")))
          {
            pos++;
            break;
          }
 
 
          // All other cases will be mapped to 'R', with RR treated like R.
          metaphone.append("R");
 
          if (valueString.charAt(pos+1) == 'R')
          {
            pos++;
          }
 
          pos++;
          break;
 
 
        case 'S':
          // Special cases like isle and carlysle will be silent.
          if (hasSubstring(valueString, pos-1, "ISL") ||
              hasSubstring(valueString, pos-1, "YSL"))
          {
            pos++;
            break;
          }
 
 
          // Special case of sugar mapped to 'X'.
          if (hasSubstring(valueString, pos+1, "UGAR"))
          {
            metaphone.append("X");
            pos++;
            break;
          }
 
 
          // SH is generally mapped to 'X', but not in Germanic cases.
          if ((posPlusOne = valueString.charAt(pos+1)) == 'H')
          {
            if (hasSubstring(valueString, pos+1, "HEIM") ||
                hasSubstring(valueString, pos+1, "HOEK") ||
                hasSubstring(valueString, pos+1, "HOLM") ||
                hasSubstring(valueString, pos+1, "HOLZ"))
            {
              metaphone.append("S");
            }
            else
            {
              metaphone.append("X");
            }
 
            pos += 2;
            break;
          }
 
 
          // Italian and Armenian cases will map to "S".
          if (hasSubstring(valueString, pos+1, "IO") ||
              hasSubstring(valueString, pos+1, "IA"))
          {
            metaphone.append("S");
            pos += 3;
            break;
          }
 
 
          // SZ should be mapped to 'S'.
          if (posPlusOne == 'Z')
          {
            metaphone.append("S");
            pos += 2;
            break;
          }
 
 
          // Various combinations at the beginning of words will be mapped to
          // 'S'.
          if ((pos == 0) &&
              ((posPlusOne == 'M') || (posPlusOne == 'N') ||
               (posPlusOne == 'L') || (posPlusOne == 'W')))
          {
            metaphone.append("S");
            pos++;
            break;
          }
 
 
          // SC should be mapped to either SK, X, or S.
          if (posPlusOne == 'C')
          {
            if ((posPlusTwo = valueString.charAt(pos+2)) == 'H')
            {
              if (hasSubstring(valueString, pos+3, "OO") ||
                  hasSubstring(valueString, pos+3, "UY") ||
                  hasSubstring(valueString, pos+3, "ED") ||
                  hasSubstring(valueString, pos+3, "EM"))
              {
                metaphone.append("SK");
              }
              else
              {
                metaphone.append("X");
              }
 
              pos += 3;
              break;
            }
 
            if ((posPlusTwo == 'I') || (posPlusTwo == 'E') ||
                (posPlusTwo == 'Y'))
            {
              metaphone.append("S");
              pos += 3;
              break;
            }
 
            metaphone.append("SK");
            pos += 3;
            break;
          }
 
 
          // Ignore a trailing S in French words.  All others will be mapped to
          // 'S'.
          if (! ((pos == last) &&
                 (hasSubstring(valueString, pos-2, "AI") ||
                  hasSubstring(valueString, pos-2, "OI"))))
          {
            metaphone.append("S");
          }
 
          if ((posPlusOne == 'S') || (posPlusOne == 'Z'))
          {
            pos++;
          }
 
          pos++;
          break;
 
 
        case 'T':
          // "TION", "TIA", and "TCH" will be mapped to 'X'.
          if (hasSubstring(valueString, pos, "TION") ||
              hasSubstring(valueString, pos, "TIA") ||
              hasSubstring(valueString, pos, "TCH"))
          {
            metaphone.append("X");
            pos += 3;
            break;
          }
 
 
          // TH or TTH  will be mapped to either T (for Germanic cases) or
          // 0 (zero) for the rest.
          if (((posPlusOne = valueString.charAt(pos+1)) == 'H') ||
              ((posPlusOne == 'T') && (valueString.charAt(pos+2) == 'H')))
          {
            if (isGermanic(valueString) ||
                hasSubstring(valueString, pos+2, "OM") ||
                hasSubstring(valueString, pos+2, "AM"))
            {
              metaphone.append("T");
            }
            else
            {
              metaphone.append("0");
            }
 
            pos += 2;
            break;
          }
 
 
          // All other cases will map to T, with TT and TD being treated like T.
          metaphone.append("T");
 
          if ((posPlusOne == 'T') || (posPlusOne == 'D'))
          {
            pos++;
          }
 
          pos++;
          break;
 
 
        case 'V':
          // 'V' will always be mapped to 'F', with VV treated like V.
          metaphone.append("F");
 
          if (valueString.charAt(pos+1) == 'V')
          {
            pos++;
          }
 
          pos++;
          break;
 
 
        case 'W':
          // WR should always map to R.
          if ((posPlusOne = valueString.charAt(pos+1)) == 'R')
          {
            metaphone.append("R");
            pos += 2;
            break;
          }
 
 
          // W[AEIOUYH] at the beginning of the word should be mapped to A.
          if ((pos == 0) && (isVowel(posPlusOne) || (posPlusOne == 'H')))
          {
            metaphone.append("A");
 
            // FIXME -- This isn't in the algorithm as written.  Should it be?
            pos += 2;
            break;
          }
 
 
          // A Polish value like WICZ or WITZ should be mapped to TS.
          if (hasSubstring(valueString, pos+1, "WICZ") ||
              hasSubstring(valueString, pos+1, "WITZ"))
          {
            metaphone.append("TS");
            pos += 4;
            break;
          }
 
 
          // Otherwise, we'll just skip it.
          pos++;
          break;
 
 
        case 'X':
          // X maps to KS except at the end of French words.
          if (! ((pos == last) &&
                 (hasSubstring(valueString, pos-3, "IAU") ||
                  hasSubstring(valueString, pos-3, "EAU") ||
                  hasSubstring(valueString, pos-2, "AU") ||
                  hasSubstring(valueString, pos-2, "OU"))))
          {
            metaphone.append("KS");
          }
 
          if (((posPlusOne = valueString.charAt(pos+1)) == 'C') ||
              (posPlusOne == 'X'))
          {
            pos++;
          }
 
          pos++;
          break;
 
 
        case 'Z':
          // Chinese usages like zhao will map to J.
          if ((posPlusOne = valueString.charAt(pos+1)) == 'H')
          {
            metaphone.append("J");
            pos += 2;
            break;
          }
 
 
          // All other cases map to "S".  ZZ will be treated like Z.
          metaphone.append("S");
 
          if (posPlusOne == 'Z')
          {
            pos++;
          }
 
          pos++;
          break;
 
 
        case '\u00C7': // C with a cedilla
          // This will always be mapped to 'S'.
          metaphone.append("S");
          pos++;
          break;
 
 
        case '\u00D1': // N with a tilde
          // This will always be mapped to 'N'.
          metaphone.append("N");
          pos++;
          break;
 
 
        default:
          // We don't have any special treatment for this character, so skip it.
          pos++;
          break;
      }
    }
 
 
    return ByteString.valueOf(metaphone.toString());
  }
 
 
 
  /**
   * Indicates whether the two provided normalized values are approximately
   * equal to each other.
   *
   * @param  value1  The normalized form of the first value to compare.
   * @param  value2  The normalized form of the second value to compare.
   *
   * @return  <CODE>true</CODE> if the provided values are approximately equal,
   *          or <CODE>false</CODE> if not.
   */
  @Override
  public boolean approximatelyMatch(ByteSequence value1, ByteSequence value2)
  {
    // If the values have been normalized, then we just need to compare their
    // byte arrays.
    return value1.equals(value2);
  }
 
 
 
  /**
   * Indicates whether the provided value has the given substring at the
   * specified position.
   *
   * @param  value      The value containing the range for which to make the
   *                    determination.
   * @param  start      The position in the value at which to start the
   *                    comparison.
   * @param  substring  The substring to compare against the specified value
   *                    range.
   *
   * @return  <CODE>true</CODE> if the specified portion of the value matches
   *          the given substring, or <CODE>false</CODE> if it does not.
   */
  private boolean hasSubstring(String value, int start,
                               String substring)
  {
    try
    {
      // This can happen since a lot of the rules "look behind" and
      // rightfully don't check if it's the first character
      if (start < 0) {
        return false;
      }
 
      int end = start + substring.length();
 
      // value isn't big enough to do the comparison
      if (end > value.length())
      {
        return false;
      }
 
      for (int i=0,pos=start; pos < end; i++,pos++)
      {
        if (value.charAt(pos) != substring.charAt(i))
        {
          return false;
        }
      }
 
      return true;
    }
    catch (Exception e)
    {
      if (debugEnabled())
      {
        TRACER.debugCaught(DebugLogLevel.ERROR, e);
      }
 
      return false;
    }
  }
 
 
 
  /**
   * Indicates whether the provided character is a vowel (including "Y").
   *
   * @param  c  The character for which to make the determination.
   *
   * @return  <CODE>true</CODE> if the provided character is a vowel, or
   *          <CODE>false</CODE> if not.
   */
  private boolean isVowel(char c)
  {
    switch (c)
    {
      case 'A':
      case 'E':
      case 'I':
      case 'O':
      case 'U':
      case 'Y':
        return true;
 
      default:
        return false;
    }
  }
 
 
 
  /**
   * Indicates whether the provided string appears to be Slavo-Germanic.
   *
   * @param  s  The string for which to make the determination.
   *
   * @return  <CODE>true</CODE> if the provided string appears to be
   *          Slavo-Germanic, or <CODE>false</CODE> if not.
   */
  private boolean isSlavoGermanic(String s)
  {
    return (s.contains("W") || s.contains("K") || s.contains("CZ") ||
            s.contains("WITZ"));
  }
 
 
 
  /**
   * Indicates whether the provided string appears Germanic (starts with "VAN ",
   * "VON ", or "SCH").
   *
   * @param  s  The string for which to make the determination.
   *
   * @return  <CODE>true</CODE> if the provided string appears Germanic, or
   *          <CODE>false</CODE> if not.
   */
  private boolean isGermanic(String s)
  {
    return (s.startsWith("VAN ") || s.startsWith("VON ") ||
            s.startsWith("SCH"));
  }
}