String parseDomain(final ByteSequence buf, final ParserCursor cursor, final BitSet delimiters) { StringBuilder dst = new StringBuilder(); while (!cursor.atEnd()) { char current = (char) (buf.byteAt(cursor.getPos()) & 0xff); if (delimiters != null && delimiters.get(current)) { break; } else if (CharsetUtil.isWhitespace(current)) { this.parser.skipWhiteSpace(buf, cursor); } else if (current == '(') { this.parser.skipComment(buf, cursor); } else { this.parser.copyContent(buf, cursor, delimiters, dst); } } return dst.toString(); }
/** * Parses the given byte sequence and returns an instance of the {@link ParsedField} class. * The type of the class returned depends on the field name; see {@link #parse(String)} for * a table of field names and their corresponding classes. * * @param raw the bytes to parse. * @param monitor decoding monitor used while parsing/decoding. * @return a parsed field. * @throws MimeException if the raw string cannot be split into field name and body. */ public static ParsedField parse( final ByteSequence raw, final DecodeMonitor monitor) throws MimeException { Field rawField = RawFieldParser.DEFAULT.parseField(raw); return PARSER.parse(rawField, monitor); }
private void parse() { parsed = true; RawField f = getRawField(); RawBody body = RawFieldParser.DEFAULT.parseRawBody(f); String main = body.getValue(); if (main != null) { dispositionType = main.toLowerCase(Locale.US); } else { dispositionType = null; } parameters.clear(); for (NameValuePair nmp: body.getParams()) { String name = nmp.getName().toLowerCase(Locale.US); parameters.put(name, nmp.getValue()); } }
/** * Parses the sequence of bytes containing field parameters delimited with semicolon into * a list of {@link NameValuePair}s. * * @param buf buffer with the sequence of bytes to be parsed * @param cursor defines the bounds and current position of the buffer */ public List<NameValuePair> parseParameters(final ByteSequence buf, final ParserCursor cursor) { List<NameValuePair> params = new ArrayList<NameValuePair>(); skipWhiteSpace(buf, cursor); while (!cursor.atEnd()) { NameValuePair param = parseParameter(buf, cursor); params.add(param); } return params; }
break; } else if (CharsetUtil.isWhitespace(current)) { skipWhiteSpace(buf, cursor); whitespace = true; } else if (current == '(') { skipComment(buf, cursor); } else if (current == '\"') { if (dst.length() > 0 && whitespace) { dst.append(' '); copyQuotedContent(buf, cursor, dst); whitespace = false; } else { dst.append(' '); copyUnquotedContent(buf, cursor, delimiters, dst); whitespace = false;
String localPart = this.parser.parseValue(buf, cursor, AT_AND_CLOSING_BRACKET); if (cursor.atEnd()) { return createMailbox(openingText, domainList, localPart, null); current = (char) (buf.byteAt(pos) & 0xff); if (CharsetUtil.isWhitespace(current)) { this.parser.skipWhiteSpace(buf, cursor); } else if (current == '(') { this.parser.skipComment(buf, cursor); } else { break;
/** * Skips semantically insignificant whitespace characters and comments and moves the cursor * to the closest semantically significant non-whitespace character. * Nested comments and escaped characters are recognized and handled appropriately. * * @param buf buffer with the sequence of bytes to be parsed * @param cursor defines the bounds and current position of the buffer */ public void skipAllWhiteSpace(final ByteSequence buf, final ParserCursor cursor) { while (!cursor.atEnd()) { char current = (char) (buf.byteAt(cursor.getPos()) & 0xff); if (CharsetUtil.isWhitespace(current)) { skipWhiteSpace(buf, cursor); } else if (current == '(') { skipComment(buf, cursor); } else { break; } } }
public Mailbox parseMailbox( final ByteSequence buf, final ParserCursor cursor, final BitSet delimiters) { BitSet bitset = RawFieldParser.INIT_BITSET(AT, OPENING_BRACKET); if (delimiters != null) { bitset.or(delimiters); } String openingText = this.parser.parseValue(buf, cursor, bitset); if (cursor.atEnd()) { return createMailbox(openingText); } int pos = cursor.getPos(); char current = (char) (buf.byteAt(pos) & 0xff); if (current == OPENING_BRACKET) { // name <localPart @ domain> form return parseMailboxAddress(openingText, buf, cursor); } else if (current == AT) { // localPart @ domain form cursor.updatePos(pos + 1); String domain = parseDomain(buf, cursor, delimiters); return new Mailbox(null, null, openingText, domain); } else { return createMailbox(openingText); } }
/** * Parses the sequence of bytes containing a field parameter delimited with semicolon into * {@link NameValuePair}. * * @param buf buffer with the sequence of bytes to be parsed * @param cursor defines the bounds and current position of the buffer */ public NameValuePair parseParameter(final ByteSequence buf, final ParserCursor cursor) { String name = parseToken(buf, cursor, EQUAL_OR_SEMICOLON); if (cursor.atEnd()) { return new NameValuePair(name, null); } int delim = buf.byteAt(cursor.getPos()); cursor.updatePos(cursor.getPos() + 1); if (delim == ';') { return new NameValuePair(name, null); } String value = parseValue(buf, cursor, SEMICOLON); if (!cursor.atEnd()) { cursor.updatePos(cursor.getPos() + 1); } return new NameValuePair(name, value); }
/** * Parses the sequence of bytes into {@link RawField}. * * @throws MimeException if the input data does not contain a valid MIME field. */ public RawField parseField(final ByteSequence raw) throws MimeException { if (raw == null) { return null; } ParserCursor cursor = new ParserCursor(0, raw.length()); String name = parseToken(raw, cursor, COLON); if (cursor.atEnd()) { throw new MimeException("Invalid MIME field: no name/value separator found: " + raw.toString()); } return new RawField(raw, cursor.getPos(), name, null); }
/** * Parses the sequence of bytes containing a value with parameters into {@link RawBody}. * * @param buf buffer with the sequence of bytes to be parsed * @param cursor defines the bounds and current position of the buffer */ public RawBody parseRawBody(final ByteSequence buf, final ParserCursor cursor) { String value = parseToken(buf, cursor, SEMICOLON); if (cursor.atEnd()) { return new RawBody(value, new ArrayList<NameValuePair>()); } cursor.updatePos(cursor.getPos() + 1); List<NameValuePair> params = parseParameters(buf, cursor); return new RawBody(value, params); }
private void parse() { parsed = true; location = null; RawField f = getRawField(); ByteSequence buf = f.getRaw(); int pos = f.getDelimiterIdx() + 1; if (buf == null) { String body = f.getBody(); if (body == null) { return; } buf = ContentUtil.encode(body); pos = 0; } RawFieldParser parser = RawFieldParser.DEFAULT; ParserCursor cursor = new ParserCursor(pos, buf.length()); String token = parser.parseValue(buf, cursor, null); StringBuilder sb = new StringBuilder(token.length()); for (int i = 0; i < token.length(); i++) { char ch = token.charAt(i); if (!CharsetUtil.isWhitespace(ch)) { sb.append(ch); } } this.location = sb.toString(); }
DomainList parseRoute(final ByteSequence buf, final ParserCursor cursor, final BitSet delimiters) { BitSet bitset = RawFieldParser.INIT_BITSET(COMMA, COLON); if (delimiters != null) { bitset.or(delimiters); this.parser.skipAllWhiteSpace(buf, cursor); if (cursor.atEnd()) { break;
protected LenientAddressParser(final DecodeMonitor monitor) { super(); this.monitor = monitor; this.parser = new RawFieldParser(); }
List<Mailbox> parseMailboxes( final ByteSequence buf, final ParserCursor cursor, final BitSet delimiters) { BitSet bitset = RawFieldParser.INIT_BITSET(COMMA); if (delimiters != null) { bitset.or(delimiters); } List<Mailbox> mboxes = new ArrayList<Mailbox>(); while (!cursor.atEnd()) { int pos = cursor.getPos(); int current = (char) (buf.byteAt(pos) & 0xff); if (delimiters != null && delimiters.get(current)) { break; } else if (current == COMMA) { cursor.updatePos(pos + 1); } else { Mailbox mbox = parseMailbox(buf, cursor, bitset); if (mbox != null) { mboxes.add(mbox); } } } return mboxes; }
public Address parseAddress( final ByteSequence buf, final ParserCursor cursor, final BitSet delimiters) { BitSet bitset = RawFieldParser.INIT_BITSET(COLON, AT, OPENING_BRACKET); if (delimiters != null) { bitset.or(delimiters); String openingText = this.parser.parseValue(buf, cursor, bitset); if (cursor.atEnd()) { return createMailbox(openingText);
public Group parseGroup(final ByteSequence buf, final ParserCursor cursor) { String name = this.parser.parseToken(buf, cursor, COLON_ONLY); if (cursor.atEnd()) { return new Group(name, Collections.<Mailbox>emptyList()); } int pos = cursor.getPos(); int current = (char) (buf.byteAt(pos) & 0xff); if (current == COLON) { cursor.updatePos(pos + 1); } List<Mailbox> mboxes = parseMailboxes(buf, cursor, SEMICOLON_ONLY); return new Group(name, mboxes); }
String token1 = parser.parseValue(buf, cursor, DELIM); try { major = Integer.parseInt(token1); cursor.updatePos(cursor.getPos() + 1); String token2 = parser.parseValue(buf, cursor, null); try { minor = Integer.parseInt(token2);
break; } else if (CharsetUtil.isWhitespace(current)) { skipWhiteSpace(buf, cursor); whitespace = true; } else if (current == '(') { skipComment(buf, cursor); } else { if (dst.length() > 0 && whitespace) { dst.append(' '); copyContent(buf, cursor, delimiters, dst); whitespace = false;
/** * Parses the given byte sequence and returns an instance of the {@link ParsedField} class. * The type of the class returned depends on the field name; see {@link #parse(String)} for * a table of field names and their corresponding classes. * * @param raw the bytes to parse. * @param monitor decoding monitor used while parsing/decoding. * @return a parsed field. * @throws MimeException if the raw string cannot be split into field name and body. */ public static ParsedField parse( final ByteSequence raw, final DecodeMonitor monitor) throws MimeException { Field rawField = RawFieldParser.DEFAULT.parseField(raw); return PARSER.parse(rawField, monitor); }