/
FkEMail_ENGLISH_GOOGLE_TRANSLATE.java
2705 lines (2489 loc) · 104 KB
/
FkEMail_ENGLISH_GOOGLE_TRANSLATE.java
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
package de.fk.email;
/**
* Roughly translated with google translate.
* And then roughhly hand optimized translated and reformated.
* You may find better words.
*
* Sometimes it reads a little bit like Yoda-Mode.
* ... but grown up Person that you are, cope you will with it.
*
* I am well capable of writing better documentation in english.
* But as it is, this is the English Version.
*/
public class FkEMail_ENGLISH_GOOGLE_TRANSLATE
{
/**
* <pre>
* Validation of an email address.
* </pre>
*
* @param pInput the input to be checked
* @return TRUE, if the input according to the structure could be an eMail address.
*/
public static boolean validateEMailAddress( String pInput )
{
return checkEMailAddress( pInput ) < 10;
}
/**
* <pre>
* Validation of an email address.
*
* No TRIM is made on the input parameter.
*
* Return values less than 10 are OK.
*
* A return of 0, is an eMail address without any other specials
* (... 0 can now be an email in pointed brackets)
*
* Return values from 1 to 9, are email addresses with special string, comment or IP address.
*
* The following returns are possible:
*
* 0 = eMail address correct
* 1 = eMail address correct (local part with string)
* 2 = eMail address correct (IP4 address)
* 3 = eMail address correct (Local part with string and IP4 address)
* 4 = eMail address correct (IP6 address)
* 5 = eMail address correct (Local part with string and IP6 address)
* 6 = eMail adress correct (comment)
* 7 = email address correct (comment, string)
* 8 = eMail address correct (comment, string, IP4 address)
* 9 = eMail address correct (comment, string, IP6 address)
*
*
* 10 length: input is zero
* 11 length: input is empty string
* 12 length: length limitations are not correct
* 13 Length: RFC 5321 = SMTP protocol = maximum length of the local part is 64 bytes
* 14 Lenght: Top-level domain must be at least 2 digits long.
* 15 Length: top-level domain can not be more than X-places long. (X is 10 here)
*
* 16 Structure: no opening square bracket found.
* 17 Structure: no closing square bracket found.
* 18 Structure: Error in Address String X
*
* 20 characters: number or special characters only after one letter (substring may not begin with numbers or special characters)
* 21 characters: Special characters not allowed in the domain part
* 22 characters: invalid character found in the input
* 23 characters: top-level domain can not begin with numbers
* 24 characters: No special characters at the end of the eMail address
*
* 26 AT-Character: no AT-Character at the beginning
* 27 AT character: no AT character at the end
* 28 AT characters: no AT character found
* 29 AT characters: do not allow another AT character if AT character has already been found
*
* 30 separators: no beginning with a dot
* 31 separators: no two dots in a row
* 32 separators: invalid character combination ". @"
* 33 separator: invalid character combination "@."
* 34 separators: no point found (there must be at least one point for the domain separator)
* 35 separator: the last dot must be after the AT character
* 36 separators: the last point must not be at the end
*
* Domain name with IP address:
*
* 40 IP6 address part: string "IPv6:" expected
* 41 IP6 address part: Separator number is 0
* 42 IP6 address part: too many separators, maximum of 8 separators
* 43 IP6 address part: Too few separators
* 44 IP6 address part: invalid combination ":]"
* 45 IP6 address part: Terminator "]" must be at the end
* 46 IP6 address part: too many digits, max. 4 digits
* 47 IP6 address section: IPv4 in IPv6 - delimiter number incorrect
* 48 IP6 address part: IPv4 in IPv6 - too many characters in the first IP4 block
* 49 IP6 address part: Wrong character in the IP address
* 50 IP6 address part: Only one double colon may exist.
* 51 IP address part: IP address before AT sign
* 52 IP address part: IP address must come directly after the AT sign (correct combination "@ [")
* 53 IP4 address part: too many digits, a maximum of 3 digits
* 54 IP4 Address Section: Byte Overflow
* 55 IP4 address section: no digits available
* 56 IP4 address part: too many separators
* 57 IP4 address part: IP address Separator number must be 3
* 58 IP4 address part: invalid combination ".]"
* 59 IP4 address part: Wrong character in the IP address
* 60 IP4 address part: Terminator "]" must be at the end
* 61 IP address part: No completion of the IP address on ']'
*
* Local part with quotation marks
*
* 80 String: A starting quotation mark must be at the beginning, the character error must not be greater than 0
* 81 String: A starting leader must come immediately after a period
* 82 String: no quotation mark after the AT character
* 83 String: escape character not at the end of the input
* 85 String: Empty string in quotation marks
* 86 String: no final quotation mark found.
* 87 String: After a closing quotation mark, an AT sign or a period must follow
* 88 String: The string ends at the end of the string (premature end of input)
* 89 String: Invalid character within quotes
*
* Comments
*
* 91 Comment: Invalid escape sequence in the comment
* 92 Comment: Invalid character in the comment
* 93 Comment: no conclusive sign for the commentary found. ')' expected
* 94 Comment: no comment after the AT sign
* 95 Comment: The comment ends at the end of the string (premature end of the input)
* 96 Comment: Escape characters do not end at the input
* 97 Comment: After the comment an AT-Character must come
* 98 Comment: No local part available
* 99 Comment: no second comment valid
*
* Correct email addresses that are not recognized ---------------------------------------- ---------------------
*
* FkEMail.checkemailadresse ("ABC@localhost") = 34 = Separator: no point found (at least one point must exist for the domain separator)
* FkEMail.checkemailaddress ("ABC.DEF@localhost") = 35 = separator: the last dot must be after the AT character
*
* ALTERNATE: Error 34 and error 35 specify exactly this CASE.
* These error numbers can be accepted as correct if
* such eMail address information should be allowed
*
* not clarified ----------------------------------------------- -------------------------------------------------- -
*
* Comment at the end of the email address correct?
* Can an email domain end with a number?
*
* IPv6 address information is not 100% correctly recognized
* - Overflow in the information is not recognized
*
* ABC@[IPv6123::ffff:127.0.0.1] = prefix "IPv6" or "IPv6:" (with or without colon as separator?)
*
* ABC.DEF@192.0.2.123 = IP4 address without brackets valid?
*
* "- --- .. -."@sh.de = character combination "or." in the string correct?
* (otherwise there will be a "... .... .. -"@storm.de)
*
* "(A (B (C) DEF@GHI.JKL" = Allow opening in a comment '('?
*
* "<ABC.DEF@GHI.JKL>" = correct eMail address information?
* "<ABC.DEF@GHI.JKL> ABC DEF" = correct eMail address information? (... the parentheses come at the start of the string)
*
*
* ------------------------------------------------- -------------------------------------------------- ---------------
*
* http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html
* - It is impossible to match these restrictions with a single technique.
* - Using regular expressions results in long patterns giving incomplete results.
*
* https://davidcel.is/posts/stop-validating-email-addresses-with-regex/
* - Some people, when confronted with a problem, think, "I know, I'll use regular expressions." Now they have two problems.
*
* https://stackoverflow.com/questions/624581/what-is-the-best-java-email-address-validation-method
* - There don't seem to be any perfect libraries or ways to do this yourself ...
* - Another option is use the Hibernate email validator, using the annotation Email or using the validator class programatically
* - I agree that the Apache Commons validator works well, but I find it to be quite slow - over 3ms per call.
* - After actually trying to build my project, it seems apache commons doesn't work with Android very well, hundreds of warnings and some errors, it didn't even compile.
* - Same problem with me as of Benjiko99. After adding the dependency, the project wont compile, says java.exe finished with non zero exit code 2.
* - You may also want to check for the length - emails are a maximum of 254 chars long. I use the apache commons validator and it doesn't check for this.
* - But really what you want is a lexer that properly parses a string and breaks it up into the component structure according to the RFC grammar.
* EmailValidator4J seems promising in that regard, but is still young and limited.
*
* https://docs.microsoft.com/en-us/dotnet/standard/base-types/how-to-verify-that-strings-are-in-valid-email-format
*
* https://www.regular-expressions.info/email.html
* https://de.wikipedia.org/wiki/Top-Level-Domain
* https://stackoverflow.com/questions/2049502/what-characters-are-allowed-in-an-email-address?rq=1
* https://tools.ietf.org/id/draft-ietf-behave-address-format-10.html
* </pre>
*
* @param pInput die auf eine eMail-Adresse zu pruefende Eingabe
* @return wenn die Eingabe nach der Struktur her eine eMail-Adresse ergeben kann einen Wert kleiner 10, ansonsten einen der oben genannten Rueckgabewerte
*/
public static int checkEMailAddress( String pInput )
{
/*
* Rough schedule:
*
* Length tests 1 - input total
*
* End on '>'
* YES = search '<'
*
* Start on '<'
* YES = search '>'
*
* Square Brackets found?
* YES = check the non-email string
*
* Length tests 2 - eMail address
*
* While Loop 1
*
* A-Z, a-z and Number characters
*
* Special characters domain part - _
*
* Character .
*
* Character @
*
* Character \
*
* Special characters Local-Part
*
* Character "
*
* While Loop 2 - String
*
* Characters A-Z, a-z, numbers and special characters
*
* Character "
*
* Character \
*
* Character [
*
* Characters "IPv6:"
*
* While loop 3 - IP address
*
* IPv6
*
* Characters A-F, a-f and numbers
*
* Character .
*
* Character :
*
* Character ]
*
* IPv4
*
* Numbers
*
* Character .
*
* Character ]
*
* Character (
*
* While Loop 4 - Comments
*
* Characters A-Z, a-z, numbers and special characters
*
* Character \
*
* Character )
*
* Final checks
*/
/*
* Check: Input equals "null"?
*
* If the input is "null", it is not a valid eMail address.
* The error 10 is returned.
*/
if ( pInput == null )
{
return 10;// Length: input is zero
}
/*
* Check: Input length equals 0?
*
* No trim is done as a trim would consume time.
* This function should check the string as it was passed.
* It is not the job of this function to manipulate the input.
* If the input consists of spaces only, the first space will be used
* an invalid character is detected and error 29 is returned.
*
* If the input without trim already an empty string, the
* Error 11 returned.
*/
if ( pInput.length() == 0 )
{
return 11;// Length: input is empty string
}
/*
* Length input string
* The variable "length_input_string" referred to in the first version
* the actual length of the input string. By installing the check routine
* for square brackets, but the content can also be the meaning of a
* Have position information.
*/
int length_input_string = pInput.length();
/*
* In general, the input must not be longer than 255 characters.
* The check on the minimum length of the email address follows below.
*/
if ( length_input_string > 255 )
{
return 12;
}
/*
* Position AT sign
* Initial value -1 stands for "no AT character found"
*/
int position_at_character = -1;
/*
* Marker "last position of a point"
* Initial value -1 stands for "no point found"
*/
int position_last_point = -1;
/*
* Saves the position of the last found quotation mark.
* Start or end character.
*/
int position_anf_character_act = -1;
/*
* Saves the position of the last closed parenthesis ')' of a valid comment.
*/
int position_comment_end = -1;
int position_kommentar_start = -1;
/*
* Counter for characters between two separators.
* The separators are dot and AT character
*/
int character_counter = 0;
/*
* Start reading position
* The start position for the while loop is 0. This is
* the first character of the input.
*
* For email addresses in angle brackets is the starting position
* always the position after the opening square bracket.
*/
int current_index = 0;
char current_character = ' ';
/*
* Check: square brackets
*
*
* ABC DEF <ABC.DEF@GHI.JKL>
*
* <ABC.DEF@GHI.JKL> ABC DEF
*
* Starts the email address with an opening square bracket, will
* looking for a closing square bracket. From the front to the back.
*
* End the email address with a closing square bracket, will
* looking for an opening square bracket. From back to front.
*
* If no corrosponding bracket is found, the function becomes
* ended with an error code (16 or 17).
*
* If the corrosponding bracket is found, the start and end
* End position for the actual check routine on the in the
* Square brackets containing eMail address limited.
*
* The string outside the square brackets will have its own
* Checked while loop. For this, existing variables
* deprecated to not have to declare more variables.
* If the characters in the non-email string are OK, they will
* Variables reset to their initial value of -1.
*
* If there is an invalid character in the "non-email address string",
* the error 18 is returned.
*/
current_character = pInput.charAt( length_input_string - 1 );
/*
* Check: End with a closing square bracket?
*/
if ( current_character == '>' )
{
/*
* The last character in this case is a closing square bracket.
*
* This character may not be from the while loop below
* be checked, otherwise an invalid character detected
* would become.
*
* The BIS-Poisition for the eMail address check loop will be changed
* diminished one character.
*/
length_input_string--;
/*
* In a while loop, the opening square bracket is searched.
* It is searched from the back to the front.
*/
current_index = length_input_string;
while ( ( current_index > 0 ) && ( current_character != '<' ) )
{
current_index--;
current_character = pInput.charAt( current_index );
}
/*
* If the last character is a closing square bracket, it must be one
* Give square starting bracket.
*
* After the while loop must be in the variable "current_character"
* The open square bracket should be included.
*
* If it is another sign, the structure is not right.
*/
if ( current_character != '<' )
{
return 16; // structure: no opening square bracket found.
}
/*
* Determining the positions of the "non-email address string" to be checked separately
*/
position_last_point = 0;
position_comment_end = current_index;
/*
* The current index is now in the position of the opening square bracket.
* The sign has been checked and is OK, so the reading process starts
* passed on by one character. (In addition error avoidance No. 22)
*/
current_index++;
}
else
{
/*
* Input did not end on a closing square bracket.
* It is checked if the entry opens with a square
* Clip starts.
*
* The current_characteracter is read at position 0.
*/
current_character = pInput.charAt( current_index );
if ( current_character == '<' )
{
/*
* Starts the input with a square open parenthesis, will
* searched the closing square bracket in a while loop.
* It is searched from the front to the back.
*/
while ( ( current_index < ( length_input_string - 1 ) ) && ( current_character != '>' ) )
{
current_index++;
current_character = pInput.charAt( current_index );
}
/*
* If the first character is an opening square bracket, it must be a
* Give square closing bracket.
*
* After the while loop must be in the variable "current_character"
* The closing square bracket should be included.
*
* If it is another sign, the structure is not right.
*/
if ( current_character != '>' )
{
return 17; // structure: no closing square bracket found.
}
/*
* Determining the positions of the "non-email address string" to be checked separately
*
* The string to be checked starts after the character behind the current position.
* The string ends at the index of the last character.
*/
position_last_point = current_index + 1;
position_comment_end = length_input_string;
/*
* The reading process must end one character before the found closing square bracket.
* The length of the input string will be adjusted accordingly.
*/
length_input_string = current_index;
/*
* The character at position 0 is the opening square bracket.
* The reading process must start at index 1.
*/
current_index = 1;
}
}
int email_local_part_gesamt_start = current_index;
/*
* Check: is there a separate "non-email address string" to check?
*/
if ( position_last_point != -1 )
{
/*
* Input = "<ABC@DEF.GHI>"
*
* The search routine detects an angle bracket. It will also
* one - in this case - opening parenthesis found.
*
* The position of the last point is set to 0.
* However, there is no "non-email string" left.
*
* Is this a mistake or not?
* At the moment, such an input will be passed through as the correct eMail address.
*/
// if (position_last_point == position_comment_end)
// {
// return 19; // structure: there is no "non-email string"
//}
/*
* The characters in the "non-email address string" are checked via a while loop.
* If an invalid character is detected, error 18 is returned.
*/
while ( position_last_point < position_comment_end )
{
current_character = pInput.charAt( position_last_point );
if ( ( ( current_character >= 'a' ) && ( current_character <= 'z' ) ) || ( ( current_character >= 'A' ) && ( current_character <= 'Z' ) ) || ( ( current_character >= '0' ) && ( current_character <= '9' ) ) )
{
// OK
}
else if ( ( current_character == ' ' ) || ( current_character == '(' ) || ( current_character == ')' ) || ( current_character == '\"' ) )
{
// OK ... maybe more characters here, which would be added
}
else
{
//System.out.println (current_character + "error");
return 18; // structure: error in address-string-X
}
position_last_point++;
}
/*
* Restoration of the default values
* The temporary variables used for other purposes will return to their
* Default values of -1 are set so that the actual check routine works correctly.
*/
position_last_point = -1;
position_comment_end = -1;
}
current_character = ' ';
/*
* Calculation of the length of the pure email address information.
*
* The variable "current_index" stands here on the first character of the eMail address.
* The variable "length_input_string" is after the last to be checked
* Sign of the email address. This order with the previous variable name
* to be compliant, which was the length of the input string.
*/
character_counter = length_input_string - current_index;
/*
* http://en.wikipedia.org/wiki/Email_address
* Within RFC 5322, there is no limit to the length of eMail addresses.
*
* In RFC 5321, the maximum length of the local part is 64 bytes and
* the maximum length of the domain name specified with 255 bytes. Together
* with the "@" - sign this results in the maximum length of a
* eMail address of 320 bytes.
*
* RFC 5321 also defines the maximum length of the "Path" element
* which determines the elements "FROM" and "RCPT TO" in the envelope and the
* maximum length of bytes including separators "<" and ">".
* This results in a maximum length of the eMail address of 254 bytes
* including the "@". An e-mail with a longer address, can
* are neither sent nor received via RFC-compliant SMTP servers.
*
* Minimum possible email address is "A@B.CD", equal to 6 digits.
*/
if ( ( character_counter < 6 ) || ( character_counter > 254 ) )
{
return 12;// Length: length limitations are not correct
}
character_counter = 0;
/*
* Variable "fkt_result_email_ok"
*
* Storage of the function result for correct eMail indications.
*
* If the eMail address is purely text without special forms, the value remains at 0.
*
* If there is a string in the local part, the value is changed to 1.
*
* If there is an IP4 address, the value is increased by 2.
*
* If there is an IP6 address specification, the value is increased by 4.
*
* If there is a comment, the return value is converted.
*
* Only the 10 result values below are supplied,
* so that correct email addresses stay below the value 10.
*
* Regarding the increase of the value in a string specification, it is
* so that the IP address is recognized correctly only after the AT sign
* becomes. A correctly read string specification always precedes one
* possible IP address are read and thus the result-
* value is set to 1 before the increase in the IP address
* can be made.
*
* The following results are possible:
*
* 0 = eMail address correct
* 1 = eMail address correct (local part with string)
* 2 = eMail address correct (IP4 address)
* 3 = eMail address correct (Local part with string and IP4 address)
* 4 = eMail address correct (IP6 address)
* 5 = eMail address correct (Local part with string and IP6 address)
* 6 = eMail adress correct (comment)
* 7 = email address correct (comment, string)
* 8 = eMail address correct (comment, string, IP4 address)
* 9 = eMail address correct (comment, string, IP6 address)
*
* Due to the different return values, the caller can get email addresses
* with IP address or string parts after rejection.
*/
int fkt_result_email_ok = 0;
boolean knz_kommentar_abschluss_am_stringende = false;
/*
* While Loop 1
*
* In the outer while loop, a basic email address structure is parsed.
*
* There are three inner while loops for parsing string components, comments, and IP addresses.
*
* In the inner while loops all conditions are checked, which are to be tested there.
*
* Conditions from an inner while loops should not (if possible) pass through the outer while loop.
*/
while ( current_index < length_input_string )
{
/*
* Current character
* The current_characteracter is read from the input at the current index.
*/
current_character = pInput.charAt( current_index );
/*
* Conditions Characters A-Z, a-z and numbers
*/
if ( ( ( current_character >= 'a' ) && ( current_character <= 'z' ) ) || ( ( current_character >= 'A' ) && ( current_character <= 'Z' ) ) || ( ( current_character >= '0' ) && ( current_character <= '9' ) ) )
{
/*
* Letters ("A" to "Z" and "a" to "z") and numbers may appear anywhere in the email address.
*
* Such a sign can not produce an error itself.
*
* The character error is increased by one.
*/
character_counter++;
}
else if ( ( current_character == '_' ) || ( current_character == '-' ) )
{
/*
* In the domain part the special characters '_' and '-' are not allowed to start.
*
* If the character error is 0, the current_characteracter is in an invalid position.
*
* According to RFC 952, no part string may start with a number or a dot in the domain part.
* According to RFC 1123, hostnames may start with numbers.
*
* https://verifalia.com/help/email-validations/can-email-addresses-have-hyphens-minus-signs-dashes
*
* Placement - email addresses cannot have a hyphen (or minus sign, or dash) as the first or last letter in
* the domain part. Similarly, the hyphen cannot be placed directly in front of or following, the dot (.).
* Have a look at some examples of correct vs. incorrect use:
*
* Correct
*
* username@exam-ple.com
* username@e-xample.com
*
* Incorrect
*
* username@example-.com
* username@-example.com
*/
if ( position_at_character > 0 ) // no beginning with a number or special characters in the domain part
{
if ( character_counter == 0 )
{
return 20; // character: number or special character only after one letter (substring may not start with numbers or special characters)
}
if ( ( current_index + 1 ) == length_input_string )
{
return 24; // character: No special character at the end of the eMail address
}
else
{
/*
* https://en.wikipedia.org/wiki/Email_address
*
* Domain-Part:
* hyphen -, provided that it is not the first or last character.
*/
if ( pInput.charAt( current_index + 1 ) == '.' )
{
return 20; // Trennzeichen: ungueltige Zeichenkombination "-."
}
//else if ( pInput.charAt( current_index - 1 ) == '.' )
//{
// return 20; // Trennzeichen: ungueltige Zeichenkombination ".-"
//}
}
}
}
/*
* Conditions for a point
*/
else if ( current_character == '.' )
{
/*
* Check: Has a point been found yet?
*
* No, if the initial value of -1 is still in the storage variable "position_last_point".
*/
if ( position_last_point == -1 )
{
/*
* Check: Reading position equals 0?
*
* If the current index is 0 and the current_characteracter is a point,
* error 30 is returned.
*
* It must not be checked with the sign error, since the first
* Local part can also be a string. There is the sign error
* not increased.
*/
if ( current_index == ( position_comment_end + 1 ) )
{
return 30; // Separator: no beginning with a dot
}
}
else
{
/*
* Check: Two points in a row?
*
* Is the difference from the current reading position and the last
* Point position equal to 1, stand 2 points in a row. It will
* in this case the error 31 returned.
*/
if ( ( current_index - position_last_point ) == 1 )
{
return 31; // Separator: no two dots in a row
}
}
if ( position_at_character > 0 )
{
/*
* Domain part label length
* https://de.wikipedia.org/wiki/Hostname
* https://en.wikipedia.org/wiki/Hostname
*
* A domain label must have 1 characters and can be a maximum of 63 characters.
* 64 characters are checked in the calculation, since the subtraction is not
* must be complicated (the position of the last point is counted).
*
* If the current domain label is too long, error 63 is returned.
*/
if ( ( current_index - position_last_point ) > 64 )
{
// System.out.println ("" + (current_index - position_letzt_punkt));
return 63; // Domain part: Domain label too long (maximum 63 characters)
}
}
/*
* Save index of the last point
*/
position_last_point = current_index;
/*
* Set the character and number counter after one point to 0
*/
character_counter = 0;
}
/*
* Conditions for the AT-Character
*/
else if ( current_character == '@' )
{
/*
* Check: Position AT-Character not equal to -1?
*
* If an AT character has already been found, it is in the position variable
* a value greater than 0 exists. It may only be used once in the reading process
* an (unmasked) AT character can be found as a separator character.
* If an AT character position already exists, the error becomes
* 29 returned.
*
* If no AT character has yet been found, further checks are made.
*/
if ( position_at_character != -1 )
{
return 29; // AT character: do not allow another AT character if AT character has already been found
}
if ( current_index == 0 )
{
return 26; // AT character: no AT character at the beginning
}
if ( current_index > 64 )
{
return 13;// Length: RFC 5321 = SMTP protocol = maximum length of the local part is 64 bytes
}
if ( ( current_index + 1 ) == length_input_string )
{
return 27; // AT character: no AT character at the end
}
if ( current_index - position_last_point == 1 )
{
return 32; // Separator: invalid character combination ".@"
}
/*
* Combination "@."
* At this position it is ensured that in the input still
* at least 1 character follows. There is no index-out-of-bounds exception here.
*
Otherwise the AT sign would be at the end and the caller
* would get 8 as a result of the function.
*/
if ( pInput.charAt( current_index + 1 ) == '.' )
{
return 33; // Separator: invalid character combination "@."
}
/*
* Note the position of the AT sign
*/
position_at_character = current_index;
/*
* Set the character error after the AT-Character to 0
*/
character_counter = 0;
/*
* Position of last point
*
* The AT symbol separates the local and domain part.
* The position of the last point must be zeroed out
* Side effects in the length calculation of the individual
* Avoid domain parts.
*
* The domain part starts at the AT symbol and on its index
* the position of the last point is also set.
*/
position_last_point = current_index;
}
else if ( current_character == '\\' )
{
/*
* Special characters with Qoutierung, which may occur only in the Local Part
*
* \ @
*
* If the position for the AT sign is greater than 0, is
* the reading process in the domain part of the eMail address. There are these
* Characters are not allowed and 21 will be returned.
*/
if ( position_at_character > 0 )
{
return 21; // Characters: Special characters not allowed in the domain part
}
/*
* Masked character
* The reading process must still check the next character.
* The reading process index is advanced by one character.
*/
current_index++;
/*
* Check: String end?
*/
if ( current_index == length_input_string )
{
return 83; // String: escape character not at the end of the input
}
/*
* Read characters after the backslash.
* The character may be a backslash or a quotation mark.
* All other characters lead to error 84.
*/
current_character = pInput.charAt( current_index );
if ( ( current_character != '\\' ) && ( current_character != '@' ) && ( current_character != ' ' ) && ( current_character != '\'' ) )
{
return 84; // String: Invalid escape sequence in the string
}
}
else if ( ( current_character == '!' ) || ( current_character == '#' ) || ( current_character == '$' ) || ( current_character == '%' ) || ( current_character == '&' ) || ( current_character == '\'' ) || ( current_character == '*' ) || ( current_character == '+' ) || ( current_character == '-' ) || ( current_character == '/' ) || ( current_character == '=' ) || ( current_character == '?' ) || ( current_character == '^' ) || ( current_character == '`' ) || ( current_character == '{' ) || ( current_character == '|' ) || ( current_character == '}' ) || ( current_character == '~' ) )
{
/*
* asc ("!") = 033 asc ("*") = 042
* asc ("#") = 035 asc ("+") = 043
* asc ("$") = 036 asc ("-") = 045
* asc ("%") = 037 asc ("/") = 047
* asc ("&") = 038
* asc ("'") = 039
*
* asc ("=") = 061 asc ("{") = 123
* asc ("?") = 063 asc ("|") = 124
* asc ("^") = 094 asc ("}") = 125
* asc ("_") = 095 asc ("~") = 126
* asc ("` ") = 096 asc (" ") = 032
*/
/*
* Special characters, which may occur only in the local part
*
*! # $% & '* + - / =? ^ _ `{|} ~
*
* If the position for the AT sign is greater than 0, is
* the reading process in the domain part of the eMail address. There are these
* Characters are not allowed and 21 will be returned.
*/
if ( position_at_character > 0 )
{
return 21; // Characters: Special characters not allowed in the domain part
}
}
else if ( current_character == '"' )
{
/*
* While Loop 2 - String
*/
/*
* Check: Reading process in the domain part?
* No quotation marks are allowed in the domain part.
* The error 82 is returned.
*/
if ( position_at_character > 0 )
{
return 82; // String: no quotation mark after the AT character
}
/*
* Check: Character combination. "
*
* A local part in quotes must start after a delimiter.
*
* Has already found a point and the difference of the reading process
* is not 1, the error 81 is returned.
*/
if ( position_last_point > 0 )
{
if ( current_index - position_last_point != 1 )
{
return 81; // String: A starting leader must come immediately after a period
}
}