Package org.apache.james.mime4j

Examples of org.apache.james.mime4j.MimeStreamParser


    IModelFactory factory = Owl.getModelFactory();
    final INews news = factory.createNews(null, feed, new Date());
    final Map<String, StringBuilder> mimeToContent = new HashMap<String, StringBuilder>();

    /* Create parser for this message */
    final MimeStreamParser parser = new MimeStreamParser();
    parser.setContentHandler(new AbstractContentHandler() {
      boolean fBodyReached = false;

      @Override
      public void field(String fieldData) {

        /* Support early cancellation */
        if (monitor.isCanceled()) {
          parser.stop();
          return;
        }

        /* Not yet in Body */
        if (!fBodyReached) {

          /* From */
          if (fieldData.startsWith(HEADER_FROM))
            interpretFrom(news, DecoderUtil.decodeEncodedWords(fieldData.substring(HEADER_FROM.length())));

          /* Subject */
          else if (fieldData.startsWith(HEADER_SUBJECT))
            interpretSubject(news, DecoderUtil.decodeEncodedWords(fieldData.substring(HEADER_SUBJECT.length())));

          /* Date */
          else if (fieldData.startsWith(HEADER_DATE))
            interpretDate(news, DecoderUtil.decodeEncodedWords(fieldData.substring(HEADER_DATE.length())));

          /* Message ID */
          else if (fieldData.startsWith(HEADER_MESSAGE_ID))
            interpretMessageId(news, DecoderUtil.decodeEncodedWords(fieldData.substring(HEADER_MESSAGE_ID.length())));

          /* References */
          else if (fieldData.startsWith(HEADER_REFERENCES))
            interpretReferences(news, DecoderUtil.decodeEncodedWords(fieldData.substring(HEADER_REFERENCES.length())));
        }
      }

      @Override
      public void body(BodyDescriptor bd, InputStream is) throws IOException {

        /* Support early cancellation */
        if (monitor.isCanceled()) {
          parser.stop();
          return;
        }

        /* Require a mimetype */
        String mimeType = bd.getMimeType();
        if (mimeType == null)
          return;

        /* Require a text-mime */
        if (!mimeType.contains(MIME_TEXT))
          return;

        /* Assign StringBuilder with Mime-Type */
        StringBuilder strBuilder = mimeToContent.get(mimeType);
        if (strBuilder == null) {
          strBuilder = new StringBuilder();
          mimeToContent.put(mimeType, strBuilder);
        }

        /* Handle encodings */
        if (bd.isBase64Encoded())
          is = new Base64InputStream(is);
        else if (bd.isQuotedPrintableEncoded())
          is = new QuotedPrintableInputStream(is);

        /* Read Body */
        BufferedReader reader;
        if (!DEFAULT_ENCODING.equals(bd.getCharset()) && Charset.isSupported(bd.getCharset()))
          reader = new BufferedReader(new InputStreamReader(is, bd.getCharset()));
        else
          reader = new BufferedReader(new InputStreamReader(is));

        String line = null;
        while ((line = reader.readLine()) != null && !monitor.isCanceled()) {

          /* Check for quote */
          boolean isQuote = line.startsWith(">"); //$NON-NLS-1$
          if (line.startsWith(">>>>")) //$NON-NLS-1$
            strBuilder.append("<span class=\"quote_lvl4\">"); //$NON-NLS-1$
          else if (line.startsWith(">>>")) //$NON-NLS-1$
            strBuilder.append("<span class=\"quote_lvl3\">"); //$NON-NLS-1$
          else if (line.startsWith(">>")) //$NON-NLS-1$
            strBuilder.append("<span class=\"quote_lvl2\">"); //$NON-NLS-1$
          else if (line.startsWith(">")) //$NON-NLS-1$
            strBuilder.append("<span class=\"quote_lvl1\">"); //$NON-NLS-1$

          /* Beautify Body (if non-html) */
          if (!MIME_TEXT_HTML.equals(mimeType))
            strBuilder.append(beautifyBody(line)).append("<br>\n"); //$NON-NLS-1$
          else
            strBuilder.append(line);

          /* Check for quote */
          if (isQuote)
            strBuilder.append("</span>"); //$NON-NLS-1$
        }
      }
    });

    /* Parse Body */
    ReaderInputStream inS = new ReaderInputStream(articleReader);
    parser.parse(inS);

    /* Prefer HTML over text/plain */
    if (mimeToContent.containsKey(MIME_TEXT_HTML))
      news.setDescription(mimeToContent.get(MIME_TEXT_HTML).toString());

View Full Code Here


     * Creates a new <code>Header</code> from the specified stream.
     *
     * @param is the stream to read the header from.
     */
    public Header(InputStream is) throws IOException {
        final MimeStreamParser parser = new MimeStreamParser();
        parser.setContentHandler(new AbstractContentHandler() {
            public void endHeader() {
                parser.stop();
            }
            public void field(String fieldData) {
                addField(Field.parse(fieldData));
            }
        });
        parser.parse(is);
    }
View Full Code Here

     *
     * @param is the stream to parse.
     * @throws IOException on I/O errors.
     */
    public Message(InputStream is) throws IOException {
        MimeStreamParser parser = new MimeStreamParser();
        parser.setContentHandler(new MessageBuilder());
        parser.parse(is);
    }
View Full Code Here

TOP

Related Classes of org.apache.james.mime4j.MimeStreamParser

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.