Clover Coverage Report - Commons Codec
Coverage timestamp: Fri May 9 2008 10:49:04 EST
../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
36   162   12   18
14   61   0.33   2
2     6  
1    
 
  RFC1522Codec       Line # 45 36 12 100% 1.0
 
  (28)
 
1    /*
2    * Copyright 2001-2006 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
15    */
16   
17    package org.apache.commons.codec.net;
18   
19    import java.io.UnsupportedEncodingException;
20   
21    import org.apache.commons.codec.DecoderException;
22    import org.apache.commons.codec.EncoderException;
23   
24    /**
25    * <p>
26    * Implements methods common to all codecs defined in RFC 1522.
27    * </p>
28    *
29    * <p>
30    * <a href="http://www.ietf.org/rfc/rfc1522.txt">RFC 1522</a>
31    * describes techniques to allow the encoding of non-ASCII text in
32    * various portions of a RFC 822 [2] message header, in a manner which
33    * is unlikely to confuse existing message handling software.
34    * </p>
35   
36    * @see <a href="http://www.ietf.org/rfc/rfc1522.txt">
37    * MIME (Multipurpose Internet Mail Extensions) Part Two:
38    * Message Header Extensions for Non-ASCII Text</a>
39    * </p>
40    *
41    * @author Apache Software Foundation
42    * @since 1.3
43    * @version $Id: RFC1522Codec.java 437232 2006-08-26 21:27:08Z ggregory $
44    */
 
45    abstract class RFC1522Codec {
46   
47    /**
48    * Applies an RFC 1522 compliant encoding scheme to the given string of text with the
49    * given charset. This method constructs the "encoded-word" header common to all the
50    * RFC 1522 codecs and then invokes {@link #doEncoding(byte [])} method of a concrete
51    * class to perform the specific enconding.
52    *
53    * @param text a string to encode
54    * @param charset a charset to be used
55    *
56    * @return RFC 1522 compliant "encoded-word"
57    *
58    * @throws EncoderException thrown if there is an error conidition during the Encoding
59    * process.
60    * @throws UnsupportedEncodingException thrown if charset is not supported
61    *
62    * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
63    * encoding names</a>
64    */
 
65  36 toggle protected String encodeText(final String text, final String charset)
66    throws EncoderException, UnsupportedEncodingException
67    {
68  36 if (text == null) {
69  2 return null;
70    }
71  34 StringBuffer buffer = new StringBuffer();
72  34 buffer.append("=?");
73  34 buffer.append(charset);
74  34 buffer.append('?');
75  34 buffer.append(getEncoding());
76  34 buffer.append('?');
77  34 byte [] rawdata = doEncoding(text.getBytes(charset));
78  30 buffer.append(new String(rawdata, CharacterEncodingNames.US_ASCII));
79  30 buffer.append("?=");
80  30 return buffer.toString();
81    }
82   
83    /**
84    * Applies an RFC 1522 compliant decoding scheme to the given string of text. This method
85    * processes the "encoded-word" header common to all the RFC 1522 codecs and then invokes
86    * {@link #doEncoding(byte [])} method of a concrete class to perform the specific deconding.
87    *
88    * @param text a string to decode
89    * @return A new decoded String or <code>null</code> if the input is <code>null</code>.
90    *
91    * @throws DecoderException thrown if there is an error conidition during the Decoding
92    * process.
93    * @throws UnsupportedEncodingException thrown if charset specified in the "encoded-word"
94    * header is not supported
95    */
 
96  42 toggle protected String decodeText(final String text)
97    throws DecoderException, UnsupportedEncodingException
98    {
99  42 if (text == null) {
100  2 return null;
101    }
102  40 if ((!text.startsWith("=?")) || (!text.endsWith("?="))) {
103  4 throw new DecoderException("RFC 1522 violation: malformed encoded content");
104    }
105  36 int termnator = text.length() - 2;
106  36 int from = 2;
107  36 int to = text.indexOf("?", from);
108  36 if ((to == -1) || (to == termnator)) {
109  2 throw new DecoderException("RFC 1522 violation: charset token not found");
110    }
111  34 String charset = text.substring(from, to);
112  34 if (charset.equals("")) {
113  2 throw new DecoderException("RFC 1522 violation: charset not specified");
114    }
115  32 from = to + 1;
116  32 to = text.indexOf("?", from);
117  32 if ((to == -1) || (to == termnator)) {
118  2 throw new DecoderException("RFC 1522 violation: encoding token not found");
119    }
120  30 String encoding = text.substring(from, to);
121  30 if (!getEncoding().equalsIgnoreCase(encoding)) {
122  4 throw new DecoderException("This codec cannot decode " +
123    encoding + " encoded content");
124    }
125  26 from = to + 1;
126  26 to = text.indexOf("?", from);
127  26 byte[] data = text.substring(from, to).getBytes(CharacterEncodingNames.US_ASCII);
128  26 data = doDecoding(data);
129  26 return new String(data, charset);
130    }
131   
132    /**
133    * Returns the codec name (referred to as encoding in the RFC 1522)
134    *
135