source: contrib/MailArchiver/sources/vendor/mime4j/apache-mime4j-0.7-SNAPSHOT-20110327.010440-17/core/src/test/java/org/apache/james/mime4j/parser/TestHandler.java @ 6785

Revision 6785, 3.7 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
1/****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one   *
3 * or more contributor license agreements.  See the NOTICE file *
4 * distributed with this work for additional information        *
5 * regarding copyright ownership.  The ASF licenses this file   *
6 * to you under the Apache License, Version 2.0 (the            *
7 * "License"); you may not use this file except in compliance   *
8 * with the License.  You may obtain a copy of the License at   *
9 *                                                              *
10 *   http://www.apache.org/licenses/LICENSE-2.0                 *
11 *                                                              *
12 * Unless required by applicable law or agreed to in writing,   *
13 * software distributed under the License is distributed on an  *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15 * KIND, either express or implied.  See the License for the    *
16 * specific language governing permissions and limitations      *
17 * under the License.                                           *
18 ****************************************************************/
19
20package org.apache.james.mime4j.parser;
21
22import java.io.IOException;
23import java.io.InputStream;
24
25import org.apache.james.mime4j.parser.ContentHandler;
26import org.apache.james.mime4j.stream.BodyDescriptor;
27import org.apache.james.mime4j.stream.RawField;
28import org.apache.james.mime4j.util.ContentUtil;
29
30/**
31 * Helper class to run comparison of parsed results
32 */
33class TestHandler implements ContentHandler {
34    StringBuilder sb = new StringBuilder();
35
36    private String escape(char c) {
37        if (c == '&') {
38            return "&";
39        }
40        if (c == '>') {
41            return ">";
42        }
43        if (c == '<') {
44            return "&lt;";
45        }
46        return "" + c;
47    }
48   
49    private String escape(String s) {
50        s = s.replaceAll("&", "&amp;");
51        s = s.replaceAll(">", "&gt;");
52        s = s.replaceAll("<", "&lt;");
53        return s;
54    }
55   
56    public void epilogue(InputStream is) throws IOException {
57        sb.append("<epilogue>\r\n");
58        int b = 0;
59        while ((b = is.read()) != -1) {
60            sb.append(escape((char) b));
61        }
62        sb.append("</epilogue>\r\n");
63    }
64    public void preamble(InputStream is) throws IOException {
65        sb.append("<preamble>\r\n");
66        int b = 0;
67        while ((b = is.read()) != -1) {
68            sb.append(escape((char) b));
69        }
70        sb.append("</preamble>\r\n");
71    }
72    public void startMultipart(BodyDescriptor bd) {
73        sb.append("<multipart>\r\n");
74    }
75    public void body(BodyDescriptor bd, InputStream is) throws IOException {
76        sb.append("<body>\r\n");
77        int b = 0;
78        while ((b = is.read()) != -1) {
79            sb.append(escape((char) b));
80        }
81        sb.append("</body>\r\n");
82    }
83    public void endMultipart() {
84        sb.append("</multipart>\r\n");
85    }
86    public void startBodyPart() {
87        sb.append("<body-part>\r\n");
88    }
89    public void endBodyPart() {
90        sb.append("</body-part>\r\n");
91    }
92    public void startHeader() {
93        sb.append("<header>\r\n");
94    }
95    public void field(RawField field) {
96        sb.append("<field>\r\n"
97                + escape(ContentUtil.decode(field.getRaw()))
98                + "</field>\r\n");
99    }
100    public void endHeader() {
101        sb.append("</header>\r\n");
102    }
103    public void startMessage() {
104        sb.append("<message>\r\n");
105    }
106    public void endMessage() {
107        sb.append("</message>\r\n");
108    }
109
110    public void raw(InputStream is) throws IOException {
111        MimeStreamParserExampleMessagesTest.fail("raw should never be called");
112    }
113}
Note: See TracBrowser for help on using the repository browser.