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.field; |
---|
21 | |
---|
22 | import java.io.IOException; |
---|
23 | import java.io.OutputStream; |
---|
24 | |
---|
25 | import org.apache.james.mime4j.codec.DecodeMonitor; |
---|
26 | import org.apache.james.mime4j.dom.field.ParseException; |
---|
27 | import org.apache.james.mime4j.dom.field.ParsedField; |
---|
28 | import org.apache.james.mime4j.util.ByteSequence; |
---|
29 | |
---|
30 | /** |
---|
31 | * The base class of all field classes. |
---|
32 | */ |
---|
33 | public abstract class AbstractField implements ParsedField { |
---|
34 | |
---|
35 | private final String name; |
---|
36 | private final String body; |
---|
37 | private final ByteSequence raw; |
---|
38 | protected DecodeMonitor monitor; |
---|
39 | |
---|
40 | protected AbstractField( |
---|
41 | final String name, |
---|
42 | final String body, |
---|
43 | final ByteSequence raw, |
---|
44 | final DecodeMonitor monitor) { |
---|
45 | this.name = name; |
---|
46 | this.body = body; |
---|
47 | this.raw = raw; |
---|
48 | this.monitor = monitor != null ? monitor : DecodeMonitor.SILENT; |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * Gets the name of the field (<code>Subject</code>, |
---|
53 | * <code>From</code>, etc). |
---|
54 | * |
---|
55 | * @return the field name. |
---|
56 | */ |
---|
57 | public String getName() { |
---|
58 | return name; |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * @see org.apache.james.mime4j.dom.field.Field#writeTo(java.io.OutputStream) |
---|
63 | */ |
---|
64 | public void writeTo(OutputStream out) throws IOException { |
---|
65 | out.write(raw.toByteArray()); |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * Gets the unfolded, unparsed and possibly encoded (see RFC 2047) field |
---|
70 | * body string. |
---|
71 | * |
---|
72 | * @return the unfolded unparsed field body string. |
---|
73 | */ |
---|
74 | public String getBody() { |
---|
75 | return body; |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * @see ParsedField#isValidField() |
---|
80 | */ |
---|
81 | public boolean isValidField() { |
---|
82 | return getParseException() == null; |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * @see ParsedField#getParseException() |
---|
87 | */ |
---|
88 | public ParseException getParseException() { |
---|
89 | return null; |
---|
90 | } |
---|
91 | |
---|
92 | @Override |
---|
93 | public String toString() { |
---|
94 | return name + ": " + body; |
---|
95 | } |
---|
96 | |
---|
97 | } |
---|