Example usage for javax.mail.internet ParseException ParseException

List of usage examples for javax.mail.internet ParseException ParseException

Introduction

In this page you can find the example usage for javax.mail.internet ParseException ParseException.

Prototype

public ParseException(String s) 

Source Link

Document

Constructs a ParseException with the specified detail message.

Usage

From source file:immf.Util.java

public static String getParameter(String header, String name) {

    HeaderTokenizer tokenizer = new HeaderTokenizer(header, HeaderTokenizer.MIME, true);
    HeaderTokenizer.Token token;/*www .j  a va 2s.  c o m*/
    StringBuffer sb = new StringBuffer();
    // It is specified in first encoded-part.
    Encoding encoding = new Encoding();

    String n;
    String v;

    try {
        while (true) {
            token = tokenizer.next();
            if (token.getType() == HeaderTokenizer.Token.EOF)
                break;
            if (token.getType() != ';')
                continue;

            token = tokenizer.next();
            checkType(token);
            n = token.getValue();

            token = tokenizer.next();
            if (token.getType() != '=') {
                throw new ParseException("Illegal token : " + token.getValue());
            }

            token = tokenizer.next();
            checkType(token);
            v = token.getValue();

            if (n.equalsIgnoreCase(name)) {
                // It is not divided and is not encoded.
                return v;
            }

            int index = name.length();

            if (!n.startsWith(name) || n.charAt(index) != '*') {
                // another parameter
                continue;
            }
            // be folded, or be encoded
            int lastIndex = n.length() - 1;
            if (n.charAt(lastIndex) == '*') {
                sb.append(decodeRFC2231(v, encoding));
            } else {
                sb.append(v);
            }
            if (index == lastIndex) {
                // not folding
                break;
            }
        }
        return new String(sb);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    throw new InternalError();
}

From source file:immf.Util.java

private static void checkType(HeaderTokenizer.Token token) throws ParseException {
    int t = token.getType();
    if (t != HeaderTokenizer.Token.ATOM && t != HeaderTokenizer.Token.QUOTEDSTRING) {
        throw new ParseException("Illegal token : " + token.getValue());
    }//from   w  ww . ja  v  a  2 s  .c  o m
}

From source file:immf.Util.java

private static String decodeRFC2231(String s, Encoding encoding)
        throws ParseException, UnsupportedEncodingException {
    StringBuffer sb = new StringBuffer();
    int i = 0;//from  w w  w . j  ava  2  s  . c  o m

    int work = s.indexOf('\'');
    if (work > 0) {
        encoding.encoding = s.substring(0, work);
        work++;
        i = s.indexOf('\'', work);
        encoding.lang = s.substring(work, i);
        i++;
    }

    try {
        for (; i < s.length(); i++) {
            if (s.charAt(i) == '%') {
                sb.append((char) Integer.parseInt(s.substring(i + 1, i + 3), 16));
                i += 2;
                continue;
            }
            sb.append(s.charAt(i));
        }
        return new String(new String(sb).getBytes("ISO-8859-1"), encoding.encoding);
    } catch (IndexOutOfBoundsException e) {
        throw new ParseException(s + " :: this string were not decoded.");
    }
}

From source file:immf.Util.java

public static String decodeParameterSpciallyJapanese(String s) throws ParseException {
    try {//from  w ww.j  a va2s.c o m
        boolean unicode = false;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) > 0xff) { // Unicode
                unicode = true;
                break;
            }
        }
        if (!unicode) {
            // decode by character encoding.
            s = new String(s.getBytes("ISO-8859-1"), "JISAutoDetect");
        }
        // decode by RFC2047.
        // if variable s isn't encoded-word, it's ignored.
        return MimeUtility.decodeText(s);
    } catch (UnsupportedEncodingException e) {
    }
    throw new ParseException("Unsupported Encoding");
}