Package org.evolvis.tartools.rfc822
Class Path
- java.lang.Object
-
- org.evolvis.tartools.rfc822.Path
-
- Direct Known Subclasses:
UXAddress
public class Path extends Object
Represents an RFC822 (and successors) eMail address header content, either From or To, or subsets. In domain literals (square brackets) the General-address-literal syntax is not recognised (as downstream MTAs cannot support it as no use is specified yet), and a IPv6 Zone Identifier isn’t supported as it’s special local use only. Handling of line endings is lenient: CRLF := ([CR] LF) / CR Create a new instance via theof(String)factory method by passing it the address list string to analyse. Then call one of the parse methods on the instance:asAddressList()to validate recipients,asMailboxList()orforSender(boolean)for message senders (but read their JavaDoc). Validating unlabelled addr-specs is possible withasAddrSpec().- Author:
- mirabilos (t.glaser@tarent.de)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classPath.AddressRepresentation for an address (either mailbox or group)classPath.AddressListRepresentation for an address-list or a mailbox-listclassPath.AddrSpecRepresentation for an addr-spec (eMail address) comprised of localPart and domainprotected classPath.AddrSpecSIDERepresentation for a local-part (FWS unfolded) or a domain (dot-atom only)static interfacePath.ParserResultMethods allPathparser results implement.protected classPath.UnfoldedSubstringRepresentation for a substring of the input string, FWS unfolded
-
Constructor Summary
Constructors Modifier Constructor Description protectedPath(String input)Private constructor, use the factory methodof(String)instead
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected intaccept()Advances the current position to the next characterPath.AddressListasAddressList()Parses the address as address-list, e.g.Path.AddrSpecasAddrSpec()Parses the address as addr-spec (unlabelled address) This method is mostly used in input validation.Path.AddressListasMailboxList()Parses the address as mailbox-list, e.g.protected intbra(int deltapos)Jumps to a specified input character position, relative jumpprotected intcur()Returns the wide character at the current positionPath.AddressforSender(boolean allowRFC6854forLimitedUse)Parses the address for the Sender and Resent-Sender headers These headers normally use the address production, but RFC6854 allows for the mailbox production, with the RFC2026 §3.3(d) Limited Use caveat that permits it but only for specific circumstances.protected static booleanis(int c, byte what)protected static booleanisAtext(int c)protected static booleanisCtext(int c)protected static booleanisDtext(int c)protected static booleanisQtext(int c)protected intjmp(int pos)Jumps to a specified input character position, absolute jumpprotected static <T extends org.evolvis.tartools.rfc822.Parser>
Tof(Class<T> cls, String input)Constructs a parser.static Pathof(String addresses)Creates and initialises a new parser for eMail addresses.protected Path.AddresspAddress()protected Path.AddressListpAddressList()protected Path.AddrSpecpAddrSpec()protected Path.AddrSpecpAngleAddr()protected org.evolvis.tartools.rfc822.Path.WordpAtom()Returns the parse result of the atom production: result.body is a raw Substring of the atom, with surrounding CFWS stripped (no unfolding necessary), no extra data result.cfws is null or the trailing CFWS as raw Substring, not unfoldedprotected booleanpCcontent()protected org.evolvis.tartools.rfc822.Parser.SubstringpCFWS()Parses CFWSprotected org.evolvis.tartools.rfc822.Parser.SubstringpComment()Parses commentprotected org.evolvis.tartools.rfc822.Parser.SubstringpDisplayName()protected org.evolvis.tartools.rfc822.Parser.SubstringpDomain()protected org.evolvis.tartools.rfc822.Parser.SubstringpDomainLiteral()protected org.evolvis.tartools.rfc822.Parser.SubstringpDotAtom()protected intpeek()Returns the wide character after the one at the current positionprotected org.evolvis.tartools.rfc822.Parser.SubstringpFWS()Parses FWSprotected Path.AddresspGroup()protected Path.AddrSpecSIDEpLocalPart()protected Path.AddresspMailbox()protected Path.AddressListpMailboxList()protected Path.AddresspNameAddr()protected intpos()Returns the current input character position, for saving and restoring (withjmp(int)) and for error messagesprotected org.evolvis.tartools.rfc822.Parser.SubstringpPhrase()protected intpQcontent()protected intpQuotedPair()protected org.evolvis.tartools.rfc822.Path.WordpQuotedString()Returns the parse result of the quoted-string production: result.body is anPath.UnfoldedSubstringof the entire quoted string, with surrounding double quotes; its String data is dequoted and backslash-removed result.cfws is null or the trailing CFWS as raw Substring, not unfoldedprotected org.evolvis.tartools.rfc822.Path.WordpWord()protected Strings()Returns the input string, for use with substring comparisons (this is safe because Java™ strings are immutable)protected intskip(BiFunction<Integer,Integer,Boolean> matcher)Advances the current position as long as the matcher returns true and end of input is not yet reachedprotected intskip(Function<Integer,Boolean> matcher)Advances the current position as long as the matcher returns true and end of input is not yet reached; cf.static Stringunfold(String s)Removes all occurrences of CR and/or LF from a string.protected org.evolvis.tartools.rfc822.Parser.Substringunfold(org.evolvis.tartools.rfc822.Parser.Substring ss)Unfolds FWS in the passed Substring if necessary
-
-
-
Constructor Detail
-
Path
protected Path(String input)
Private constructor, use the factory methodof(String)instead- Parameters:
input- string to analyse
-
-
Method Detail
-
is
protected static boolean is(int c, byte what)
-
isAtext
protected static boolean isAtext(int c)
-
isCtext
protected static boolean isCtext(int c)
-
isDtext
protected static boolean isDtext(int c)
-
isQtext
protected static boolean isQtext(int c)
-
unfold
public static String unfold(String s)
Removes all occurrences of CR and/or LF from a string.- Parameters:
s- input string- Returns:
- null if there was nothing to remove, a new shorter String otherwise
-
unfold
protected org.evolvis.tartools.rfc822.Parser.Substring unfold(org.evolvis.tartools.rfc822.Parser.Substring ss)
Unfolds FWS in the passed Substring if necessary- Parameters:
ss-Parser.Substringto unfold- Returns:
- instance of an unfolded equivalent of the original substring
-
of
public static Path of(String addresses)
Creates and initialises a new parser for eMail addresses.- Parameters:
addresses- to parse- Returns:
- null if addresses was null or very large, the new instance otherwise
-
asMailboxList
public Path.AddressList asMailboxList()
Parses the address as mailbox-list, e.g. for the From and Resent-From headers (but seeasAddressList()for RFC6854’s RFC2026 §3.3(d) Limited Use)- Returns:
- parser result; remember to call isValid() on it first!
-
forSender
public Path.Address forSender(boolean allowRFC6854forLimitedUse)
Parses the address for the Sender and Resent-Sender headers These headers normally use the address production, but RFC6854 allows for the mailbox production, with the RFC2026 §3.3(d) Limited Use caveat that permits it but only for specific circumstances.- Parameters:
allowRFC6854forLimitedUse- use address instead of mailbox parsing- Returns:
- parser result; remember to call isValid() on it first!
-
asAddrSpec
public Path.AddrSpec asAddrSpec()
Parses the address as addr-spec (unlabelled address) This method is mostly used in input validation. In most cases, useforSender(boolean)(false) instead which allows “user <lcl@example.com>” then extract the addr-spec “lcl@example.com” from the return value.- Returns:
- parser result; remember to call isValid() on it first!
-
asAddressList
public Path.AddressList asAddressList()
Parses the address as address-list, e.g. for the Reply-To, To, Cc, (optionally) Bcc, Resent-To, Resent-Cc and (optionally) Resent-Bcc headers. RFC6854 (under RFC2026 §3.3(d) Limited Use circumstances) allows using this for the From and Resent-From headers, normally covered by theasMailboxList()method.- Returns:
- parser result; remember to call isValid() on it first!
-
pAddressList
protected Path.AddressList pAddressList()
-
pMailboxList
protected Path.AddressList pMailboxList()
-
pAddress
protected Path.Address pAddress()
-
pGroup
protected Path.Address pGroup()
-
pMailbox
protected Path.Address pMailbox()
-
pNameAddr
protected Path.Address pNameAddr()
-
pAngleAddr
protected Path.AddrSpec pAngleAddr()
-
pDisplayName
protected org.evolvis.tartools.rfc822.Parser.Substring pDisplayName()
-
pPhrase
protected org.evolvis.tartools.rfc822.Parser.Substring pPhrase()
-
pWord
protected org.evolvis.tartools.rfc822.Path.Word pWord()
-
pAtom
protected org.evolvis.tartools.rfc822.Path.Word pAtom()
Returns the parse result of the atom production: result.body is a raw Substring of the atom, with surrounding CFWS stripped (no unfolding necessary), no extra data result.cfws is null or the trailing CFWS as raw Substring, not unfolded- Returns:
- result (see above)
-
pQuotedPair
protected int pQuotedPair()
-
pQcontent
protected int pQcontent()
-
pQuotedString
protected org.evolvis.tartools.rfc822.Path.Word pQuotedString()
Returns the parse result of the quoted-string production: result.body is anPath.UnfoldedSubstringof the entire quoted string, with surrounding double quotes; its String data is dequoted and backslash-removed result.cfws is null or the trailing CFWS as raw Substring, not unfolded- Returns:
- result (see above)
-
pFWS
protected org.evolvis.tartools.rfc822.Parser.Substring pFWS()
Parses FWS- Returns:
- raw Substring, not unfolded
-
pCcontent
protected boolean pCcontent()
-
pComment
protected org.evolvis.tartools.rfc822.Parser.Substring pComment()
Parses comment- Returns:
- raw Substring, not unfolded (unfolded is human-visible form for now; may wish to simplify quoted-pairs)
-
pCFWS
protected org.evolvis.tartools.rfc822.Parser.Substring pCFWS()
Parses CFWS- Returns:
- raw Substring, not unfolded
-
pDotAtom
protected org.evolvis.tartools.rfc822.Parser.Substring pDotAtom()
-
pLocalPart
protected Path.AddrSpecSIDE pLocalPart()
-
pDomainLiteral
protected org.evolvis.tartools.rfc822.Parser.Substring pDomainLiteral()
-
pDomain
protected org.evolvis.tartools.rfc822.Parser.Substring pDomain()
-
pAddrSpec
protected Path.AddrSpec pAddrSpec()
-
of
protected static <T extends org.evolvis.tartools.rfc822.Parser> T of(Class<T> cls, String input)
Constructs a parser. Intended to be used by subclasses from static factory methods *only*; seeof(String)for an example.- Type Parameters:
T- subclass of Parser to construct- Parameters:
cls- subclass of Parser to constructinput- user-providedStringto parse- Returns:
- null if input was null or too large, the new instance otherwise
-
jmp
protected final int jmp(int pos)
Jumps to a specified input character position, absolute jump- Parameters:
pos- to jump to- Returns:
- the codepoint at that position
- Throws:
IndexOutOfBoundsException- if pos is not in or just past the input
-
bra
protected final int bra(int deltapos)
Jumps to a specified input character position, relative jump- Parameters:
deltapos- to add to the current positioin- Returns:
- the codepoint at that position
- Throws:
IndexOutOfBoundsException- if pos is not in or just past the input
-
pos
protected final int pos()
Returns the current input character position, for saving and restoring (withjmp(int)) and for error messages- Returns:
- position
-
s
protected final String s()
Returns the input string, for use with substring comparisons (this is safe because Java™ strings are immutable)- Returns:
- String input
-
cur
protected final int cur()
Returns the wide character at the current position- Returns:
- UCS-4 codepoint, or -1 if end of input is reached
-
peek
protected final int peek()
Returns the wide character after the one at the current position- Returns:
- UCS-4 codepoint, or -1 if end of input is reached
-
accept
protected final int accept()
Advances the current position to the next character- Returns:
- codepoint of the next character, or -1 if end of input is reached
- Throws:
IndexOutOfBoundsException- if end of input was already reached
-
skip
protected final int skip(BiFunction<Integer,Integer,Boolean> matcher)
Advances the current position as long as the matcher returns true and end of input is not yet reached
-
skip
protected final int skip(Function<Integer,Boolean> matcher)
Advances the current position as long as the matcher returns true and end of input is not yet reached; cf.skip(BiFunction)- Parameters:
matcher- gets called with justcur()as argument- Returns:
- codepoint of the first character where the matcher returned false, or -1
-
-