forked from openmrs/openmrs-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenmrsCharacterEscapes.java
49 lines (41 loc) · 1.54 KB
/
OpenmrsCharacterEscapes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http:https://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http:https://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs;
import com.fasterxml.jackson.core.SerializableString;
import com.fasterxml.jackson.core.io.CharacterEscapes;
import com.fasterxml.jackson.core.io.SerializedString;
/**
* An instance of this class can be passed to an ObjectMapper instance when serializing objects to
* JSON using the jackson API so as to escape html and scripts inside html tags
*/
public class OpenmrsCharacterEscapes extends CharacterEscapes {
private final int[] asciiEscapes;
public OpenmrsCharacterEscapes() {
// start with set of characters known to require escaping (double-quote, backslash etc)
int[] esc = CharacterEscapes.standardAsciiEscapesForJSON();
// and force escaping of a few others:
esc['<'] = CharacterEscapes.ESCAPE_CUSTOM;
esc['>'] = CharacterEscapes.ESCAPE_CUSTOM;
asciiEscapes = esc;
}
@Override
public int[] getEscapeCodesForAscii() {
return asciiEscapes;
}
@Override
public SerializableString getEscapeSequence(int ch) {
if (ch == '<') {
return new SerializedString("<");
} else if (ch == '>') {
return new SerializedString(">");
}
return null;
}
}