1 | package fr.sii.ogham.sms.sender.impl.cloudhopper.encoder; | |
2 | ||
3 | import java.util.HashMap; | |
4 | import java.util.Map; | |
5 | import java.util.Map.Entry; | |
6 | ||
7 | import org.slf4j.Logger; | |
8 | import org.slf4j.LoggerFactory; | |
9 | ||
10 | import com.cloudhopper.commons.charset.Charset; | |
11 | import com.cloudhopper.commons.charset.CharsetUtil; | |
12 | ||
13 | import fr.sii.ogham.core.charset.CharsetDetector; | |
14 | import fr.sii.ogham.sms.encoder.Encoded; | |
15 | import fr.sii.ogham.sms.encoder.EncodedMessage; | |
16 | import fr.sii.ogham.sms.encoder.Encoder; | |
17 | import fr.sii.ogham.sms.exception.message.EncodingException; | |
18 | ||
19 | /** | |
20 | * Default implementation of a configurable {@link Encoder}. | |
21 | * | |
22 | * @author cdejonghe | |
23 | * | |
24 | */ | |
25 | public class MapCloudhopperCharsetEncoder implements Encoder { | |
26 | private static final Logger LOG = LoggerFactory.getLogger(MapCloudhopperCharsetEncoder.class); | |
27 | ||
28 | /** | |
29 | * The map of {@link Charset} indexed by the NIO | |
30 | * {@link java.nio.charset.Charset} code. | |
31 | */ | |
32 | private final Map<String, NamedCharset> mapCloudhopperCharsetByNioCharsetName = new HashMap<>(); | |
33 | ||
34 | /** The charset provider. */ | |
35 | private final CharsetDetector charsetProvider; | |
36 | ||
37 | public MapCloudhopperCharsetEncoder(CharsetDetector charsetProvider) { | |
38 | super(); | |
39 | this.charsetProvider = charsetProvider; | |
40 | } | |
41 | ||
42 | /** | |
43 | * Initializes with the map of {@link Charset} code handlers indexed indexed | |
44 | * by the NIO {@link java.nio.charset.Charset} code. | |
45 | * | |
46 | * @param charsetProvider | |
47 | * the charset provider that gives a charset for the message | |
48 | * @param mapCloudhopperNameByNioName | |
49 | * the map of content handlers indexed by the content class | |
50 | * @throws EncodingException | |
51 | * If the map contains any invalid cloudhopper charset name | |
52 | * | |
53 | */ | |
54 | public MapCloudhopperCharsetEncoder(CharsetDetector charsetProvider, Map<String, String> mapCloudhopperNameByNioName) throws EncodingException { | |
55 | this(charsetProvider); | |
56 | ||
57 | for (Entry<String, String> nioCharset : mapCloudhopperNameByNioName.entrySet()) { | |
58 |
2
1. <init> : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/MapCloudhopperCharsetEncoder::addCharset → SURVIVED 2. <init> : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/MapCloudhopperCharsetEncoder::addCharset → NO_COVERAGE |
addCharset(nioCharset.getKey(), nioCharset.getValue()); |
59 | } | |
60 | } | |
61 | ||
62 | /** | |
63 | * Add a charset mapping. | |
64 | * | |
65 | * @param nioCharsetName | |
66 | * Java NIO charset name | |
67 | * @param cloudhopperCharsetName | |
68 | * Cloudhopper charset name | |
69 | * @see CharsetUtil | |
70 | * @throws EncodingException | |
71 | * If Cloudhopper charset name is invalid | |
72 | */ | |
73 | public final void addCharset(String nioCharsetName, String cloudhopperCharsetName) throws EncodingException { | |
74 | Charset charset = CharsetUtil.map(cloudhopperCharsetName); | |
75 |
2
1. addCharset : negated conditional → NO_COVERAGE 2. addCharset : negated conditional → KILLED |
if (charset != null) { |
76 |
2
1. addCharset : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/MapCloudhopperCharsetEncoder::addCharset → SURVIVED 2. addCharset : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/MapCloudhopperCharsetEncoder::addCharset → NO_COVERAGE |
addCharset(nioCharsetName, new NamedCharset(cloudhopperCharsetName, charset)); |
77 | } else { | |
78 | throw new EncodingException("Invalid cloudhopper charset name : " + cloudhopperCharsetName); | |
79 | } | |
80 | } | |
81 | ||
82 | /** | |
83 | * Add a charset mapping. | |
84 | * | |
85 | * @param nioCharsetName | |
86 | * Java NIO charset name | |
87 | * @param cloudhopperCharset | |
88 | * Cloudhopper charset | |
89 | */ | |
90 | public void addCharset(String nioCharsetName, NamedCharset cloudhopperCharset) { | |
91 | LOG.debug("Added charset mapping nio {} -> {}", nioCharsetName, cloudhopperCharset); | |
92 | mapCloudhopperCharsetByNioCharsetName.put(nioCharsetName, cloudhopperCharset); | |
93 | } | |
94 | ||
95 | private NamedCharset get(java.nio.charset.Charset nioCharset) throws EncodingException { | |
96 | NamedCharset cloudhopperCharset = mapCloudhopperCharsetByNioCharsetName.get(nioCharset.name()); | |
97 |
2
1. get : negated conditional → NO_COVERAGE 2. get : negated conditional → KILLED |
if (cloudhopperCharset == null) { |
98 | throw new EncodingException("No cloudhopper charset registered for nio charset : " + nioCharset.name()); | |
99 | } | |
100 | ||
101 |
2
1. get : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/MapCloudhopperCharsetEncoder::get → NO_COVERAGE 2. get : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/MapCloudhopperCharsetEncoder::get → KILLED |
return mapCloudhopperCharsetByNioCharsetName.get(nioCharset.name()); |
102 | } | |
103 | ||
104 | @Override | |
105 | public Encoded encode(String messageStringContent) throws EncodingException { | |
106 | java.nio.charset.Charset nioCharset = charsetProvider.detect(messageStringContent); | |
107 |
2
1. encode : negated conditional → NO_COVERAGE 2. encode : negated conditional → KILLED |
if (nioCharset == null) { |
108 | throw new EncodingException("No charset provided for message : \n" + messageStringContent); | |
109 | } | |
110 | ||
111 | NamedCharset cloudhopperCharset = get(nioCharset); | |
112 | LOG.debug("Encoding message using mapping nio {} -> {}", nioCharset, cloudhopperCharset); | |
113 |
2
1. encode : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/MapCloudhopperCharsetEncoder::encode → NO_COVERAGE 2. encode : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/encoder/MapCloudhopperCharsetEncoder::encode → KILLED |
return new EncodedMessage(messageStringContent, CharsetUtil.encode(messageStringContent, cloudhopperCharset.getCharset()), cloudhopperCharset.getCharsetName()); |
114 | } | |
115 | } | |
Mutations | ||
58 |
1.1 2.2 |
|
75 |
1.1 2.2 |
|
76 |
1.1 2.2 |
|
97 |
1.1 2.2 |
|
101 |
1.1 2.2 |
|
107 |
1.1 2.2 |
|
113 |
1.1 2.2 |