1 /*
2 * Copyright 2007-2013 smartics, Kronseder & Reiner GmbH
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 package de.smartics.maven.exceptions.util;
17
18 import java.io.UnsupportedEncodingException;
19
20 import org.apache.commons.lang.ObjectUtils;
21
22 import de.smartics.exceptions.report.data.ProjectConfiguration;
23
24 /**
25 * Helper to encode and decode texts according to the encodings requested by the
26 * project configuration.
27 */
28 public final class TextCoder
29 {
30 // ********************************* Fields *********************************
31
32 // --- constants ------------------------------------------------------------
33
34 // --- members --------------------------------------------------------------
35
36 /**
37 * The configuration to use.
38 */
39 private final ProjectConfiguration<?> config;
40
41 // ****************************** Initializer *******************************
42
43 // ****************************** Constructors ******************************
44
45 /**
46 * Default constructor.
47 *
48 * @param config the configuration to use.
49 */
50 public TextCoder(final ProjectConfiguration<?> config)
51 {
52 this.config = config;
53 }
54
55 // ****************************** Inner Classes *****************************
56
57 // ********************************* Methods ********************************
58
59 // --- init -----------------------------------------------------------------
60
61 // --- get&set --------------------------------------------------------------
62
63 // --- business -------------------------------------------------------------
64
65 /**
66 * Decodes and encodes the given text with the encodings provided by the
67 * configuration.
68 *
69 * @param text the text to deal with.
70 * @return the de- and encoded text.
71 * @throws IllegalArgumentException if any encoding is not supported on the
72 * platform.
73 */
74 public String run(final String text) throws IllegalArgumentException
75 {
76 final String sourceEncoding = config.getSourceEncoding();
77 final String reportEncoding = config.getReportEncoding();
78 try
79 {
80 final String sourceDecoded = new String(text.getBytes(), sourceEncoding);
81 final String reportEncoded =
82 (ObjectUtils.equals(sourceEncoding, reportEncoding) ? sourceDecoded
83 : new String(sourceEncoding.getBytes(), reportEncoding));
84 return reportEncoded;
85 }
86 catch (final UnsupportedEncodingException e)
87 {
88 throw new IllegalArgumentException("Unsupported encoding: "
89 + e.getMessage(), e);
90 }
91 }
92
93 // --- object basics --------------------------------------------------------
94
95 }