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 | |
---|
20 | package org.apache.james.mime4j.message; |
---|
21 | |
---|
22 | import org.apache.james.mime4j.MimeException; |
---|
23 | import org.apache.james.mime4j.codec.DecodeMonitor; |
---|
24 | import org.apache.james.mime4j.dom.Header; |
---|
25 | import org.apache.james.mime4j.dom.field.Field; |
---|
26 | import org.apache.james.mime4j.field.DefaultFieldParser; |
---|
27 | import org.apache.james.mime4j.parser.AbstractContentHandler; |
---|
28 | import org.apache.james.mime4j.stream.RawField; |
---|
29 | |
---|
30 | /** |
---|
31 | * Abstract implementation of ContentHandler that automates common |
---|
32 | * tasks. Currently performs header parsing. |
---|
33 | * |
---|
34 | * Older versions of this class performed decoding of content streams. |
---|
35 | * This can be now easily achieved by calling setContentDecoding(true) on the MimeStreamParser. |
---|
36 | */ |
---|
37 | public abstract class SimpleContentHandler extends AbstractContentHandler { |
---|
38 | |
---|
39 | private final DecodeMonitor monitor; |
---|
40 | |
---|
41 | public SimpleContentHandler(final DecodeMonitor monitor) { |
---|
42 | super(); |
---|
43 | this.monitor = monitor; |
---|
44 | } |
---|
45 | |
---|
46 | public SimpleContentHandler() { |
---|
47 | this(null); |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | * Called after headers are parsed. |
---|
52 | */ |
---|
53 | public abstract void headers(Header header); |
---|
54 | |
---|
55 | /* Implement introduced callbacks. */ |
---|
56 | |
---|
57 | private Header currHeader; |
---|
58 | |
---|
59 | /** |
---|
60 | * @see org.apache.james.mime4j.parser.AbstractContentHandler#startHeader() |
---|
61 | */ |
---|
62 | @Override |
---|
63 | public final void startHeader() { |
---|
64 | currHeader = new HeaderImpl(); |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * @see org.apache.james.mime4j.parser.AbstractContentHandler#field(RawField) |
---|
69 | */ |
---|
70 | @Override |
---|
71 | public final void field(RawField field) throws MimeException { |
---|
72 | Field parsedField = DefaultFieldParser.parse(field.getRaw(), monitor); |
---|
73 | currHeader.addField(parsedField); |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * @see org.apache.james.mime4j.parser.AbstractContentHandler#endHeader() |
---|
78 | */ |
---|
79 | @Override |
---|
80 | public final void endHeader() { |
---|
81 | Header tmp = currHeader; |
---|
82 | currHeader = null; |
---|
83 | headers(tmp); |
---|
84 | } |
---|
85 | |
---|
86 | } |
---|